From 2f05783da90ccde611821c6552765f922910a859 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Thu, 11 May 2023 05:40:33 +0100 Subject: [PATCH] location changes --- .../pyr/znpcsplus/commands/MoveCommand.java | 1 - .../pyr/znpcsplus/commands/NearCommand.java | 10 ++++----- .../znpcsplus/commands/TeleportCommand.java | 2 +- .../java/lol/pyr/znpcsplus/npc/NpcImpl.java | 21 ++++++++++++------- .../znpcsplus/storage/yaml/YamlStorage.java | 7 +------ .../znpcsplus/tasks/NpcVisibilityTask.java | 2 +- .../java/lol/pyr/znpcsplus/util/Viewable.java | 4 ++++ 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java index 192cdb3..3bd41d0 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java @@ -21,7 +21,6 @@ public class MoveCommand implements CommandHandler { Player player = context.ensureSenderIsPlayer(); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); npc.setLocation(new ZLocation(player.getLocation())); - npc.respawn(); context.send(Component.text("NPC moved to your current location.", NamedTextColor.GREEN)); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java index cb44ff7..7da6914 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java @@ -15,16 +15,16 @@ public class NearCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { Player player = context.ensureSenderIsPlayer(); - double radius = Math.pow(context.parse(Integer.class), 2); + int raw = context.parse(Integer.class); + double radius = Math.pow(raw, 2); String npcs = NpcRegistryImpl.get().allModifiable().stream() - .filter(entry -> entry.getNpc().getLocation().toBukkitLocation(entry.getNpc().getWorld()).distanceSquared(player.getLocation()) < radius) + .filter(entry -> entry.getNpc().getBukkitLocation().distanceSquared(player.getLocation()) < radius) .map(NpcEntryImpl::getId) .collect(Collectors.joining(", ")); - if (npcs.length() == 0) context.halt(Component.text("There are no npcs within " + ((int) radius) + " blocks around you.", NamedTextColor.RED)); - context.send(Component.text("All NPCs that are within " + radius + " blocks from you:", NamedTextColor.GREEN).appendNewline() + if (npcs.length() == 0) context.halt(Component.text("There are no npcs within " + raw + " blocks around you.", NamedTextColor.RED)); + context.send(Component.text("All NPCs that are within " + raw + " blocks from you:", NamedTextColor.GREEN).appendNewline() .append(Component.text(npcs, NamedTextColor.GREEN))); - } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java index 55fd0fe..8bda10e 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java @@ -20,7 +20,7 @@ public class TeleportCommand implements CommandHandler { context.setUsage(context.getLabel() + " teleport "); Player player = context.ensureSenderIsPlayer(); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); - FoliaUtil.teleport(player, npc.getLocation().toBukkitLocation(npc.getWorld())); + FoliaUtil.teleport(player, npc.getBukkitLocation()); context.send(Component.text("Teleported to NPC!", NamedTextColor.GREEN)); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java index 0da9cb3..0450ddc 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java @@ -9,13 +9,13 @@ import lol.pyr.znpcsplus.interaction.NpcAction; import lol.pyr.znpcsplus.util.Viewable; import lol.pyr.znpcsplus.util.ZLocation; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import java.util.*; public class NpcImpl extends Viewable implements Npc { - private final Set viewers = new HashSet<>(); private final String worldName; private PacketEntity entity; private ZLocation location; @@ -57,9 +57,13 @@ public class NpcImpl extends Viewable implements Npc { return location; } + public Location getBukkitLocation() { + return location.toBukkitLocation(getWorld()); + } + public void setLocation(ZLocation location) { this.location = location; - entity.setLocation(location, viewers); + entity.setLocation(location, getViewers()); hologram.setLocation(location.withY(location.getY() + type.getHologramOffset())); } @@ -88,7 +92,7 @@ public class NpcImpl extends Viewable implements Npc { } private void _refreshMeta() { - for (Player viewer : viewers) entity.refreshMeta(viewer); + for (Player viewer : getViewers()) entity.refreshMeta(viewer); } @SuppressWarnings("unchecked") @@ -102,10 +106,13 @@ public class NpcImpl extends Viewable implements Npc { public void setProperty(EntityPropertyImpl key, T value) { if (value.equals(key.getDefaultValue())) removeProperty(key); - else { - propertyMap.put(key, value); - _refreshMeta(); - } + else propertyMap.put(key, value); + _refreshMeta(); + } + + @SuppressWarnings("unchecked") + public void UNSAFE_setProperty(EntityPropertyImpl property, Object value) { + setProperty((EntityPropertyImpl) property, (T) value); } public void removeProperty(EntityPropertyImpl key) { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java index 648f886..26e12e7 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java @@ -43,7 +43,7 @@ public class YamlStorage implements NpcStorage { ConfigurationSection properties = config.getConfigurationSection("properties"); for (String key : properties.getKeys(false)) { EntityPropertyImpl property = EntityPropertyImpl.getByName(key); - _setProperty(npc, property, property.deserialize(properties.getString(key))); + npc.UNSAFE_setProperty(property, property.deserialize(properties.getString(key))); } for (String line : config.getStringList("hologram")) { @@ -67,11 +67,6 @@ public class YamlStorage implements NpcStorage { return npcs; } - @SuppressWarnings("unchecked") - private void _setProperty(NpcImpl npc, EntityPropertyImpl property, Object value) { - npc.setProperty((EntityPropertyImpl) property, (T) value); - } - @Override public void saveNpcs(Collection npcs) { for (NpcEntryImpl entry : npcs) if (entry.isSave()) try { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcVisibilityTask.java b/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcVisibilityTask.java index 19f9037..e6ca961 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcVisibilityTask.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcVisibilityTask.java @@ -21,7 +21,7 @@ public class NpcVisibilityTask extends BukkitRunnable { if (!entry.isProcessed()) continue; NpcImpl npc = entry.getNpc(); for (Player player : Bukkit.getOnlinePlayers()) { - boolean inRange = (player.getWorld() == npc.getWorld() && player.getLocation().distanceSquared(npc.getLocation().toBukkitLocation(npc.getWorld())) <= distSq); + boolean inRange = (player.getWorld() == npc.getWorld() && player.getLocation().distanceSquared(npc.getBukkitLocation()) <= distSq); if (!inRange && npc.isShown(player)) npc.hide(player); if (inRange && !npc.isShown(player)) npc.show(player); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/util/Viewable.java b/plugin/src/main/java/lol/pyr/znpcsplus/util/Viewable.java index 60b39f3..e5d4867 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/util/Viewable.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/util/Viewable.java @@ -53,6 +53,10 @@ public abstract class Viewable { return Collections.unmodifiableSet(viewers); } + public boolean isVisibleTo(Player player) { + return viewers.contains(player); + } + protected abstract void _show(Player player); protected abstract void _hide(Player player);