From 17fb03f0730767f39391d44f5bb813e911b3a37d Mon Sep 17 00:00:00 2001 From: ceze88 Date: Thu, 29 Dec 2022 11:50:51 +0100 Subject: [PATCH] Fix pre stacking items --- .../core/lootables/loot/DropUtils.java | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/lootables/loot/DropUtils.java b/Core/src/main/java/com/songoda/core/lootables/loot/DropUtils.java index 4e8e7b72..3f26cd21 100644 --- a/Core/src/main/java/com/songoda/core/lootables/loot/DropUtils.java +++ b/Core/src/main/java/com/songoda/core/lootables/loot/DropUtils.java @@ -1,5 +1,6 @@ package com.songoda.core.lootables.loot; +import com.bgsoftware.wildstacker.api.objects.StackedItem; import com.songoda.core.SongodaCore; import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.settings.Settings; @@ -69,30 +70,25 @@ public class DropUtils { private static void dropItems(List items, EntityDeathEvent event) { if (SongodaCore.isRegistered("UltimateStacker")) { List stacks = new ArrayList<>(); + int maxSize = Settings.MAX_STACK_ITEMS.getInt(); for (ItemStack item : items) { - if (stacks.isEmpty()) { + StackedItem stack = stacks.stream().filter(stackedItem -> stackedItem.getItem().getType() == item.getType()).findFirst().orElse(null); + if (stack == null) { stacks.add(new StackedItem(item, item.getAmount())); continue; } - for (StackedItem stackedItem : stacks.toArray(new StackedItem[0])) { - if (stackedItem.getMaterial().equals(item.getType())) { - int newAmount = stackedItem.getAmount() + item.getAmount(); - int maxSize = Settings.MAX_STACK_ITEMS.getInt(); - while (newAmount > maxSize) { - newAmount -= maxSize; - stacks.add(new StackedItem(item, newAmount)); - } - if (newAmount > 0) { - stacks.add(new StackedItem(item, newAmount)); - } - } else { - stacks.add(new StackedItem(item, item.getAmount())); - } + int newAmount = stack.getAmount() + item.getAmount(); + while (newAmount > maxSize) { + newAmount -= maxSize; + stacks.add(new StackedItem(item, maxSize)); } + stack.setamount(newAmount); } - for (StackedItem stack : stacks) { - UltimateStacker.spawnStackedItem(stack.getItem(), stack.getAmount(), event.getEntity().getLocation()); - } + Bukkit.getScheduler().runTask(UltimateStacker.getInstance(), () -> { + for (StackedItem stack : stacks) { + UltimateStacker.spawnStackedItem(stack.getItem(), stack.getAmount(), event.getEntity().getLocation()); + } + }); return; } for (ItemStack item : items) { @@ -101,21 +97,24 @@ public class DropUtils { } private static void runCommands(LivingEntity entity, List commands) { - for (String command : commands) { - if (entity.getKiller() != null) { - command = command.replace("%player%", entity.getKiller().getName()); - } + Bukkit.getScheduler().runTask(SongodaCore.getHijackedPlugin(), () -> { + for (String command : commands) { + if (entity.getKiller() != null) { + command = command.replace("%player%", entity.getKiller().getName()); + } - if (!command.contains("%player%")) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); + if (!command.contains("%player%")) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); + } } - } + }); + } private static class StackedItem { private final ItemStack item; - private final int amount; + private int amount; public StackedItem(ItemStack item, int amount) { this.item = item; @@ -133,5 +132,9 @@ public class DropUtils { public int getAmount() { return amount; } + + public void setamount(int amount) { + this.amount = amount; + } } }