diff --git a/BauSystem_Main/build.gradle b/BauSystem_Main/build.gradle index 9629d086..32d7760b 100644 --- a/BauSystem_Main/build.gradle +++ b/BauSystem_Main/build.gradle @@ -61,6 +61,5 @@ dependencies { compileOnly files("${projectDir}/../lib/Spigot-1.15.jar") compileOnly files("${projectDir}/../lib/WorldEdit-1.15.jar") - compileOnly files("${projectDir}/../lib/ProtocolLib.jar") compileOnly files("${projectDir}/../lib/SpigotCore.jar") } 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 c699211e..8b6b91da 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java @@ -19,17 +19,14 @@ package de.steamwar.bausystem.features.detonator; -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.tinyprotocol.Reflection; 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; @@ -41,7 +38,6 @@ import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerSwapHandItemsEvent; -import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -49,30 +45,29 @@ import java.util.Set; @Linked(LinkageType.LISTENER) public class DetonatorListener implements Listener { + public static final Class useEntity = Reflection.getClass("{nms}.PacketPlayInUseEntity"); + private static final Reflection.FieldAccessor entityIdFieldAccessor = Reflection.getField(useEntity, int.class, 0); + private static final Set HAS_UPDATED = new HashSet<>(); public DetonatorListener() { - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.USE_ENTITY) { - @Override - public void onPacketReceiving(PacketEvent event) { - List entities = new ArrayList<>(Detonator.getDetoEntities(event.getPlayer())); - if (entities.isEmpty()) { - return; - } - - PacketContainer container = event.getPacket(); - int entityId = container.getIntegers().read(0); - entities.removeIf(abstractDetonatorEntity -> abstractDetonatorEntity.getId() != entityId); - - if (entities.isEmpty()) { - return; - } - AbstractDetonatorEntity entity = entities.get(0); - Location location = entity.getBukkitEntity().getLocation().getBlock().getLocation(); - addLocationToDetonator(location, event.getPlayer()); - HAS_UPDATED.add(event.getPlayer()); - event.setCancelled(true); + ProtocolAPI.setIncomingHandler(useEntity, (player, o) -> { + List entities = Detonator.getDetoEntities(player); + if (entities.isEmpty()) { + return o; } + + int entityId = entityIdFieldAccessor.get(o); + AbstractDetonatorEntity entity = entities.stream().filter(abstractDetonatorEntity -> abstractDetonatorEntity.getId() == entityId).findFirst().orElse(null); + + if (entity == null) { + return o; + } + + Location location = entity.getBukkitEntity().getLocation().getBlock().getLocation(); + addLocationToDetonator(location, player); + HAS_UPDATED.add(player); + return null; }); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java index 4d8d5f1f..ae2034b7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java @@ -19,23 +19,17 @@ package de.steamwar.bausystem.features.other; -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.wrappers.EnumWrappers; -import com.comphenix.protocol.wrappers.PlayerInfoData; -import com.comphenix.protocol.wrappers.WrappedChatComponent; -import com.comphenix.protocol.wrappers.WrappedGameProfile; +import com.comphenix.tinyprotocol.Reflection; +import com.mojang.authlib.GameProfile; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.utils.ProtocolAPI; import de.steamwar.command.SWCommand; +import de.steamwar.core.Core; import de.steamwar.core.VersionedRunnable; import lombok.Getter; -import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; @@ -46,42 +40,64 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerToggleFlightEvent; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; +import java.util.*; +import java.util.function.BiFunction; @Linked(LinkageType.COMMAND) @Linked(LinkageType.LISTENER) public class NoClipCommand extends SWCommand implements Listener { + private static final Class playerInfoPacket = Reflection.getClass("{nms}.PacketPlayOutPlayerInfo"); + private static final Reflection.ConstructorInvoker playerInfoConstructor = Reflection.getConstructor(playerInfoPacket); + private static final Class playerInfoActionClass = Reflection.getClass("{nms}.PacketPlayOutPlayerInfo$EnumPlayerInfoAction"); + private static final Reflection.FieldAccessor playerInfoAction = Reflection.getField(playerInfoPacket, playerInfoActionClass, 0); + private static final Object updateGamemode = playerInfoActionClass.getEnumConstants()[1]; + private static final Reflection.FieldAccessor playerInfoData = Reflection.getField(playerInfoPacket, List.class, 0); + private static final Class playerInfoDataClass = Reflection.getClass("{nms}.PacketPlayOutPlayerInfo$PlayerInfoData"); + private static final Class enumGamemode = Reflection.getClass("{nms}.EnumGamemode"); + private static final Object spectator = enumGamemode.getEnumConstants()[4]; + private static final Class iChatBaseComponent = Reflection.getClass("{nms}.IChatBaseComponent"); + + private static final Reflection.ConstructorInvoker playerInfoDataConstructor = Reflection.getConstructor(playerInfoDataClass, playerInfoPacket, GameProfile.class, int.class, enumGamemode, iChatBaseComponent); + + private static final Class gameStateChange = Reflection.getClass("{nms}.PacketPlayOutGameStateChange"); + private static final Reflection.FieldAccessor integerFieldAccessor = Reflection.getField(gameStateChange, int.class, 0); + private static final Reflection.FieldAccessor floatFieldAccessor = Reflection.getField(gameStateChange, float.class, 0); + + private static final Class position = Reflection.getClass("{nms}.PacketPlayInFlying$PacketPlayInPosition"); + private static final Class positionLook = Reflection.getClass("{nms}.PacketPlayInFlying$PacketPlayInPositionLook"); + private static final Class useItem = Reflection.getClass("{nms}.PacketPlayInUseItem"); + private static final Class blockDig = Reflection.getClass("{nms}.PacketPlayInBlockDig"); + private static final Class windowClick = Reflection.getClass("{nms}.PacketPlayInWindowClick"); + @Getter private static final List NOCLIPS = new ArrayList<>(); private static final Map LAST_TICKS = new HashMap<>(); protected NoClipCommand() { super("noclip", "nc"); - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.POSITION, PacketType.Play.Client.POSITION_LOOK) { - @Override - public void onPacketReceiving(PacketEvent event) { - if (NOCLIPS.contains(event.getPlayer())) { - if (LAST_TICKS.getOrDefault(event.getPlayer(), -1L).equals(TPSUtils.currentTick.get())) return; - VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(event.getPlayer(), GameMode.SPECTATOR), 15)); - LAST_TICKS.put(event.getPlayer(), TPSUtils.currentTick.get()); - } + + BiFunction first = (player, o) -> { + if (NOCLIPS.contains(player)) { + if (LAST_TICKS.getOrDefault(player, -1L).equals(TPSUtils.currentTick.get())) return o; + VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(player, GameMode.SPECTATOR), 15)); + LAST_TICKS.put(player, TPSUtils.currentTick.get()); } - }); - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.USE_ITEM, PacketType.Play.Client.BLOCK_DIG, PacketType.Play.Client.WINDOW_CLICK) { - @Override - public void onPacketReceiving(PacketEvent event) { - if (NOCLIPS.contains(event.getPlayer())) { - VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(event.getPlayer(), GameMode.CREATIVE), 15)); - LAST_TICKS.put(event.getPlayer(), TPSUtils.currentTick.get()); - } + return o; + }; + ProtocolAPI.setIncomingHandler(position, first); + ProtocolAPI.setIncomingHandler(positionLook, first); + + BiFunction second = (player, o) -> { + if (NOCLIPS.contains(player)) { + VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(player, GameMode.CREATIVE), 15)); + LAST_TICKS.put(player, TPSUtils.currentTick.get()); } - }); + return o; + }; + ProtocolAPI.setIncomingHandler(useItem, second); + ProtocolAPI.setIncomingHandler(blockDig, second); + ProtocolAPI.setIncomingHandler(windowClick, second); } @Register(help = true) @@ -93,23 +109,15 @@ public class NoClipCommand extends SWCommand implements Listener { player.setGameMode(GameMode.SPECTATOR); ((CraftPlayer) player).getHandle().abilities.mayBuild = true; ((CraftPlayer) player).getHandle().abilities.canInstantlyBuild = true; - PacketContainer gm3packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO); - gm3packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE); - List playerInfoActions = new ArrayList<>(); - playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, EnumWrappers.NativeGameMode.SPECTATOR, WrappedChatComponent.fromText(player.getDisplayName()))); - gm3packet.getPlayerInfoDataLists().write(0, playerInfoActions); - PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.GAME_STATE_CHANGE); - gm1packet.getIntegers().write(0, 3); - gm1packet.getFloat().write(0, 1F); + + Object gameStateChangeObject = Reflection.newInstance(gameStateChange); + integerFieldAccessor.set(gameStateChangeObject, 3); + floatFieldAccessor.set(gameStateChangeObject, 1F); + NOCLIPS.add(player); BauSystem.MESSAGE.send("OTHER_NOCLIP_SLOT_INFO", player); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm1packet); - ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm3packet); - } catch (InvocationTargetException e) { - Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e); - player.performCommand("noclip"); - } + ProtocolAPI.tinyProtocol.sendPacket(player, gameStateChangeObject); + pseudoSpectator(player); } } @@ -145,4 +153,15 @@ public class NoClipCommand extends SWCommand implements Listener { event.setCancelled(true); } } + + private static void pseudoSpectator(Player player) { + ProtocolAPI.tinyProtocol.sendPacket(player, playerInfoPacket(updateGamemode, new GameProfile(player.getUniqueId(), player.getName()), spectator)); + } + + private static Object playerInfoPacket(Object action, GameProfile profile, Object mode) { + Object packet = playerInfoConstructor.invoke(); + playerInfoAction.set(packet, action); + playerInfoData.set(packet, Collections.singletonList(playerInfoDataConstructor.invoke(packet, profile, 0, mode, null))); + return packet; + } } \ No newline at end of file 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 dc15ef2a..5d525f0c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/SignEdit.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/SignEdit.java @@ -19,17 +19,15 @@ package de.steamwar.bausystem.features.world; -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.wrappers.BlockPosition; +import com.comphenix.tinyprotocol.Reflection; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.utils.ProtocolAPI; +import net.minecraft.server.v1_15_R1.BlockPosition; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.Sign; @@ -38,13 +36,18 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; - -import java.lang.reflect.InvocationTargetException; -import java.util.logging.Level; +import org.bukkit.util.Vector; @Linked(LinkageType.LISTENER) public class SignEdit implements Listener { + public static final Class openSign = Reflection.getClass("{nms}.PacketPlayOutOpenSignEditor"); + private static final Reflection.FieldAccessor blockPositionFieldAccessor = Reflection.getField(openSign, net.minecraft.server.v1_15_R1.BlockPosition.class, 0); + + public static final Class updateSign = Reflection.getClass("{nms}.PacketPlayInUpdateSign"); + private static final Reflection.FieldAccessor getBlockPositionFieldAccessor = Reflection.getField(updateSign, BlockPosition.class, 0); + private static final Reflection.FieldAccessor stringFieldAccessor = Reflection.getField(updateSign, String[].class, 0); + @EventHandler public void editSign(PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK || @@ -62,38 +65,29 @@ public class SignEdit implements Listener { } sign.update(); - PacketContainer signOpen = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.OPEN_SIGN_EDITOR); - signOpen.getBlockPositionModifier().write(0, new BlockPosition(event.getClickedBlock().getLocation().toVector())); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, signOpen); - } catch (InvocationTargetException e) { - Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e); - } + Object openSignObject = Reflection.newInstance(openSign); + Vector vector = event.getClickedBlock().getLocation().toVector(); + blockPositionFieldAccessor.set(openSignObject, new BlockPosition(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ())); + ProtocolAPI.tinyProtocol.sendPacket(player, openSignObject); + } - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.UPDATE_SIGN) { - @Override - public void onPacketReceiving(PacketEvent event) { - if (!event.getPlayer().equals(player)) + { + ProtocolAPI.setIncomingHandler(updateSign, (player, o) -> { + Bukkit.getScheduler().runTask(BauSystem.getInstance(), () -> { + BlockPosition position = getBlockPositionFieldAccessor.get(o); + String[] lines = stringFieldAccessor.get(o); + + Block signLoc = new Location(player.getWorld(), position.getX(), position.getY(), position.getZ()).getBlock(); + if (!signLoc.getType().name().contains("SIGN")) return; - event.setCancelled(true); - Bukkit.getScheduler().runTask(BauSystem.getInstance(), () -> { - PacketContainer packetContainer = event.getPacket(); - BlockPosition position = packetContainer.getBlockPositionModifier().read(0); - String[] lines = packetContainer.getStringArrays().read(0); - Block signLoc = position.toLocation(player.getWorld()).getBlock(); - if (!signLoc.getType().name().contains("SIGN")) - return; - - org.bukkit.block.Sign sign = ((Sign) signLoc.getState()); - for (int i = 0; i < lines.length; i++) { - sign.setLine(i, ChatColor.translateAlternateColorCodes('&', lines[i])); - } - sign.update(); - - ProtocolLibrary.getProtocolManager().removePacketListener(this); - }); - } + org.bukkit.block.Sign sign = ((Sign) signLoc.getState()); + for (int i = 0; i < lines.length; i++) { + sign.setLine(i, ChatColor.translateAlternateColorCodes('&', lines[i])); + } + sign.update(); + }); + return o; }); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java new file mode 100644 index 00000000..351a27fb --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/utils/ProtocolAPI.java @@ -0,0 +1,76 @@ +/* + * 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 de.steamwar.bausystem.BauSystem; +import io.netty.channel.Channel; +import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiFunction; + +@UtilityClass +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(BauSystem.getInstance()) { + @Override + public Object onPacketOutAsync(Player receiver, Channel channel, Object 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) { + 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); + } + + public static void removeOutgoingHandler(Class packetClass) { + outgoingHandler.remove(packetClass); + } + + public static void setIncomingHandler(Class packetClass, BiFunction handler) { + incomingHandler.put(packetClass, handler); + } + + public static void removeIncomingHandler(Class packetClass) { + incomingHandler.remove(packetClass); + } + + public static void broadcastPacket(Object packet) { + Bukkit.getOnlinePlayers().stream().map(tinyProtocol::getChannel).filter(tinyProtocol::hasInjected).forEach(channel -> tinyProtocol.sendPacket(channel, packet)); + } +} diff --git a/BauSystem_Main/src/plugin.yml b/BauSystem_Main/src/plugin.yml index 36a8d970..a75ce068 100644 --- a/BauSystem_Main/src/plugin.yml +++ b/BauSystem_Main/src/plugin.yml @@ -1,7 +1,7 @@ name: BauSystem authors: [Lixfel, YoyoNow, Chaoscaot, Zeanon] version: "2.0" -depend: [WorldEdit, SpigotCore, ProtocolLib] +depend: [WorldEdit, SpigotCore] load: POSTWORLD main: de.steamwar.bausystem.BauSystem api-version: "1.13"