From b343976dc8de35e287c4a87d544ea9fbf13c3dfd Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Tue, 18 Aug 2020 23:37:54 +0200 Subject: [PATCH] Clone class chest contents before modifying them. Certain class chest items are cloned before they are made unbreakable, but not all. This commit changes that, so all items in the input array are cloned prior to setting the unbreakable flag on them. The items that were previously cloned specifically no longer need to be, since they are included in the initial cloning. Fixes #637 --- changelog.md | 1 + .../com/garbagemule/MobArena/ArenaImpl.java | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index 9a4effe..a69e29a 100644 --- a/changelog.md +++ b/changelog.md @@ -24,6 +24,7 @@ These changes will (most likely) be included in the next version. - The `soft-restore` setting has been fixed for blocks broken by players. Note that the functionality is still unreliable for non-trivial blocks. - Config-file errors imposed by incorrect usage of `/ma setting` no longer cause "internal errors". Instead, the errors are properly communicated in the command output similar to how the `/ma reload` command works. - Guardians and elder guardians no longer instantly retarget players when they break line of sight. This should make their behavior work a bit closer to vanilla. +- Items in class chests are now cloned before they are made unbreakable and given to players. This fixes an issue where setting `unbreakable-weapons: false` had no effect on the items. Note that any affected items in existing class chests will need to be replaced. - The MagicSpells integration has been removed. This means that the extra `magicspells.yml` config-file (if it exists) no longer does anything and can be removed. ## [0.104.2] - 2020-01-03 diff --git a/src/main/java/com/garbagemule/MobArena/ArenaImpl.java b/src/main/java/com/garbagemule/MobArena/ArenaImpl.java index 8902c8f..84ceab7 100644 --- a/src/main/java/com/garbagemule/MobArena/ArenaImpl.java +++ b/src/main/java/com/garbagemule/MobArena/ArenaImpl.java @@ -1147,7 +1147,7 @@ public class ArenaImpl implements Arena } @Override - public void assignClassGiveInv(Player p, String className, ItemStack[] contents) { + public void assignClassGiveInv(Player p, String className, ItemStack[] source) { ArenaPlayer arenaPlayer = arenaPlayerMap.get(p); ArenaClass arenaClass = classes.get(className); @@ -1162,6 +1162,14 @@ public class ArenaImpl implements Arena PlayerInventory inv = p.getInventory(); + // Clone the source array to make sure we don't modify its contents + ItemStack[] contents = new ItemStack[source.length]; + for (int i = 0; i < source.length; i++) { + if (source[i] != null) { + contents[i] = source[i].clone(); + } + } + // Collect armor items, because setContents() now overwrites everyhing ItemStack helmet = null; ItemStack chestplate = null; @@ -1172,7 +1180,7 @@ public class ArenaImpl implements Arena // Check the very last slot to see if it'll work as a helmet int last = contents.length-1; if (contents[last] != null) { - helmet = contents[last].clone(); + helmet = contents[last]; if (arenaClass.hasUnbreakableArmor()) { makeUnbreakable(helmet); } @@ -1187,7 +1195,7 @@ public class ArenaImpl implements Arena String type = parts[parts.length - 1]; if (type.equals("HELMET")) continue; - ItemStack stack = contents[i].clone(); + ItemStack stack = contents[i]; if (arenaClass.hasUnbreakableArmor()) { makeUnbreakable(stack); } @@ -1202,9 +1210,8 @@ public class ArenaImpl implements Arena } // Equip the fifth last slot as the off-hand - ItemStack fifth = contents[contents.length - 5]; - if (fifth != null) { - offhand = fifth.clone(); + offhand = contents[contents.length - 5]; + if (offhand != null) { if (arenaClass.hasUnbreakableWeapons()) { makeUnbreakable(offhand); }