Skip to content

Commit

Permalink
Add UUIDPersister, fix NPC evoker saving, make TeleportStuckAction re…
Browse files Browse the repository at this point in the history
…use old code
  • Loading branch information
fullwall committed Dec 10, 2016
1 parent 67d5056 commit a7bf1c2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
20 changes: 5 additions & 15 deletions src/main/java/net/citizensnpcs/api/ai/TeleportStuckAction.java
@@ -1,24 +1,21 @@
package net.citizensnpcs.api.ai;

import java.util.EnumSet;
import java.util.Set;

import net.citizensnpcs.api.npc.NPC;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.npc.NPC;

public class TeleportStuckAction implements StuckAction {
private TeleportStuckAction() {
// singleton
}

private boolean canStand(Block block) {
return TELEPORTABLE_BLOCKS.contains(block.getType())
&& TELEPORTABLE_BLOCKS.contains(block.getRelative(BlockFace.UP).getType());
return MinecraftBlockExaminer.canStandIn(block.getType())
&& MinecraftBlockExaminer.canStandIn(block.getRelative(BlockFace.UP).getType());
}

@Override
Expand All @@ -44,11 +41,4 @@ public boolean run(NPC npc, Navigator navigator) {
public static TeleportStuckAction INSTANCE = new TeleportStuckAction();
private static final int MAX_ITERATIONS = 10;
private static final double RANGE = 10;
private static final Set<Material> TELEPORTABLE_BLOCKS = EnumSet.of(Material.AIR, Material.BIRCH_WOOD_STAIRS,
Material.BRICK_STAIRS, Material.CAKE_BLOCK, Material.COBBLESTONE_STAIRS, Material.DEAD_BUSH,
Material.JUNGLE_WOOD_STAIRS, Material.LADDER, Material.LONG_GRASS, Material.RAILS, Material.POWERED_RAIL,
Material.REDSTONE_WIRE, Material.REDSTONE_TORCH_ON, Material.REDSTONE_TORCH_OFF, Material.TRAP_DOOR,
Material.TRIPWIRE, Material.TRIPWIRE_HOOK, Material.VINE, Material.WALL_SIGN, Material.WATER,
Material.WOOD_STAIRS, Material.WOOD_STEP, Material.WOODEN_DOOR, Material.YELLOW_FLOWER, Material.RED_ROSE,
Material.SIGN, Material.SIGN_POST, Material.WATER);
}
26 changes: 12 additions & 14 deletions src/main/java/net/citizensnpcs/api/npc/NPCRegistry.java
Expand Up @@ -12,7 +12,7 @@ public interface NPCRegistry extends Iterable<NPC> {

/**
* Creates an despawned {@link NPC}.
*
*
* @param type
* {@link EntityType} to assign to the NPC
* @param name
Expand All @@ -22,9 +22,9 @@ public interface NPCRegistry extends Iterable<NPC> {
public NPC createNPC(EntityType type, String name);

/**
* Creates an {@link NPC} with the given id. WARNING: may overwrite any
* existing NPC in the registry with the same ID.
*
* Creates an {@link NPC} with the given id. WARNING: may overwrite any existing NPC in the registry with the same
* ID.
*
* @param type
* The {@link EntityType} of the NPC.
* @param id
Expand All @@ -36,9 +36,8 @@ public interface NPCRegistry extends Iterable<NPC> {
public NPC createNPC(EntityType type, UUID uuid, int id, String name);

/**
* Deregisters the {@link NPC} and removes all data about it from the data
* store.
*
* Deregisters the {@link NPC} and removes all data about it from the data store.
*
* @param npc
* The NPC to deregister
*/
Expand All @@ -51,7 +50,7 @@ public interface NPCRegistry extends Iterable<NPC> {

/**
* Gets the {@link NPC} with the given ID if it exists.
*
*
* @param id
* ID of the NPC
* @return NPC with the given ID (may or may not be spawned)
Expand All @@ -61,9 +60,8 @@ public interface NPCRegistry extends Iterable<NPC> {
public NPC getByUniqueId(UUID uuid);

/**
* Gets the {@link NPC} with the given unique ID if it exists, otherwise
* null.
*
* Gets the {@link NPC} with the given unique ID if it exists, otherwise null.
*
* @param uuid
* ID of the NPC
* @return NPC with the given UUID
Expand All @@ -72,7 +70,7 @@ public interface NPCRegistry extends Iterable<NPC> {

/**
* Tries to convert the given {@link Entity} to a spawned {@link NPC}.
*
*
* @param entity
* Entity to get the NPC from
* @return NPC from the given entity or null if not found.
Expand All @@ -81,7 +79,7 @@ public interface NPCRegistry extends Iterable<NPC> {

/**
* Checks whether the given {@link Entity} is convertable to an {@link NPC}.
*
*
* @param entity
* Entity to check
* @return Whether the given entity is an NPC
Expand All @@ -90,7 +88,7 @@ public interface NPCRegistry extends Iterable<NPC> {

/**
* Returns a <em>sorted</em> view of this registry, sorted by NPC id.
*
*
* @return A sorted view of the registry
*/
Iterable<NPC> sorted();
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/net/citizensnpcs/api/npc/SimpleNPCDataStore.java
Expand Up @@ -2,12 +2,12 @@

import java.util.UUID;

import org.bukkit.entity.EntityType;

import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Storage;

import org.bukkit.entity.EntityType;

public class SimpleNPCDataStore implements NPCDataStore {
private final Storage root;

Expand Down Expand Up @@ -55,7 +55,7 @@ public void loadInto(NPCRegistry registry) {
}
NPC npc = registry.createNPC(type,
!key.getString("uuid", "").isEmpty() ? UUID.fromString(key.getString("uuid")) : UUID.randomUUID(),
id, key.getString("name"));
id, key.getString("name"));
npc.load(key);
}
}
Expand Down Expand Up @@ -92,7 +92,12 @@ public static NPCDataStore create(Storage storage) {
}

private static EntityType matchEntityType(String toMatch) {
EntityType type = EntityType.fromName(toMatch);
EntityType type;
try {
type = EntityType.valueOf(toMatch);
} catch (IllegalArgumentException ex) {
type = EntityType.fromName(toMatch);
}
if (type != null)
return type;
return matchEnum(EntityType.values(), toMatch);
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;

import org.bukkit.Location;
Expand Down Expand Up @@ -422,5 +423,6 @@ public void fillInStackTrace(StackTraceElement[] elements) {
registerPersistDelegate(Location.class, LocationPersister.class);
registerPersistDelegate(ItemStack.class, ItemStackPersister.class);
registerPersistDelegate(EulerAngle.class, EulerAnglePersister.class);
registerPersistDelegate(UUID.class, UUIDPersister.class);
}
}
21 changes: 21 additions & 0 deletions src/main/java/net/citizensnpcs/api/persistence/UUIDPersister.java
@@ -0,0 +1,21 @@
package net.citizensnpcs.api.persistence;

import java.util.UUID;

import net.citizensnpcs.api.util.DataKey;

public class UUIDPersister implements Persister<UUID> {
@Override
public UUID create(DataKey root) {
try {
return UUID.fromString(root.getString(""));
} catch (IllegalArgumentException e) {
return null;
}
}

@Override
public void save(UUID instance, DataKey root) {
root.setString("", instance.toString());
}
}

0 comments on commit a7bf1c2

Please sign in to comment.