Add stack all feature for spawners

This commit is contained in:
ceze88 2024-03-23 19:17:44 +01:00
parent 1c79ce5485
commit 29e6750539
2 changed files with 39 additions and 1 deletions

View File

@ -181,7 +181,41 @@ public class BlockListeners implements Listener {
if (itemType == blockType) {
SpawnerStack stack = UltimateStackerApi.getSpawnerStackManager().getSpawner(block);
if (stack == null) return;
if (player.isSneaking()) return;
if (player.isSneaking()) {
//Add all to stack from hand
if (Settings.SNEAK_TO_ADD_ALL.getBoolean()) {
//Redo this logic, if we have 10 items in hand and each item is 5 stack size, we need to take into consideration that what happens if we can only add 2 stack total in a stack? We have to remove one extra item and add one spawner with 3 stack back
int amountToAdd = Math.min(maxStackSize - stack.getAmount(), itemAmount * inHand.getAmount()); //Multiply by inHand.getAmount() to get the total amount of items in hand
int remaining = itemAmount * inHand.getAmount() - amountToAdd; //Calculate the remaining amount of items in hand
stack.setAmount(stack.getAmount() + amountToAdd);
plugin.updateHologram(stack);
plugin.getDataManager().save(stack);
if (remaining % itemAmount == 0) { //We don't have to worry about leftovers
if (player.getGameMode() != GameMode.CREATIVE) {
hand.takeItem(player, amountToAdd / itemAmount);
}
} else {
int fullStacks = amountToAdd / itemAmount;
int overflow = remaining % itemAmount;
//remove fullstacks-1 and add back overflow as a new item stack
if (player.getGameMode() != GameMode.CREATIVE) {
if (overflow > 0) {
hand.takeItem(player, fullStacks+1);
ItemStack overflowItem = Methods.getSpawnerItem(blockType, overflow);
if (player.getInventory().firstEmpty() == -1) {
block.getWorld().dropItemNaturally(block.getLocation().add(.5, 0, .5), overflowItem);
} else {
player.getInventory().addItem(overflowItem);
}
} else {
hand.takeItem(player, fullStacks);
}
}
}
}
return;
}
if (player.hasPermission("ultimatestacker.spawner.nostack") && !player.isOp()) {
event.setCancelled(false);
return;
@ -270,6 +304,7 @@ public class BlockListeners implements Listener {
ItemStack item = player.getInventory().getItemInHand();
SpawnerStack stack = UltimateStackerApi.getSpawnerStackManager().getSpawner(block);
if (stack == null) return;
event.setCancelled(true);

View File

@ -239,6 +239,9 @@ public class Settings implements com.craftaro.ultimatestacker.api.Settings {
public static final ConfigSetting SNEAK_FOR_STACK = new ConfigSetting(config, "Spawners.Sneak To Receive A Stacked Spawner", true,
"Toggle ability to receive a stacked spawner when breaking a spawner while sneaking.");
public static final ConfigSetting SNEAK_TO_ADD_ALL = new ConfigSetting(config, "Spawners.Sneak To Add All", true,
"Should the player be able to add all spawners to the stack if they are sneaking?");
public static final ConfigSetting SPAWNERS_DONT_EXPLODE = new ConfigSetting(config, "Spawners.Prevent Spawners From Exploding", false,
"Should spawners not break when blown up?");