Skip to content

Commit

Permalink
Add getWorld() to BlockSource, fix NPC#setName not updating the entit…
Browse files Browse the repository at this point in the history
…y name, add NPC#clone
  • Loading branch information
fullwall committed Jul 5, 2013
1 parent 85d6dc9 commit 3109fee
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 21 deletions.
Expand Up @@ -11,9 +11,9 @@
public class NavigatorParameters implements Cloneable {
private AttackStrategy attackStrategy;
private boolean avoidWater;
private float baseSpeed;
private float baseSpeed = 1F;
private final List<NavigatorCallback> callbacks = Lists.newArrayListWithExpectedSize(3);
private double distanceMargin = 3F;
private double distanceMargin = 2F;
private final List<BlockExaminer> examiners = Lists.newArrayList();
private float range;
private float speedModifier = 1F;
Expand Down
@@ -1,6 +1,7 @@
package net.citizensnpcs.api.astar.pathfinder;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.util.Vector;

public abstract class BlockSource {
Expand All @@ -13,4 +14,6 @@ public Material getMaterialAt(int x, int y, int z) {
public Material getMaterialAt(Vector pos) {
return getMaterialAt(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ());
}

public abstract World getWorld();
}
Expand Up @@ -8,7 +8,6 @@ public abstract class CachingChunkBlockSource<T> extends BlockSource {
private final int chunkX;
private final int chunkZ;
int t;

protected final World world;

protected CachingChunkBlockSource(Location location, float radius) {
Expand Down Expand Up @@ -59,4 +58,9 @@ private T getSpecific(int x, int z) {
}
return null;
}

@Override
public World getWorld() {
return world;
}
}
Expand Up @@ -11,9 +11,10 @@
public class MinecraftBlockExaminer implements BlockExaminer {
private boolean contains(Material[] search, Material... find) {
for (Material haystack : search) {
for (Material needle : find)
for (Material needle : find) {
if (haystack == needle)
return true;
}
}
return false;
}
Expand Down Expand Up @@ -52,22 +53,6 @@ public boolean isPassable(BlockSource source, PathPoint point) {
return true;
}

private static final Vector DOWN = new Vector(0, -1, 0);

private static final Set<Material> PASSABLE = EnumSet.of(Material.AIR, Material.DEAD_BUSH, Material.DETECTOR_RAIL,
Material.DIODE, Material.DIODE_BLOCK_OFF, Material.DIODE_BLOCK_ON, Material.FENCE_GATE,
Material.ITEM_FRAME, Material.LADDER, Material.LEVER, Material.LONG_GRASS, Material.MELON_STEM,
Material.NETHER_FENCE, Material.PUMPKIN_STEM, Material.POWERED_RAIL, Material.RAILS, Material.RED_ROSE,
Material.RED_MUSHROOM, Material.REDSTONE, Material.REDSTONE_TORCH_OFF, Material.REDSTONE_TORCH_OFF,
Material.REDSTONE_WIRE, Material.SIGN, Material.SIGN_POST, Material.SNOW, Material.STRING,
Material.STONE_BUTTON, Material.SUGAR_CANE_BLOCK, Material.TRIPWIRE, Material.VINE, Material.WALL_SIGN,
Material.WHEAT, Material.WATER, Material.WEB, Material.WOOD_BUTTON, Material.WOODEN_DOOR,
Material.STATIONARY_WATER);

private static final Set<Material> UNWALKABLE = EnumSet.of(Material.AIR, Material.LAVA, Material.STATIONARY_LAVA,
Material.CACTUS);
private static final Vector UP = new Vector(0, 1, 0);

public static boolean canStandIn(Material mat) {
return PASSABLE.contains(mat);
}
Expand All @@ -81,4 +66,19 @@ public static boolean canStandOn(Block block) {
public static boolean canStandOn(Material mat) {
return !UNWALKABLE.contains(mat) && !PASSABLE.contains(mat);
}

private static final Vector DOWN = new Vector(0, -1, 0);
private static final Set<Material> PASSABLE = EnumSet.of(Material.AIR, Material.DEAD_BUSH, Material.DETECTOR_RAIL,
Material.DIODE, Material.DIODE_BLOCK_OFF, Material.DIODE_BLOCK_ON, Material.FENCE_GATE,
Material.ITEM_FRAME, Material.LADDER, Material.LEVER, Material.LONG_GRASS, Material.CARPET,
Material.MELON_STEM, Material.NETHER_FENCE, Material.PUMPKIN_STEM, Material.POWERED_RAIL, Material.RAILS,
Material.RED_ROSE, Material.RED_MUSHROOM, Material.REDSTONE, Material.REDSTONE_TORCH_OFF,
Material.REDSTONE_TORCH_OFF, Material.REDSTONE_WIRE, Material.SIGN, Material.SIGN_POST, Material.SNOW,
Material.STRING, Material.STONE_BUTTON, Material.SUGAR_CANE_BLOCK, Material.TRIPWIRE, Material.VINE,
Material.WALL_SIGN, Material.WHEAT, Material.WATER, Material.WEB, Material.WOOD_BUTTON,
Material.WOODEN_DOOR, Material.STATIONARY_WATER);
private static final Set<Material> UNWALKABLE = EnumSet.of(Material.AIR, Material.LAVA, Material.STATIONARY_LAVA,
Material.CACTUS);

private static final Vector UP = new Vector(0, 1, 0);
}
Expand Up @@ -6,6 +6,7 @@
import net.citizensnpcs.api.astar.Plan;

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

import com.google.common.collect.Lists;
Expand All @@ -17,6 +18,8 @@ public class VectorNode extends AStarNode implements PathPoint {
private final BlockExaminer[] examiners;
final Vector location;

boolean played = false;

public VectorNode(Location location, BlockSource source, BlockExaminer... examiners) {
this(location.toVector(), source, examiners);
}
Expand Down Expand Up @@ -56,6 +59,7 @@ private float getBlockCost() {

@Override
public Iterable<AStarNode> getNeighbours() {
World world = blockSource.getWorld();
List<AStarNode> nodes = Lists.newArrayList();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Expand Up @@ -14,11 +14,16 @@
import net.citizensnpcs.api.event.NPCRemoveTraitEvent;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.MobType;
import net.citizensnpcs.api.trait.trait.Speech;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.MemoryDataKey;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.HandlerList;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -72,6 +77,19 @@ public void addTrait(Trait trait) {
Bukkit.getPluginManager().callEvent(new NPCAddTraitEvent(this, trait));
}

@Override
public NPC clone() {
NPC copy = CitizensAPI.getNPCRegistry().createNPC(getTrait(MobType.class).getType(), getFullName());
DataKey key = new MemoryDataKey();
this.save(key);
copy.load(key);

for (Trait trait : copy.getTraits()) {
trait.onCopy();
}
return copy;
}

@Override
public MetadataStore data() {
return this.metadata;
Expand Down Expand Up @@ -196,6 +214,15 @@ public void save(DataKey root) {
@Override
public void setName(String name) {
this.name = name;
if (!isSpawned())
return;
LivingEntity bukkitEntity = getBukkitEntity();
bukkitEntity.setCustomName(getFullName());
if (bukkitEntity.getType() == EntityType.PLAYER) {
Location old = bukkitEntity.getLocation();
despawn(DespawnReason.PENDING_RESPAWN);
spawn(old);
}
}

public void update() {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/citizensnpcs/api/npc/NPC.java
Expand Up @@ -17,7 +17,7 @@
/**
* Represents an NPC with optional {@link Trait}s.
*/
public interface NPC extends Agent {
public interface NPC extends Agent, Cloneable {

/**
* Adds a trait to this NPC. This will use the {@link TraitFactory} defined
Expand All @@ -37,6 +37,12 @@ public interface NPC extends Agent {
*/
public void addTrait(Trait trait);

/**
* @return A clone of the NPC. May not be an exact copy depending on the
* {@link Trait}s installed.
*/
public NPC clone();

/**
* @return The metadata store of this NPC.
*/
Expand Down

0 comments on commit 3109fee

Please sign in to comment.