55import com .hypixel .hytale .server .core .entity .ItemUtils ;
66import com .hypixel .hytale .server .core .entity .entities .Player ;
77import com .hypixel .hytale .server .core .event .events .entity .LivingEntityInventoryChangeEvent ;
8- import com .hypixel .hytale .server .core .inventory .Inventory ;
98import com .hypixel .hytale .server .core .inventory .ItemStack ;
109import com .hypixel .hytale .server .core .inventory .container .ItemContainer ;
1110import com .hypixel .hytale .server .core .inventory .transaction .ItemStackTransaction ;
@@ -51,23 +50,24 @@ public void shutdown() {
5150 }
5251
5352 /**
54- * Validates the armor equipped by the player to ensure they meet the required level criteria.
55- * Removes any armor items that the player does not meet the level requirement for and
56- * either gives them back to the player or drops them in the game world.
57- * Temporarily ignores related armor events to avoid cyclic processes during this validation.
53+ * Validates the armor equipped by the player to ensure they meet the required level criteria. Removes any armor
54+ * items that the player does not meet the level requirement for and either gives them back to the player or drops
55+ * them in the game world. Temporarily ignores related armor events to avoid cyclic processes during this
56+ * validation.
5857 *
5958 * @param player the player whose equipped armor is to be validated
6059 */
6160 public void validateArmorOnReady (@ Nonnull Player player ) {
6261 ignoreArmorEvents .add (player .getUuid ());
6362 HytaleServer .SCHEDULED_EXECUTOR .schedule (
64- () -> ignoreArmorEvents .remove (player .getUuid ()),
65- 500L ,
66- TimeUnit .MILLISECONDS
63+ () -> ignoreArmorEvents .remove (player .getUuid ()),
64+ 500L ,
65+ TimeUnit .MILLISECONDS
6766 );
6867 var inventory = player .getInventory ();
6968 var armor = inventory .getArmor ();
70- if (armor == null ) return ;
69+ if (armor == null )
70+ return ;
7171
7272 var playerLevel = LevelingCore .getLevelService ().getLevel (player .getUuid ());
7373
@@ -76,18 +76,21 @@ public void validateArmorOnReady(@Nonnull Player player) {
7676 var capacity = armor .getCapacity ();
7777 for (short slot = 0 ; slot < capacity ; slot ++) {
7878 var stack = armor .getItemStack (slot );
79- if (stack == null || ItemStack .isEmpty (stack )) continue ;
79+ if (stack == null || ItemStack .isEmpty (stack ))
80+ continue ;
8081
8182 var itemId = stack .getItemId ();
8283 var req = LevelingCore .itemLevelMapping .get (itemId );
83- if (req == null ) continue ;
84- if (playerLevel >= req ) continue ;
84+ if (req == null )
85+ continue ;
86+ if (playerLevel >= req )
87+ continue ;
8588
8689 player .sendMessage (
87- CommandLang .LEVEL_REQUIRED
88- .param ("requiredlevel" , req )
89- .param ("itemid" , itemId )
90- .param ("level" , playerLevel )
90+ CommandLang .LEVEL_REQUIRED
91+ .param ("requiredlevel" , req )
92+ .param ("itemid" , itemId )
93+ .param ("level" , playerLevel )
9194 );
9295
9396 armor .setItemStackForSlot (slot , null , true );
@@ -99,13 +102,13 @@ public void validateArmorOnReady(@Nonnull Player player) {
99102 }
100103
101104 /**
102- * Handles inventory change events specifically related to armor slots for players.
103- * This method ensures that any unauthorized changes to the armor slots, such as equipping items
104- * without meeting level restrictions, are rolled back and appropriate actions are taken.
105- * The method temporarily ignores specific players or states to avoid cyclic event handling.
105+ * Handles inventory change events specifically related to armor slots for players. This method ensures that any
106+ * unauthorized changes to the armor slots, such as equipping items without meeting level restrictions, are rolled
107+ * back and appropriate actions are taken. The method temporarily ignores specific players or states to avoid cyclic
108+ * event handling.
106109 *
107- * @param event the inventory change event that contains context about the entity, transaction,
108- * and the affected inventory container
110+ * @param event the inventory change event that contains context about the entity, transaction, and the affected
111+ * inventory container
109112 */
110113 private void onInventoryChange (@ Nonnull LivingEntityInventoryChangeEvent event ) {
111114 if (!(event .getEntity () instanceof Player player )) {
@@ -145,15 +148,16 @@ private void onInventoryChange(@Nonnull LivingEntityInventoryChangeEvent event)
145148 }
146149
147150 /**
148- * Rolls back the player's armor transaction in the event of a failed or invalid transaction, ensuring
149- * that unauthorized or restricted items are removed and returned to the player or dropped into the world.
150- * This method is recursively called for nested transactions and handles various transaction types, including
151- * move transactions, list transactions, item stack transactions, and slot transactions.
151+ * Rolls back the player's armor transaction in the event of a failed or invalid transaction, ensuring that
152+ * unauthorized or restricted items are removed and returned to the player or dropped into the world. This method is
153+ * recursively called for nested transactions and handles various transaction types, including move transactions,
154+ * list transactions, item stack transactions, and slot transactions.
152155 *
153- * @param player the player whose armor transaction is being rolled back; must not be null
156+ * @param player the player whose armor transaction is being rolled back; must not be null
154157 * @param armorContainer the container representing the player's current armor; must not be null
155- * @param transaction the transaction object representing the changes to the armor; may be null
156- * @param refundedKeys a set of keys used to track already refunded or processed slots and avoid duplicates; must not be null
158+ * @param transaction the transaction object representing the changes to the armor; may be null
159+ * @param refundedKeys a set of keys used to track already refunded or processed slots and avoid duplicates; must
160+ * not be null
157161 */
158162 private void rollbackArmorTransaction (
159163 @ Nonnull Player player ,
@@ -183,24 +187,28 @@ private void rollbackArmorTransaction(
183187 }
184188 case SlotTransaction slotTransaction -> {
185189 var before = slotTransaction .getSlotBefore ();
186- var after = slotTransaction .getSlotAfter ();
190+ var after = slotTransaction .getSlotAfter ();
187191
188- if (after == null || ItemStack .isEmpty (after )) return ;
192+ if (after == null || ItemStack .isEmpty (after ))
193+ return ;
189194
190- if (sameStack (before , after )) return ;
195+ if (sameStack (before , after ))
196+ return ;
191197
192198 var itemId = after .getItemId ();
193199 var levelRestriction = LevelingCore .itemLevelMapping .get (itemId );
194- if (levelRestriction == null ) return ;
200+ if (levelRestriction == null )
201+ return ;
195202
196203 var playerLevel = LevelingCore .getLevelService ().getLevel (player .getUuid ());
197- if (playerLevel >= levelRestriction ) return ;
204+ if (playerLevel >= levelRestriction )
205+ return ;
198206
199207 player .sendMessage (
200- CommandLang .LEVEL_REQUIRED
201- .param ("requiredlevel" , levelRestriction )
202- .param ("itemid" , itemId )
203- .param ("level" , playerLevel )
208+ CommandLang .LEVEL_REQUIRED
209+ .param ("requiredlevel" , levelRestriction )
210+ .param ("itemid" , itemId )
211+ .param ("level" , playerLevel )
204212 );
205213
206214 var swapping = (before != null && !ItemStack .isEmpty (before ));
@@ -217,35 +225,40 @@ private void rollbackArmorTransaction(
217225 }
218226 }
219227 }
220- default -> {
221- }
228+ default -> {}
222229 }
223230 }
224231
225232 /**
226- * Compares two {@link ItemStack} objects and determines if they are considered equivalent.
227- * Two stacks are considered equivalent if they have the same item ID, quantity, and metadata.
228- * Empty stacks are treated specially and considered equivalent if both are empty.
233+ * Compares two {@link ItemStack} objects and determines if they are considered equivalent. Two stacks are
234+ * considered equivalent if they have the same item ID, quantity, and metadata. Empty stacks are treated specially
235+ * and considered equivalent if both are empty.
229236 *
230237 * @param a the first {@link ItemStack} to compare, or {@code null}
231238 * @param b the second {@link ItemStack} to compare, or {@code null}
232239 * @return {@code true} if the two stacks are considered equivalent; {@code false} otherwise
233240 */
234241 private static boolean sameStack (@ Nullable ItemStack a , @ Nullable ItemStack b ) {
235- if (a == b ) return true ;
236- if (a == null || b == null ) return false ;
237- if (ItemStack .isEmpty (a ) && ItemStack .isEmpty (b )) return true ;
238- if (ItemStack .isEmpty (a ) || ItemStack .isEmpty (b )) return false ;
239-
240- if (!Objects .equals (a .getItemId (), b .getItemId ())) return false ;
241- if (a .getQuantity () != b .getQuantity ()) return false ;
242+ if (a == b )
243+ return true ;
244+ if (a == null || b == null )
245+ return false ;
246+ if (ItemStack .isEmpty (a ) && ItemStack .isEmpty (b ))
247+ return true ;
248+ if (ItemStack .isEmpty (a ) || ItemStack .isEmpty (b ))
249+ return false ;
250+
251+ if (!Objects .equals (a .getItemId (), b .getItemId ()))
252+ return false ;
253+ if (a .getQuantity () != b .getQuantity ())
254+ return false ;
242255 return Objects .equals (a .getMetadata (), b .getMetadata ());
243256 }
244257
245258 /**
246- * Creates a new {@link ItemStack} with a quantity of 1, based on the given {@code stack}.
247- * This method retains the item ID and metadata of the provided stack but modifies the quantity
248- * to ensure that only a single item is represented.
259+ * Creates a new {@link ItemStack} with a quantity of 1, based on the given {@code stack}. This method retains the
260+ * item ID and metadata of the provided stack but modifies the quantity to ensure that only a single item is
261+ * represented.
249262 *
250263 * @param stack the {@link ItemStack} to be used as the base for creating the new stack; must not be null
251264 * @return a new {@link ItemStack} with the same item ID and metadata as the input stack, but with a quantity of 1
@@ -255,16 +268,17 @@ private static ItemStack oneOf(@Nonnull ItemStack stack) {
255268 }
256269
257270 /**
258- * Attempts to add the specified item stack to the player's inventory. If the item stack
259- * cannot be fully added to the inventory (e.g., due to lack of space), the remaining
260- * items are dropped in the game world at the player's location.
271+ * Attempts to add the specified item stack to the player's inventory. If the item stack cannot be fully added to
272+ * the inventory (e.g., due to lack of space), the remaining items are dropped in the game world at the player's
273+ * location.
261274 *
262- * @param player the player to whom the item stack will be given or near whom the items
263- * will be dropped if there is not enough inventory space; must not be null
275+ * @param player the player to whom the item stack will be given or near whom the items will be dropped if there is
276+ * not enough inventory space; must not be null
264277 * @param stack the item stack to be given to the player or partially dropped; must not be null
265278 */
266279 private static void giveOrDrop (@ Nonnull Player player , @ Nonnull ItemStack stack ) {
267- if (ItemStack .isEmpty (stack )) return ;
280+ if (ItemStack .isEmpty (stack ))
281+ return ;
268282
269283 var inv = player .getInventory ().getCombinedHotbarFirst ();
270284
0 commit comments