Skip to content

Commit

Permalink
Minor API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 5, 2023
1 parent b1c8fc8 commit c8a5132
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 70 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/citizensnpcs/api/LocationLookup.java
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.Sets;

import ch.ethz.globis.phtree.PhTreeF;
import net.citizensnpcs.api.npc.NPC;

public class LocationLookup extends BukkitRunnable {
private final Map<String, PerPlayerMetadata<?>> metadata = Maps.newHashMap();
Expand All @@ -37,6 +38,10 @@ public Iterable<Player> getNearbyPlayers(Location base, double dist) {
return () -> tree.rangeQuery(dist, base.getX(), base.getY(), base.getZ());
}

public Iterable<Player> getNearbyPlayers(NPC npc) {
return getNearbyPlayers(npc.getStoredLocation(), npc.data().get(NPC.Metadata.TRACKING_RANGE, 64));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public void onJoin(PlayerJoinEvent event) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> {
Expand Down
Expand Up @@ -51,25 +51,26 @@ private boolean isClimbable(Material mat) {
@Override
public PassableState isPassable(BlockSource source, PathPoint point) {
Vector pos = point.getVector();
if (!SpigotUtil.checkYSafe(pos.getBlockY(), source.getWorld())) {
if (!SpigotUtil.checkYSafe(pos.getBlockY(), source.getWorld()))
return PassableState.UNPASSABLE;
}

Block above = source.getBlockAt(pos.getBlockX(), pos.getBlockY() + 1, pos.getBlockZ());
Material below = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() - 1, pos.getBlockZ());
Block in = source.getBlockAt(pos);
boolean canStand = canStandOn(below) || isLiquid(in.getType(), below) || isClimbable(below);
if (!canStand) {
if (!canStand)
return PassableState.UNPASSABLE;
}

if (isClimbable(in.getType()) && (isClimbable(above.getType()) || isClimbable(below))) {
point.addCallback(new LadderClimber());
} else if (!canStandIn(above) || !canStandIn(in)) {
return PassableState.UNPASSABLE;
}

if (!canJumpOn(below)) {
if (point.getParentPoint() == null) {
if (point.getParentPoint() == null)
return PassableState.UNPASSABLE;
}

Vector parentPos = point.getParentPoint().getVector();
if ((parentPos.getX() != pos.getX() || parentPos.getZ() != pos.getZ())
&& pos.clone().subtract(point.getParentPoint().getVector()).getY() == 1) {
Expand Down
Expand Up @@ -25,12 +25,9 @@ public boolean canSwimInLava() {
@Override
public float getCost(BlockSource source, PathPoint point) {
// penalise non water blocks for fish
if (isWaterMob(npc.getEntity())) {
Vector vector = point.getVector();
if (!MinecraftBlockExaminer.isLiquidOrInLiquid(
source.getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()))) {
return 1F;
}
if (isWaterMob(npc.getEntity())
&& !MinecraftBlockExaminer.isLiquidOrInLiquid(source.getBlockAt(point.getVector()))) {
return 1F;
}
return 0;
}
Expand Down Expand Up @@ -58,11 +55,6 @@ private boolean isSwimmableLiquid(Material material) {
|| (!SpigotUtil.isUsing1_13API() && material == Material.valueOf("STATIONARY_WATER"));
}

public boolean isSwimmingUp() {
return npc.data().has(NPC.Metadata.SWIMMING) ? npc.data().<Boolean> get(NPC.Metadata.SWIMMING)
: !isWaterMob(npc.getEntity());
}

public void setCanSwimInLava(boolean canSwimInLava) {
this.canSwimInLava = canSwimInLava;
}
Expand Down
Expand Up @@ -114,6 +114,10 @@ public Iterable<AStarNode> getNeighbours() {
}

public List<PathPoint> getNeighbours(BlockSource source, PathPoint point) {
return getNeighbours(source, point, true);
}

public List<PathPoint> getNeighbours(BlockSource source, PathPoint point, boolean checkPassable) {
List<PathPoint> neighbours = Lists.newArrayList();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
Expand All @@ -127,7 +131,7 @@ public List<PathPoint> getNeighbours(BlockSource source, PathPoint point) {
Vector mod = new Vector(location.getX() + x, modY, location.getZ() + z);
if (mod.equals(location))
continue;
if (x != 0 && z != 0) {
if (x != 0 && z != 0 && checkPassable) {
if (!isPassable(point.createAtOffset(new Vector(location.getX() + x, modY, location.getZ())))
|| !isPassable(
point.createAtOffset(new Vector(location.getX(), modY, location.getZ() + z)))) {
Expand Down Expand Up @@ -161,8 +165,7 @@ public Vector getVector() {

@Override
public int hashCode() {
final int prime = 31;
return prime + ((location == null) ? 0 : location.hashCode());
return 31 + ((location == null) ? 0 : location.hashCode());
}

public float heuristicDistance(Vector goal) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Expand Up @@ -501,9 +501,6 @@ private void setNameInternal(String name) {
this.name = name;
coloredNameComponentCache = Messaging.minecraftComponentFromRawMessage(this.name);
coloredNameStringCache = Messaging.parseComponents(this.name);
Messaging.idebug(() -> {
return "Parsed name " + getId() + ": " + coloredNameStringCache + " " + coloredNameComponentCache;
});
}

@Override
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/citizensnpcs/api/npc/NPC.java
Expand Up @@ -298,13 +298,11 @@ public interface NPC extends Agent, Cloneable {
public void setAlwaysUseNameHologram(boolean use);

/**
* Sets the {@link EntityType} of this NPC. Currently only accepts <em>living</em> entity types, with scope for
* additional types in the future. The NPC will respawned if currently spawned, or will remain despawned otherwise.
* Sets the {@link EntityType} of this NPC. The NPC will respawned if currently spawned, or will remain despawned
* otherwise.
*
* @param type
* The new mob type
* @throws IllegalArgumentException
* If the type is not a living entity type
*/
public void setBukkitEntityType(EntityType type);

Expand Down
Expand Up @@ -37,7 +37,7 @@ public <T> T get(NPC.Metadata key) {

@Override
public <T> T get(NPC.Metadata key, T def) {
T t = this.get(key);
T t = get(key);
return t == null ? def : t;
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/citizensnpcs/api/util/EyeHeightBoundingBox.java
@@ -0,0 +1,18 @@
package net.citizensnpcs.api.util;

import org.bukkit.util.Vector;

public class EyeHeightBoundingBox extends BoundingBox {
public double eyeHeight;

public EyeHeightBoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ,
double eyeHeight) {
super(minX, minY, minZ, maxX, maxY, maxZ);
this.eyeHeight = eyeHeight;
}

public Vector getCenter() {
double cx = (minX + maxX) / 2, cy = (minY + maxY) / 2, cz = (minZ + maxZ) / 2;
return new Vector(-cx, cy, -cz);
}
}
97 changes: 55 additions & 42 deletions src/main/java/net/citizensnpcs/api/util/Placeholders.java
Expand Up @@ -15,7 +15,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

Expand Down Expand Up @@ -67,12 +67,16 @@ public static String replace(String text, CommandSender sender, NPC npc) {
while (matcher.find()) {
String replacement = "";
String group = matcher.group(1);
if (group.equals("id")) {
replacement = Integer.toString(npc.getId());
} else if (group.equals("npc")) {
replacement = npc.getFullName();
} else if (group.equals("owner")) {
replacement = npc.getOrAddTrait(Owner.class).getOwner();
switch (group) {
case "id":
replacement = Integer.toString(npc.getId());
break;
case "npc":
replacement = npc.getFullName();
break;
case "owner":
replacement = npc.getOrAddTrait(Owner.class).getOwner();
break;
}
matcher.appendReplacement(out, "");
out.append(replacement);
Expand Down Expand Up @@ -106,41 +110,50 @@ public static String replace(String text, OfflinePlayer player) {
String group = matcher.group(1);
if (PLAYER_VARIABLES.contains(group)) {
replacement = player.getName();
} else if (group.equals("<random_player>")) {
Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
Player possible = Iterables.get(players, new Random().nextInt(players.size()), null);
if (possible != null) {
replacement = possible.getName();
}
} else if (group.equals("<random_npc>")) {
List<NPC> all = Lists.newArrayList(CitizensAPI.getNPCRegistry());
if (all.size() > 0) {
replacement = all.get(new Random().nextInt(all.size())).getName();
}
} else if (group.equals("<random_npc_id>")) {
List<NPC> all = Lists.newArrayList(CitizensAPI.getNPCRegistry());
if (all.size() > 0) {
replacement = Integer.toString(all.get(new Random().nextInt(all.size())).getId());
}
} else if (group.equals("<nearest_player>")) {
double min = Double.MAX_VALUE;
Player closest = null;
for (Player entity : CitizensAPI.getLocationLookup()
.getNearbyPlayers(player.getPlayer().getLocation(), 25)) {
if (entity == player || CitizensAPI.getNPCRegistry().isNPC(entity))
continue;
Location location = entity.getLocation();
double dist = location.distanceSquared(player.getPlayer().getLocation());
if (dist > min)
continue;
min = dist;
closest = entity;
}
if (closest != null) {
replacement = closest.getName();
} else {
switch (group) {
case "<random_player>":
Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
Player possible = Iterables.get(players, new Random().nextInt(players.size()), null);
if (possible != null) {
replacement = possible.getName();
}
break;
case "<random_npc>":
List<NPC> all = Lists.newArrayList(CitizensAPI.getNPCRegistry());
if (all.size() > 0) {
replacement = all.get(new Random().nextInt(all.size())).getName();
}
break;
case "<random_npc_id>":
all = Lists.newArrayList(CitizensAPI.getNPCRegistry());
if (all.size() > 0) {
replacement = Integer.toString(all.get(new Random().nextInt(all.size())).getId());
}
break;
case "<nearest_player>":
double min = Double.MAX_VALUE;
Player closest = null;
Location location = player.getPlayer().getLocation();
for (Player entity : CitizensAPI.getLocationLookup()
.getNearbyPlayers(player.getPlayer().getLocation(), 25)) {
if (entity == player || CitizensAPI.getNPCRegistry().isNPC(entity))
continue;
double dist = entity.getLocation().distanceSquared(location);
if (dist > min)
continue;
min = dist;
closest = entity;
}
if (closest != null) {
replacement = closest.getName();
}
break;
case "<world>":
replacement = player.getPlayer().getWorld().getName();
break;

}
} else if (group.equals("<world>")) {
replacement = player.getPlayer().getWorld().getName();
}
matcher.appendReplacement(out, "");
out.append(replacement);
Expand Down Expand Up @@ -173,5 +186,5 @@ private static String setPlaceholderAPIPlaceholders(String text, OfflinePlayer p
private static final Pattern PLAYER_PLACEHOLDER_MATCHER = Pattern.compile(
"(<player>|<p>|@p|%player%|<random_player>|<random_npc>|<random_npc_id>|<nearest_player>|<world>)");
private static final String[] PLAYER_PLACEHOLDERS = { "<player>", "<p>", "@p", "%player%" };
private static final Collection<String> PLAYER_VARIABLES = ImmutableList.of("<player>", "<p>", "@p", "%player%");
private static final Collection<String> PLAYER_VARIABLES = ImmutableSet.of("<player>", "<p>", "@p", "%player%");
}

0 comments on commit c8a5132

Please sign in to comment.