mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2026-01-26 23:46:00 +00:00
properly handle breaking chest gen
This commit is contained in:
@@ -18,23 +18,28 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.Ageable;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockDropItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class BlockBreak implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
public List<Block> brokenContainers = new ArrayList<>();
|
||||
|
||||
public BlockBreak(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
@@ -56,6 +61,11 @@ public class BlockBreak implements Listener {
|
||||
ItemStack heldItem = inventory.getItemInMainHand();
|
||||
Block block = event.getBlock();
|
||||
|
||||
if (block.getType() == Material.CHEST || block.getType() == Material.BARREL) {
|
||||
brokenContainers.add(block);
|
||||
Bukkit.getGlobalRegionScheduler().runDelayed(toolStats, scheduledTask -> brokenContainers.remove(block), 20);
|
||||
}
|
||||
|
||||
// only check certain items
|
||||
if (!toolStats.itemChecker.isMineTool(heldItem.getType())) {
|
||||
return;
|
||||
@@ -92,4 +102,41 @@ public class BlockBreak implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onBreak(BlockDropItemEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (!toolStats.configTools.checkWorld(player.getWorld().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
Location eventLocation = event.getBlock().getLocation();
|
||||
Chunk eventChunk = eventLocation.getChunk();
|
||||
Bukkit.getRegionScheduler().runDelayed(toolStats, eventLocation.getWorld(), eventChunk.getX(), eventChunk.getZ(), scheduledTask -> {
|
||||
boolean validLootDrops = false;
|
||||
for (Location droppedLootLocation : toolStats.generateLoot.droppedLootLocations) {
|
||||
if (eventLocation.getWorld() == droppedLootLocation.getWorld()) {
|
||||
double distance = droppedLootLocation.distance(eventLocation);
|
||||
if (distance <= 1.0) {
|
||||
validLootDrops = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (validLootDrops) {
|
||||
toolStats.generateLoot.droppedLootLocations.remove(eventLocation);
|
||||
for (Item droppedItemEntity : event.getItems()) {
|
||||
ItemStack droppedItem = droppedItemEntity.getItemStack();
|
||||
if (!toolStats.itemChecker.isValidItem(droppedItem.getType())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemStack newItem = toolStats.inventoryClose.addLootedOrigin(droppedItem, player);
|
||||
if (newItem != null) {
|
||||
droppedItemEntity.setItemStack(newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,13 +31,16 @@ import org.bukkit.event.world.LootGenerateEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GenerateLoot implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
public Map<Inventory, Location> generatedInventory = new HashMap<>();
|
||||
public List<Location> droppedLootLocations = new ArrayList<>();
|
||||
|
||||
public GenerateLoot(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
@@ -70,6 +73,16 @@ public class GenerateLoot implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Block brokenChest : toolStats.blockBreak.brokenContainers) {
|
||||
Location brokenChestLocation = brokenChest.getLocation();
|
||||
if (brokenChestLocation.getWorld() == lootLocation.getWorld()) {
|
||||
double distance = lootLocation.distance(brokenChestLocation);
|
||||
if (distance <= 1.0) {
|
||||
droppedLootLocations.add(brokenChestLocation);
|
||||
Bukkit.getGlobalRegionScheduler().runDelayed(toolStats, scheduledTask2 -> droppedLootLocations.remove(brokenChestLocation), 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
// ignore if the chest is not in the same location
|
||||
if (openedChest != null) {
|
||||
generatedInventory.put(inventoryHolder.getInventory(), chestLocation);
|
||||
@@ -81,6 +94,6 @@ public class GenerateLoot implements Listener {
|
||||
generatedInventory.put(mineCartInventory, mineCart.getLocation());
|
||||
}
|
||||
}
|
||||
}, 5);
|
||||
}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ public class InventoryClose implements Listener {
|
||||
* @param owner The player that found the item.
|
||||
* @return The item with the lore.
|
||||
*/
|
||||
private ItemStack addLootedOrigin(ItemStack itemStack, Player owner) {
|
||||
public ItemStack addLootedOrigin(ItemStack itemStack, Player owner) {
|
||||
ItemStack newItem = itemStack.clone();
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
|
||||
Reference in New Issue
Block a user