Skip to content

Commit

Permalink
Add WIP API to VectorNode
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 6, 2022
1 parent 4abea2f commit 193e320
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
Expand Up @@ -34,8 +34,9 @@ public float getCost(BlockSource source, PathPoint point) {
if (below == Material.SOUL_SAND || below == Material.ICE)
return 1F;
if (isLiquidOrInLiquid(source.getWorld().getBlockAt(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()))) {
if (in == Material.LAVA)
if (in == Material.LAVA) {
return 2F;
}
return 1F;
}
return 0F; // TODO: add light level-specific costs?
Expand Down
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.block.Block;
import org.bukkit.util.Vector;

import net.citizensnpcs.api.npc.MetadataStore;
import net.citizensnpcs.api.npc.NPC;

public interface PathPoint {
Expand All @@ -19,6 +20,13 @@ public interface PathPoint {
*/
PathPoint createAtOffset(Vector vector);

/**
* Returns the {@link MetadataStore}, initialising it if not already present. This will be passed on to child nodes
* if nonempty.
*
*/
MetadataStore data();

/**
* Gets the destination Vector
*/
Expand Down
@@ -1,16 +1,18 @@
package net.citizensnpcs.api.astar.pathfinder;

import java.util.Collections;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.util.Vector;

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

import net.citizensnpcs.api.astar.AStarNode;
import net.citizensnpcs.api.astar.Plan;
import net.citizensnpcs.api.astar.pathfinder.BlockExaminer.PassableState;
import net.citizensnpcs.api.npc.MetadataStore;
import net.citizensnpcs.api.npc.SimpleMetadataStore;

public class VectorNode extends AStarNode implements PathPoint {
private float blockCost = -1;
Expand All @@ -19,13 +21,15 @@ public class VectorNode extends AStarNode implements PathPoint {
Vector location;
Vector locationCache;
List<Vector> pathVectors;
MetadataStore store;

public VectorNode(VectorGoal goal, Location location, BlockSource source, BlockExaminer... examiners) {
this(null, goal, location.toVector(), source, examiners);
}

public VectorNode(VectorNode parent, Vector location, PathInfo info) {
super(parent);
this.store = parent.store != null && parent.store.size() > 0 ? parent.store.clone() : null;
this.location = new Vector(location.getBlockX(), location.getBlockY(), location.getBlockZ());
this.info = info;
}
Expand Down Expand Up @@ -53,6 +57,11 @@ public VectorNode createAtOffset(Vector mod) {
return new VectorNode(this, mod, info);
}

@Override
public MetadataStore data() {
return store == null ? (store = new SimpleMetadataStore()) : store;
}

public float distance(VectorNode to) {
return (float) location.distance(to.location);
}
Expand Down Expand Up @@ -146,7 +155,7 @@ public PathPoint getParentPoint() {

@Override
public List<Vector> getPathVectors() {
return pathVectors != null ? pathVectors : Collections.singletonList(location);
return pathVectors != null ? pathVectors : ImmutableList.of(location);
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/citizensnpcs/api/npc/MetadataStore.java
Expand Up @@ -6,6 +6,11 @@
* Represents a storage system for metadata
*/
public interface MetadataStore {
/**
* Copies the metadata store.
*/
MetadataStore clone();

/**
* Fetches metadata from the given key.
*
Expand Down Expand Up @@ -129,4 +134,10 @@ public interface MetadataStore {
* The data to store
*/
void setPersistent(String key, Object data);

/**
*
* @return The number of elements in the store
*/
public int size();
}
12 changes: 12 additions & 0 deletions src/main/java/net/citizensnpcs/api/npc/SimpleMetadataStore.java
Expand Up @@ -20,6 +20,13 @@ private void checkPrimitive(Object data) {
}
}

@Override
public MetadataStore clone() {
SimpleMetadataStore copy = new SimpleMetadataStore();
copy.metadata.putAll(metadata);
return copy;
}

@Override
public <T> T get(NPC.Metadata key) {
return get(key.getKey());
Expand Down Expand Up @@ -114,6 +121,11 @@ public void setPersistent(String key, Object data) {
metadata.put(key, new MetadataObject(data, true));
}

@Override
public int size() {
return metadata.size();
}

private static class MetadataObject {
final boolean persistent;
final Object value;
Expand Down

0 comments on commit 193e320

Please sign in to comment.