11package org .plugins .rpghorses .managers .gui ;
22
33import org .bukkit .Bukkit ;
4+ import org .bukkit .Effect ;
45import org .bukkit .configuration .file .FileConfiguration ;
56import org .bukkit .entity .Player ;
67import org .bukkit .inventory .Inventory ;
1314import org .plugins .rpghorses .guis .TrailGUIItem ;
1415import org .plugins .rpghorses .guis .instances .HorseGUI ;
1516import org .plugins .rpghorses .guis .instances .TrailsGUI ;
17+ import org .plugins .rpghorses .horseinfo .LegacyHorseInfo ;
1618import org .plugins .rpghorses .horses .RPGHorse ;
1719import org .plugins .rpghorses .managers .HorseOwnerManager ;
1820import org .plugins .rpghorses .managers .ParticleManager ;
1921import org .plugins .rpghorses .players .HorseOwner ;
20- import org .plugins .rpghorses .utils .RPGMessagingUtil ;
2122import org .plugins .rpghorses .utils .ItemUtil ;
23+ import org .plugins .rpghorses .utils .RPGMessagingUtil ;
24+ import org .plugins .rpghorses .version .Version ;
2225
23- import java .util .ArrayList ;
24- import java .util .HashSet ;
25- import java .util .List ;
26+ import java .util .*;
2627
2728public class TrailGUIManager {
28-
29+
2930 private final RPGHorsesMain plugin ;
3031 private final ParticleManager particleManager ;
3132 private final HorseOwnerManager horseOwnerManager ;
32-
33+
3334 private ItemStack unknownTrailItem , trailItem , fillItem ;
3435 private GUIItem clearTrailItem , backItem ;
35- private List < TrailGUIItem > validTrails = new ArrayList <>();
36+ private final LinkedHashMap < String , TrailGUIItem > validTrails = new LinkedHashMap <>();
3637 private int rows = 3 ;
37-
38+
3839 public TrailGUIManager (RPGHorsesMain plugin , ParticleManager particleManager , HorseOwnerManager horseOwnerManager ) {
3940 this .plugin = plugin ;
4041 this .particleManager = particleManager ;
4142 this .horseOwnerManager = horseOwnerManager ;
42-
43+
4344 reload ();
4445 }
45-
46+
4647 public void reload () {
4748 validTrails .clear ();
48-
49+
4950 FileConfiguration config = plugin .getConfig ();
50-
51+
5152 String path = "trail-gui-options." ;
52-
53+
5354 unknownTrailItem = ItemUtil .getItemStack (config , path + "unknown-trail" );
5455 trailItem = ItemUtil .getItemStack (config , path + "trail-item" );
5556 clearTrailItem = new GUIItem (ItemUtil .getItemStack (config , path + "clear-trail-item" ), ItemPurpose .CLEAR_TRAIL , true , ItemUtil .getSlot (config , path + "clear-trail-item" ));
5657 fillItem = ItemUtil .getItemStack (config , path + "fill-item" );
5758 backItem = new GUIItem (ItemUtil .getItemStack (config , path + "back-item" ), ItemPurpose .BACK , true , ItemUtil .getSlot (config , path + "back-item" ));
58-
59+
5960 for (String trailName : config .getConfigurationSection (path + "trails" ).getKeys (false )) {
6061 if (particleManager .isValidParticle (trailName )) {
6162 ItemStack item = trailItem .clone ();
6263 ItemMeta itemMeta = trailItem .getItemMeta ();
63- ItemMeta newMeta = ItemUtil .applyCustomHead (itemMeta .clone (), config .getString (path + "trails." + trailName ));
64+ ItemMeta newMeta ;
65+
66+ if (Version .getVersion ().getWeight () >= Version .v1_20 .getWeight ()) {
67+ newMeta = ItemUtil .applyCustomHead (itemMeta .clone (), config .getString (path + "trails." + trailName + ".textures-url" , "" ), config .getString (path + "trails." + trailName + ".skin-value" , "" ));
68+ } else {
69+ newMeta = ItemUtil .applyCustomHead (itemMeta .clone (), config .getString (path + "trails." + trailName , "" ));
70+ }
71+
6472 if (newMeta != null ) {
6573 item .setItemMeta (newMeta );
6674 }
67- validTrails .add ( replacePlaceholders (new TrailGUIItem (item , ItemPurpose .TRAIL , true , -1 , trailName )));
75+ validTrails .put ( trailName , replacePlaceholders (new TrailGUIItem (item , ItemPurpose .TRAIL , true , -1 , trailName )));
6876 }
6977 }
70-
78+
7179 int totalTrails = validTrails .size (), slot = 10 , trailsLeft = totalTrails , row = 1 ;
7280 int skipSlot = -1 ;
7381 if (trailsLeft < 7 ) {
@@ -78,14 +86,14 @@ public void reload() {
7886 slot += ((7 - trailsLeft ) / 2 );
7987 }
8088 }
81-
82- for (TrailGUIItem trailGUIItem : validTrails ) {
89+
90+ for (TrailGUIItem trailGUIItem : validTrails . values () ) {
8391 if (slot == skipSlot ) {
8492 slot ++;
8593 }
86-
94+
8795 trailGUIItem .setSlot (slot );
88-
96+
8997 trailsLeft --;
9098 if ((totalTrails - trailsLeft ) % 7 == 0 ) {
9199 row ++;
@@ -102,29 +110,30 @@ public void reload() {
102110 slot ++;
103111 }
104112 }
105-
113+
106114 rows = row + 3 ;
107- backItem .setSlot (((rows - 1 ) * 9 ) + 4 );
108-
115+ backItem .setSlot (((rows - 1 ) * 9 ) + 3 );
116+ clearTrailItem .setSlot (((rows - 1 ) * 9 ) + 5 );
117+
109118 for (HorseOwner horseOwner : horseOwnerManager .getHorseOwners ().values ()) {
110119 if (horseOwner .getGUILocation () == GUILocation .TRAILS_GUI ) {
111120 horseOwner .openTrailsGUI (setupTrailsGUI (horseOwner .getHorseGUI ()));
112121 }
113122 }
114-
123+
115124 }
116-
125+
117126 public TrailsGUI setupTrailsGUI (HorseGUI horseGUI ) {
118127 RPGHorse rpgHorse = horseGUI .getRpgHorse ();
119128 Player p = rpgHorse .getHorseOwner ().getPlayer ();
120-
129+
121130 HashSet <TrailGUIItem > trails = new HashSet <>(), unknownTrails = new HashSet <>();
122-
131+
123132 Inventory inv = Bukkit .createInventory (p , rows * 9 , RPGMessagingUtil .format (plugin .getConfig ().getString ("trail-gui-options.title" )));
124133
125134 boolean perHorsePerms = plugin .getConfig ().getBoolean ("trails-options.per-horse-permissions" , false );
126135 boolean hasStarPerm = perHorsePerms ? p .hasPermission ("rpghorses.trail." + horseGUI .getRpgHorse ().getIndex () + ".*" ) : p .hasPermission ("rpghorses.trail.*" );
127- for (TrailGUIItem trailGUIItem : validTrails ) {
136+ for (TrailGUIItem trailGUIItem : validTrails . values () ) {
128137 ItemStack item ;
129138 if (hasStarPerm || (perHorsePerms && p .hasPermission ("rpghorses.trail." + horseGUI .getRpgHorse ().getIndex () + "." + trailGUIItem .getTrailName ().toLowerCase ())) || (!perHorsePerms && p .hasPermission ("rpghorses.trail." + trailGUIItem .getTrailName ().toLowerCase ()))) {
130139 trails .add (trailGUIItem );
@@ -133,53 +142,67 @@ public TrailsGUI setupTrailsGUI(HorseGUI horseGUI) {
133142 unknownTrails .add (trailGUIItem );
134143 item = unknownTrailItem ;
135144 }
136- inv .setItem (trailGUIItem .getSlot (), item );
145+ inv .setItem (trailGUIItem .getSlot (), item . clone () );
137146 }
138-
147+
139148 inv .setItem (backItem .getSlot (), backItem .getItem ());
140149 inv .setItem (clearTrailItem .getSlot (), clearTrailItem .getItem ());
141-
150+
142151 for (int i = 0 ; i < inv .getSize (); i ++) {
143152 if (inv .getItem (i ) == null ) {
144153 inv .setItem (i , fillItem );
145154 }
146155 }
147-
148- return new TrailsGUI (rpgHorse , inv , trails , unknownTrails );
156+
157+ TrailGUIItem currentTrail = null ;
158+
159+ if (RPGHorsesMain .getVersion ().getWeight () < 9 ) {
160+ Effect effect = ((LegacyHorseInfo ) rpgHorse .getHorseInfo ()).getEffect ();
161+ if (effect != null ) {
162+ currentTrail = validTrails .get (effect .name ());
163+ }
164+ } else {
165+ String particleName = rpgHorse .getParticle () == null ? null : rpgHorse .getParticle ().name ();
166+ if (particleName != null ) {
167+ currentTrail = validTrails .get (particleName );
168+ }
169+ }
170+
171+ return new TrailsGUI (rpgHorse , inv , trails , unknownTrails , currentTrail );
149172 }
150-
173+
151174 public TrailGUIItem replacePlaceholders (TrailGUIItem trailGUIItem ) {
152175 String name = "" ;
153176 for (String word : trailGUIItem .getTrailName ().toLowerCase ().replace ("_" , " " ).split ("\\ s" )) {
154177 name += word .substring (0 , 1 ).toUpperCase () + word .substring (1 ) + " " ;
155178 }
156179 name = name .trim ();
157-
180+
158181 ItemMeta itemMeta = trailGUIItem .getItem ().getItemMeta ();
159-
182+
160183 if (itemMeta .hasDisplayName ()) {
161184 itemMeta .setDisplayName (itemMeta .getDisplayName ().replace ("{TRAIL}" , name ));
162185 }
163-
186+
164187 if (itemMeta .hasLore ()) {
165188 List <String > lore = new ArrayList <>();
166189 for (String line : itemMeta .getLore ()) {
167190 lore .add (line .replace ("{TRAIL}" , name ));
168191 }
169192 itemMeta .setLore (lore );
170193 }
171-
194+
172195 trailGUIItem .getItem ().setItemMeta (itemMeta );
173196 return trailGUIItem ;
174197 }
175-
198+
176199 public ItemPurpose getItemPurpose (int slot , TrailsGUI trailsGUI ) {
177200 if (slot == backItem .getSlot ()) {
178201 return ItemPurpose .BACK ;
179202 } else if (slot == clearTrailItem .getSlot ()) {
180203 return ItemPurpose .CLEAR_TRAIL ;
181204 }
182-
205+
183206 return trailsGUI .getItemPurpose (slot );
184207 }
185208}
0 commit comments