Do some cleanup of WaypointMarkers to be more generic

This commit is contained in:
fullwall 2018-03-10 23:34:42 +08:00
parent 73ff01c774
commit 9c3fb30623
6 changed files with 74 additions and 17 deletions

View File

@ -0,0 +1,48 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
public class EntityMarkers<T> {
private final Map<T, Entity> markers = Maps.newHashMap();
private final NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore());
public Entity createMarker(T marker, Location at) {
Entity entity = spawnMarker(at.getWorld(), at);
if (entity == null)
return null;
markers.put(marker, entity);
return entity;
}
public void destroyMarkers() {
for (Entity entity : markers.values()) {
entity.remove();
}
markers.clear();
}
public void removeMarker(T marker) {
Entity entity = markers.remove(marker);
if (entity != null) {
entity.remove();
}
}
public Entity spawnMarker(World world, Location at) {
NPC npc = registry.createNPC(EntityType.ENDER_SIGNAL, "");
npc.spawn(at);
return npc.getEntity();
}
}

View File

@ -60,7 +60,7 @@ public class GuidedWaypointProvider implements EnumerableWaypointProvider {
}
final Player player = (Player) sender;
return new WaypointEditor() {
private final WaypointMarkers markers = new WaypointMarkers(player.getWorld());
private final EntityMarkers<Waypoint> markers = new EntityMarkers<Waypoint>();
private boolean showPath;
@Override
@ -71,12 +71,12 @@ public class GuidedWaypointProvider implements EnumerableWaypointProvider {
private void createWaypointMarkers() {
for (Waypoint waypoint : Iterables.concat(available, helpers)) {
markers.createWaypointMarker(waypoint);
markers.createMarker(waypoint, waypoint.getLocation().clone().add(0, 1, 0));
}
}
private void createWaypointMarkerWithData(Waypoint element) {
Entity entity = markers.createWaypointMarker(element);
Entity entity = markers.createMarker(element, element.getLocation().clone().add(0, 1, 0));
if (entity == null)
return;
entity.setMetadata("citizens.waypointhashcode",
@ -86,7 +86,7 @@ public class GuidedWaypointProvider implements EnumerableWaypointProvider {
@Override
public void end() {
Messaging.sendTr(player, Messages.GUIDED_WAYPOINT_EDITOR_END);
markers.destroyWaypointMarkers();
markers.destroyMarkers();
}
@EventHandler(ignoreCancelled = true)
@ -105,7 +105,7 @@ public class GuidedWaypointProvider implements EnumerableWaypointProvider {
available.clear();
helpers.clear();
if (showPath)
markers.destroyWaypointMarkers();
markers.destroyMarkers();
}
});
}
@ -160,7 +160,7 @@ public class GuidedWaypointProvider implements EnumerableWaypointProvider {
createWaypointMarkers();
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_SHOWING_MARKERS);
} else {
markers.destroyWaypointMarkers();
markers.destroyMarkers();
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_NOT_SHOWING_MARKERS);
}
}

View File

@ -146,13 +146,13 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
Conversation conversation;
boolean editing = true;
int editingSlot = waypoints.size() - 1;
WaypointMarkers markers;
EntityMarkers<Waypoint> markers;
private final Player player;
private boolean showPath;
private LinearWaypointEditor(Player player) {
this.player = player;
this.markers = new WaypointMarkers(player.getWorld());
this.markers = new EntityMarkers<Waypoint>();
}
@Override
@ -164,13 +164,13 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
editingSlot = 0;
waypoints.clear();
onWaypointsModified();
markers.destroyWaypointMarkers();
markers.destroyMarkers();
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_WAYPOINTS_CLEARED);
}
private void createWaypointMarkers() {
for (int i = 0; i < waypoints.size(); i++) {
markers.createWaypointMarker(waypoints.get(i));
markers.createMarker(waypoints.get(i), waypoints.get(i).getLocation().clone().add(0, 1, 0));
}
}
@ -178,13 +178,14 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
public void end() {
if (!editing)
return;
if (conversation != null)
if (conversation != null) {
conversation.abandon();
}
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_END);
editing = false;
if (!showPath)
return;
markers.destroyWaypointMarkers();
markers.destroyMarkers();
}
private String formatLoc(Location location) {
@ -287,7 +288,7 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
normaliseEditingSlot();
waypoints.add(editingSlot, element);
if (showPath) {
markers.createWaypointMarker(element);
markers.createMarker(element, element.getLocation().clone().add(0, 1, 0));
}
editingSlot = Math.min(editingSlot + 1, waypoints.size());
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_ADDED_WAYPOINT, formatLoc(at), editingSlot + 1,
@ -297,7 +298,7 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
normaliseEditingSlot();
Waypoint waypoint = waypoints.remove(editingSlot);
if (showPath) {
markers.removeWaypointMarker(waypoint);
markers.removeMarker(waypoint);
}
editingSlot = Math.max(0, editingSlot - 1);
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_REMOVED_WAYPOINT, waypoints.size(),
@ -350,7 +351,7 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
createWaypointMarkers();
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_SHOWING_MARKERS);
} else {
markers.destroyWaypointMarkers();
markers.destroyMarkers();
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_NOT_SHOWING_MARKERS);
}
}

View File

@ -0,0 +1,7 @@
package net.citizensnpcs.trait.waypoint;
import org.bukkit.Location;
public interface Locatable {
public Location getLocation();
}

View File

@ -16,7 +16,7 @@ import net.citizensnpcs.trait.waypoint.triggers.DelayTrigger;
import net.citizensnpcs.trait.waypoint.triggers.WaypointTrigger;
import net.citizensnpcs.trait.waypoint.triggers.WaypointTriggerRegistry;
public class Waypoint {
public class Waypoint implements Locatable {
@Persist(required = true)
private Location location;
@Persist
@ -65,6 +65,7 @@ public class Waypoint {
return true;
}
@Override
public Location getLocation() {
return location;
}

View File

@ -6,4 +6,4 @@ public abstract class WaypointEditor extends Editor {
public Waypoint getCurrentWaypoint() {
return null;
}
}
}