mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-09-28 15:17:43 +02:00
Do some cleanup of WaypointMarkers to be more generic
This commit is contained in:
parent
73ff01c774
commit
9c3fb30623
@ -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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package net.citizensnpcs.trait.waypoint;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
public interface Locatable {
|
||||
public Location getLocation();
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user