2020-04-18 22:30:14 +02:00
|
|
|
package de.ellpeck.prettypipes.pipe.modules.stacksize;
|
|
|
|
|
|
|
|
import de.ellpeck.prettypipes.Registry;
|
|
|
|
import de.ellpeck.prettypipes.items.IModule;
|
|
|
|
import de.ellpeck.prettypipes.items.ModuleItem;
|
2021-03-03 01:56:19 +01:00
|
|
|
import de.ellpeck.prettypipes.misc.ItemEquality;
|
2023-09-26 10:56:24 +02:00
|
|
|
import de.ellpeck.prettypipes.misc.ItemFilter;
|
2021-12-02 14:44:26 +01:00
|
|
|
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
|
2020-05-07 15:48:46 +02:00
|
|
|
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
2021-12-02 16:55:04 +01:00
|
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2024-02-03 15:17:58 +01:00
|
|
|
import net.neoforged.neoforge.items.IItemHandler;
|
2020-04-18 22:30:14 +02:00
|
|
|
|
|
|
|
public class StackSizeModuleItem extends ModuleItem {
|
|
|
|
|
2022-06-27 13:57:06 +02:00
|
|
|
public StackSizeModuleItem() {
|
|
|
|
super("stack_size_module");
|
2020-04-18 22:30:14 +02:00
|
|
|
}
|
|
|
|
|
2022-08-01 15:02:47 +02:00
|
|
|
public static int getMaxStackSizeForModule(ItemStack module) {
|
2020-04-18 22:30:14 +02:00
|
|
|
if (module.hasTag()) {
|
2021-12-02 17:46:56 +01:00
|
|
|
var amount = module.getTag().getInt("max_stack_size");
|
2020-04-18 22:30:14 +02:00
|
|
|
if (amount > 0)
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setMaxStackSize(ItemStack module, int amount) {
|
|
|
|
module.getOrCreateTag().putInt("max_stack_size", amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean getLimitToMaxStackSize(ItemStack module) {
|
|
|
|
if (module.hasTag())
|
|
|
|
return module.getTag().getBoolean("limit_to_max_stack_size");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setLimitToMaxStackSize(ItemStack module, boolean yes) {
|
|
|
|
module.getOrCreateTag().putBoolean("limit_to_max_stack_size", yes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public int getMaxInsertionAmount(ItemStack module, PipeBlockEntity tile, ItemStack stack, IItemHandler destination) {
|
2023-09-26 10:56:24 +02:00
|
|
|
var types = ItemFilter.getEqualityTypes(tile);
|
2022-08-01 15:02:47 +02:00
|
|
|
var max = StackSizeModuleItem.getMaxStackSizeForModule(module);
|
2022-06-27 13:57:06 +02:00
|
|
|
if (StackSizeModuleItem.getLimitToMaxStackSize(module))
|
2020-04-18 22:30:14 +02:00
|
|
|
max = Math.min(max, stack.getMaxStackSize());
|
2021-12-02 17:46:56 +01:00
|
|
|
var amount = 0;
|
|
|
|
for (var i = 0; i < destination.getSlots(); i++) {
|
|
|
|
var stored = destination.getStackInSlot(i);
|
2020-04-18 22:30:14 +02:00
|
|
|
if (stored.isEmpty())
|
|
|
|
continue;
|
2023-09-26 10:56:24 +02:00
|
|
|
if (!ItemEquality.compareItems(stored, stack, types))
|
2020-04-18 22:30:14 +02:00
|
|
|
continue;
|
|
|
|
amount += stored.getCount();
|
|
|
|
if (amount >= max)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return max - amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
|
2020-04-18 22:30:14 +02:00
|
|
|
return !(other instanceof StackSizeModuleItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
|
2020-04-18 22:30:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 16:55:04 +01:00
|
|
|
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
|
|
|
|
return new StackSizeModuleContainer(Registry.stackSizeModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
|
2020-04-18 22:30:14 +02:00
|
|
|
}
|
2023-09-26 10:56:24 +02:00
|
|
|
|
2020-04-18 22:30:14 +02:00
|
|
|
}
|