Skip to content

Commit

Permalink
Enforce collidability even if not explicitly set
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Nov 16, 2022
1 parent 9d00285 commit bfbc241
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
4 changes: 4 additions & 0 deletions main/src/main/java/net/citizensnpcs/Citizens.java
Expand Up @@ -99,6 +99,7 @@ public void updateInventoryTitle(Player player, InventoryView view, String newTi
}
};
private CitizensNPCRegistry npcRegistry;
private ProtocolLibListener protocolListener;
private boolean saveOnDisable = true;
private NPCDataStore saves;
private NPCSelector selector;
Expand Down Expand Up @@ -393,6 +394,9 @@ public void onEnable() {
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new CitizensPlaceholders(selector).register();
}
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
protocolListener = new ProtocolLibListener(this);
}

setupEconomy();

Expand Down
49 changes: 49 additions & 0 deletions 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);
}
}
});
}
}
4 changes: 2 additions & 2 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -394,9 +394,9 @@ public void chunkload(CommandContext args, CommandSender sender, NPC npc) {
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().<Boolean> get(NPC.COLLIDABLE_METADATA) ? Messages.COLLIDABLE_SET : Messages.COLLIDABLE_UNSET,
npc.data().<Boolean> get(NPC.Metadata.COLLIDABLE) ? Messages.COLLIDABLE_SET : Messages.COLLIDABLE_UNSET,
npc.getName());
}

Expand Down
Expand Up @@ -158,9 +158,10 @@ public void update() {
}
}

if (SUPPORT_COLLIDABLE_SETOPTION && npc.data().has(NPC.COLLIDABLE_METADATA)) {
if (SUPPORT_COLLIDABLE_SETOPTION) {
try {
OptionStatus collide = npc.data().<Boolean> get(NPC.COLLIDABLE_METADATA) ? OptionStatus.ALWAYS
OptionStatus collide = npc.data().<Boolean> get(NPC.COLLIDABLE_METADATA, !npc.isProtected())
? OptionStatus.ALWAYS
: OptionStatus.NEVER;
if (collide != team.getOption(Option.COLLISION_RULE)) {
changed = true;
Expand Down

0 comments on commit bfbc241

Please sign in to comment.