mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-01-02 14:38:19 +01:00
Add some waypoint triggers
This commit is contained in:
parent
c9f054b615
commit
db403bbcf0
@ -34,6 +34,18 @@ public class Controllable extends Trait implements Toggleable, CommandConfigurab
|
||||
super("controllable");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(CommandContext args) {
|
||||
if (args.hasFlag('f'))
|
||||
explicitType = EntityType.BLAZE;
|
||||
else if (args.hasFlag('n'))
|
||||
explicitType = null;
|
||||
else if (args.hasValueFlag("explicittype"))
|
||||
explicitType = Util.matchEntityType(args.getFlag("explicittype"));
|
||||
if (npc.isSpawned())
|
||||
loadController();
|
||||
}
|
||||
|
||||
private void enterOrLeaveVehicle(Player player) {
|
||||
EntityPlayer handle = ((CraftPlayer) player).getHandle();
|
||||
if (getHandle().passenger != null) {
|
||||
@ -58,6 +70,34 @@ public class Controllable extends Trait implements Toggleable, CommandConfigurab
|
||||
explicitType = Util.matchEntityType(key.getString("explicittype"));
|
||||
}
|
||||
|
||||
private void loadController() {
|
||||
EntityType type = npc.getBukkitEntity().getType();
|
||||
if (explicitType != null)
|
||||
type = explicitType;
|
||||
Class<? extends Controller> clazz = controllerTypes.get(type);
|
||||
if (clazz == null) {
|
||||
controller = new GroundController();
|
||||
return;
|
||||
}
|
||||
Constructor<? extends Controller> innerConstructor = null;
|
||||
try {
|
||||
innerConstructor = clazz.getConstructor(Controllable.class);
|
||||
innerConstructor.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
if (innerConstructor == null) {
|
||||
controller = clazz.newInstance();
|
||||
} else
|
||||
controller = innerConstructor.newInstance(this);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
controller = new GroundController();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean mount(Player toMount) {
|
||||
if (npc.getBukkitEntity().getPassenger() != null)
|
||||
return false;
|
||||
@ -99,34 +139,6 @@ public class Controllable extends Trait implements Toggleable, CommandConfigurab
|
||||
loadController();
|
||||
}
|
||||
|
||||
private void loadController() {
|
||||
EntityType type = npc.getBukkitEntity().getType();
|
||||
if (explicitType != null)
|
||||
type = explicitType;
|
||||
Class<? extends Controller> clazz = controllerTypes.get(type);
|
||||
if (clazz == null) {
|
||||
controller = new GroundController();
|
||||
return;
|
||||
}
|
||||
Constructor<? extends Controller> innerConstructor = null;
|
||||
try {
|
||||
innerConstructor = clazz.getConstructor(Controllable.class);
|
||||
innerConstructor.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
if (innerConstructor == null) {
|
||||
controller = clazz.newInstance();
|
||||
} else
|
||||
controller = innerConstructor.newInstance(this);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
controller = new GroundController();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!enabled || !npc.isSpawned() || getHandle().passenger == null)
|
||||
@ -236,16 +248,4 @@ public class Controllable extends Trait implements Toggleable, CommandConfigurab
|
||||
controllerTypes.put(EntityType.ENDER_DRAGON, AirController.class);
|
||||
controllerTypes.put(EntityType.GHAST, AirController.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(CommandContext args) {
|
||||
if (args.hasFlag('f'))
|
||||
explicitType = EntityType.BLAZE;
|
||||
else if (args.hasFlag('n'))
|
||||
explicitType = null;
|
||||
else if (args.hasValueFlag("explicittype"))
|
||||
explicitType = Util.matchEntityType(args.getFlag("explicittype"));
|
||||
if (npc.isSpawned())
|
||||
loadController();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
package net.citizensnpcs.trait.waypoint;
|
||||
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.persistence.Persist;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class DelayWaypointTrigger implements WaypointTrigger {
|
||||
@Persist
|
||||
private int delay = 0;
|
||||
|
||||
@Override
|
||||
public void onWaypointReached(NPC npc, Location waypoint) {
|
||||
if (delay > 0)
|
||||
scheduleTask(npc.getTrait(Waypoints.class).getCurrentProvider());
|
||||
}
|
||||
|
||||
private void scheduleTask(final WaypointProvider provider) {
|
||||
provider.setPaused(true);
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
provider.setPaused(false);
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
|
||||
public void setDelay(int newDelay) {
|
||||
delay = newDelay;
|
||||
}
|
||||
}
|
@ -4,8 +4,6 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.ai.Goal;
|
||||
import net.citizensnpcs.api.ai.GoalSelector;
|
||||
@ -36,8 +34,6 @@ import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerItemHeldEvent;
|
||||
import org.bukkit.metadata.FixedMetadataValue;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
@ -280,14 +276,14 @@ public class LinearWaypointProvider implements WaypointProvider {
|
||||
}
|
||||
|
||||
private class LinearWaypointGoal implements Goal {
|
||||
private Location currentDestination;
|
||||
private Iterator<Location> itr;
|
||||
private Waypoint currentDestination;
|
||||
private Iterator<Waypoint> itr;
|
||||
private boolean paused;
|
||||
private GoalSelector selector;
|
||||
|
||||
private void ensureItr() {
|
||||
if (itr == null || !itr.hasNext())
|
||||
itr = Iterators.transform(waypoints.iterator(), WAYPOINT_TRANSFORMER);
|
||||
itr = waypoints.iterator();
|
||||
}
|
||||
|
||||
private Navigator getNavigator() {
|
||||
@ -303,10 +299,12 @@ public class LinearWaypointProvider implements WaypointProvider {
|
||||
if (selector == null || !event.getNavigator().equals(getNavigator()))
|
||||
return;
|
||||
selector.finish();
|
||||
if (event.getNavigator().getTargetAsLocation().equals(currentDestination.getLocation()))
|
||||
currentDestination.onReach(npc);
|
||||
}
|
||||
|
||||
public void onProviderChanged() {
|
||||
itr = Iterators.transform(waypoints.iterator(), WAYPOINT_TRANSFORMER);
|
||||
itr = waypoints.iterator();
|
||||
if (currentDestination != null)
|
||||
selector.finish();
|
||||
}
|
||||
@ -336,7 +334,7 @@ public class LinearWaypointProvider implements WaypointProvider {
|
||||
return false;
|
||||
}
|
||||
if (waypoints.size() == 1) {
|
||||
// avoid pathing to the same point and wasting memory.
|
||||
// avoid pathing to the same point repeatedly
|
||||
Location dest = npc.getBukkitEntity().getLocation();
|
||||
if (waypoints.get(0).getLocation().distanceSquared(dest) < 3)
|
||||
return false;
|
||||
@ -346,16 +344,9 @@ public class LinearWaypointProvider implements WaypointProvider {
|
||||
if (shouldExecute) {
|
||||
this.selector = selector;
|
||||
currentDestination = itr.next();
|
||||
getNavigator().setTarget(currentDestination);
|
||||
getNavigator().setTarget(currentDestination.getLocation());
|
||||
}
|
||||
return shouldExecute;
|
||||
}
|
||||
}
|
||||
|
||||
private static final Function<Waypoint, Location> WAYPOINT_TRANSFORMER = new Function<Waypoint, Location>() {
|
||||
@Override
|
||||
public Location apply(@Nullable Waypoint input) {
|
||||
return input == null ? null : input.getLocation();
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package net.citizensnpcs.trait.waypoint;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.persistence.Persist;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class TeleportWaypointTrigger implements WaypointTrigger {
|
||||
@Persist
|
||||
private Location location;
|
||||
|
||||
@Override
|
||||
public void onWaypointReached(NPC npc, Location waypoint) {
|
||||
if (location != null)
|
||||
npc.getBukkitEntity().teleport(waypoint);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package net.citizensnpcs.trait.waypoint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.persistence.Persist;
|
||||
import net.citizensnpcs.api.persistence.PersistenceLoader;
|
||||
|
||||
@ -24,7 +25,12 @@ public class Waypoint {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void onReach(NPC npc) {
|
||||
for (WaypointTrigger trigger : triggers)
|
||||
trigger.onWaypointReached(npc, location);
|
||||
}
|
||||
|
||||
static {
|
||||
PersistenceLoader.registerPersistDelegate(WaypointTrigger.class, WaypointTriggerPersister.class);
|
||||
PersistenceLoader.registerPersistDelegate(WaypointTrigger.class, WaypointTriggerRegistry.class);
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package net.citizensnpcs.trait.waypoint;
|
||||
|
||||
import net.citizensnpcs.api.persistence.Persister;
|
||||
import net.citizensnpcs.api.util.DataKey;
|
||||
|
||||
public class WaypointTriggerPersister implements Persister {
|
||||
@Override
|
||||
public Object create(DataKey root) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Object instance, DataKey root) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package net.citizensnpcs.trait.waypoint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.citizensnpcs.api.persistence.PersistenceLoader;
|
||||
import net.citizensnpcs.api.persistence.Persister;
|
||||
import net.citizensnpcs.api.util.DataKey;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class WaypointTriggerRegistry implements Persister {
|
||||
@Override
|
||||
public Object create(DataKey root) {
|
||||
String type = root.getString("type");
|
||||
Class<? extends WaypointTrigger> clazz = triggers.get(type);
|
||||
return clazz == null ? null : PersistenceLoader.load(clazz, root);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Object instance, DataKey root) {
|
||||
PersistenceLoader.save(instance, root);
|
||||
}
|
||||
|
||||
private static final Map<String, Class<? extends WaypointTrigger>> triggers = Maps.newHashMap();
|
||||
static {
|
||||
triggers.put("teleport", TeleportWaypointTrigger.class);
|
||||
triggers.put("delay", DelayWaypointTrigger.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user