handle loot generation better

edit the loot itself, not the contents of chest

also handle breaking chest instead of opening it
This commit is contained in:
hyperdefined
2023-09-30 20:43:54 -04:00
parent 664eddeab0
commit b0733935ae
2 changed files with 39 additions and 38 deletions

View File

@@ -20,6 +20,7 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats; import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker; import lol.hyper.toolstats.tools.ItemChecker;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable; import org.bukkit.block.data.Ageable;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -32,6 +33,7 @@ import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@@ -56,6 +58,18 @@ public class BlocksMined implements Listener {
PlayerInventory inventory = player.getInventory(); PlayerInventory inventory = player.getInventory();
ItemStack heldItem = inventory.getItemInMainHand(); ItemStack heldItem = inventory.getItemInMainHand();
Block block = event.getBlock(); Block block = event.getBlock();
if (block.getType() == Material.CHEST) {
toolStats.playerInteract.openedChests.put(block, player);
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
toolStats.playerInteract.openedChests.remove(block);
}
};
toolStats.scheduleGlobal(runnable, 20);
}
// only check certain items // only check certain items
if (!ItemChecker.isMineTool(heldItem.getType())) { if (!ItemChecker.isMineTool(heldItem.getType())) {
return; return;

View File

@@ -56,7 +56,6 @@ public class GenerateLoot implements Listener {
return; return;
} }
Location lootLocation = event.getLootContext().getLocation(); Location lootLocation = event.getLootContext().getLocation();
Inventory chestInv = inventoryHolder.getInventory();
if (inventoryHolder instanceof Chest) { if (inventoryHolder instanceof Chest) {
Block openedChest = null; Block openedChest = null;
@@ -77,48 +76,14 @@ public class GenerateLoot implements Listener {
return; return;
} }
// run task later since if it runs on the same tick it breaks Player player = toolStats.playerInteract.openedChests.get(openedChest);
Block finalOpenedChest = openedChest; setLoot(event.getLoot(), player);
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
// keep track of chest index of item
for (int i = 0; i < chestInv.getContents().length; i++) {
ItemStack itemStack = chestInv.getItem(i);
// ignore air
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
if (ItemChecker.isValidItem(itemStack.getType())) {
ItemStack newItem = addLore(itemStack, player);
if (newItem != null) {
chestInv.setItem(i, newItem);
}
}
}
}
};
toolStats.scheduleRegion(runnable, lootLocation.getWorld(), lootLocation.getChunk(), 1);
} }
if (inventoryHolder instanceof StorageMinecart) { if (inventoryHolder instanceof StorageMinecart) {
StorageMinecart mineCart = (StorageMinecart) inventoryHolder; StorageMinecart mineCart = (StorageMinecart) inventoryHolder;
if (toolStats.playerInteract.openedMineCarts.containsKey(mineCart)) { if (toolStats.playerInteract.openedMineCarts.containsKey(mineCart)) {
Player player = toolStats.playerInteract.openedMineCarts.get(mineCart); Player player = toolStats.playerInteract.openedMineCarts.get(mineCart);
// player clicked this minecart setLoot(event.getLoot(), player);
for (int i = 0; i < chestInv.getContents().length; i++) {
ItemStack itemStack = chestInv.getItem(i);
// ignore air
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
if (ItemChecker.isValidItem(itemStack.getType())) {
ItemStack newItem = addLore(itemStack, player);
if (newItem != null) {
chestInv.setItem(i, newItem);
}
}
}
} }
} }
} }
@@ -163,4 +128,26 @@ public class GenerateLoot implements Listener {
newItem.setItemMeta(meta); newItem.setItemMeta(meta);
return newItem; return newItem;
} }
/**
* Add tags to the generated loot.
*
* @param loot The loot from the event.
* @param player The player triggering the event.
*/
private void setLoot(List<ItemStack> loot, Player player) {
for (int i = 0; i < loot.size(); i++) {
ItemStack itemStack = loot.get(i);
// ignore air
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
if (ItemChecker.isValidItem(itemStack.getType())) {
ItemStack newItem = addLore(itemStack, player);
if (newItem != null) {
loot.set(i, newItem);
}
}
}
}
} }