Skip to content

Commit

Permalink
Sponge support
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Mar 13, 2024
1 parent 8a64746 commit d37d660
Show file tree
Hide file tree
Showing 30 changed files with 2,802 additions and 9 deletions.
4 changes: 0 additions & 4 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ repositories {
maven { url 'https://repo.papermc.io/repository/maven-public/' }
}

java {
disableAutoTargetJvm()
}

dependencies {
compileOnly(adventureDependencies)

Expand Down
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ subprojects {
"net.kyori:adventure-text-serializer-legacy:${adventureVersion}",
"net.kyori:adventure-nbt:${adventureVersion}"]

java.sourceCompatibility = JavaVersion.VERSION_17
java.targetCompatibility = JavaVersion.VERSION_1_8
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
disableAutoTargetJvm()
}

repositories {
mavenLocal()
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include 'api'
include 'netty-common'
//Real modules
include 'spigot'
include 'sponge'
include 'bungeecord'
include 'velocity'

3 changes: 0 additions & 3 deletions spigot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ repositories {
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
}

java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8

shadowJar {
relocate 'net.kyori.adventure.text.serializer.gson', 'io.github.retrooper.packetevents.adventure.serializer.gson'
relocate 'net.kyori.adventure.text.serializer.legacy', 'io.github.retrooper.packetevents.adventure.serializer.legacy'
Expand Down
53 changes: 53 additions & 0 deletions sponge/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import org.spongepowered.gradle.plugin.config.PluginLoaders
import org.spongepowered.plugin.metadata.model.PluginDependency

plugins {
id("org.spongepowered.gradle.plugin") version("2.0.2")
}

repositories {
maven { url 'https://repo.spongepowered.org/repository/maven-public/' } // Sponge
maven { url 'https://repo.viaversion.com/' }
}

sponge {
apiVersion("11.0.0-SNAPSHOT")
loader {
name(PluginLoaders.JAVA_PLAIN)
version("2.2.0")
}

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)
}
}
}

shadowJar {
relocate 'net.kyori.adventure.text.serializer.gson', 'io.github.retrooper.packetevents.adventure.serializer.gson'
relocate 'net.kyori.adventure.text.serializer.legacy', 'io.github.retrooper.packetevents.adventure.serializer.legacy'
dependencies {
exclude(dependency('com.google.code.gson:gson:2.8.0'))
exclude(dependency('com.google.code.gson:gson:2.8.5'))
exclude(dependency('com.google.code.gson:gson:2.8.8'))
}
}

dependencies {
compileOnly 'io.netty:netty-all:4.1.75.Final'
api project(':api')
api project(':netty-common')
api(adventureDependencies)

compileOnly 'com.viaversion:viaversion:4.5.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.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.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);
}
}
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.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
public void onServerStart(final StartingEngineEvent<Server> event) {
PacketEvents.setAPI(SpongePacketEventsBuilder.build(pluginContainer));
PacketEvents.getAPI().load();

// Register your listeners
// TODO disable debug
PacketEvents.getAPI().getSettings().debug(true).bStats(true).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
public void onStopping(StoppingEngineEvent<Server> event) {
PacketEvents.getAPI().terminate();
}
}
Loading

0 comments on commit d37d660

Please sign in to comment.