-
Notifications
You must be signed in to change notification settings - Fork 2
/
Observation.java
282 lines (231 loc) · 9.63 KB
/
Observation.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package edu.whimc.observations.models;
import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler;
import com.gmail.filoghost.holographicdisplays.api.line.TouchableLine;
import edu.whimc.observations.Observations;
import edu.whimc.observations.observetemplate.models.ObservationTemplate;
import edu.whimc.observations.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Observation {
private static final List<Observation> observations = new ArrayList<>();
private final Observations plugin;
private final Timestamp timestamp;
private final String playerName;
private final Location holoLoc;
private final Location viewLoc;
private final String observation;
private final boolean isTemporary;
private final ObservationTemplate template;
private int id;
private Hologram hologram;
private Timestamp expiration;
private Material hologramItem;
protected Observation(Observations plugin, int id, Timestamp timestamp, String playerName,
Location viewLoc, String observation, Timestamp expiration, ObservationTemplate template,
boolean isTemporary, boolean isNew) {
this.plugin = plugin;
this.timestamp = timestamp;
this.playerName = playerName;
this.holoLoc = viewLoc.clone().add(0, 3, 0).add(viewLoc.getDirection().multiply(2));
this.viewLoc = viewLoc;
this.observation = observation;
this.expiration = expiration;
this.template = template;
this.isTemporary = isTemporary;
if (this.template == null) {
this.hologramItem = Utils.matchMaterial(
plugin, plugin.getConfig().getString("template-gui.uncategorized.item"), Material.STONE);
} else {
this.hologramItem = this.template.getGuiItem();
}
if (!isNew) {
this.id = id;
createHologram();
return;
}
plugin.getQueryer().storeNewObservation(this, newId -> {
this.id = newId;
createHologram();
});
}
/**
* Creates a new observation and calls a new observe event with current time and configured expiration
* @param plugin Observations plugin being used
* @param player Player making the observation
* @param observation Text of the observation being made
* @param template The observation template being used
* @return New observation being created
*/
public static Observation createPlayerObservation(Observations plugin, Player player, String observation, ObservationTemplate template) {
int days = plugin.getConfig().getInt("expiration-days");
Timestamp expiration = Timestamp.from(Instant.now().plus(days, ChronoUnit.DAYS));
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Observation obs = new Observation(plugin, -1, timestamp, player.getName(), player.getLocation(), observation, expiration, template, false, true);
Utils.msg(player,
"&7Your observation has been placed:",
" &8\"&f&l" + observation + "&8\"");
// Call custom event
ObserveEvent observeEvent = new ObserveEvent(obs, player);
Bukkit.getScheduler().runTask(plugin, () -> Bukkit.getServer().getPluginManager().callEvent(observeEvent));
observations.add(obs);
return obs;
}
public static Observation loadObservation(Observations plugin, int id, Timestamp timestamp,
String playerName, Location viewLoc, String observation,
Timestamp expiration, ObservationTemplate template, boolean isTemporary) {
Observation obs = new Observation(plugin, id, timestamp, playerName, viewLoc, observation, expiration, template, isTemporary, false);
observations.add(obs);
return obs;
}
public static void startExpiredObservationScanningTask(Observations plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
Utils.debug("Scanning for expired observations...");
List<Observation> toRemove = observations.stream()
.filter(Observation::hasExpired)
.filter(v -> !v.isTemporary())
.collect(Collectors.toList());
int count = toRemove.size();
toRemove.forEach(observation -> observation.deleteObservation());
if (count > 0) {
plugin.getQueryer().makeExpiredObservationsInactive(dbCount -> {
Utils.debug("Removed " + count + " expired observation(s). (" + dbCount + " from the database)");
});
}
}, 20 * 60, 20 * 60);
}
public static List<Observation> getObservations() {
return observations;
}
public static Observation getObservation(int id) {
for (Observation obs : observations) {
if (obs.getId() == id) return obs;
}
return null;
}
public static List<String> getObservationsTabComplete(String hint) {
return observations.stream()
.filter(v -> Integer.toString(v.getId()).startsWith(hint))
.sorted(Comparator.comparing(Observation::getId))
.map(v -> Integer.toString(v.getId()))
.collect(Collectors.toList());
}
public static List<String> getPlayersTabComplete(String hint) {
Set<String> players = observations.stream()
.map(Observation::getPlayer)
.distinct()
.filter(v -> v.toLowerCase().startsWith(hint.toLowerCase()))
.collect(Collectors.toSet());
players.addAll(Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.toSet()));
return new ArrayList<>(players);
}
private void createHologram() {
Hologram holo = HologramsAPI.createHologram(this.plugin, this.holoLoc);
List<TouchableLine> lines = new ArrayList<>();
lines.add(holo.appendItemLine(new ItemStack(this.hologramItem)));
lines.add(holo.appendTextLine(Utils.color(this.observation)));
lines.add(holo.appendTextLine(ChatColor.GRAY + this.playerName + " - " + Utils.getDate(this.timestamp)));
if (this.expiration != null) {
lines.add(holo.appendTextLine(ChatColor.GRAY + "Expires " + Utils.getDate(this.expiration)));
}
if (this.isTemporary) {
lines.add(holo.appendTextLine(ChatColor.DARK_GRAY + "*temporary*"));
}
if (this.plugin.getConfig().getBoolean("enable-click-to-view")) {
ObservationClick clickListener = new ObservationClick(this.viewLoc);
for (TouchableLine touchable : lines) {
touchable.setTouchHandler(clickListener);
}
}
this.hologram = holo;
}
public void reRender() {
deleteHologramOnly();
createHologram();
}
public String getPlayer() {
return this.playerName;
}
public Location getHoloLocation() {
return this.holoLoc;
}
public Location getViewLocation() {
return this.viewLoc;
}
public String getObservation() {
return this.observation;
}
public int getId() {
return this.id;
}
public Hologram getHologram() {
return this.hologram;
}
public Timestamp getTimestamp() {
return this.timestamp;
}
public Timestamp getExpiration() {
return this.expiration;
}
public void setExpiration(Timestamp timestamp) {
this.expiration = timestamp;
}
public boolean hasExpired() {
return this.expiration != null && Instant.now().isAfter(this.expiration.toInstant());
}
public ObservationTemplate getTemplate() {
return this.template;
}
public boolean isTemporary() {
return this.isTemporary;
}
@Override
public String toString() {
String text = Utils.color("&f&l" + this.observation);
if (ChatColor.stripColor(text).length() > 20) {
text = Utils.coloredSubstring(text, 20) + "&7 . . .";
}
return "&9&l" + this.id + ".&r &8\"" + text + "&8\" &9> &7&o" + this.playerName + " " +
"&7(" + this.holoLoc.getWorld().getName() + ", " + this.holoLoc.getBlockX() + ", " +
this.holoLoc.getBlockY() + ", " + this.holoLoc.getBlockZ() + "&7)";
}
public void deleteAndSetInactive(Runnable callback) {
this.plugin.getQueryer().makeSingleObservationInactive(this.id, callback);
deleteObservation();
}
public void deleteObservation() {
deleteHologramOnly();
observations.remove(this);
}
public void deleteHologramOnly() {
if (this.hologram != null) {
this.hologram.delete();
this.hologram = null;
}
}
private class ObservationClick implements TouchHandler {
private final Location loc;
public ObservationClick(Location loc) {
this.loc = loc;
}
@Override
public void onTouch(Player player) {
player.teleport(this.loc);
}
}
}