From bfbc2415a56b2639d3bb56c07128599e340c26bc Mon Sep 17 00:00:00 2001 From: fullwall Date: Wed, 16 Nov 2022 21:58:05 +0800 Subject: [PATCH] Enforce collidability even if not explicitly set --- .../main/java/net/citizensnpcs/Citizens.java | 4 ++ .../net/citizensnpcs/ProtocolLibListener.java | 49 +++++++++++++++++++ .../citizensnpcs/commands/NPCCommands.java | 4 +- .../citizensnpcs/trait/ScoreboardTrait.java | 5 +- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 main/src/main/java/net/citizensnpcs/ProtocolLibListener.java diff --git a/main/src/main/java/net/citizensnpcs/Citizens.java b/main/src/main/java/net/citizensnpcs/Citizens.java index 5760bd85e..dd420c32c 100644 --- a/main/src/main/java/net/citizensnpcs/Citizens.java +++ b/main/src/main/java/net/citizensnpcs/Citizens.java @@ -99,6 +99,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { } }; private CitizensNPCRegistry npcRegistry; + private ProtocolLibListener protocolListener; private boolean saveOnDisable = true; private NPCDataStore saves; private NPCSelector selector; @@ -393,6 +394,9 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { new CitizensPlaceholders(selector).register(); } + if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) { + protocolListener = new ProtocolLibListener(this); + } setupEconomy(); diff --git a/main/src/main/java/net/citizensnpcs/ProtocolLibListener.java b/main/src/main/java/net/citizensnpcs/ProtocolLibListener.java new file mode 100644 index 000000000..67563d223 --- /dev/null +++ b/main/src/main/java/net/citizensnpcs/ProtocolLibListener.java @@ -0,0 +1,49 @@ +package net.citizensnpcs; + +import org.bukkit.entity.Entity; + +import com.comphenix.protocol.PacketType.Play.Server; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketEvent; + +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.npc.ai.NPCHolder; +import net.citizensnpcs.trait.SmoothRotationTrait; + +public class ProtocolLibListener { + private final ProtocolManager manager; + private final Citizens plugin; + + public ProtocolLibListener(Citizens plugin) { + this.plugin = plugin; + this.manager = ProtocolLibrary.getProtocolManager(); + manager.addPacketListener( + new PacketAdapter(plugin, ListenerPriority.MONITOR, Server.ENTITY_HEAD_ROTATION, Server.ENTITY_LOOK) { + @Override + public void onPacketSending(PacketEvent event) { + Entity entity = manager.getEntityFromID(event.getPlayer().getWorld(), + event.getPacket().getIntegers().getValues().get(0)); + if (!(entity instanceof NPCHolder)) + return; + NPC npc = ((NPCHolder) entity).getNPC(); + SmoothRotationTrait trait = npc.getTraitNullable(SmoothRotationTrait.class); + if (trait == null) + return; + + if (event.getPacketType() == Server.ENTITY_HEAD_ROTATION) { + byte headYaw = event.getPacket().getBytes().read(0); + } else if (event.getPacketType() == Server.ENTITY_LOOK) { + byte yaw = event.getPacket().getBytes().read(0); + byte pitch = event.getPacket().getBytes().read(1); + } else if (event.getPacketType() == Server.ENTITY_MOVE_LOOK + || event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK) { + byte yaw = event.getPacket().getBytes().read(0); + byte pitch = event.getPacket().getBytes().read(1); + } + } + }); + } +} diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 0f483d8a5..5f0e4f459 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -394,9 +394,9 @@ public class NPCCommands { permission = "citizens.npc.collidable") @Requirements(ownership = true, selected = true) public void collidable(CommandContext args, CommandSender sender, NPC npc) throws CommandException { - npc.data().setPersistent(NPC.COLLIDABLE_METADATA, !npc.data().get(NPC.COLLIDABLE_METADATA, false)); + npc.data().setPersistent(NPC.Metadata.COLLIDABLE, !npc.data().get(NPC.Metadata.COLLIDABLE, false)); Messaging.sendTr(sender, - npc.data(). get(NPC.COLLIDABLE_METADATA) ? Messages.COLLIDABLE_SET : Messages.COLLIDABLE_UNSET, + npc.data(). get(NPC.Metadata.COLLIDABLE) ? Messages.COLLIDABLE_SET : Messages.COLLIDABLE_UNSET, npc.getName()); } diff --git a/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java b/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java index 0371f6d64..8e08c6c1d 100644 --- a/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java @@ -158,9 +158,10 @@ public class ScoreboardTrait extends Trait { } } - if (SUPPORT_COLLIDABLE_SETOPTION && npc.data().has(NPC.COLLIDABLE_METADATA)) { + if (SUPPORT_COLLIDABLE_SETOPTION) { try { - OptionStatus collide = npc.data(). get(NPC.COLLIDABLE_METADATA) ? OptionStatus.ALWAYS + OptionStatus collide = npc.data(). get(NPC.COLLIDABLE_METADATA, !npc.isProtected()) + ? OptionStatus.ALWAYS : OptionStatus.NEVER; if (collide != team.getOption(Option.COLLISION_RULE)) { changed = true;