From 81d1b4a914f40b2e835401bd88f46d6012eeb298 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 12 Oct 2021 20:50:31 +0200 Subject: [PATCH] Add tinyprotocol for SignEdit Signed-off-by: yoyosource --- .../bausystem/features/world/SignEdit.java | 70 +++++++++---------- 1 file changed, 32 insertions(+), 38 deletions(-) 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; }); } }