11package edu .whimc .observations .models ;
22
3- import com .gmail .filoghost .holographicdisplays .api .Hologram ;
4- import com .gmail .filoghost .holographicdisplays .api .HologramsAPI ;
5- import com .gmail .filoghost .holographicdisplays .api .handler .TouchHandler ;
6- import com .gmail .filoghost .holographicdisplays .api .line .TouchableLine ;
73import edu .whimc .observations .Observations ;
84import edu .whimc .observations .observetemplate .models .ObservationTemplate ;
95import edu .whimc .observations .utils .Utils ;
10- import org .bukkit .Bukkit ;
11- import org .bukkit .ChatColor ;
12- import org .bukkit .Location ;
13- import org .bukkit .Material ;
14- import org .bukkit .entity .Player ;
15- import org .bukkit .inventory .ItemStack ;
16-
6+ import eu .decentsoftware .holograms .api .DHAPI ;
7+ import eu .decentsoftware .holograms .api .holograms .Hologram ;
8+ import eu .decentsoftware .holograms .event .HologramClickEvent ;
179import java .sql .Timestamp ;
1810import java .time .Instant ;
1911import java .time .temporal .ChronoUnit ;
2214import java .util .List ;
2315import java .util .Set ;
2416import java .util .stream .Collectors ;
17+ import org .bukkit .Bukkit ;
18+ import org .bukkit .ChatColor ;
19+ import org .bukkit .Location ;
20+ import org .bukkit .Material ;
21+ import org .bukkit .entity .Player ;
22+ import org .bukkit .event .EventHandler ;
23+ import org .bukkit .event .Listener ;
2524
26- public class Observation {
25+ public class Observation implements Listener {
2726
2827 private static final List <Observation > observations = new ArrayList <>();
2928
@@ -59,6 +58,11 @@ protected Observation(Observations plugin, int id, Timestamp timestamp, String p
5958 this .hologramItem = this .template .getGuiItem ();
6059 }
6160
61+ if (this .plugin .getConfig ().getBoolean ("enable-click-to-view" )) {
62+ // Register this class as a listener to handle hologram clicks
63+ Bukkit .getPluginManager ().registerEvents (this , plugin );
64+ }
65+
6266 if (!isNew ) {
6367 this .id = id ;
6468 createHologram ();
@@ -156,28 +160,18 @@ public static List<String> getPlayersTabComplete(String hint) {
156160 }
157161
158162 private void createHologram () {
159- Hologram holo = HologramsAPI .createHologram (this .plugin , this .holoLoc );
160-
161- List <TouchableLine > lines = new ArrayList <>();
163+ Hologram holo = DHAPI .createHologram (Integer .toString (this .id ), this .holoLoc );
162164
163- lines . add (holo . appendItemLine ( new ItemStack ( this .hologramItem )) );
164- lines . add (holo . appendTextLine ( Utils .color (this .observation ) ));
165- lines . add (holo . appendTextLine ( ChatColor .GRAY + this .playerName + " - " + Utils .getDate (this .timestamp ) ));
165+ DHAPI . addHologramLine (holo , this .hologramItem );
166+ DHAPI . addHologramLine (holo , Utils .color (this .observation ));
167+ DHAPI . addHologramLine (holo , ChatColor .GRAY + this .playerName + " - " + Utils .getDate (this .timestamp ));
166168
167169 if (this .expiration != null ) {
168- lines . add (holo . appendTextLine ( ChatColor .GRAY + "Expires " + Utils .getDate (this .expiration ) ));
170+ DHAPI . addHologramLine (holo , ChatColor .GRAY + "Expires " + Utils .getDate (this .expiration ));
169171 }
170172
171173 if (this .isTemporary ) {
172- lines .add (holo .appendTextLine (ChatColor .DARK_GRAY + "*temporary*" ));
173- }
174-
175- if (this .plugin .getConfig ().getBoolean ("enable-click-to-view" )) {
176- ObservationClick clickListener = new ObservationClick (this .viewLoc );
177- for (TouchableLine touchable : lines ) {
178- touchable .setTouchHandler (clickListener );
179- }
180-
174+ DHAPI .addHologramLine (holo , ChatColor .DARK_GRAY + "*temporary*" );
181175 }
182176
183177 this .hologram = holo ;
@@ -265,18 +259,13 @@ public void deleteHologramOnly() {
265259 }
266260 }
267261
268- private class ObservationClick implements TouchHandler {
269-
270- private final Location loc ;
271-
272- public ObservationClick (Location loc ) {
273- this .loc = loc ;
262+ @ EventHandler
263+ public void onClick (HologramClickEvent event ) {
264+ if (event .getHologram () != this .hologram ) {
265+ return ;
274266 }
275267
276- @ Override
277- public void onTouch (Player player ) {
278- player .teleport (this .loc );
279- }
268+ Bukkit .getScheduler ().runTask (this .plugin , () -> event .getPlayer ().teleport (this .viewLoc ));
280269 }
281270
282- }
271+ }
0 commit comments