diff --git a/main/src/main/java/net/citizensnpcs/Citizens.java b/main/src/main/java/net/citizensnpcs/Citizens.java index 0a4e359f7..ca41439c1 100644 --- a/main/src/main/java/net/citizensnpcs/Citizens.java +++ b/main/src/main/java/net/citizensnpcs/Citizens.java @@ -70,6 +70,7 @@ import net.citizensnpcs.commands.TemplateCommands; import net.citizensnpcs.commands.TraitCommands; import net.citizensnpcs.commands.WaypointCommands; import net.citizensnpcs.editor.Editor; +import net.citizensnpcs.model.ModelRegistry; import net.citizensnpcs.npc.CitizensNPCRegistry; import net.citizensnpcs.npc.CitizensTraitFactory; import net.citizensnpcs.npc.NPCSelector; @@ -93,6 +94,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { private Settings config; private boolean enabled; private LocationLookup locationLookup; + private ModelRegistry modelRegistry; private final NMSHelper nmsHelper = new NMSHelper() { private boolean SUPPORT_OWNER_PROFILE = true; @@ -245,6 +247,10 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { return locationLookup; } + public ModelRegistry getModelRegistry() { + return modelRegistry; + } + @Override public NPCRegistry getNamedNPCRegistry(String name) { if (name.equals(npcRegistry.getName())) @@ -384,6 +390,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { Editor.leaveAll(); despawnNPCs(saveOnDisable); HandlerList.unregisterAll(this); + modelRegistry.reset(); npcRegistry = null; locationLookup = null; enabled = false; @@ -436,6 +443,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { return new ShopTrait(shops); })); selector = new NPCSelector(this); + modelRegistry = new ModelRegistry(); Bukkit.getPluginManager().registerEvents(new EventListen(storedRegistries), this); Bukkit.getPluginManager().registerEvents(new Placeholders(), this); @@ -509,9 +517,12 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { despawnNPCs(false); ProfileFetcher.reset(); Skin.clearCache(); + modelRegistry.reset(); getServer().getPluginManager().callEvent(new CitizensPreReloadEvent()); + modelRegistry.load(new File(Setting.RESOURCE_PACK_PATH.asString()), new File(getDataFolder(), "models")); + saves.reloadFromSource(); saves.loadInto(npcRegistry); @@ -617,6 +628,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { private class CitizensLoadTask implements Runnable { @Override public void run() { + modelRegistry.load(new File(Setting.RESOURCE_PACK_PATH.asString()), new File(getDataFolder(), "models")); saves.loadInto(npcRegistry); shops.load(); diff --git a/main/src/main/java/net/citizensnpcs/Settings.java b/main/src/main/java/net/citizensnpcs/Settings.java index 3c5000398..aff67411b 100644 --- a/main/src/main/java/net/citizensnpcs/Settings.java +++ b/main/src/main/java/net/citizensnpcs/Settings.java @@ -160,6 +160,7 @@ public class Settings { PLACEHOLDER_SKIN_UPDATE_FREQUENCY("npc.skins.placeholder-update-frequency-ticks", 5 * 60 * 20), PLAYER_TELEPORT_DELAY("npc.teleport-delay", "npc.delay-player-teleport-ticks", -1), REMOVE_PLAYERS_FROM_PLAYER_LIST("npc.player.remove-from-list", true), + RESOURCE_PACK_PATH("general.resource-pack-path", "plugins/Citizens/resourcepack"), SAVE_TASK_DELAY("storage.save-task.delay", 20 * 60 * 60), SCOREBOARD_SEND_TICKS("npc.scoreboard-teams.packet-send-ticks", 1), SELECTION_ITEM("npc.selection.item", "stick"), diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index e7a5a20e0..5ce5a3a66 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -84,6 +84,7 @@ import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.trait.Inventory; import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.Owner; +import net.citizensnpcs.api.trait.trait.PlayerFilter; import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.api.trait.trait.Speech; import net.citizensnpcs.api.util.EntityDim; @@ -642,7 +643,7 @@ public class NPCCommands { @Command( aliases = { "npc" }, - usage = "create [name] ((-b(aby),u(nspawned),s(ilent),t(emporary),c(enter),p(acket)) --at [x:y:z:world] --type [type] --item (item) --trait ['trait1, trait2...'] --nameplate [true|false|hover] --temporaryticks [ticks] --registry [registry name]", + usage = "create [name] ((-b(aby),u(nspawned),s(ilent),t(emporary),c(enter),p(acket)) --at [x:y:z:world] --type [type] --item (item) --trait ['trait1, trait2...'] --model [model name] --nameplate [true|false|hover] --temporaryticks [ticks] --registry [registry name]", desc = "Create a new NPC", flags = "bustpc", modifiers = { "create" }, @@ -652,7 +653,7 @@ public class NPCCommands { public void create(CommandContext args, CommandSender sender, NPC npc, @Flag("at") Location at, @Flag(value = "type", defValue = "PLAYER") EntityType type, @Flag("trait") String traits, @Flag(value = "nameplate", completions = { "true", "false", "hover" }) String nameplate, - @Flag("temporaryticks") Integer temporaryTicks, @Flag("item") String item, + @Flag("temporaryticks") Integer temporaryTicks, @Flag("item") String item, @Flag("model") String model, @Flag("template") String templateName, @Flag("registry") String registryName) throws CommandException { String name = args.getJoinedStrings(1).trim(); if (args.hasValueFlag("type")) { @@ -689,6 +690,10 @@ public class NPCCommands { registry = temporaryRegistry; } + if (model != null) { + type = EntityType.ARMOR_STAND; + } + if (item != null) { ItemStack stack = new ItemStack(Material.STONE, 1); try { @@ -2066,6 +2071,31 @@ public class NPCCommands { animation.play((Player) npc.getEntity(), 64); } + @Command( + aliases = { "npc" }, + usage = "playerfilter --hide [uuid] --unhide [uuid] --only [uuid]", + desc = "Sets the NPC filter", + modifiers = { "playerfilter" }, + min = 1, + max = 1, + permission = "citizens.npc.playerfilter") + public void playerfilter(CommandContext args, CommandSender sender, NPC npc, @Flag("hide") UUID hide, + @Flag("unhide") UUID unhide, @Flag("only") UUID only) { + PlayerFilter trait = npc.getOrAddTrait(PlayerFilter.class); + if (hide != null) { + trait.hide(hide); + Messaging.sendTr(sender, Messages.PLAYERFILTER_PLAYER_HIDDEN, hide, npc.getName()); + } + if (unhide != null) { + trait.unhide(unhide); + Messaging.sendTr(sender, Messages.PLAYERFILTER_PLAYER_UNHIDDEN, unhide, npc.getName()); + } + if (only != null) { + trait.only(only); + Messaging.sendTr(sender, Messages.PLAYERFILTER_PLAYER_ONLY_ADDED, only, npc.getName()); + } + } + @Command( aliases = { "npc" }, usage = "playerlist (-a,r)", diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index ff008a017..ffe814ceb 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -378,6 +378,8 @@ public class CitizensNPC extends AbstractNPC { } EntityType type = getEntity().getType(); + NMS.replaceTracker(getEntity()); + if (type.isAlive()) { LivingEntity entity = (LivingEntity) getEntity(); entity.setRemoveWhenFarAway(false); @@ -387,7 +389,6 @@ public class CitizensNPC extends AbstractNPC { } if (type == EntityType.PLAYER) { - NMS.replaceTrackerEntry((Player) getEntity()); PlayerUpdateTask.registerPlayer(getEntity()); } else if (data().has(NPC.Metadata.AGGRESSIVE)) { NMS.setAggressive(entity, data(). get(NPC.Metadata.AGGRESSIVE)); diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index 17924af34..9a3f50a37 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java @@ -17,8 +17,10 @@ import net.citizensnpcs.api.trait.trait.Equipment; import net.citizensnpcs.api.trait.trait.Inventory; import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.Owner; +import net.citizensnpcs.api.trait.trait.PlayerFilter; import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.api.trait.trait.Speech; +import net.citizensnpcs.model.ModelTrait; import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Anchors; import net.citizensnpcs.trait.ArmorStandTrait; @@ -89,12 +91,14 @@ public class CitizensTraitFactory implements TraitFactory { registerTrait(TraitInfo.create(Inventory.class)); registerTrait(TraitInfo.create(LookClose.class)); registerTrait(TraitInfo.create(MirrorTrait.class)); + registerTrait(TraitInfo.create(ModelTrait.class).withSupplier(() -> new ModelTrait(plugin.getModelRegistry()))); registerTrait(TraitInfo.create(MountTrait.class)); registerTrait(TraitInfo.create(MobType.class).asDefaultTrait()); registerTrait(TraitInfo.create(OcelotModifiers.class)); registerTrait(TraitInfo.create(Owner.class)); registerTrait(TraitInfo.create(PacketNPC.class)); registerTrait(TraitInfo.create(PausePathfindingTrait.class)); + registerTrait(TraitInfo.create(PlayerFilter.class)); registerTrait(TraitInfo.create(Poses.class)); registerTrait(TraitInfo.create(Powered.class)); registerTrait(TraitInfo.create(RabbitType.class)); diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index 3807360b6..7fdd89a42 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -318,6 +318,9 @@ public class Messages { public static final String PIGLIN_DANCING_SET = "citizens.commands.npc.piglin.dancing-set"; public static final String PIGLIN_DANCING_UNSET = "citizens.commands.npc.piglin.dancing-unset"; public static final String PLAYER_NOT_FOUND_FOR_SPAWN = "citizens.commands.npc.create.no-player-for-spawn"; + public static final String PLAYERFILTER_PLAYER_HIDDEN = "citizens.commands.npc.playerfilter.hidden"; + public static final String PLAYERFILTER_PLAYER_ONLY_ADDED = "citizens.commands.npc.playerfilter.only-added"; + public static final String PLAYERFILTER_PLAYER_UNHIDDEN = "citizens.commands.npc.playerfilter.unhidden"; public static final String POLAR_BEAR_REARING = "citizens.commands.npc.polarbear.rearing-set"; public static final String POLAR_BEAR_STOPPED_REARING = "citizens.commands.npc.polarbear.rearing-unset"; public static final String POSE_ADDED = "citizens.commands.npc.pose.added"; diff --git a/main/src/main/java/net/citizensnpcs/util/NMS.java b/main/src/main/java/net/citizensnpcs/util/NMS.java index eeae8af3f..381ee8993 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMS.java +++ b/main/src/main/java/net/citizensnpcs/util/NMS.java @@ -90,6 +90,10 @@ public class NMS { * an Exception like it should. */ + public static EntityPacketTracker createPacketTracker(Entity entity) { + return BRIDGE.createPacketTracker(entity); + } + public static void enderTeleportTo(NPC npc, Runnable cb) { if (npc == null) { cb.run(); @@ -229,6 +233,18 @@ public class NMS { return found; } + public static MethodHandle getFirstFinalSetter(Class clazz, Class type) { + try { + Field found = getFirstFieldMatchingType(clazz, type, false); + if (found == null) + return null; + return getFinalSetter(clazz, found.getName()); + } catch (Exception e) { + Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage()); + } + return null; + } + public static MethodHandle getFirstGetter(Class clazz, Class type) { try { Field found = getFirstFieldMatchingType(clazz, type, false); @@ -374,10 +390,6 @@ public class NMS { return BRIDGE.getPassengers(entity); } - public static EntityPacketTracker createPacketTracker(Entity entity) { - return BRIDGE.createPacketTracker(entity); - } - public static GameProfile getProfile(Player player) { return BRIDGE.getProfile(player); } @@ -563,8 +575,8 @@ public class NMS { BRIDGE.removeHookIfNecessary(npcRegistry, entity); } - public static void replaceTrackerEntry(Player player) { - BRIDGE.replaceTrackerEntry(player); + public static void replaceTracker(Entity entity) { + BRIDGE.replaceTrackerEntry(entity); } public static void sendPositionUpdate(Player excluding, org.bukkit.entity.Entity from, Location location) { diff --git a/main/src/main/java/net/citizensnpcs/util/NMSBridge.java b/main/src/main/java/net/citizensnpcs/util/NMSBridge.java index d51fd32f4..d6d7aa89a 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMSBridge.java +++ b/main/src/main/java/net/citizensnpcs/util/NMSBridge.java @@ -150,7 +150,7 @@ public interface NMSBridge { public void removeHookIfNecessary(NPCRegistry npcRegistry, FishHook entity);; - public void replaceTrackerEntry(Player player); + public void replaceTrackerEntry(Entity entity); public void sendPositionUpdate(Player excluding, Entity from, Location location); diff --git a/main/src/main/resources/messages_en.properties b/main/src/main/resources/messages_en.properties index 14c363124..de8826908 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -220,6 +220,9 @@ citizens.commands.npc.pausepathfinding.rightclick-set=[[{0}]] will now pause pat citizens.commands.npc.pausepathfinding.rightclick-unset=[[{0}]] will no longer pause pathfinding on right click. citizens.commands.npc.playerlist.added=Added [[{0}]] to the player list. citizens.commands.npc.playerlist.removed=Removed [[{0}]] from the player list. +citizens.commands.npc.playerfilter.only-added=[[{0}]] added to the list of players that will be allowed to see [[{1}]]. +citizens.commands.npc.playerfilter.hidden=[[{0}]] added to the list of players hidden from [[{1}]]. +citizens.commands.npc.playerfilter.unhidden=[[{0}]] will now be allowed to see [[{1}]]. citizens.commands.npc.polarbear.rearing-set=[[{0}]] is now rearing. citizens.commands.npc.polarbear.rearing-unset=[[{0}]] is no longer rearing. citizens.commands.npc.pickupitems.set=[[{0}]] will now pickup items. diff --git a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java index 656da53aa..f4b020a09 100644 --- a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java +++ b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java @@ -1020,13 +1020,13 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTrackerEntry entry = server.getTracker().trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTrackerEntry entry = server.getTracker().trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTrackerEntry replace = new PlayerlistTrackerEntry(entry); - server.getTracker().trackedEntities.a(player.getEntityId(), replace); + server.getTracker().trackedEntities.a(entity.getEntityId(), replace); if (TRACKED_ENTITY_SET != null) { try { Set set = (Set) TRACKED_ENTITY_SET.get(server.getTracker()); @@ -1038,8 +1038,8 @@ public class NMSImpl implements NMSBridge { e.printStackTrace(); } } - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(); } } diff --git a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/PlayerlistTrackerEntry.java b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/PlayerlistTrackerEntry.java index 8948b0b88..730124fd2 100644 --- a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/PlayerlistTrackerEntry.java +++ b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/PlayerlistTrackerEntry.java @@ -2,6 +2,7 @@ package net.citizensnpcs.nms.v1_10_R1.util; import java.lang.reflect.Field; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.api.npc.NPC; @@ -28,6 +29,10 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { if (entityplayer instanceof EntityHumanNPC) return; Entity tracker = getTracker(this); + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; + if (tracker.dead || tracker.getBukkitEntity().getType() != EntityType.PLAYER) + return; if (entityplayer != tracker && c(entityplayer)) { if (!this.trackedPlayers.contains(entityplayer) && ((entityplayer.x().getPlayerChunkMap().a(entityplayer, tracker.ac, tracker.ae)) diff --git a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java index e87cada4f..b8ae11ee5 100644 --- a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java +++ b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java @@ -1077,13 +1077,13 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTrackerEntry entry = server.getTracker().trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTrackerEntry entry = server.getTracker().trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTrackerEntry replace = new PlayerlistTrackerEntry(entry); - server.getTracker().trackedEntities.a(player.getEntityId(), replace); + server.getTracker().trackedEntities.a(entity.getEntityId(), replace); if (TRACKED_ENTITY_SET != null) { try { Set set = (Set) TRACKED_ENTITY_SET.get(server.getTracker()); @@ -1095,8 +1095,8 @@ public class NMSImpl implements NMSBridge { e.printStackTrace(); } } - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(); } } diff --git a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java index 30af6b2c9..7062d0971 100644 --- a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java +++ b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/PlayerlistTrackerEntry.java @@ -2,6 +2,7 @@ package net.citizensnpcs.nms.v1_11_R1.util; import java.lang.reflect.Field; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.api.npc.NPC; @@ -28,6 +29,10 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { if (entityplayer instanceof EntityHumanNPC) return; Entity tracker = getTracker(this); + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; + if (tracker.dead || tracker.getBukkitEntity().getType() != EntityType.PLAYER) + return; if (entityplayer != tracker && c(entityplayer)) { if (!this.trackedPlayers.contains(entityplayer) && ((entityplayer.x().getPlayerChunkMap().a(entityplayer, tracker.ab, tracker.ad)) diff --git a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java index b49b10bd2..d1eaad149 100644 --- a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java +++ b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java @@ -1085,13 +1085,13 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTrackerEntry entry = server.getTracker().trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTrackerEntry entry = server.getTracker().trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTrackerEntry replace = new PlayerlistTrackerEntry(entry); - server.getTracker().trackedEntities.a(player.getEntityId(), replace); + server.getTracker().trackedEntities.a(entity.getEntityId(), replace); if (TRACKED_ENTITY_SET != null) { try { Set set = (Set) TRACKED_ENTITY_SET.get(server.getTracker()); @@ -1103,8 +1103,8 @@ public class NMSImpl implements NMSBridge { e.printStackTrace(); } } - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java index b6d73e295..386017505 100644 --- a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java +++ b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/PlayerlistTrackerEntry.java @@ -3,6 +3,7 @@ package net.citizensnpcs.nms.v1_12_R1.util; import java.lang.reflect.Field; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.Settings.Setting; @@ -17,9 +18,11 @@ import net.minecraft.server.v1_12_R1.EntityTrackerEntry; public class PlayerlistTrackerEntry extends EntityTrackerEntry { private EntityPlayer lastUpdatedPlayer; + private final Entity tracker; public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) { super(entity, i, j, k, flag); + this.tracker = getTracker(this); } public PlayerlistTrackerEntry(EntityTrackerEntry entry) { @@ -33,7 +36,8 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { public void updateLastPlayer() { if (lastUpdatedPlayer == null) return; - final Entity tracker = getTracker(this); + if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER) + return; final EntityPlayer entityplayer = lastUpdatedPlayer; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); lastUpdatedPlayer = null; @@ -52,6 +56,8 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { // prevent updates to NPC "viewers" if (entityplayer instanceof EntityHumanNPC) return; + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); lastUpdatedPlayer = null; diff --git a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/NMSImpl.java b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/NMSImpl.java index 99a819813..2437d4760 100644 --- a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/NMSImpl.java +++ b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/NMSImpl.java @@ -1129,13 +1129,13 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTrackerEntry entry = server.getTracker().trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTrackerEntry entry = server.getTracker().trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTrackerEntry replace = new PlayerlistTrackerEntry(entry); - server.getTracker().trackedEntities.a(player.getEntityId(), replace); + server.getTracker().trackedEntities.a(entity.getEntityId(), replace); if (TRACKED_ENTITY_SET != null) { try { Set set = (Set) TRACKED_ENTITY_SET.get(server.getTracker()); @@ -1147,8 +1147,8 @@ public class NMSImpl implements NMSBridge { e.printStackTrace(); } } - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java index 0b7ac19f4..e9daa77b9 100644 --- a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java +++ b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/util/PlayerlistTrackerEntry.java @@ -3,6 +3,7 @@ package net.citizensnpcs.nms.v1_13_R2.util; import java.lang.reflect.Field; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.Settings.Setting; @@ -17,9 +18,11 @@ import net.minecraft.server.v1_13_R2.EntityTrackerEntry; public class PlayerlistTrackerEntry extends EntityTrackerEntry { private EntityPlayer lastUpdatedPlayer; + private final Entity tracker; public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) { super(entity, i, j, k, flag); + tracker = getTracker(this); } public PlayerlistTrackerEntry(EntityTrackerEntry entry) { @@ -31,9 +34,8 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { } public void updateLastPlayer() { - if (lastUpdatedPlayer == null) + if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER) return; - final Entity tracker = getTracker(this); final EntityPlayer entityplayer = lastUpdatedPlayer; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); lastUpdatedPlayer = null; @@ -52,6 +54,8 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { // prevent updates to NPC "viewers" if (entityplayer instanceof EntityHumanNPC) return; + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); lastUpdatedPlayer = null; diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java index 1faa2533e..68409467b 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java @@ -1156,15 +1156,15 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTracker entry = server.getChunkProvider().playerChunkMap.trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTracker entry = server.getChunkProvider().playerChunkMap.trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTracker replace = new PlayerlistTracker(server.getChunkProvider().playerChunkMap, entry); - server.getChunkProvider().playerChunkMap.trackedEntities.put(player.getEntityId(), replace); - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + server.getChunkProvider().playerChunkMap.trackedEntities.put(entity.getEntityId(), replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java index 5b95697ce..0b93fb25d 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java @@ -3,6 +3,7 @@ package net.citizensnpcs.nms.v1_14_R1.util; import java.lang.invoke.MethodHandle; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.Settings.Setting; @@ -31,7 +32,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { } public void updateLastPlayer() { - if (tracker.dead) + if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER) return; final EntityPlayer entityplayer = lastUpdatedPlayer; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); @@ -49,6 +50,9 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { public void updatePlayer(final EntityPlayer entityplayer) { if (!(entityplayer instanceof EntityHumanNPC)) { // prevent updates to NPC "viewers" + if (tracker instanceof NPCHolder + && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); } diff --git a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/NMSImpl.java b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/NMSImpl.java index 342bb2f1d..906738c9e 100644 --- a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/NMSImpl.java +++ b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/NMSImpl.java @@ -1174,15 +1174,15 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTracker entry = server.getChunkProvider().playerChunkMap.trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTracker entry = server.getChunkProvider().playerChunkMap.trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTracker replace = new PlayerlistTracker(server.getChunkProvider().playerChunkMap, entry); - server.getChunkProvider().playerChunkMap.trackedEntities.put(player.getEntityId(), replace); - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + server.getChunkProvider().playerChunkMap.trackedEntities.put(entity.getEntityId(), replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java index 5683b5432..364b3c8d4 100644 --- a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java +++ b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/util/PlayerlistTracker.java @@ -3,6 +3,7 @@ package net.citizensnpcs.nms.v1_15_R1.util; import java.lang.invoke.MethodHandle; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.Settings.Setting; @@ -31,7 +32,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { } public void updateLastPlayer() { - if (tracker.dead) + if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER) return; final EntityPlayer entityplayer = lastUpdatedPlayer; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); @@ -48,6 +49,9 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { @Override public void updatePlayer(final EntityPlayer entityplayer) { if (!(entityplayer instanceof EntityHumanNPC)) { + if (tracker instanceof NPCHolder + && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; // prevent updates to NPC "viewers" this.lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); diff --git a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/NMSImpl.java b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/NMSImpl.java index f21ebcc1e..f4d6643ec 100644 --- a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/NMSImpl.java +++ b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/NMSImpl.java @@ -1204,15 +1204,15 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTracker entry = server.getChunkProvider().playerChunkMap.trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTracker entry = server.getChunkProvider().playerChunkMap.trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTracker replace = new PlayerlistTracker(server.getChunkProvider().playerChunkMap, entry); - server.getChunkProvider().playerChunkMap.trackedEntities.put(player.getEntityId(), replace); - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + server.getChunkProvider().playerChunkMap.trackedEntities.put(entity.getEntityId(), replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java index b41a609ed..cbd9fb186 100644 --- a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java +++ b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/util/PlayerlistTracker.java @@ -3,6 +3,7 @@ package net.citizensnpcs.nms.v1_16_R3.util; import java.lang.invoke.MethodHandle; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.Settings.Setting; @@ -32,11 +33,9 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { } public void updateLastPlayer() { - if (tracker.dead) + if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER) return; final EntityPlayer entityplayer = lastUpdatedPlayer; - if (entityplayer == null) - return; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); if (!Setting.DISABLE_TABLIST.asBoolean()) return; @@ -53,6 +52,8 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { public void updatePlayer(final EntityPlayer entityplayer) { if (entityplayer instanceof EntityHumanNPC) // prevent updates to NPC "viewers" return; + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; this.lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); } diff --git a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/NMSImpl.java b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/NMSImpl.java index 76068929d..ea97622c6 100644 --- a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/NMSImpl.java +++ b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/NMSImpl.java @@ -392,8 +392,9 @@ public class NMSImpl implements NMSBridge { ((Mob) handle).doHurtTarget(target); return; } - AttributeInstance attackDamage = handle.getAttribute(Attributes.ATTACK_DAMAGE); - float f = (float) (attackDamage == null ? 1 : attackDamage.getValue()); + float f = (float) (handle.getAttributes().hasAttribute(Attributes.ATTACK_DAMAGE) + ? handle.getAttributeValue(Attributes.ATTACK_DAMAGE) + : 1f); int i = 0; f += EnchantmentHelper.getDamageBonus(handle.getMainHandItem(), target.getMobType()); i += EnchantmentHelper.getKnockbackBonus(handle); @@ -1193,15 +1194,15 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - ServerLevel server = (ServerLevel) NMSImpl.getHandle(player).level; - TrackedEntity entry = server.getChunkProvider().chunkMap.G.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + ServerLevel server = (ServerLevel) NMSImpl.getHandle(entity).level; + TrackedEntity entry = server.getChunkProvider().chunkMap.G.get(entity.getEntityId()); if (entry == null) return; PlayerlistTracker replace = new PlayerlistTracker(server.getChunkProvider().chunkMap, entry); - server.getChunkProvider().chunkMap.G.put(player.getEntityId(), replace); - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + server.getChunkProvider().chunkMap.G.put(entity.getEntityId(), replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerlistTracker.java b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerlistTracker.java index a53c34eea..96277cb93 100644 --- a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerlistTracker.java +++ b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerlistTracker.java @@ -3,6 +3,7 @@ package net.citizensnpcs.nms.v1_17_R1.util; import java.lang.invoke.MethodHandle; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.Settings.Setting; @@ -32,11 +33,10 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity { } public void updateLastPlayer() { - if (tracker.isRemoved()) + if (tracker.isRemoved() || lastUpdatedPlayer == null + || tracker.getBukkitEntity().getType() != EntityType.PLAYER) return; final ServerPlayer entityplayer = lastUpdatedPlayer; - if (entityplayer == null) - return; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); if (!Setting.DISABLE_TABLIST.asBoolean()) return; @@ -53,6 +53,8 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity { public void updatePlayer(final ServerPlayer entityplayer) { if (entityplayer instanceof EntityHumanNPC) // prevent updates to NPC "viewers" return; + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; this.lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); } diff --git a/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/NMSImpl.java b/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/NMSImpl.java index 65a0da47c..b782cda0f 100644 --- a/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/NMSImpl.java +++ b/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/NMSImpl.java @@ -396,8 +396,9 @@ public class NMSImpl implements NMSBridge { ((Mob) source).doHurtTarget(target); return; } - AttributeInstance attackDamage = source.getAttribute(Attributes.ATTACK_DAMAGE); - float f = (float) (attackDamage == null ? 1 : attackDamage.getValue()); + float f = (float) (source.getAttributes().hasAttribute(Attributes.ATTACK_DAMAGE) + ? source.getAttributeValue(Attributes.ATTACK_DAMAGE) + : 1f); int i = 0; f += EnchantmentHelper.getDamageBonus(source.getMainHandItem(), target.getMobType()); i += EnchantmentHelper.getKnockbackBonus(source); @@ -1198,15 +1199,15 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - ServerLevel server = (ServerLevel) NMSImpl.getHandle(player).level; - TrackedEntity entry = server.getChunkSource().chunkMap.entityMap.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + ServerLevel server = (ServerLevel) NMSImpl.getHandle(entity).level; + TrackedEntity entry = server.getChunkSource().chunkMap.entityMap.get(entity.getEntityId()); if (entry == null) return; PlayerlistTracker replace = new PlayerlistTracker(server.getChunkSource().chunkMap, entry); - server.getChunkSource().chunkMap.entityMap.put(player.getEntityId(), replace); - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + server.getChunkSource().chunkMap.entityMap.put(entity.getEntityId(), replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerlistTracker.java b/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerlistTracker.java index cf2b0df85..c377a9916 100644 --- a/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerlistTracker.java +++ b/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerlistTracker.java @@ -3,6 +3,7 @@ package net.citizensnpcs.nms.v1_18_R2.util; import java.lang.invoke.MethodHandle; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import net.citizensnpcs.Settings.Setting; @@ -32,11 +33,10 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity { } public void updateLastPlayer() { - if (tracker.isRemoved()) + if (tracker.isRemoved() || lastUpdatedPlayer == null + || tracker.getBukkitEntity().getType() != EntityType.PLAYER) return; final ServerPlayer entityplayer = lastUpdatedPlayer; - if (entityplayer == null) - return; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); if (!Setting.DISABLE_TABLIST.asBoolean()) return; @@ -53,6 +53,8 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity { public void updatePlayer(final ServerPlayer entityplayer) { if (entityplayer instanceof EntityHumanNPC) // prevent updates to NPC "viewers" return; + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; this.lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); } diff --git a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/entity/EntityHumanNPC.java b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/entity/EntityHumanNPC.java index 62e844d7f..92d2b6ec2 100644 --- a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/entity/EntityHumanNPC.java +++ b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/entity/EntityHumanNPC.java @@ -38,7 +38,6 @@ import net.citizensnpcs.nms.v1_19_R2.util.NMSImpl; import net.citizensnpcs.nms.v1_19_R2.util.PlayerControllerJump; import net.citizensnpcs.nms.v1_19_R2.util.PlayerMoveControl; import net.citizensnpcs.nms.v1_19_R2.util.PlayerNavigation; -import net.citizensnpcs.nms.v1_19_R2.util.PlayerlistTracker; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; import net.citizensnpcs.npc.skin.SkinPacketTracker; @@ -54,7 +53,6 @@ import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.contents.LiteralContents; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.PacketFlow; -import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; @@ -89,7 +87,6 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable private PlayerNavigation navigation; private final CitizensNPC npc; private final Location packetLocationCache = new Location(null, 0, 0, 0); - private PlayerlistTracker playerlistTracker; private boolean setBukkitEntity; private final SkinPacketTracker skinTracker; private EmptyServerStatsCounter statsCache; @@ -110,14 +107,6 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable } } - @Override - public boolean broadcastToPlayer(ServerPlayer entityplayer) { - if (npc != null && playerlistTracker == null) { - return false; - } - return super.broadcastToPlayer(entityplayer); - } - public boolean canCutCorner(BlockPathTypes pathtype) { return (pathtype != BlockPathTypes.DANGER_FIRE && pathtype != BlockPathTypes.DANGER_CACTUS && pathtype != BlockPathTypes.DANGER_OTHER && pathtype != BlockPathTypes.WALKABLE_DOOR); @@ -213,14 +202,6 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable } } - @Override - public Packet getAddEntityPacket() { - if (playerlistTracker != null) { - playerlistTracker.updateLastPlayer(); - } - return super.getAddEntityPacket(); - } - @Override public CraftPlayer getBukkitEntity() { if (npc != null && !setBukkitEntity) { @@ -470,10 +451,6 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable npc.getOrAddTrait(SkinTrait.class).setSkinPersistent(skinName, signature, data); } - public void setTracked(PlayerlistTracker tracker) { - playerlistTracker = tracker; - } - @Override public void tick() { super.tick(); diff --git a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/PlayerlistTracker.java b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/CitizensEntityTracker.java similarity index 72% rename from v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/PlayerlistTracker.java rename to v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/CitizensEntityTracker.java index 1fd556228..fecff44d9 100644 --- a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/PlayerlistTracker.java +++ b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/CitizensEntityTracker.java @@ -1,10 +1,14 @@ package net.citizensnpcs.nms.v1_19_R2.util; import java.lang.invoke.MethodHandle; +import java.util.Set; import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import com.google.common.collect.ForwardingSet; + import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; @@ -18,25 +22,44 @@ import net.minecraft.server.level.ServerEntity; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; -public class PlayerlistTracker extends ChunkMap.TrackedEntity { +public class CitizensEntityTracker extends ChunkMap.TrackedEntity { private ServerPlayer lastUpdatedPlayer; private final Entity tracker; - public PlayerlistTracker(ChunkMap map, Entity entity, int i, int j, boolean flag) { + public CitizensEntityTracker(ChunkMap map, Entity entity, int i, int j, boolean flag) { map.super(entity, i, j, flag); this.tracker = entity; + try { + Set set = (Set) TRACKING_SET_GETTER.invoke(this); + TRACKING_SET_SETTER.invoke(this, new ForwardingSet() { + @Override + public boolean add(Object conn) { + boolean res = super.add(conn); + if (res) { + updateLastPlayer(); + } + return res; + } + + @Override + protected Set delegate() { + return set; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } } - public PlayerlistTracker(ChunkMap map, TrackedEntity entry) { + public CitizensEntityTracker(ChunkMap map, TrackedEntity entry) { this(map, getTracker(entry), getTrackingDistance(entry), getE(entry), getF(entry)); } public void updateLastPlayer() { - if (tracker.isRemoved()) + if (tracker.isRemoved() || lastUpdatedPlayer == null + || tracker.getBukkitEntity().getType() != EntityType.PLAYER) return; final ServerPlayer entityplayer = lastUpdatedPlayer; - if (entityplayer == null) - return; boolean sendTabRemove = NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); if (!sendTabRemove || !Setting.DISABLE_TABLIST.asBoolean()) { Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), @@ -54,6 +77,9 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity { public void updatePlayer(final ServerPlayer entityplayer) { if (entityplayer instanceof EntityHumanNPC) // prevent updates to NPC "viewers" return; + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; + this.lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); } @@ -104,4 +130,6 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity { private static final MethodHandle TRACKER = NMS.getFirstGetter(TrackedEntity.class, Entity.class); private static final MethodHandle TRACKER_ENTRY = NMS.getFirstGetter(TrackedEntity.class, ServerEntity.class); private static final MethodHandle TRACKING_RANGE = NMS.getFirstGetter(TrackedEntity.class, int.class); + private static final MethodHandle TRACKING_SET_GETTER = NMS.getFirstGetter(TrackedEntity.class, Set.class); + private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(TrackedEntity.class, Set.class); } diff --git a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/NMSImpl.java b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/NMSImpl.java index 253d3b9bd..ca1de850a 100644 --- a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/NMSImpl.java +++ b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/NMSImpl.java @@ -416,8 +416,9 @@ public class NMSImpl implements NMSBridge { ((Mob) source).doHurtTarget(target); return; } - AttributeInstance attackDamage = source.getAttribute(Attributes.ATTACK_DAMAGE); - float f = (float) (attackDamage == null ? 1 : attackDamage.getValue()); + float f = (float) (source.getAttributes().hasAttribute(Attributes.ATTACK_DAMAGE) + ? source.getAttributeValue(Attributes.ATTACK_DAMAGE) + : 1f); int i = 0; f += EnchantmentHelper.getDamageBonus(source.getMainHandItem(), target.getMobType()); i += EnchantmentHelper.getKnockbackBonus(source); @@ -1282,16 +1283,13 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - ServerLevel server = (ServerLevel) NMSImpl.getHandle(player).level; - TrackedEntity entry = server.getChunkSource().chunkMap.entityMap.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + ServerLevel server = (ServerLevel) NMSImpl.getHandle(entity).level; + TrackedEntity entry = server.getChunkSource().chunkMap.entityMap.get(entity.getEntityId()); if (entry == null) return; - PlayerlistTracker replace = new PlayerlistTracker(server.getChunkSource().chunkMap, entry); - server.getChunkSource().chunkMap.entityMap.put(player.getEntityId(), replace); - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); - } + CitizensEntityTracker replace = new CitizensEntityTracker(server.getChunkSource().chunkMap, entry); + server.getChunkSource().chunkMap.entityMap.put(entity.getEntityId(), replace); } @Override @@ -1306,10 +1304,8 @@ public class NMSImpl implements NMSBridge { float oldPitch = handle.getXRot(); handle.setYBodyRot(bodyYaw); handle.setXRot(pitch); - sendPacketsNearby(null, from.getLocation(), new ClientboundTeleportEntityPacket(handle), // new - // ClientboundMoveEntityPacket.Rot(handle.getId(), - // (byte) (bodyYaw * - // 256.0F / 360.0F), + sendPacketsNearby(null, from.getLocation(), new ClientboundTeleportEntityPacket(handle), + // new ClientboundMoveEntityPacket.Rot(handle.getId(), (byte) (bodyYaw * 256.0F / 360.0F), // (byte) (pitch * 256.0F / 360.0F), handle.onGround), new ClientboundRotateHeadPacket(handle, (byte) (headYaw * 256.0F / 360.0F))); handle.setYBodyRot(oldBody); diff --git a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/NMSImpl.java b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/NMSImpl.java index 04b070257..e26ed2f03 100644 --- a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/NMSImpl.java +++ b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/NMSImpl.java @@ -947,13 +947,13 @@ public class NMSImpl implements NMSBridge { } @Override - public void replaceTrackerEntry(Player player) { - WorldServer server = (WorldServer) NMSImpl.getHandle(player).getWorld(); - EntityTrackerEntry entry = server.getTracker().trackedEntities.get(player.getEntityId()); + public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { + WorldServer server = (WorldServer) NMSImpl.getHandle(entity).getWorld(); + EntityTrackerEntry entry = server.getTracker().trackedEntities.get(entity.getEntityId()); if (entry == null) return; PlayerlistTrackerEntry replace = new PlayerlistTrackerEntry(entry); - server.getTracker().trackedEntities.a(player.getEntityId(), replace); + server.getTracker().trackedEntities.a(entity.getEntityId(), replace); if (TRACKED_ENTITY_SET != null) { try { Set set = (Set) TRACKED_ENTITY_SET.get(server.getTracker()); @@ -965,8 +965,8 @@ public class NMSImpl implements NMSBridge { e.printStackTrace(); } } - if (getHandle(player) instanceof EntityHumanNPC) { - ((EntityHumanNPC) getHandle(player)).setTracked(replace); + if (getHandle(entity) instanceof EntityHumanNPC) { + ((EntityHumanNPC) getHandle(entity)).setTracked(replace); } } diff --git a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/PlayerlistTrackerEntry.java b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/PlayerlistTrackerEntry.java index 135b98ee6..8f09d8bea 100644 --- a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/PlayerlistTrackerEntry.java +++ b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/util/PlayerlistTrackerEntry.java @@ -1,8 +1,11 @@ package net.citizensnpcs.nms.v1_8_R3.util; import java.lang.reflect.Field; + import org.bukkit.Bukkit; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; + import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; @@ -22,7 +25,7 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { } public PlayerlistTrackerEntry(EntityTrackerEntry entry) { - this(getTracker(entry), getB(entry), getC(entry), getU(entry)); + this(entry.tracker, getB(entry), getC(entry), getU(entry)); } public boolean isUpdating() { @@ -32,7 +35,8 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { public void updateLastPlayer() { if (lastUpdatedPlayer == null) return; - final Entity tracker = getTracker(this); + if (tracker.dead || tracker.getBukkitEntity().getType() != EntityType.PLAYER) + return; final EntityPlayer entityplayer = lastUpdatedPlayer; NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()); lastUpdatedPlayer = null; @@ -52,6 +56,8 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { // prevent updates to NPC "viewers" if (entityplayer instanceof EntityHumanNPC) return; + if (tracker instanceof NPCHolder && ((NPCHolder) tracker).getNPC().isHiddenFrom(entityplayer.getBukkitEntity())) + return; lastUpdatedPlayer = entityplayer; super.updatePlayer(entityplayer); lastUpdatedPlayer = null; @@ -59,7 +65,7 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { private static int getB(EntityTrackerEntry entry) { try { - Entity entity = getTracker(entry); + Entity entity = entry.tracker; if (entity instanceof NPCHolder) { return ((NPCHolder) entity).getNPC().data().get(NPC.Metadata.TRACKING_RANGE, (Integer) B.get(entry)); } @@ -83,17 +89,6 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { return 0; } - private static Entity getTracker(EntityTrackerEntry entry) { - try { - return (Entity) TRACKER.get(entry); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - return null; - } - private static boolean getU(EntityTrackerEntry entry) { try { return (Boolean) U.get(entry); @@ -107,6 +102,5 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry { private static Field B = NMS.getField(EntityTrackerEntry.class, "b"); private static Field C = NMS.getField(EntityTrackerEntry.class, "c"); - private static Field TRACKER = NMS.getField(EntityTrackerEntry.class, "tracker"); private static Field U = NMS.getField(EntityTrackerEntry.class, "u"); }