ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/SackContainer.java

236 lines
8.8 KiB
Java
Raw Normal View History

/*
* This file ("ContainerBag.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.inventory;
2023-12-20 22:02:25 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyTags;
import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotDeletion;
import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotFilter;
import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotImmovable;
import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotItemHandlerUnconditioned;
2023-12-20 22:02:25 +01:00
import de.ellpeck.actuallyadditions.mod.items.Sack;
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
import de.ellpeck.actuallyadditions.mod.tile.FilterSettings;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2021-02-27 17:22:03 +01:00
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2021-02-27 21:24:26 +01:00
import net.minecraft.network.PacketBuffer;
2021-02-26 22:15:48 +01:00
import net.minecraftforge.api.distmarker.Dist;
2021-02-27 17:22:03 +01:00
import net.minecraftforge.api.distmarker.OnlyIn;
2021-02-26 22:15:48 +01:00
2023-12-20 22:02:25 +01:00
import javax.annotation.Nonnull;
2023-03-16 00:17:57 +01:00
import java.util.UUID;
2023-12-20 22:02:25 +01:00
public class SackContainer extends Container implements IButtonReactor {
2023-03-16 00:17:57 +01:00
public final FilterSettings filter = new FilterSettings(4, false, false, false);
private final ItemStackHandlerAA bagInventory;
2021-02-27 21:24:26 +01:00
private final PlayerInventory inventory;
public boolean autoInsert;
private boolean oldAutoInsert;
2023-03-16 00:17:57 +01:00
public static final int SIZE = 28;
2021-02-27 21:24:26 +01:00
2023-12-20 22:02:25 +01:00
public static SackContainer fromNetwork(int windowId, PlayerInventory inv, PacketBuffer data) {
return new SackContainer(windowId, inv, data.readUUID(), new ItemStackHandlerAA(28));
2021-02-27 21:24:26 +01:00
}
2023-12-20 22:02:25 +01:00
public SackContainer(int windowId, PlayerInventory playerInventory, UUID uuid, ItemStackHandlerAA handler) {
2021-02-27 21:24:26 +01:00
super(ActuallyContainers.BAG_CONTAINER.get(), windowId);
2023-03-16 00:17:57 +01:00
this.inventory = playerInventory;
2023-12-20 22:02:25 +01:00
this.bagInventory = handler;
2023-12-20 22:02:25 +01:00
for (int row = 0; row < 4; row++) {
this.addSlot(new SlotFilter(this.filter, row, 155, 10 + row * 18));
}
2023-12-20 22:02:25 +01:00
if (false) { // TODO isvoid, move to its own container
2021-02-27 21:24:26 +01:00
this.addSlot(new SlotDeletion(this.bagInventory, 0, 64, 65) {
@Override
public boolean mayPlace(ItemStack stack) {
2023-12-20 22:02:25 +01:00
return SackContainer.this.filter.check(stack);
}
});
2023-03-16 00:17:57 +01:00
}
2023-12-20 22:02:25 +01:00
// Sack inventory
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 7; col++) {
this.addSlot(new SlotItemHandlerUnconditioned(this.bagInventory, col + row * 7, 10 + col * 18, 10 + row * 18) {
2023-03-16 00:17:57 +01:00
@Override
public boolean mayPlace(ItemStack stack) {
2023-12-20 22:02:25 +01:00
return !stack.getItem().is(ActuallyTags.Items.HOLDS_ITEMS) && SackContainer.this.filter.check(stack);
2023-03-16 00:17:57 +01:00
}
});
}
}
2023-12-20 22:02:25 +01:00
// Player Inventory
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
this.addSlot(new Slot(playerInventory, col + row * 9 + 9, 8 + col * 18, 94 + row * 18));
}
}
2023-12-20 22:02:25 +01:00
// Player Hotbar
2018-08-10 05:04:07 +02:00
for (int i = 0; i < 9; i++) {
2023-03-16 00:17:57 +01:00
if (i == playerInventory.selected) {
this.addSlot(new SlotImmovable(playerInventory, i, 8 + i * 18, 152));
2018-08-10 05:04:07 +02:00
} else {
2023-03-16 00:17:57 +01:00
this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 152));
}
}
2023-03-16 00:17:57 +01:00
ItemStack stack = playerInventory.getSelected();
2023-12-20 22:02:25 +01:00
if (!stack.isEmpty() && stack.getItem() instanceof Sack) {
//DrillItem.loadSlotsFromNBT(this.bagInventory, playerInventory.getSelected());
2021-02-27 21:24:26 +01:00
if (stack.hasTag()) {
CompoundNBT compound = stack.getOrCreateTag();
this.filter.readFromNBT(compound, "Filter");
this.autoInsert = compound.getBoolean("AutoInsert");
}
}
}
@Override
2023-12-20 22:02:25 +01:00
public void broadcastChanges() { // TODO is this needed anymore?
super.broadcastChanges();
2018-08-10 05:04:07 +02:00
if (this.filter.needsUpdateSend() || this.autoInsert != this.oldAutoInsert) {
2021-11-14 00:20:29 +01:00
/*
for (IContainerListener listener : this.containerListeners) {
listener.setContainerData(this, 0, this.filter.isWhitelist
2021-02-26 22:15:48 +01:00
? 1
: 0);
listener.setContainerData(this, 1, this.filter.respectMeta
2021-02-26 22:15:48 +01:00
? 1
: 0);
listener.setContainerData(this, 2, this.filter.respectNBT
2021-02-26 22:15:48 +01:00
? 1
: 0);
listener.setContainerData(this, 3, this.filter.respectOredict);
listener.setContainerData(this, 4, this.autoInsert
2021-02-26 22:15:48 +01:00
? 1
: 0);
listener.setContainerData(this, 5, this.filter.respectMod
2021-02-26 22:15:48 +01:00
? 1
: 0);
}
2021-11-14 00:20:29 +01:00
*/
this.filter.updateLasts();
this.oldAutoInsert = this.autoInsert;
}
}
@Override
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public void setData(int id, int data) {
2018-08-10 05:04:07 +02:00
if (id == 0) {
this.filter.isWhitelist = data == 1;
2018-08-10 05:04:07 +02:00
} else if (id == 1) {
this.filter.respectNBT = data == 1;
2023-03-16 00:17:57 +01:00
} else if (id == 2) {
this.autoInsert = data == 1;
2023-03-16 00:17:57 +01:00
} else if (id == 3) {
2016-11-17 14:56:26 +01:00
this.filter.respectMod = data == 1;
}
}
@Override
2023-12-20 22:02:25 +01:00
public ItemStack quickMoveStack(@Nonnull PlayerEntity player, int slot) {
2018-08-10 05:04:07 +02:00
int inventoryStart = this.bagInventory.getSlots() + 4;
int inventoryEnd = inventoryStart + 26;
int hotbarStart = inventoryEnd + 1;
int hotbarEnd = hotbarStart + 8;
Slot theSlot = this.slots.get(slot);
if (theSlot != null && theSlot.hasItem()) {
ItemStack newStack = theSlot.getItem();
ItemStack currentStack = newStack.copy();
//Other Slots in Inventory excluded
2018-08-10 05:04:07 +02:00
if (slot >= inventoryStart) {
//Shift from Inventory
2023-03-16 00:17:57 +01:00
if (!this.filter.check(newStack) || !this.moveItemStackTo(newStack, 4, 32, false)) {
2023-12-20 22:02:25 +01:00
if (slot <= inventoryEnd) {
if (!this.moveItemStackTo(newStack, hotbarStart, hotbarEnd + 1, false)) {
2022-06-24 21:38:07 +02:00
return ItemStack.EMPTY;
2021-02-26 22:15:48 +01:00
}
} else if (slot >= inventoryEnd + 1 && slot < hotbarEnd + 1 && !this.moveItemStackTo(newStack, inventoryStart, inventoryEnd + 1, false)) {
2022-06-24 21:38:07 +02:00
return ItemStack.EMPTY;
2021-02-26 22:15:48 +01:00
}
}
//
} else if (!this.moveItemStackTo(newStack, inventoryStart, hotbarEnd + 1, false)) {
2022-06-24 21:38:07 +02:00
return ItemStack.EMPTY;
2021-02-26 22:15:48 +01:00
}
2023-03-16 00:17:57 +01:00
if (newStack.isEmpty()) {
2022-06-24 21:38:07 +02:00
theSlot.set(ItemStack.EMPTY);
2018-08-10 05:04:07 +02:00
} else {
theSlot.setChanged();
}
2021-02-26 22:15:48 +01:00
if (newStack.getCount() == currentStack.getCount()) {
2022-06-24 21:38:07 +02:00
return ItemStack.EMPTY;
2021-02-26 22:15:48 +01:00
}
2016-11-26 21:32:27 +01:00
theSlot.onTake(player, newStack);
return currentStack;
}
2022-06-24 21:38:07 +02:00
return ItemStack.EMPTY;
}
@Override
public ItemStack clicked(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
2018-08-10 05:04:07 +02:00
if (SlotFilter.checkFilter(this, slotId, player)) {
2022-06-24 21:38:07 +02:00
return ItemStack.EMPTY;
} else if (clickTypeIn == ClickType.SWAP && dragType == this.inventory.selected) {
2017-11-02 22:49:53 +01:00
return ItemStack.EMPTY;
2018-08-10 05:04:07 +02:00
} else {
return super.clicked(slotId, dragType, clickTypeIn, player);
}
}
@Override
public void removed(PlayerEntity player) {
ItemStack stack = this.inventory.getSelected();
2023-12-20 22:02:25 +01:00
if (!stack.isEmpty() && stack.getItem() instanceof Sack) {
//DrillItem.writeSlotsToNBT(this.bagInventory, this.inventory.getSelected());
2021-02-27 21:24:26 +01:00
CompoundNBT compound = stack.getOrCreateTag();
this.filter.writeToNBT(compound, "Filter");
2021-02-27 16:33:00 +01:00
compound.putBoolean("AutoInsert", this.autoInsert);
}
super.removed(player);
}
@Override
public boolean stillValid(PlayerEntity player) {
2023-12-20 22:02:25 +01:00
return true;
}
@Override
2021-02-26 22:15:48 +01:00
public void onButtonPressed(int buttonID, PlayerEntity player) {
2018-08-10 05:04:07 +02:00
if (buttonID == 0) {
this.autoInsert = !this.autoInsert;
2018-08-10 05:04:07 +02:00
} else {
2023-03-16 00:17:57 +01:00
//this.filter.onButtonPressed(buttonID); //TODO
}
}
2021-02-26 22:15:48 +01:00
}