SteamWar/BauSystem2.0
Archiviert
12
0

Add tinyprotocol for SignEdit
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-10-12 20:50:31 +02:00
Ursprung e71931e757
Commit 81d1b4a914

Datei anzeigen

@ -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<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
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;
});
}
}