From bde4a2992054feac5318a4010bc3e82d63ceb639 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 22 Aug 2022 17:34:35 +0200 Subject: [PATCH] Update TinyProtocol API --- .../features/detonator/DetonatorListener.java | 4 +- .../features/tpslimit/TPSLimitUtils.java | 8 +-- .../tracer/TraceTNTClickListener.java | 4 +- .../features/util/NoClipCommand.java | 13 ++-- .../features/world/NoCreativeKnockback.java | 15 ++-- .../bausystem/features/world/SignEdit.java | 4 +- .../bausystem/features/xray/XrayCommand.java | 6 +- .../steamwar/bausystem/utils/ProtocolAPI.java | 70 ------------------- 8 files changed, 22 insertions(+), 102 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java index 9400b2c5..a7d11f69 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java @@ -20,13 +20,13 @@ package de.steamwar.bausystem.features.detonator; import com.comphenix.tinyprotocol.Reflection; +import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage; import de.steamwar.bausystem.features.detonator.storage.ItemStorage; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; -import de.steamwar.bausystem.utils.ProtocolAPI; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -51,7 +51,7 @@ public class DetonatorListener implements Listener { private static final Set HAS_UPDATED = new HashSet<>(); public DetonatorListener() { - ProtocolAPI.setIncomingHandler(useEntity, (player, o) -> { + TinyProtocol.instance.addFilter(useEntity, (player, o) -> { List entities = Detonator.getDetoEntities(player); if (entities.isEmpty()) { return o; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java index 9a562a8c..abb28103 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java @@ -20,10 +20,10 @@ package de.steamwar.bausystem.features.tpslimit; import com.comphenix.tinyprotocol.Reflection; +import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.utils.PlayerMovementWrapper; import de.steamwar.bausystem.utils.NMSWrapper; -import de.steamwar.bausystem.utils.ProtocolAPI; +import de.steamwar.bausystem.utils.PlayerMovementWrapper; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.World; @@ -105,7 +105,7 @@ public class TPSLimitUtils { } return o; }; - ProtocolAPI.setIncomingHandler(position, positionSetter); - ProtocolAPI.setIncomingHandler(positionLook, positionSetter); + TinyProtocol.instance.addFilter(position, positionSetter); + TinyProtocol.instance.addFilter(positionLook, positionSetter); } } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceTNTClickListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceTNTClickListener.java index 6a9b885e..d6f1c472 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceTNTClickListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceTNTClickListener.java @@ -20,11 +20,11 @@ package de.steamwar.bausystem.features.tracer; import com.comphenix.tinyprotocol.Reflection; +import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.tracer.show.TraceShowManager; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; -import de.steamwar.bausystem.utils.ProtocolAPI; import de.steamwar.bausystem.utils.RayTraceUtils; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -41,7 +41,7 @@ public class TraceTNTClickListener { { Set playerSet = new HashSet<>(); - ProtocolAPI.setIncomingHandler(useEntity, (player, o) -> { + TinyProtocol.instance.addFilter(useEntity, (player, o) -> { if (!playerSet.add(player)) return o; Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { RayTraceResult rayTraceResult = RayTraceUtils.trace(player, player.getLocation(), TraceShowManager.getEntities(player)); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java index ec88f9c3..33f7ac88 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java @@ -27,7 +27,6 @@ import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.utils.NMSWrapper; -import de.steamwar.bausystem.utils.ProtocolAPI; import de.steamwar.bausystem.utils.ProtocolWrapper; import de.steamwar.command.SWCommand; import de.steamwar.core.Core; @@ -84,8 +83,8 @@ public class NoClipCommand extends SWCommand implements Listener { } return o; }; - ProtocolAPI.setIncomingHandler(position, first); - ProtocolAPI.setIncomingHandler(positionLook, first); + TinyProtocol.instance.addFilter(position, first); + TinyProtocol.instance.addFilter(positionLook, first); BiFunction second = (player, o) -> { if (NOCLIPS.contains(player)) { @@ -94,9 +93,9 @@ public class NoClipCommand extends SWCommand implements Listener { } return o; }; - ProtocolAPI.setIncomingHandler(useItem, second); - ProtocolAPI.setIncomingHandler(blockDig, second); - ProtocolAPI.setIncomingHandler(windowClick, second); + TinyProtocol.instance.addFilter(useItem, second); + TinyProtocol.instance.addFilter(blockDig, second); + TinyProtocol.instance.addFilter(windowClick, second); BiFunction third = (player, o) -> { if (NOCLIPS.contains(player)) { @@ -104,7 +103,7 @@ public class NoClipCommand extends SWCommand implements Listener { } return o; }; - ProtocolAPI.setIncomingHandler(setSlotStack, third); + TinyProtocol.instance.addFilter(setSlotStack, third); } @Register(help = true) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/NoCreativeKnockback.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/NoCreativeKnockback.java index f1cb74ce..08d1d51b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/NoCreativeKnockback.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/NoCreativeKnockback.java @@ -20,26 +20,19 @@ package de.steamwar.bausystem.features.world; import com.comphenix.tinyprotocol.Reflection; +import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.utils.NMSWrapper; -import de.steamwar.bausystem.utils.ProtocolAPI; import org.bukkit.GameMode; -import org.bukkit.entity.Player; - -import java.util.function.BiFunction; @Linked(LinkageType.PLAIN) public class NoCreativeKnockback { - private Class explosionPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutExplosion"); - - { - BiFunction knockbackResetter = (player, o) -> { + public NoCreativeKnockback() { + TinyProtocol.instance.addFilter(Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutExplosion"), (player, o) -> { if (player.getGameMode() != GameMode.CREATIVE) return o; return NMSWrapper.impl.resetExplosionKnockback(o); - }; - - ProtocolAPI.setOutgoingHandler(explosionPacket, knockbackResetter); + }); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/SignEdit.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/SignEdit.java index 5a9cbce1..376adfba 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/SignEdit.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/SignEdit.java @@ -24,7 +24,6 @@ import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; -import de.steamwar.bausystem.utils.ProtocolAPI; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -34,7 +33,6 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.player.PlayerInteractEvent; @Linked(LinkageType.LISTENER) @@ -85,7 +83,7 @@ public class SignEdit implements Listener { } { - ProtocolAPI.setIncomingHandler(updateSign, (player, o) -> { + TinyProtocol.instance.addFilter(updateSign, (player, o) -> { Bukkit.getScheduler().runTask(BauSystem.getInstance(), () -> { String[] lines = stringFieldAccessor.get(o); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java index b3d5015b..abd97ee1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem.features.xray; import com.comphenix.tinyprotocol.Reflection; +import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.techhider.TechHiderCommand; import de.steamwar.bausystem.linkage.LinkageType; @@ -27,7 +28,6 @@ import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.linkage.LinkedInstance; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.utils.PlayerMovementWrapper; -import de.steamwar.bausystem.utils.ProtocolAPI; import de.steamwar.command.SWCommand; import de.steamwar.core.CraftbukkitWrapper; import de.steamwar.techhider.TechHider; @@ -123,8 +123,8 @@ public class XrayCommand extends SWCommand implements Listener { return o; }; - ProtocolAPI.setIncomingHandler(position, positionSetter); - ProtocolAPI.setIncomingHandler(positionLook, positionSetter); + TinyProtocol.instance.addFilter(position, positionSetter); + TinyProtocol.instance.addFilter(positionLook, positionSetter); } @EventHandler diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java deleted file mode 100644 index a0b650b1..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2021 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.utils; - -import com.comphenix.tinyprotocol.TinyProtocol; -import lombok.experimental.UtilityClass; -import org.bukkit.entity.Player; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.function.BiFunction; - -@UtilityClass -public class ProtocolAPI { - - private static final Map, Set>> outgoingHandler = new HashMap<>(); - private static final Map, Set>> incomingHandler = new HashMap<>(); - - static { - TinyProtocol.instance.setOutFilter((receiver, channel, packet) -> { - Set> handler = outgoingHandler.get(packet.getClass()); - if (handler == null || handler.isEmpty()) - return packet; - for (BiFunction function : handler) { - packet = function.apply(receiver, packet); - if (packet == null) return null; - } - return packet; - }); - TinyProtocol.instance.setInFilter((sender, channel, packet) -> { - Set> handler = incomingHandler.get(packet.getClass()); - if (handler == null || handler.isEmpty()) - return packet; - for (BiFunction function : handler) { - packet = function.apply(sender, packet); - if (packet == null) return null; - } - return packet; - }); - } - - public static void setOutgoingHandler - (Class packetClass, BiFunction handler) { - outgoingHandler.computeIfAbsent(packetClass, i -> new HashSet<>()).add(handler); - } - - public static void setIncomingHandler - (Class packetClass, BiFunction handler) { - incomingHandler.computeIfAbsent(packetClass, i -> new HashSet<>()).add(handler); - } -} -- 2.39.5