SteamWar/BauSystem2.0
Archiviert
12
0

TinyProtocol #51

Zusammengeführt
YoyoNow hat 12 Commits von TinyProtocol nach master 2021-10-12 21:38:59 +02:00 zusammengeführt
Nur Änderungen aus Commit 81d1b4a914 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -19,17 +19,15 @@
package de.steamwar.bausystem.features.world; package de.steamwar.bausystem.features.world;
import com.comphenix.protocol.PacketType; import com.comphenix.tinyprotocol.Reflection;
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 de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked; 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.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
@ -38,13 +36,18 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.util.Vector;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
@Linked(LinkageType.LISTENER) @Linked(LinkageType.LISTENER)
public class SignEdit implements Listener { public class SignEdit implements Listener {
public static final Class<?> openSign = Reflection.getClass("{nms}.PacketPlayOutOpenSignEditor");
private static final Reflection.FieldAccessor<net.minecraft.server.v1_15_R1.BlockPosition> 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<BlockPosition> getBlockPositionFieldAccessor = Reflection.getField(updateSign, BlockPosition.class, 0);
private static final Reflection.FieldAccessor<String[]> stringFieldAccessor = Reflection.getField(updateSign, String[].class, 0);
@EventHandler @EventHandler
public void editSign(PlayerInteractEvent event) { public void editSign(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || if (event.getAction() != Action.RIGHT_CLICK_BLOCK ||
@ -62,38 +65,29 @@ public class SignEdit implements Listener {
} }
sign.update(); sign.update();
PacketContainer signOpen = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.OPEN_SIGN_EDITOR); Object openSignObject = Reflection.newInstance(openSign);
signOpen.getBlockPositionModifier().write(0, new BlockPosition(event.getClickedBlock().getLocation().toVector())); Vector vector = event.getClickedBlock().getLocation().toVector();
try { blockPositionFieldAccessor.set(openSignObject, new BlockPosition(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()));
ProtocolLibrary.getProtocolManager().sendServerPacket(player, signOpen); ProtocolAPI.tinyProtocol.sendPacket(player, openSignObject);
} catch (InvocationTargetException e) { }
Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e);
}
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.UPDATE_SIGN) { {
@Override ProtocolAPI.setIncomingHandler(updateSign, (player, o) -> {
public void onPacketReceiving(PacketEvent event) { Bukkit.getScheduler().runTask(BauSystem.getInstance(), () -> {
if (!event.getPlayer().equals(player)) 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; 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(); org.bukkit.block.Sign sign = ((Sign) signLoc.getState());
if (!signLoc.getType().name().contains("SIGN")) for (int i = 0; i < lines.length; i++) {
return; sign.setLine(i, ChatColor.translateAlternateColorCodes('&', lines[i]));
}
org.bukkit.block.Sign sign = ((Sign) signLoc.getState()); sign.update();
for (int i = 0; i < lines.length; i++) { });
sign.setLine(i, ChatColor.translateAlternateColorCodes('&', lines[i])); return o;
}
sign.update();
ProtocolLibrary.getProtocolManager().removePacketListener(this);
});
}
}); });
} }
} }