-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Waypoint.java
142 lines (124 loc) · 4.37 KB
/
Waypoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package net.citizensnpcs.trait.waypoint;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.trait.waypoint.triggers.DelayTrigger;
import net.citizensnpcs.trait.waypoint.triggers.WaypointTrigger;
import net.citizensnpcs.trait.waypoint.triggers.WaypointTriggerRegistry;
import net.citizensnpcs.util.Messages;
/**
* Represents a {@link Location} with a number of {@link WaypointTrigger}s that activate on reaching the location.
*/
public class Waypoint {
@Persist(required = true)
private Location location;
@Persist
private List<WaypointTrigger> triggers;
/**
* For persistence - avoid using otherwise.
*/
public Waypoint() {
}
public Waypoint(Location at) {
location = at.clone();
}
public void addTrigger(WaypointTrigger trigger) {
if (triggers == null) {
triggers = Lists.newArrayList();
}
triggers.add(trigger);
}
public void describeTriggers(CommandSender sender) {
String base = " ";
if (triggers == null)
return;
for (int i = 0; i < triggers.size(); i++) {
base += "\n - " + triggers.get(i).description()
+ " [<hover:show_text:Remove trigger><click:run_command:/npc path remove_trigger " + i
+ "><u><red>-</click></hover>]";
}
Messaging.sendTr(sender, Messages.WAYPOINT_TRIGGER_LIST, base);
}
/**
* Returns the distance in blocks to another waypoint.
*/
public double distance(Waypoint dest) {
return location.distance(dest.location);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Waypoint other = (Waypoint) obj;
if (location == null) {
if (other.location != null) {
return false;
}
} else if (!location.equals(other.location)) {
return false;
}
if (triggers == null) {
if (other.triggers != null) {
return false;
}
} else if (!triggers.equals(other.triggers)) {
return false;
}
return true;
}
public Location getLocation() {
return location.clone();
}
public List<WaypointTrigger> getTriggers() {
return triggers == null ? Collections.<WaypointTrigger> emptyList() : triggers;
}
@Override
public int hashCode() {
final int prime = 31;
int result = prime + ((location == null) ? 0 : location.hashCode());
return prime * result + ((triggers == null) ? 0 : triggers.hashCode());
}
/**
* Runs waypoint triggers for the given NPC.
*/
public void onReach(NPC npc) {
if (triggers == null)
return;
runTriggers(npc, 0);
}
private void runTriggers(final NPC npc, int start) {
List<WaypointTrigger> triggers = Lists.newArrayList(this.triggers);
for (int i = start; i < triggers.size(); i++) {
WaypointTrigger trigger = triggers.get(i);
trigger.onWaypointReached(npc, location.clone());
if (!(trigger instanceof DelayTrigger))
continue;
int delay = ((DelayTrigger) trigger).getDelay();
if (delay <= 0)
continue;
final int newStart = i + 1;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> runTriggers(npc, newStart),
delay);
break;
}
}
@Override
public String toString() {
return "Waypoint [" + location + (triggers == null ? "]" : ", " + triggers.size() + " triggers]");
}
static {
PersistenceLoader.registerPersistDelegate(WaypointTrigger.class, WaypointTriggerRegistry.class);
}
}