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

View File

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

View File

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

View File

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

View File

@ -13,7 +13,6 @@ import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ConversationAbandonedListener;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -187,11 +186,10 @@ public class Text extends Trait implements Runnable, Listener, ConversationAband
if (!talkClose)
return;
List<Entity> nearby = npc.getEntity().getNearbyEntities(range, range, range);
for (Entity search : nearby) {
if (!(search instanceof Player) || ((Player) search).getGameMode() == GameMode.SPECTATOR)
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(), range)) {
if (player.getGameMode() == GameMode.SPECTATOR)
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 net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.Flag;
@ -157,11 +158,9 @@ public class BossBarTrait extends Trait {
}
if (barCache != null) {
barCache.removeAll();
for (Entity entity : npc.getEntity().getNearbyEntities(Setting.BOSSBAR_RANGE.asInt() / 2,
Setting.BOSSBAR_RANGE.asInt() / 2, Setting.BOSSBAR_RANGE.asInt() / 2)) {
if (entity instanceof Player) {
barCache.addPlayer((Player) entity);
}
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(),
Setting.BOSSBAR_RANGE.asInt())) {
barCache.addPlayer(player);
}
}
}

View File

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