|
23 | 23 | import org.bukkit.Location; |
24 | 24 | import org.bukkit.Material; |
25 | 25 | import org.bukkit.block.Block; |
| 26 | +import org.bukkit.block.Chest; |
26 | 27 | import org.bukkit.entity.Player; |
| 28 | +import org.bukkit.entity.minecart.StorageMinecart; |
27 | 29 | import org.bukkit.event.EventHandler; |
28 | 30 | import org.bukkit.event.Listener; |
29 | 31 | import org.bukkit.event.world.LootGenerateEvent; |
@@ -55,45 +57,71 @@ public void onGenerateLoot(LootGenerateEvent event) { |
55 | 57 | } |
56 | 58 | Location lootLocation = event.getLootContext().getLocation(); |
57 | 59 | Inventory chestInv = inventoryHolder.getInventory(); |
58 | | - Block openedChest = null; |
59 | | - // look at the current list of opened chest and get the distance |
60 | | - // between the lootcontext location and chest location |
61 | | - // if the distance is less than 1, it's the same chest |
62 | | - for (Block chest : toolStats.playerInteract.openedChests.keySet()) { |
63 | | - Location chestLocation = chest.getLocation(); |
64 | | - double distance = lootLocation.distance(chestLocation); |
65 | | - if (distance <= 1.0) { |
66 | | - openedChest = chest; |
| 60 | + |
| 61 | + if (inventoryHolder instanceof Chest) { |
| 62 | + Block openedChest = null; |
| 63 | + // look at the current list of opened chest and get the distance |
| 64 | + // between the lootcontext location and chest location |
| 65 | + // if the distance is less than 1, it's the same chest |
| 66 | + for (Block chest : toolStats.playerInteract.openedChests.keySet()) { |
| 67 | + Location chestLocation = chest.getLocation(); |
| 68 | + double distance = lootLocation.distance(chestLocation); |
| 69 | + if (distance <= 1.0) { |
| 70 | + openedChest = chest; |
| 71 | + } |
| 72 | + } |
| 73 | + // ignore if the chest is not in the same location |
| 74 | + if (openedChest == null) { |
| 75 | + return; |
67 | 76 | } |
68 | | - } |
69 | | - // ignore if the chest is not in the same location |
70 | | - if (openedChest == null) { |
71 | | - return; |
72 | | - } |
73 | 77 |
|
74 | | - // run task later since if it runs on the same tick it breaks idk |
75 | | - Block finalOpenedChest = openedChest; |
76 | | - Bukkit.getScheduler().runTaskLater(toolStats, () -> { |
77 | | - Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest); |
78 | | - // do a classic for loop, so we keep track of chest index of item |
79 | | - for (int i = 0; i < chestInv.getContents().length; i++) { |
80 | | - ItemStack itemStack = chestInv.getItem(i); |
81 | | - // ignore air |
82 | | - if (itemStack == null || itemStack.getType() == Material.AIR) { |
83 | | - continue; |
| 78 | + // run task later since if it runs on the same tick it breaks idk |
| 79 | + Block finalOpenedChest = openedChest; |
| 80 | + Bukkit.getScheduler().runTaskLater(toolStats, () -> { |
| 81 | + Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest); |
| 82 | + // do a classic for loop, so we keep track of chest index of item |
| 83 | + for (int i = 0; i < chestInv.getContents().length; i++) { |
| 84 | + ItemStack itemStack = chestInv.getItem(i); |
| 85 | + // ignore air |
| 86 | + if (itemStack == null || itemStack.getType() == Material.AIR) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + String name = itemStack.getType().toString().toLowerCase(Locale.ROOT); |
| 90 | + for (String x : toolStats.allValidItems) { |
| 91 | + if (name.contains(x)) { |
| 92 | + ItemStack newItem = addLore(itemStack, player); |
| 93 | + if (newItem != null) { |
| 94 | + chestInv.setItem(i, newItem); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
84 | 98 | } |
85 | | - String name = itemStack.getType().toString().toLowerCase(Locale.ROOT); |
86 | | - for (String x : toolStats.allValidItems) { |
87 | | - if (name.contains(x)) { |
88 | | - ItemStack newItem = addLore(itemStack, player); |
89 | | - if (newItem != null) { |
90 | | - chestInv.setItem(i, newItem); |
| 99 | + |
| 100 | + }, 1); |
| 101 | + } |
| 102 | + if (inventoryHolder instanceof StorageMinecart) { |
| 103 | + StorageMinecart mineCart = (StorageMinecart) inventoryHolder; |
| 104 | + if (toolStats.playerInteract.openedMineCarts.containsKey(mineCart)) { |
| 105 | + Player player = toolStats.playerInteract.openedMineCarts.get(mineCart); |
| 106 | + // player clicked this minecart |
| 107 | + for (int i = 0; i < chestInv.getContents().length; i++) { |
| 108 | + ItemStack itemStack = chestInv.getItem(i); |
| 109 | + // ignore air |
| 110 | + if (itemStack == null || itemStack.getType() == Material.AIR) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + String name = itemStack.getType().toString().toLowerCase(Locale.ROOT); |
| 114 | + for (String x : toolStats.allValidItems) { |
| 115 | + if (name.contains(x)) { |
| 116 | + ItemStack newItem = addLore(itemStack, player); |
| 117 | + if (newItem != null) { |
| 118 | + chestInv.setItem(i, newItem); |
| 119 | + } |
91 | 120 | } |
92 | 121 | } |
93 | 122 | } |
94 | 123 | } |
95 | | - |
96 | | - }, 1); |
| 124 | + } |
97 | 125 | } |
98 | 126 |
|
99 | 127 | /** |
|
0 commit comments