2012-02-27 12:16:01 +01:00
|
|
|
package net.citizensnpcs.trait.waypoint;
|
|
|
|
|
2012-03-02 11:36:54 +01:00
|
|
|
import java.util.Iterator;
|
2012-02-27 12:16:01 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.citizensnpcs.api.ai.NavigationCallback;
|
|
|
|
import net.citizensnpcs.api.util.DataKey;
|
2012-02-28 14:03:36 +01:00
|
|
|
import net.citizensnpcs.api.util.StorageUtils;
|
2012-02-27 12:16:01 +01:00
|
|
|
import net.citizensnpcs.editor.Editor;
|
2012-02-28 13:11:25 +01:00
|
|
|
import net.citizensnpcs.util.Messaging;
|
2012-02-27 12:16:01 +01:00
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.block.Action;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
2012-03-02 11:36:54 +01:00
|
|
|
public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoint> {
|
|
|
|
private final GenericWaypointCallback callback = new GenericWaypointCallback(this);
|
|
|
|
|
2012-02-27 12:16:01 +01:00
|
|
|
private final List<Waypoint> waypoints = Lists.newArrayList();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Editor createEditor(final Player player) {
|
|
|
|
return new Editor() {
|
|
|
|
@Override
|
|
|
|
public void begin() {
|
2012-02-28 13:11:25 +01:00
|
|
|
player.sendMessage(ChatColor.AQUA + "Entered the linear waypoint editor!");
|
2012-02-27 12:16:01 +01:00
|
|
|
player.sendMessage(ChatColor.GREEN + "Left click to add waypoint, right click to remove.");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void end() {
|
|
|
|
player.sendMessage(ChatColor.GREEN + "Exited linear waypoint editor.");
|
2012-03-02 11:36:54 +01:00
|
|
|
callback.onProviderChanged();
|
2012-02-27 12:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public void onPlayerInteract(PlayerInteractEvent event) {
|
2012-02-28 08:39:00 +01:00
|
|
|
if (!event.getPlayer().equals(player))
|
|
|
|
return;
|
2012-02-28 15:17:49 +01:00
|
|
|
|
2012-02-27 12:16:01 +01:00
|
|
|
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
|
|
|
|
waypoints.add(new Waypoint(event.getClickedBlock().getLocation()));
|
2012-02-28 13:11:25 +01:00
|
|
|
Messaging.send(player, "<e>Added<a> a waypoint.");
|
2012-02-27 12:16:01 +01:00
|
|
|
} else if (waypoints.size() > 0) {
|
|
|
|
waypoints.remove(waypoints.size() - 1);
|
2012-02-28 13:11:25 +01:00
|
|
|
Messaging.send(player,
|
|
|
|
String.format("<e>Removed<a> a waypoint (<e>%d<a> remaining)", waypoints.size()));
|
2012-02-27 12:16:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-03-02 11:36:54 +01:00
|
|
|
@Override
|
|
|
|
public NavigationCallback getCallback() {
|
|
|
|
return callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<Waypoint> iterator() {
|
|
|
|
return waypoints.iterator();
|
|
|
|
}
|
|
|
|
|
2012-02-27 12:16:01 +01:00
|
|
|
@Override
|
|
|
|
public void load(DataKey key) {
|
|
|
|
for (DataKey root : key.getRelative("waypoints").getIntegerSubKeys()) {
|
|
|
|
waypoints.add(new Waypoint(StorageUtils.loadLocation(root)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void save(DataKey key) {
|
|
|
|
key = key.getRelative("waypoints");
|
|
|
|
for (int i = 0; i < waypoints.size(); ++i) {
|
|
|
|
StorageUtils.saveLocation(key.getRelative(Integer.toString(i)), waypoints.get(i).getLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|