location changes

This commit is contained in:
Pyrbu 2023-05-11 05:40:33 +01:00
parent 74ed109543
commit 2f05783da9
7 changed files with 26 additions and 21 deletions

@ -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));
}

@ -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)));
}
}

@ -20,7 +20,7 @@ public class TeleportCommand implements CommandHandler {
context.setUsage(context.getLabel() + " teleport <npc_id>");
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));
}

@ -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<Player> 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 <T> void setProperty(EntityPropertyImpl<T> key, T value) {
if (value.equals(key.getDefaultValue())) removeProperty(key);
else {
propertyMap.put(key, value);
else propertyMap.put(key, value);
_refreshMeta();
}
@SuppressWarnings("unchecked")
public <T> void UNSAFE_setProperty(EntityPropertyImpl<?> property, Object value) {
setProperty((EntityPropertyImpl<T>) property, (T) value);
}
public void removeProperty(EntityPropertyImpl<?> key) {

@ -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 <T> void _setProperty(NpcImpl npc, EntityPropertyImpl<?> property, Object value) {
npc.setProperty((EntityPropertyImpl<T>) property, (T) value);
}
@Override
public void saveNpcs(Collection<NpcEntryImpl> npcs) {
for (NpcEntryImpl entry : npcs) if (entry.isSave()) try {

@ -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);
}

@ -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);