mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-12-26 02:57:39 +01:00
Fix part of CITIZENS-11 (waypoint saving incorrectly)
This commit is contained in:
parent
60e65e9343
commit
15ebeab5a9
@ -16,7 +16,6 @@ import net.citizensnpcs.npc.ai.CitizensAI;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.util.Messaging;
|
||||
import net.citizensnpcs.util.StringHelper;
|
||||
|
||||
import net.minecraft.server.EntityLiving;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@ -26,8 +25,8 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public abstract class CitizensNPC extends AbstractNPC {
|
||||
protected final CitizensAI ai = new CitizensAI(this);
|
||||
protected final CitizensNPCManager manager;
|
||||
private final CitizensAI ai = new CitizensAI(this);
|
||||
private final CitizensNPCManager manager;
|
||||
protected EntityLiving mcEntity;
|
||||
private final CitizensTraitManager traitManager;
|
||||
|
||||
@ -139,6 +138,7 @@ public abstract class CitizensNPC extends AbstractNPC {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
super.update();
|
||||
ai.update();
|
||||
|
@ -3,8 +3,8 @@ package net.citizensnpcs.npc;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import net.citizensnpcs.api.exception.TraitException;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.trait.Trait;
|
||||
import net.citizensnpcs.api.trait.TraitFactory;
|
||||
@ -20,6 +20,8 @@ import net.citizensnpcs.trait.Powered;
|
||||
import net.citizensnpcs.trait.text.Text;
|
||||
import net.citizensnpcs.trait.waypoint.Waypoints;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class CitizensTraitManager implements TraitManager {
|
||||
private final Map<String, Class<? extends Trait>> registered = new HashMap<String, Class<? extends Trait>>();
|
||||
|
||||
@ -57,52 +59,54 @@ public class CitizensTraitManager implements TraitManager {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Trait> T getTrait(Class<T> clazz, NPC npc) {
|
||||
for (String name : registered.keySet())
|
||||
if (registered.get(name).equals(clazz)) {
|
||||
Trait t = create(registered.get(name), npc);
|
||||
try {
|
||||
if (t.getName() == null)
|
||||
t.setName(name);
|
||||
return (T) t;
|
||||
} catch (TraitException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
for (Entry<String, Class<? extends Trait>> entry : registered.entrySet()) {
|
||||
if (!entry.getValue().equals(clazz))
|
||||
continue;
|
||||
Trait t = create(entry.getValue(), npc);
|
||||
t.setName(entry.getKey());
|
||||
return (T) t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Trait> T getTrait(String name, NPC npc) {
|
||||
if (!registered.containsKey(name))
|
||||
// TODO: we could replace NPC with Object... and search for the
|
||||
// constructor
|
||||
Class<? extends Trait> clazz = registered.get(name);
|
||||
if (clazz == null)
|
||||
return null;
|
||||
Trait t = getTrait(registered.get(name), npc);
|
||||
try {
|
||||
if (t.getName() == null)
|
||||
t.setName(name);
|
||||
return (T) t;
|
||||
} catch (TraitException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
Trait t = getTrait(clazz, npc);
|
||||
t.setName(name);
|
||||
return (T) t;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T extends Trait> T create(Class<T> trait, NPC npc) {
|
||||
Constructor<? extends Trait> constructor;
|
||||
|
||||
try {
|
||||
constructor = trait.getConstructor(NPC.class);
|
||||
} catch (Exception ex) {
|
||||
constructor = null;
|
||||
if (!CACHED_CTORS.containsKey(trait)) {
|
||||
try {
|
||||
constructor = trait.getConstructor(NPC.class);
|
||||
constructor.setAccessible(true); // do we want to allow private
|
||||
// constructors?
|
||||
} catch (Exception ex) {
|
||||
constructor = null;
|
||||
}
|
||||
CACHED_CTORS.put(trait, constructor);
|
||||
} else {
|
||||
constructor = CACHED_CTORS.get(trait);
|
||||
}
|
||||
|
||||
try {
|
||||
if (npc == null)
|
||||
return (T) trait.newInstance();
|
||||
return constructor != null ? (T) constructor.newInstance(npc) : (T) trait.newInstance();
|
||||
if (constructor == null || npc == null)
|
||||
return trait.newInstance();
|
||||
return (T) constructor.newInstance(npc);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Map<Class<? extends Trait>, Constructor<? extends Trait>> CACHED_CTORS = Maps.newHashMap();
|
||||
}
|
@ -8,7 +8,7 @@ import java.util.List;
|
||||
import net.citizensnpcs.api.ai.AI;
|
||||
import net.citizensnpcs.api.ai.Goal;
|
||||
import net.citizensnpcs.api.ai.NavigationCallback;
|
||||
import net.citizensnpcs.api.ai.NavigationCallback.PathCancelReason;
|
||||
import net.citizensnpcs.api.ai.NavigationCallback.CancelReason;
|
||||
import net.citizensnpcs.npc.CitizensNPC;
|
||||
import net.citizensnpcs.util.Messaging;
|
||||
|
||||
@ -44,7 +44,7 @@ public class CitizensAI implements AI {
|
||||
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
|
||||
while (itr.hasNext()) {
|
||||
NavigationCallback next = itr.next().get();
|
||||
if (next == null || next.onCancel(this, PathCancelReason.CANCEL)) {
|
||||
if (next == null || next.onCancel(this, CancelReason.CANCEL)) {
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
@ -104,7 +104,7 @@ public class CitizensAI implements AI {
|
||||
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
|
||||
while (itr.hasNext()) {
|
||||
NavigationCallback next = itr.next().get();
|
||||
if (next == null || (replaced && next.onCancel(this, PathCancelReason.REPLACE)) || next.onBegin(this)) {
|
||||
if (next == null || (replaced && next.onCancel(this, CancelReason.REPLACE)) || next.onBegin(this)) {
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
@ -121,7 +121,7 @@ public class CitizensAI implements AI {
|
||||
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
|
||||
while (itr.hasNext()) {
|
||||
NavigationCallback next = itr.next().get();
|
||||
if (next == null || (replaced && next.onCancel(this, PathCancelReason.REPLACE)) || next.onBegin(this)) {
|
||||
if (next == null || (replaced && next.onCancel(this, CancelReason.REPLACE)) || next.onBegin(this)) {
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
|
@ -8,8 +8,8 @@ import net.citizensnpcs.api.ai.NavigationCallback;
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class GenericWaypointCallback extends NavigationCallback {
|
||||
private Location dest;
|
||||
private AI ai;
|
||||
private Location dest;
|
||||
private boolean executing;
|
||||
private Iterator<Waypoint> itr;
|
||||
private final Iterable<Waypoint> provider;
|
||||
@ -27,7 +27,7 @@ public class GenericWaypointCallback extends NavigationCallback {
|
||||
@Override
|
||||
public void onAttach(AI ai) {
|
||||
this.ai = ai;
|
||||
executing = !ai.hasDestination();
|
||||
executing |= !ai.hasDestination();
|
||||
if (!executing)
|
||||
return;
|
||||
if (dest == null) {
|
||||
@ -42,17 +42,17 @@ public class GenericWaypointCallback extends NavigationCallback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCancel(AI ai, PathCancelReason reason) {
|
||||
if (executing && reason == PathCancelReason.REPLACE) {
|
||||
public boolean onCancel(AI ai, CancelReason reason) {
|
||||
if (executing && reason == CancelReason.REPLACE) {
|
||||
executing = false;
|
||||
} else {
|
||||
executing = true;
|
||||
ensureItr();
|
||||
if (dest != null)
|
||||
ai.setDestination(dest);
|
||||
else if (itr.hasNext()) {
|
||||
ai.setDestination(itr.next().getLocation());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
executing = true;
|
||||
ensureItr();
|
||||
if (dest == null && itr.hasNext())
|
||||
dest = itr.next().getLocation();
|
||||
if (dest != null) {
|
||||
ai.setDestination(dest);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -60,8 +60,7 @@ public class GenericWaypointCallback extends NavigationCallback {
|
||||
@Override
|
||||
public boolean onCompletion(AI ai) {
|
||||
if (executing) { // if we're executing, we need to get the next waypoint
|
||||
if (!itr.hasNext())
|
||||
itr = provider.iterator();
|
||||
ensureItr();
|
||||
dest = itr.hasNext() ? itr.next().getLocation() : null;
|
||||
} else {
|
||||
executing = true;
|
||||
|
@ -50,8 +50,8 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
|
||||
at.getBlockX(), at.getBlockY(), at.getBlockZ()));
|
||||
} else if (waypoints.size() > 0) {
|
||||
waypoints.remove(waypoints.size() - 1);
|
||||
Messaging.send(player, String.format("<e>Removed<a> a waypoint (<e>%d<a> remaining)", waypoints
|
||||
.size()));
|
||||
Messaging.send(player,
|
||||
String.format("<e>Removed<a> a waypoint (<e>%d<a> remaining)", waypoints.size()));
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -80,16 +80,17 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
|
||||
|
||||
@Override
|
||||
public void save(DataKey key) {
|
||||
key.removeKey("waypoints");
|
||||
key = key.getRelative("waypoints");
|
||||
for (int i = 0; i < waypoints.size(); ++i) {
|
||||
Location location = waypoints.get(i).getLocation();
|
||||
key = key.getRelative(Integer.toString(i) + ".location");
|
||||
key.setString("world", location.getWorld().getName());
|
||||
key.setDouble("x", location.getX());
|
||||
key.setDouble("y", location.getY());
|
||||
key.setDouble("z", location.getZ());
|
||||
key.setDouble("yaw", location.getYaw());
|
||||
key.setDouble("pitch", location.getPitch());
|
||||
DataKey root = key.getRelative(Integer.toString(i) + ".location");
|
||||
root.setString("world", location.getWorld().getName());
|
||||
root.setDouble("x", location.getX());
|
||||
root.setDouble("y", location.getY());
|
||||
root.setDouble("z", location.getZ());
|
||||
root.setDouble("yaw", location.getYaw());
|
||||
root.setDouble("pitch", location.getPitch());
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package net.citizensnpcs.trait.waypoint;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import net.citizensnpcs.api.exception.NPCLoadException;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
@ -18,7 +19,6 @@ public class Waypoints extends Trait {
|
||||
|
||||
public Waypoints(NPC npc) {
|
||||
this.npc = npc;
|
||||
npc.getAI().registerNavigationCallback(provider.getCallback());
|
||||
}
|
||||
|
||||
public Editor getEditor(Player player) {
|
||||
@ -27,12 +27,14 @@ public class Waypoints extends Trait {
|
||||
|
||||
@Override
|
||||
public void load(DataKey key) throws NPCLoadException {
|
||||
provider = null;
|
||||
providerName = key.getString("provider", "linear");
|
||||
for (Class<? extends WaypointProvider> clazz : providers.keySet())
|
||||
if (providers.get(clazz).equals(providerName)) {
|
||||
provider = create(clazz);
|
||||
for (Entry<Class<? extends WaypointProvider>, String> entry : providers.entrySet()) {
|
||||
if (entry.getValue().equals(providerName)) {
|
||||
provider = create(entry.getKey());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (provider == null)
|
||||
return;
|
||||
provider.load(key.getRelative(providerName));
|
||||
|
Loading…
Reference in New Issue
Block a user