Use new lookup api

This commit is contained in:
fullwall 2022-12-04 00:59:16 +08:00
parent 6fec436f55
commit fef03ea6da
7 changed files with 81 additions and 50 deletions

View File

@ -10,7 +10,6 @@ import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -2245,12 +2244,9 @@ public class NPCCommands {
Entity player = (Player) sender; Entity player = (Player) sender;
final Location location = args.getSenderLocation(); final Location location = args.getSenderLocation();
List<Entity> search = player.getNearbyEntities(range, range, range); List<Entity> search = player.getNearbyEntities(range, range, range);
Collections.sort(search, new Comparator<Entity>() { Collections.sort(search, (o1, o2) -> {
@Override
public int compare(Entity o1, Entity o2) {
double d = o1.getLocation().distanceSquared(location) - o2.getLocation().distanceSquared(location); double d = o1.getLocation().distanceSquared(location) - o2.getLocation().distanceSquared(location);
return d > 0 ? 1 : d < 0 ? -1 : 0; return d > 0 ? 1 : d < 0 ? -1 : 0;
}
}); });
for (Entity possibleNPC : search) { for (Entity possibleNPC : search) {
NPC test = registry.getNPC(possibleNPC); NPC test = registry.getNPC(possibleNPC);

View File

@ -6,7 +6,6 @@ import java.util.Random;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue; import org.bukkit.metadata.MetadataValue;
@ -92,12 +91,11 @@ public class LookClose extends Trait implements Toggleable {
if (lookingAt != null) { if (lookingAt != null) {
if (randomSwitchTargets && t <= 0) { if (randomSwitchTargets && t <= 0) {
List<Player> options = Lists.newArrayList(); List<Player> options = Lists.newArrayList();
for (Entity entity : npc.getEntity().getNearbyEntities(range, range, range)) { for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(),
if (entity == lookingAt || !(entity instanceof Player) range)) {
|| CitizensAPI.getNPCRegistry().getNPC(entity) != null) { if (player == lookingAt || CitizensAPI.getNPCRegistry().getNPC(player) != null) {
continue; continue;
} }
Player player = (Player) entity;
if (player.getLocation().getWorld() != NPC_LOCATION.getWorld() || isInvisible(player)) if (player.getLocation().getWorld() != NPC_LOCATION.getWorld() || isInvisible(player))
continue; continue;
options.add(player); options.add(player);
@ -109,15 +107,13 @@ public class LookClose extends Trait implements Toggleable {
} }
} else { } else {
double min = range; double min = range;
for (Entity entity : npc.getEntity().getNearbyEntities(range, range, range)) { for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(),
if (!(entity instanceof Player)) range)) {
continue;
Player player = (Player) entity;
Location location = player.getLocation(CACHE_LOCATION); Location location = player.getLocation(CACHE_LOCATION);
if (location.getWorld() != NPC_LOCATION.getWorld()) if (location.getWorld() != NPC_LOCATION.getWorld())
continue; continue;
double dist = location.distance(NPC_LOCATION); double dist = location.distance(NPC_LOCATION);
if (dist > min || CitizensAPI.getNPCRegistry().getNPC(entity) != null || isInvisible(player)) if (dist > min || CitizensAPI.getNPCRegistry().getNPC(player) != null || isInvisible(player))
continue; continue;
min = dist; min = dist;
lookingAt = player; lookingAt = player;

View File

@ -1,7 +1,11 @@
package net.citizensnpcs.trait; package net.citizensnpcs.trait;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function; import java.util.function.Function;
import org.bukkit.Location; import org.bukkit.Location;
@ -9,7 +13,10 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.persistence.Persistable; import net.citizensnpcs.api.persistence.Persistable;
@ -25,6 +32,7 @@ public class RotationTrait extends Trait {
private final RotationParams globalParameters = new RotationParams(); private final RotationParams globalParameters = new RotationParams();
private final RotationSession globalSession = new RotationSession(globalParameters); private final RotationSession globalSession = new RotationSession(globalParameters);
private final List<LocalRotationSession> localSessions = Lists.newArrayList(); private final List<LocalRotationSession> localSessions = Lists.newArrayList();
private final Map<UUID, LocalRotationSession> localSessionsByUUID = Maps.newHashMap();
public RotationTrait() { public RotationTrait() {
super("rotationtrait"); super("rotationtrait");
@ -37,12 +45,19 @@ public class RotationTrait extends Trait {
/** /**
* @return The created session * @return The created session
*/ */
public RotationSession createLocalSession(RotationParams params) { public LocalRotationSession createLocalSession(RotationParams params) {
if (params.filter == null) if (params.filter == null && params.uuidFilter == null)
throw new IllegalStateException(); throw new IllegalStateException();
RotationSession session = new RotationSession(params); RotationSession session = new RotationSession(params);
localSessions.add(new LocalRotationSession(session)); LocalRotationSession lrs = new LocalRotationSession(session);
return session; if (params.uuidFilter != null) {
for (UUID uuid : params.uuidFilter) {
localSessionsByUUID.put(uuid, lrs);
}
} else {
localSessions.add(lrs);
}
return lrs;
} }
private double getEyeY() { private double getEyeY() {
@ -57,6 +72,9 @@ public class RotationTrait extends Trait {
} }
public LocalRotationSession getLocalSession(Player player) { public LocalRotationSession getLocalSession(Player player) {
LocalRotationSession lrs = localSessionsByUUID.get(player.getUniqueId());
if (lrs != null)
return lrs;
for (LocalRotationSession session : localSessions) { for (LocalRotationSession session : localSessions) {
if (session.accepts(player)) { if (session.accepts(player)) {
return session; return session;
@ -111,8 +129,13 @@ public class RotationTrait extends Trait {
if (!npc.isSpawned()) if (!npc.isSpawned())
return; return;
for (Iterator<LocalRotationSession> itr = localSessions.iterator(); itr.hasNext();) { Set<LocalRotationSession> run = Sets.newHashSet();
for (Iterator<LocalRotationSession> itr = Iterables.concat(localSessions, localSessionsByUUID.values())
.iterator(); itr.hasNext();) {
LocalRotationSession session = itr.next(); LocalRotationSession session = itr.next();
if (run.contains(session))
continue;
run.add(session);
session.run(npc.getEntity()); session.run(npc.getEntity());
if (!session.isActive()) { if (!session.isActive()) {
itr.remove(); itr.remove();
@ -144,6 +167,7 @@ public class RotationTrait extends Trait {
} }
public static class LocalRotationSession { public static class LocalRotationSession {
private boolean ended;
private final RotationSession session; private final RotationSession session;
private RotationTriple triple; private RotationTriple triple;
@ -155,6 +179,10 @@ public class RotationTrait extends Trait {
return session.params.accepts(player); return session.params.accepts(player);
} }
public void end() {
this.ended = true;
}
public float getBodyYaw() { public float getBodyYaw() {
return triple.bodyYaw; return triple.bodyYaw;
} }
@ -168,7 +196,7 @@ public class RotationTrait extends Trait {
} }
public boolean isActive() { public boolean isActive() {
return session.isActive(); return !ended && session.isActive();
} }
public void run(Entity entity) { public void run(Entity entity) {
@ -204,6 +232,7 @@ public class RotationTrait extends Trait {
private float maxYawPerTick = 40; private float maxYawPerTick = 40;
private boolean persist = false; private boolean persist = false;
private float[] pitchRange = { -180, 180 }; private float[] pitchRange = { -180, 180 };
private List<UUID> uuidFilter;
private float[] yawRange = { -180, 180 }; private float[] yawRange = { -180, 180 };
public boolean accepts(Player player) { public boolean accepts(Player player) {
@ -288,13 +317,6 @@ public class RotationTrait extends Trait {
return Util.clamp(out, pitchRange[0], pitchRange[1], 360); return Util.clamp(out, pitchRange[0], pitchRange[1], 360);
} }
/*
* public Vector3 SuperSmoothVector3Lerp( Vector3 pastPosition, Vector3 pastTargetPosition, Vector3 targetPosition, float time, float speed ){
Vector3 f = pastPosition - pastTargetPosition + (targetPosition - pastTargetPosition) / (speed * time);
return targetPosition - (targetPosition - pastTargetPosition) / (speed*time) + f * Mathf.Exp(-speed*time);
}
*/
private float rotateTowards(float target, float current, float maxRotPerTick) { private float rotateTowards(float target, float current, float maxRotPerTick) {
float diff = Util.clamp(current - target); float diff = Util.clamp(current - target);
return target + clamp(diff, -maxRotPerTick, maxRotPerTick); return target + clamp(diff, -maxRotPerTick, maxRotPerTick);
@ -335,6 +357,22 @@ public class RotationTrait extends Trait {
} }
} }
/*
* public Vector3 SuperSmoothVector3Lerp( Vector3 pastPosition, Vector3 pastTargetPosition, Vector3 targetPosition, float time, float speed ){
Vector3 f = pastPosition - pastTargetPosition + (targetPosition - pastTargetPosition) / (speed * time);
return targetPosition - (targetPosition - pastTargetPosition) / (speed*time) + f * Mathf.Exp(-speed*time);
}
*/
public RotationParams uuidFilter(List<UUID> uuids) {
this.uuidFilter = uuids;
return this;
}
public RotationParams uuidFilter(UUID... uuids) {
return uuidFilter(Arrays.asList(uuids));
}
public RotationParams yawRange(float[] val) { public RotationParams yawRange(float[] val) {
this.yawRange = val; this.yawRange = val;
return this; return this;

View File

@ -15,6 +15,7 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.citizensnpcs.api.gui.BooleanSlotHandler; import net.citizensnpcs.api.gui.BooleanSlotHandler;
@ -212,9 +213,12 @@ public class ItemAction extends NPCShopAction {
base.compareSimilarity = res; base.compareSimilarity = res;
return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off"; return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off";
}, base.compareSimilarity)); }, base.compareSimilarity));
ctx.getSlot(3 * 9 + 3).setItemStack(new ItemStack(Material.BOOK), "NBT comparison filter"); ctx.getSlot(3 * 9 + 3).setItemStack(new ItemStack(Material.BOOK), "NBT comparison filter",
ctx.getSlot(3 * 9 + 3).addClickHandler((event) -> ctx.getMenu() Joiner.on("\n").join(base.metaFilter));
.transition(InputMenus.stringSetter(() -> "", res -> base.metaFilter = Lists.newArrayList(res)))); ctx.getSlot(3 * 9 + 3)
.addClickHandler((event) -> ctx.getMenu()
.transition(InputMenus.stringSetter(() -> Joiner.on(',').join(base.metaFilter),
res -> base.metaFilter = Arrays.asList(res.split(",")))));
} }
@Override @Override

View File

@ -13,7 +13,6 @@ import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent; import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ConversationAbandonedListener; import org.bukkit.conversations.ConversationAbandonedListener;
import org.bukkit.conversations.ConversationFactory; import org.bukkit.conversations.ConversationFactory;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -187,11 +186,10 @@ public class Text extends Trait implements Runnable, Listener, ConversationAband
if (!talkClose) if (!talkClose)
return; return;
List<Entity> nearby = npc.getEntity().getNearbyEntities(range, range, range); for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(), range)) {
for (Entity search : nearby) { if (player.getGameMode() == GameMode.SPECTATOR)
if (!(search instanceof Player) || ((Player) search).getGameMode() == GameMode.SPECTATOR)
continue; continue;
sendCooldownMessage((Player) search); sendCooldownMessage(player);
} }
} }

View File

@ -19,6 +19,7 @@ import com.google.common.base.Splitter;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.command.Command; import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext; import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.Flag; import net.citizensnpcs.api.command.Flag;
@ -157,11 +158,9 @@ public class BossBarTrait extends Trait {
} }
if (barCache != null) { if (barCache != null) {
barCache.removeAll(); barCache.removeAll();
for (Entity entity : npc.getEntity().getNearbyEntities(Setting.BOSSBAR_RANGE.asInt() / 2, for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(),
Setting.BOSSBAR_RANGE.asInt() / 2, Setting.BOSSBAR_RANGE.asInt() / 2)) { Setting.BOSSBAR_RANGE.asInt())) {
if (entity instanceof Player) { barCache.addPlayer(player);
barCache.addPlayer((Player) entity);
}
} }
} }
} }

View File

@ -4,12 +4,12 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.Messaging;
@ -37,15 +37,15 @@ public class ChatTrigger implements WaypointTrigger {
public void onWaypointReached(NPC npc, Location waypoint) { public void onWaypointReached(NPC npc, Location waypoint) {
if (radius < 0) { if (radius < 0) {
for (Player player : npc.getEntity().getWorld().getPlayers()) { for (Player player : npc.getEntity().getWorld().getPlayers()) {
for (String line : lines) for (String line : lines) {
Messaging.send(player, line); Messaging.send(player, line);
} }
}
} else { } else {
for (Entity entity : npc.getEntity().getNearbyEntities(radius, radius, radius)) { for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getStoredLocation(), radius)) {
if (!(entity instanceof Player)) for (String line : lines) {
continue; Messaging.send(player, line);
for (String line : lines) }
Messaging.send((Player) entity, line);
} }
} }
} }