Skip to content

Commit

Permalink
Add new PlayerFilter method
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jun 25, 2023
1 parent e4bdd06 commit ae7d4db
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 87 deletions.
Expand Up @@ -564,12 +564,5 @@ public NavigatorParameters useNewPathfinder(boolean use) {
return this;
}

private static final Function<org.bukkit.entity.Entity, Location> DEFAULT_MAPPER = new Function<Entity, Location>() {
Location location = new Location(null, 0, 0, 0);

@Override
public Location apply(Entity input) {
return input.getLocation(location);
}
};
private static final Function<org.bukkit.entity.Entity, Location> DEFAULT_MAPPER = Entity::getLocation;
}
Expand Up @@ -23,16 +23,15 @@ public CohesionBehavior(double weight) {
@Override
public Vector getVector(NPC npc, Collection<NPC> nearby) {
Vector positions = new Vector(0, 0, 0);
Location cacheLoc = new Location(null, 0, 0, 0);
for (NPC neighbor : nearby) {
positions = positions.add(neighbor.getEntity().getLocation(CACHE).toVector());
positions = positions.add(neighbor.getEntity().getLocation(cacheLoc).toVector());
}
Vector center = positions.multiply(1.0 / nearby.size());
Vector temp = npc.getEntity().getLocation(CACHE).toVector().subtract(center);
Vector temp = npc.getEntity().getLocation(cacheLoc).toVector().subtract(center);
if (temp.length() == 0) {
return new Vector(0, 0, 0);
}
return temp.normalize().multiply(weight);
}

private static final Location CACHE = new Location(null, 0, 0, 0);
}
Expand Up @@ -10,7 +10,6 @@
import net.citizensnpcs.api.ai.Goal;
import net.citizensnpcs.api.ai.Navigator;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.ai.event.NavigatorCallback;
import net.citizensnpcs.api.ai.tree.Behavior;
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;
import net.citizensnpcs.api.ai.tree.BehaviorStatus;
Expand All @@ -27,11 +26,11 @@ public class TargetNearbyEntityGoal extends BehaviorGoalAdapter {
private final double radius;
private CancelReason reason;
private Entity target;
private final Set<EntityType> targets;
private final Set<EntityType> targetTypes;

private TargetNearbyEntityGoal(NPC npc, Set<EntityType> targets, boolean aggressive, double radius) {
this.npc = npc;
this.targets = targets;
this.targetTypes = targets;
this.aggressive = aggressive;
this.radius = radius;
}
Expand All @@ -54,24 +53,21 @@ public BehaviorStatus run() {

@Override
public boolean shouldExecute() {
if (targets.size() == 0 || !npc.isSpawned())
if (targetTypes.isEmpty() || !npc.isSpawned())
return false;
Collection<Entity> nearby = npc.getEntity().getNearbyEntities(radius, radius, radius);
this.target = null;
for (Entity entity : nearby) {
if (targets.contains(entity.getType())) {
if (targetTypes.contains(entity.getType())) {
target = entity;
break;
}
}
if (target != null) {
npc.getNavigator().setTarget(target, aggressive);
npc.getNavigator().getLocalParameters().addSingleUseCallback(new NavigatorCallback() {
@Override
public void onCompletion(CancelReason cancelReason) {
reason = cancelReason;
finished = true;
}
npc.getNavigator().getLocalParameters().addSingleUseCallback(cancelReason -> {
reason = cancelReason;
finished = true;
});
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/citizensnpcs/api/ai/goals/WanderGoal.java
Expand Up @@ -113,7 +113,7 @@ public BehaviorStatus run() {
return BehaviorStatus.SUCCESS;
}
} else {
if (npc.getEntity().getLocation(NPC_LOCATION).distance(target) >= 0.1) {
if (npc.getEntity().getLocation().distance(target) >= 0.1) {
npc.setMoveDestination(target);
} else {
return BehaviorStatus.SUCCESS;
Expand Down Expand Up @@ -245,6 +245,5 @@ public static Builder builder(NPC npc) {
return new Builder(npc);
}

private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
private static final Random RANDOM = new Random();
}
33 changes: 17 additions & 16 deletions src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Expand Up @@ -47,6 +47,7 @@
import net.citizensnpcs.api.util.MemoryDataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.api.util.RemoveReason;
import net.citizensnpcs.api.util.SpigotUtil;

public abstract class AbstractNPC implements NPC {
Expand Down Expand Up @@ -184,7 +185,7 @@ public void destroy() {
runnables.clear();
for (Trait trait : traits.values()) {
HandlerList.unregisterAll(trait);
trait.onRemove();
trait.onRemove(RemoveReason.DESTROYED);
}
traits.clear();
goalController.clear();
Expand Down Expand Up @@ -216,20 +217,6 @@ public boolean equals(Object obj) {
return true;
}

@Override
public UUID getMinecraftUniqueId() {
if (getEntityType() == EntityType.PLAYER) {
UUID uuid = getUniqueId();
if (uuid.version() == 4) { // set version to 2
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
return new UUID(msb, uuid.getLeastSignificantBits());
}
}
return getUniqueId();
}

@Override
public GoalController getDefaultGoalController() {
return goalController;
Expand Down Expand Up @@ -267,6 +254,20 @@ public Supplier<ItemStack> getItemProvider() {
return itemProvider;
}

@Override
public UUID getMinecraftUniqueId() {
if (getEntityType() == EntityType.PLAYER) {
UUID uuid = getUniqueId();
if (uuid.version() == 4) { // set version to 2
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
return new UUID(msb, uuid.getLeastSignificantBits());
}
}
return getUniqueId();
}

@Override
public String getName() {
return ChatColor.stripColor(coloredNameStringCache);
Expand Down Expand Up @@ -411,7 +412,7 @@ public void removeTrait(Class<? extends Trait> traitClass) {
runnables.remove(trait);
}
HandlerList.unregisterAll(trait);
trait.onRemove();
trait.onRemove(RemoveReason.REMOVAL);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/citizensnpcs/api/npc/NPCRegistry.java
Expand Up @@ -49,7 +49,7 @@ public interface NPCRegistry extends Iterable<NPC> {
* The NPC name
* @return The created NPC
*/
public NPC createNPC(EntityType type, UUID uuid, int id, String name);;
public NPC createNPC(EntityType type, UUID uuid, int id, String name);

/**
* Creates an despawned {@link NPC} using the given ItemStack to configure it if possible.
Expand All @@ -62,7 +62,7 @@ public interface NPCRegistry extends Iterable<NPC> {
* ItemStack to configure with
* @return Created NPC
*/
public NPC createNPCUsingItem(EntityType type, String name, ItemStack item);
public NPC createNPCUsingItem(EntityType type, String name, ItemStack item);;

/**
* Deregisters the {@link NPC} and removes all data about it from the data store.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/citizensnpcs/api/trait/Trait.java
Expand Up @@ -6,6 +6,7 @@
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.RemoveReason;

/**
* Represents a Trait linked to an {@link NPC} that can be loaded and saved. This will be kept persisted inside a
Expand Down Expand Up @@ -99,6 +100,10 @@ public void onPreSpawn() {
public void onRemove() {
}

public void onRemove(RemoveReason reason) {
onRemove();
}

/**
* Called when an {@link NPC} is spawned. {@link NPC#getEntity()} will return null until this is called. This is
* also called onAttach when the NPC is already spawned.
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/net/citizensnpcs/api/trait/trait/PlayerFilter.java
@@ -1,24 +1,29 @@
package net.citizensnpcs.api.trait.trait;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import com.google.common.collect.Sets;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPC.NPCUpdate;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;

@TraitName("playerfilter")
public class PlayerFilter extends Trait {
private final Set<UUID> children = Sets.newHashSet();
private Function<Player, Boolean> filter;
@Persist
private Set<String> groups = null;
Expand Down Expand Up @@ -66,6 +71,10 @@ public PlayerFilter(BiConsumer<Player, Entity> hideFunction, BiConsumer<Player,
this.viewFunction = viewFunction;
}

public void addChildNPC(NPC npc) {
children.add(npc.getUniqueId());
}

/**
* Hides the NPC from the given permissions group
*/
Expand All @@ -91,6 +100,14 @@ public void addPlayer(UUID uuid) {
recalculate();
}

public boolean affectsGroup(String group) {
return groups.contains(group);
}

public boolean affectsPlayer(UUID uuid) {
return players.contains(uuid);
}

/**
* Clears all set UUID filters.
*/
Expand Down Expand Up @@ -121,6 +138,14 @@ private Set<UUID> getSet() {
return mode == Mode.DENYLIST ? viewingPlayers : hiddenPlayers;
}

public boolean isAllowlist() {
return mode == Mode.ALLOWLIST;
}

public boolean isDenylist() {
return mode == Mode.DENYLIST;
}

/**
* Whether the NPC should be hidden from the given Player
*/
Expand Down Expand Up @@ -151,6 +176,9 @@ public boolean onSeenByPlayer(Player player) {
* that should no longer view the NPC.
*/
public void recalculate() {
Collection<NPC> npcs = children.stream().map(u -> CitizensAPI.getNPCRegistry().getByUniqueIdGlobal(u))
.filter(n -> n != null).collect(Collectors.toList());
System.out.println(npcs);
for (Iterator<UUID> itr = viewingPlayers.iterator(); itr.hasNext();) {
UUID uuid = itr.next();
Player player = Bukkit.getPlayer(uuid);
Expand All @@ -160,6 +188,9 @@ public void recalculate() {
}
if (hideFunction != null && filter.apply(player)) {
hideFunction.accept(player, npc.getEntity());
for (NPC npc : npcs) {
hideFunction.accept(player, npc.getEntity());
}
itr.remove();
}
}
Expand All @@ -172,6 +203,9 @@ public void recalculate() {
}
if (viewFunction != null && !filter.apply(player)) {
viewFunction.accept(player, npc.getEntity());
for (NPC npc : npcs) {
viewFunction.accept(player, npc.getEntity());
}
itr.remove();
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/citizensnpcs/api/util/RemoveReason.java
@@ -0,0 +1,6 @@
package net.citizensnpcs.api.util;

public enum RemoveReason {
DESTROYED,
REMOVAL;
}
44 changes: 0 additions & 44 deletions src/main/java/net/citizensnpcs/api/util/ResultSetHandler.java

This file was deleted.

0 comments on commit ae7d4db

Please sign in to comment.