2020-09-09 16:49:01 +02:00
|
|
|
package de.ellpeck.actuallyadditions.common.inventory;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2017-12-01 22:19:52 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
2020-09-09 16:49:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
|
|
|
|
import de.ellpeck.actuallyadditions.common.config.values.ConfigStringListValues;
|
|
|
|
import de.ellpeck.actuallyadditions.common.inventory.slot.SlotDeletion;
|
|
|
|
import de.ellpeck.actuallyadditions.common.inventory.slot.SlotFilter;
|
|
|
|
import de.ellpeck.actuallyadditions.common.inventory.slot.SlotImmovable;
|
|
|
|
import de.ellpeck.actuallyadditions.common.inventory.slot.SlotItemHandlerUnconditioned;
|
|
|
|
import de.ellpeck.actuallyadditions.common.items.ItemBag;
|
|
|
|
import de.ellpeck.actuallyadditions.common.items.ItemDrill;
|
|
|
|
import de.ellpeck.actuallyadditions.common.network.gui.IButtonReactor;
|
|
|
|
import de.ellpeck.actuallyadditions.common.tile.FilterSettings;
|
|
|
|
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA;
|
|
|
|
import de.ellpeck.actuallyadditions.common.util.StackUtil;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
2016-12-18 17:50:31 +01:00
|
|
|
import net.minecraft.inventory.ClickType;
|
|
|
|
import net.minecraft.inventory.Container;
|
|
|
|
import net.minecraft.inventory.IContainerListener;
|
|
|
|
import net.minecraft.inventory.Slot;
|
2017-12-01 22:19:52 +01:00
|
|
|
import net.minecraft.item.Item;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2017-12-01 22:19:52 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public class ContainerBag extends Container implements IButtonReactor {
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2016-11-27 11:37:55 +01:00
|
|
|
public final FilterSettings filter = new FilterSettings(4, false, true, false, false, 0, -1000);
|
2018-06-23 01:39:30 +02:00
|
|
|
private final ItemStackHandlerAA bagInventory;
|
2016-08-02 16:32:13 +02:00
|
|
|
private final InventoryPlayer inventory;
|
|
|
|
private final boolean isVoid;
|
|
|
|
public boolean autoInsert;
|
|
|
|
private boolean oldAutoInsert;
|
2017-11-30 08:58:26 +01:00
|
|
|
private final ItemStack sack;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public ContainerBag(ItemStack sack, InventoryPlayer inventory, boolean isVoid) {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.inventory = inventory;
|
2018-08-10 05:04:07 +02:00
|
|
|
this.bagInventory = new ItemStackHandlerAA(getSlotAmount(isVoid), (slot, stack, automation) -> !isBlacklisted(stack), ItemStackHandlerAA.REMOVE_TRUE);
|
2016-08-02 16:32:13 +02:00
|
|
|
this.isVoid = isVoid;
|
2017-11-30 08:58:26 +01:00
|
|
|
this.sack = sack;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
this.addSlotToContainer(new SlotFilter(this.filter, i, 155, 10 + i * 18));
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
if (this.isVoid) {
|
|
|
|
this.addSlotToContainer(new SlotDeletion(this.bagInventory, 0, 64, 65) {
|
2016-08-02 16:32:13 +02:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean isItemValid(ItemStack stack) {
|
2016-11-27 11:37:55 +01:00
|
|
|
return ContainerBag.this.filter.check(stack);
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
});
|
2018-08-10 05:04:07 +02:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
for (int j = 0; j < 7; j++) {
|
|
|
|
this.addSlotToContainer(new SlotItemHandlerUnconditioned(this.bagInventory, j + i * 7, 10 + j * 18, 10 + i * 18) {
|
2016-08-02 16:32:13 +02:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean isItemValid(ItemStack stack) {
|
2017-12-01 22:19:52 +01:00
|
|
|
return !isBlacklisted(stack) && ContainerBag.this.filter.check(stack);
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
for (int j = 0; j < 9; j++) {
|
|
|
|
this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 94 + i * 18));
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 05:04:07 +02:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
if (i == inventory.currentItem) {
|
|
|
|
this.addSlotToContainer(new SlotImmovable(inventory, i, 8 + i * 18, 152));
|
|
|
|
} else {
|
|
|
|
this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 152));
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack stack = inventory.getCurrentItem();
|
2018-08-10 05:04:07 +02:00
|
|
|
if (StackUtil.isValid(stack) && stack.getItem() instanceof ItemBag) {
|
2016-11-27 11:37:55 +01:00
|
|
|
ItemDrill.loadSlotsFromNBT(this.bagInventory, inventory.getCurrentItem());
|
2018-08-10 05:04:07 +02:00
|
|
|
if (stack.hasTagCompound()) {
|
2016-08-02 16:32:13 +02:00
|
|
|
NBTTagCompound compound = stack.getTagCompound();
|
|
|
|
this.filter.readFromNBT(compound, "Filter");
|
|
|
|
this.autoInsert = compound.getBoolean("AutoInsert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public static int getSlotAmount(boolean isVoid) {
|
2016-11-27 11:37:55 +01:00
|
|
|
return isVoid ? 1 : 28;
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void detectAndSendChanges() {
|
2016-08-02 16:32:13 +02:00
|
|
|
super.detectAndSendChanges();
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
if (this.filter.needsUpdateSend() || this.autoInsert != this.oldAutoInsert) {
|
|
|
|
for (IContainerListener listener : this.listeners) {
|
2017-06-29 18:30:02 +02:00
|
|
|
listener.sendWindowProperty(this, 0, this.filter.isWhitelist ? 1 : 0);
|
|
|
|
listener.sendWindowProperty(this, 1, this.filter.respectMeta ? 1 : 0);
|
|
|
|
listener.sendWindowProperty(this, 2, this.filter.respectNBT ? 1 : 0);
|
|
|
|
listener.sendWindowProperty(this, 3, this.filter.respectOredict);
|
|
|
|
listener.sendWindowProperty(this, 4, this.autoInsert ? 1 : 0);
|
|
|
|
listener.sendWindowProperty(this, 5, this.filter.respectMod ? 1 : 0);
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
this.filter.updateLasts();
|
|
|
|
this.oldAutoInsert = this.autoInsert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2018-08-10 05:04:07 +02:00
|
|
|
public void updateProgressBar(int id, int data) {
|
|
|
|
if (id == 0) {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.filter.isWhitelist = data == 1;
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (id == 1) {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.filter.respectMeta = data == 1;
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (id == 2) {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.filter.respectNBT = data == 1;
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (id == 3) {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.filter.respectOredict = data;
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (id == 4) {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.autoInsert = data == 1;
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (id == 5) {
|
2016-11-17 14:56:26 +01:00
|
|
|
this.filter.respectMod = data == 1;
|
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
|
|
|
|
int inventoryStart = this.bagInventory.getSlots() + 4;
|
|
|
|
int inventoryEnd = inventoryStart + 26;
|
|
|
|
int hotbarStart = inventoryEnd + 1;
|
|
|
|
int hotbarEnd = hotbarStart + 8;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
|
|
|
Slot theSlot = this.inventorySlots.get(slot);
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
if (theSlot != null && theSlot.getHasStack()) {
|
2016-08-02 16:32:13 +02:00
|
|
|
ItemStack newStack = theSlot.getStack();
|
|
|
|
ItemStack currentStack = newStack.copy();
|
|
|
|
|
|
|
|
//Other Slots in Inventory excluded
|
2018-08-10 05:04:07 +02:00
|
|
|
if (slot >= inventoryStart) {
|
2016-08-02 16:32:13 +02:00
|
|
|
//Shift from Inventory
|
2018-08-10 05:04:07 +02:00
|
|
|
if (this.isVoid || !this.filter.check(newStack) || !this.mergeItemStack(newStack, 4, 32, false)) {
|
|
|
|
if (slot >= inventoryStart && slot <= inventoryEnd) {
|
|
|
|
if (!this.mergeItemStack(newStack, hotbarStart, hotbarEnd + 1, false)) { return StackUtil.getEmpty(); }
|
|
|
|
} else if (slot >= inventoryEnd + 1 && slot < hotbarEnd + 1 && !this.mergeItemStack(newStack, inventoryStart, inventoryEnd + 1, false)) { return StackUtil.getEmpty(); }
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (!this.mergeItemStack(newStack, inventoryStart, hotbarEnd + 1, false)) { return StackUtil.getEmpty(); }
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
if (!StackUtil.isValid(newStack)) {
|
2017-11-02 22:49:53 +01:00
|
|
|
theSlot.putStack(StackUtil.getEmpty());
|
2018-08-10 05:04:07 +02:00
|
|
|
} else {
|
2016-08-02 16:32:13 +02:00
|
|
|
theSlot.onSlotChanged();
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
if (newStack.getCount() == currentStack.getCount()) { return StackUtil.getEmpty(); }
|
2016-11-26 21:32:27 +01:00
|
|
|
theSlot.onTake(player, newStack);
|
2016-08-02 16:32:13 +02:00
|
|
|
|
|
|
|
return currentStack;
|
|
|
|
}
|
2017-11-02 22:49:53 +01:00
|
|
|
return StackUtil.getEmpty();
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player) {
|
|
|
|
if (SlotFilter.checkFilter(this, slotId, player)) {
|
2017-11-02 22:49:53 +01:00
|
|
|
return StackUtil.getEmpty();
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (clickTypeIn == ClickType.SWAP && dragType == this.inventory.currentItem) {
|
2017-11-02 22:49:53 +01:00
|
|
|
return ItemStack.EMPTY;
|
2018-08-10 05:04:07 +02:00
|
|
|
} else {
|
2016-08-02 16:32:13 +02:00
|
|
|
return super.slotClick(slotId, dragType, clickTypeIn, player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void onContainerClosed(EntityPlayer player) {
|
2016-08-02 16:32:13 +02:00
|
|
|
ItemStack stack = this.inventory.getCurrentItem();
|
2018-08-10 05:04:07 +02:00
|
|
|
if (StackUtil.isValid(stack) && stack.getItem() instanceof ItemBag) {
|
2016-11-27 11:37:55 +01:00
|
|
|
ItemDrill.writeSlotsToNBT(this.bagInventory, this.inventory.getCurrentItem());
|
2016-08-02 16:32:13 +02:00
|
|
|
NBTTagCompound compound = stack.getTagCompound();
|
|
|
|
this.filter.writeToNBT(compound, "Filter");
|
|
|
|
compound.setBoolean("AutoInsert", this.autoInsert);
|
|
|
|
}
|
|
|
|
super.onContainerClosed(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean canInteractWith(EntityPlayer player) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return !this.sack.isEmpty() && player.getHeldItemMainhand() == this.sack;
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void onButtonPressed(int buttonID, EntityPlayer player) {
|
|
|
|
if (buttonID == 0) {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.autoInsert = !this.autoInsert;
|
2018-08-10 05:04:07 +02:00
|
|
|
} else {
|
2016-08-02 16:32:13 +02:00
|
|
|
this.filter.onButtonPressed(buttonID);
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 05:04:07 +02:00
|
|
|
|
2017-12-01 22:19:52 +01:00
|
|
|
private static final List<Pair<Item, Integer>> BLACKLIST = new ArrayList<>();
|
2018-08-10 05:04:07 +02:00
|
|
|
|
2017-12-01 22:19:52 +01:00
|
|
|
private static boolean runOnce = false;
|
2018-08-10 05:04:07 +02:00
|
|
|
|
2017-12-01 22:19:52 +01:00
|
|
|
public static boolean isBlacklisted(ItemStack stack) {
|
2018-08-10 05:04:07 +02:00
|
|
|
if (!runOnce) {
|
|
|
|
runOnce = true;
|
|
|
|
for (String s : ConfigStringListValues.SACK_BLACKLIST.getValue()) {
|
|
|
|
String[] split = s.split("@");
|
|
|
|
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(split[0]));
|
|
|
|
if (item == null) {
|
|
|
|
ActuallyAdditions.LOGGER.error("Invalid item in sack blacklist: " + s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (split.length == 1) BLACKLIST.add(Pair.of(item, 0));
|
|
|
|
else if (split.length == 2) {
|
|
|
|
BLACKLIST.add(Pair.of(item, Integer.parseInt(split[1])));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BLACKLIST.contains(Pair.of(stack.getItem(), stack.getMetadata()));
|
2017-12-01 22:19:52 +01:00
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|