From b97d83333bd2fdddf7ff5c6cd122ee997afa8f4a Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 6 May 2020 10:47:29 +0200 Subject: [PATCH] refactor: use correct EventHandler implementation --- .../varo/listeners/BannedItemListener.java | 4 +- .../varo/listeners/ChatListener.java | 2 +- .../varo/listeners/CreatureSpawnListener.java | 2 +- .../listeners/EntityRegainHealthListener.java | 2 +- .../PlayerClientOptionsChangeListener.java | 2 +- .../varo/listeners/PlayerDeathListener.java | 2 +- .../listeners/PlayerPreLoginListener.java | 2 +- .../cliffbreak/varo/uitls/PacketReader.java | 79 +++++++++++++++++++ 8 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 varo/src/main/java/de/cliffbreak/varo/uitls/PacketReader.java diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/BannedItemListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/BannedItemListener.java index 9ebb33a..89c3633 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/BannedItemListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/BannedItemListener.java @@ -9,7 +9,7 @@ import org.bukkit.event.inventory.InventoryClickEvent; public class BannedItemListener implements Listener { - @EventHandler() + @EventHandler public void onEntityPickupItem(EntityPickupItemEvent e) { if (e.getEntity() instanceof Player) { Material mat = e.getItem().getItemStack().getType(); @@ -21,7 +21,7 @@ public class BannedItemListener implements Listener { } } - @EventHandler() + @EventHandler public void onInventoryClick(InventoryClickEvent e) { if (e.getCurrentItem() == null) return; diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java index a6eb88e..a9a40ab 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java @@ -9,7 +9,7 @@ import de.cliffbreak.varo.uitls.MessageUtils; public class ChatListener implements Listener { - @EventHandler() + @EventHandler public void onChat(AsyncPlayerChatEvent e) { e.setCancelled(true); Bukkit.broadcast(MessageUtils.getRichTextComponent(e.getPlayer().getName(), e.getMessage())); diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/CreatureSpawnListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/CreatureSpawnListener.java index 1791bad..d4dbfa8 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/CreatureSpawnListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/CreatureSpawnListener.java @@ -16,7 +16,7 @@ public class CreatureSpawnListener implements Listener { this.plugin = plugin; } - @EventHandler() + @EventHandler public void onCreatureSpawn(CreatureSpawnEvent e) { LivingEntity livingEntity = e.getEntity(); if (livingEntity.getType().equals(EntityType.WANDERING_TRADER) diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/EntityRegainHealthListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/EntityRegainHealthListener.java index 99f2af4..8b7a540 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/EntityRegainHealthListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/EntityRegainHealthListener.java @@ -7,7 +7,7 @@ import org.bukkit.event.entity.EntityRegainHealthEvent; public class EntityRegainHealthListener implements Listener { - @EventHandler() + @EventHandler public void onEntityRegainHealth(EntityRegainHealthEvent e) { if (e.getEntity() instanceof Player) { if (e.isFastRegen()) { diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerClientOptionsChangeListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerClientOptionsChangeListener.java index 1b280b8..d2610c1 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerClientOptionsChangeListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerClientOptionsChangeListener.java @@ -16,7 +16,7 @@ public class PlayerClientOptionsChangeListener implements Listener { this.plugin = plugin; } - @EventHandler() + @EventHandler public void onPlayerClientOptionsChange(PlayerClientOptionsChangeEvent e) { String uuid = e.getPlayer().getUniqueId().toString().replace("-", ""); plugin.playerCache.setPlayerCache(uuid, PlayerCacheType.SKINPARTS, e.getSkinParts().getRaw()); diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java index 95db3e7..93f04a1 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java @@ -18,7 +18,7 @@ public class PlayerDeathListener implements Listener { this.plugin = plugin; } - @EventHandler() + @EventHandler public void onPlayerDeath(final PlayerDeathEvent e) { Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { @Override diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java index aad6e97..e414577 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java @@ -17,7 +17,7 @@ public class PlayerPreLoginListener implements Listener { this.plugin = plugin; } - @EventHandler() + @EventHandler public void onPlayerPreLogin(final AsyncPlayerPreLoginEvent e) { @SuppressWarnings("unchecked") final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); diff --git a/varo/src/main/java/de/cliffbreak/varo/uitls/PacketReader.java b/varo/src/main/java/de/cliffbreak/varo/uitls/PacketReader.java new file mode 100644 index 0000000..058a045 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/uitls/PacketReader.java @@ -0,0 +1,79 @@ +package de.cliffbreak.varo.uitls; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; +import net.minecraft.server.v1_15_R1.Packet; + +import java.lang.reflect.Field; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.entity.Player; + +public class PacketReader { + + Player player; + Channel channel; + + public PacketReader(final Player player) { + this.player = player; + } + + public void inject() { + final CraftPlayer cPlayer = (CraftPlayer) this.player; + channel = cPlayer.getHandle().playerConnection.networkManager.channel; + channel.pipeline().addAfter("decoder", "PacketInjector", new MessageToMessageDecoder>() { + @Override + protected void decode(final ChannelHandlerContext arg0, final Packet packet, final List arg2) + throws Exception { + arg2.add(packet); + readPacket(packet); + } + }); + } + + public void uninject() { + if (channel.pipeline().get("PacketInjector") != null) { + channel.pipeline().remove("PacketInjector"); + } + } + + public void readPacket(final Packet packet) { + if (packet.getClass().getSimpleName().equalsIgnoreCase("PacketPlayInUseEntity")) { + final int id = (Integer) getValue(packet, "a"); + + System.out.println(getValue(packet, "action").toString()); + System.out.println(getValue(packet, "a").toString()); + + // if (Main.npc.getEntityID() == id) { + // if (getValue(packet, "action").toString().equalsIgnoreCase("ATTACK")) { + // Main.npc.animation(1); + // } else if (getValue(packet, "action").toString().equalsIgnoreCase("INTERACT")) { + + // } + // } + } + } + + public void setValue(final Object obj, final String name, final Object value) { + try { + final Field field = obj.getClass().getDeclaredField(name); + field.setAccessible(true); + field.set(obj, value); + } catch (final Exception e) { + } + } + + public Object getValue(final Object obj, final String name) { + try { + final Field field = obj.getClass().getDeclaredField(name); + field.setAccessible(true); + return field.get(obj); + } catch (final Exception e) { + } + return null; + } + +} \ No newline at end of file