From 50995bca82d371c86d94ba08a23b3e22dcb39915 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 21 Dec 2021 21:15:36 +0100 Subject: [PATCH] Use SpigotCore TinyProtocol instance Signed-off-by: Lixfel --- .../de/steamwar/fightsystem/FightSystem.java | 1 - .../de/steamwar/fightsystem/fight/Fight.java | 4 ++-- .../steamwar/fightsystem/record/REntity.java | 17 +++++++++-------- .../fightsystem/utils/ProtocolAPI.java | 19 +++++++------------ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java index 1ef5115..ee08281 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java @@ -151,7 +151,6 @@ public class FightSystem extends JavaPlugin { @Override public void onDisable() { - ProtocolAPI.tinyProtocol.close(); GlobalRecorder.getInstance().close(); } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java index 80129fb..6a1a3c6 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java @@ -20,13 +20,13 @@ package de.steamwar.fightsystem.fight; import com.comphenix.tinyprotocol.Reflection; +import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; import de.steamwar.core.Core; import de.steamwar.fightsystem.ArenaMode; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.record.GlobalRecorder; -import de.steamwar.fightsystem.utils.ProtocolAPI; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Sound; @@ -146,7 +146,7 @@ public class Fight { private static final Reflection.ConstructorInvoker playerInfoDataConstructor = Reflection.getConstructor(playerInfoDataClass, playerInfoPacket, GameProfile.class, int.class, enumGamemode, iChatBaseComponent); public static void pseudoSpectator(Player player, boolean enable) { - ProtocolAPI.tinyProtocol.sendPacket(player, playerInfoPacket(updateGamemode, new GameProfile(player.getUniqueId(), player.getName()), enable ? creative : spectator)); + TinyProtocol.instance.sendPacket(player, playerInfoPacket(updateGamemode, new GameProfile(player.getUniqueId(), player.getName()), enable ? creative : spectator)); } public static Object playerInfoPacket(Object action, GameProfile profile, Object mode) { diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java b/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java index c0e70e1..8466333 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java @@ -20,6 +20,7 @@ package de.steamwar.fightsystem.record; import com.comphenix.tinyprotocol.Reflection; +import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; import de.steamwar.core.Core; import de.steamwar.fightsystem.fight.Fight; @@ -72,22 +73,22 @@ public class REntity { public static void playerJoins(Player player) { for(REntity entity : entities.values()){ if(entity.entityType == EntityType.PLAYER){ - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getPlayerInfoPacket()); - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getNamedSpawnPacket()); + TinyProtocol.instance.sendPacket(player, entity.getPlayerInfoPacket()); + TinyProtocol.instance.sendPacket(player, entity.getNamedSpawnPacket()); for (Map.Entry entry : entity.itemSlots.entrySet()) { - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getEquipmentPacket(entry.getKey(), entry.getValue())); + TinyProtocol.instance.sendPacket(player, entity.getEquipmentPacket(entry.getKey(), entry.getValue())); } }else{ - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getSpawnEntityPacket()); + TinyProtocol.instance.sendPacket(player, entity.getSpawnEntityPacket()); } - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getTeleportPacket()); - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getHeadRotationPacket()); + TinyProtocol.instance.sendPacket(player, entity.getTeleportPacket()); + TinyProtocol.instance.sendPacket(player, entity.getHeadRotationPacket()); if(entity.fireTick != 0) { - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getDataWatcherPacket(entityStatusWatcher, (byte) 1)); + TinyProtocol.instance.sendPacket(player, entity.getDataWatcherPacket(entityStatusWatcher, (byte) 1)); } if(entity.sneaks) { - ProtocolAPI.tinyProtocol.sendPacket(player, entity.getDataWatcherPacket(sneakingDataWatcher, BlockIdWrapper.impl.getPose(true))); + TinyProtocol.instance.sendPacket(player, entity.getDataWatcherPacket(sneakingDataWatcher, BlockIdWrapper.impl.getPose(true))); } } } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java index 05c5f3f..28ad17a 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java @@ -20,8 +20,6 @@ package de.steamwar.fightsystem.utils; import com.comphenix.tinyprotocol.TinyProtocol; -import de.steamwar.fightsystem.FightSystem; -import io.netty.channel.Channel; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -42,23 +40,20 @@ public class ProtocolAPI { private static final Map, BiFunction> outgoingHandler = new HashMap<>(); private static final Map, BiFunction> incomingHandler = new HashMap<>(); - public static final TinyProtocol tinyProtocol = new TinyProtocol(FightSystem.getPlugin()) { - @Override - public Object onPacketOutAsync(Player receiver, Channel channel, Object packet) { + static { + TinyProtocol.instance.setOutFilter((receiver, channel, packet) -> { BiFunction handler = outgoingHandler.get(packet.getClass()); if(handler == null) return packet; return handler.apply(receiver, packet); - } - - @Override - public Object onPacketInAsync(Player sender, Channel channel, Object packet) { + }); + TinyProtocol.instance.setInFilter((sender, channel, packet) -> { BiFunction handler = incomingHandler.get(packet.getClass()); if(handler == null) return packet; return handler.apply(sender, packet); - } - }; + }); + } public static void setOutgoingHandler(Class packetClass, BiFunction handler) { outgoingHandler.put(packetClass, handler); @@ -77,7 +72,7 @@ public class ProtocolAPI { } public static void broadcastPacket(Object packet) { - Bukkit.getOnlinePlayers().stream().map(tinyProtocol::getChannel).filter(tinyProtocol::hasInjected).forEach(channel -> tinyProtocol.sendPacket(channel, packet)); + Bukkit.getOnlinePlayers().stream().map(TinyProtocol.instance::getChannel).filter(TinyProtocol.instance::hasInjected).forEach(channel -> TinyProtocol.instance.sendPacket(channel, packet)); } public static BiFunction, Object> arrayCloneGenerator(Class elementClass) { -- 2.39.2