Skip to content

Commit

Permalink
Merge pull request #670 from SamB440/feat/sponge
Browse files Browse the repository at this point in the history
(WIP) Sponge support
  • Loading branch information
retrooper authored Oct 13, 2024
2 parents 487bcc3 + cec46d1 commit 0031d6a
Show file tree
Hide file tree
Showing 27 changed files with 2,898 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include("netty-common")
include("spigot")
include("bungeecord")
include("velocity")
include("sponge")
include("fabric")
// Patch modules
include(":patch:adventure-text-serializer-gson")
47 changes: 47 additions & 0 deletions sponge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import org.spongepowered.gradle.plugin.config.PluginLoaders
import org.spongepowered.plugin.metadata.model.PluginDependency

plugins {
packetevents.`shadow-conventions`
packetevents.`library-conventions`
id("org.spongepowered.gradle.plugin") version("2.0.2")
}

repositories {
maven("https://repo.spongepowered.org/repository/maven-public/") // Sponge
maven("https://repo.viaversion.com/")
}

sponge {
apiVersion("12.0.0-SNAPSHOT")
loader {
name(PluginLoaders.JAVA_PLAIN)
version("2.5.1-SNAPSHOT")
}

plugin("packetevents") {
displayName("PacketEvents")
entrypoint("io.github.retrooper.packetevents.sponge.PacketEventsPlugin")
license("GPL-3")
dependency("spongeapi") {
loadOrder(PluginDependency.LoadOrder.AFTER)
optional(false)
}
dependency("viaversion") {
version("*")
loadOrder(PluginDependency.LoadOrder.AFTER)
optional(true)
}
}
}

dependencies {
compileOnly(libs.netty)
shadow(libs.adventure.nbt) {
isTransitive = false
}
shadow(project(":api", "shadow"))
shadow(project(":netty-common"))

compileOnly(libs.via.version)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.sponge;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.FakeChannelUtil;
import io.github.retrooper.packetevents.sponge.injector.SpongeChannelInjector;
import net.kyori.adventure.text.Component;
import org.spongepowered.api.Server;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.filter.Getter;
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
import org.spongepowered.api.event.network.ServerSideConnectionEvent;

public class InternalSpongeListener {

@Listener(order = Order.LATE)
public void onJoin(ServerSideConnectionEvent.Join event, @Getter("player") ServerPlayer player) {
SpongeChannelInjector injector = (SpongeChannelInjector) PacketEvents.getAPI().getInjector();

User user = PacketEvents.getAPI().getPlayerManager().getUser(player);
if (user == null) {
// We did not inject this user
Object channel = PacketEvents.getAPI().getPlayerManager().getChannel(player);
// Check if it is a fake connection...
if (!FakeChannelUtil.isFakeChannel(channel)) {
// Kick them, if they are not a fake player.
player.kick(Component.text("PacketEvents 2.0 failed to inject"));
}
return;
}

// Set player object in the injectors
injector.updatePlayer(user, player);
}

@Listener(order = Order.EARLY)
public void onStart(StartedEngineEvent<Server> event) {
if (PacketEvents.getAPI().getSettings().shouldCheckForUpdates()) {
PacketEvents.getAPI().getUpdateChecker().handleUpdateCheck();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.github.retrooper.packetevents.sponge;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.SimplePacketListenerAbstract;
import com.github.retrooper.packetevents.event.UserConnectEvent;
import com.github.retrooper.packetevents.event.UserDisconnectEvent;
import com.github.retrooper.packetevents.event.UserLoginEvent;
import com.github.retrooper.packetevents.event.simple.PacketPlaySendEvent;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.util.TimeStampMode;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerWindowItems;
import com.google.inject.Inject;
import io.github.retrooper.packetevents.sponge.factory.SpongePacketEventsBuilder;
import io.github.retrooper.packetevents.sponge.util.SpongeConversionUtil;
import org.spongepowered.api.Server;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.lifecycle.StartingEngineEvent;
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
import org.spongepowered.api.registry.RegistryTypes;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.builtin.jvm.Plugin;

@Plugin("packetevents")
public class PacketEventsPlugin {

@Inject
private PluginContainer pluginContainer;

@Listener(order = Order.EARLY)
public void onServerStart(final StartingEngineEvent<Server> event) {
PacketEvents.setAPI(SpongePacketEventsBuilder.build(pluginContainer));
PacketEvents.getAPI().load();

// Register your listeners
PacketEvents.getAPI().getSettings().debug(false).downsampleColors(false).checkForUpdates(true).timeStampMode(TimeStampMode.MILLIS).reEncodeByDefault(true);
PacketEvents.getAPI().init();

SimplePacketListenerAbstract listener = new SimplePacketListenerAbstract(PacketListenerPriority.HIGH) {

// Testing ItemStack conversion, can be removed in future
@Override
public void onPacketPlaySend(PacketPlaySendEvent event) {
if (event.getPacketType() == PacketType.Play.Server.WINDOW_ITEMS) {
WrapperPlayServerWindowItems items = new WrapperPlayServerWindowItems(event);
for (ItemStack item : items.getItems()) {
org.spongepowered.api.item.inventory.ItemStack sponge = SpongeConversionUtil.toSpongeItemStack(item);
System.out.println(sponge.type().key(RegistryTypes.ITEM_TYPE).formatted());
System.out.println(SpongeConversionUtil.fromSpongeItemStack(sponge).getType().getName().toString());
}
}
}

@Override
public void onUserConnect(UserConnectEvent event) {
PacketEvents.getAPI().getLogManager().debug("User: (host-name) " + event.getUser().getAddress().getHostString() + " connected...");
}

@Override
public void onUserLogin(UserLoginEvent event) {
PacketEvents.getAPI().getLogManager().debug("You logged in! User name: " + event.getUser().getProfile().getName());
}

@Override
public void onUserDisconnect(UserDisconnectEvent event) {
PacketEvents.getAPI().getLogManager().debug("User: (host-name) " + event.getUser().getAddress().getHostString() + " disconnected...");
}
};
// PacketEvents.getAPI().getEventManager().registerListener(listener);
}

@Listener(order = Order.LATE)
public void onStopping(StoppingEngineEvent<Server> event) {
PacketEvents.getAPI().terminate();
}
}
Loading

0 comments on commit 0031d6a

Please sign in to comment.