From 8f8ee2e523ee41ed0e8c0b2a9f4c701a13b29c27 Mon Sep 17 00:00:00 2001 From: Dragon2488 Date: Tue, 22 Aug 2017 13:21:31 +0700 Subject: [PATCH] SlotWidget --- .../internal/IGregTechTileEntity.java | 2 +- .../internal/ISimpleFluidInventory.java | 18 +++ .../internal/ISimpleSlotInventory.java | 18 +++ .../api/gui/widgets/IInventoryWrapper.java | 132 ++++++++++++++++++ .../gregtech/api/gui/widgets/SlotWidget.java | 132 ++++++++++++++++++ .../api/metatileentity/IMetaTileEntity.java | 8 +- 6 files changed, 304 insertions(+), 6 deletions(-) create mode 100644 src/main/java/gregtech/api/capability/internal/ISimpleFluidInventory.java create mode 100644 src/main/java/gregtech/api/capability/internal/ISimpleSlotInventory.java create mode 100644 src/main/java/gregtech/api/gui/widgets/IInventoryWrapper.java create mode 100644 src/main/java/gregtech/api/gui/widgets/SlotWidget.java diff --git a/src/main/java/gregtech/api/capability/internal/IGregTechTileEntity.java b/src/main/java/gregtech/api/capability/internal/IGregTechTileEntity.java index 1f8eb3e8..fa11a6d7 100644 --- a/src/main/java/gregtech/api/capability/internal/IGregTechTileEntity.java +++ b/src/main/java/gregtech/api/capability/internal/IGregTechTileEntity.java @@ -11,7 +11,7 @@ import javax.annotation.Nullable; * Also delivers most of the Informations about my TileEntities. *

*/ -public interface IGregTechTileEntity extends IHasWorldObjectAndCoords { +public interface IGregTechTileEntity extends IHasWorldObjectAndCoords, IUIHolder { @Nullable IMetaTileEntity getMetaTileEntity(); diff --git a/src/main/java/gregtech/api/capability/internal/ISimpleFluidInventory.java b/src/main/java/gregtech/api/capability/internal/ISimpleFluidInventory.java new file mode 100644 index 00000000..79c64bd8 --- /dev/null +++ b/src/main/java/gregtech/api/capability/internal/ISimpleFluidInventory.java @@ -0,0 +1,18 @@ +package gregtech.api.capability.internal; + +import gregtech.api.gui.IUIHolder; +import net.minecraftforge.fluids.FluidStack; + +public interface ISimpleFluidInventory extends IUIHolder { + + int getTanksCount(); + int getTankCapacity(int tankIndex); + + /** + * @return a COPY of stack in slot. Actual stack won't change. + */ + FluidStack getFluidInTank(int tankIndex); + void setFluidInTank(int index, FluidStack fluidStack); + boolean isValidFluidTank(int tankIndex); + +} diff --git a/src/main/java/gregtech/api/capability/internal/ISimpleSlotInventory.java b/src/main/java/gregtech/api/capability/internal/ISimpleSlotInventory.java new file mode 100644 index 00000000..d90d98e7 --- /dev/null +++ b/src/main/java/gregtech/api/capability/internal/ISimpleSlotInventory.java @@ -0,0 +1,18 @@ +package gregtech.api.capability.internal; + +import gregtech.api.gui.IUIHolder; +import net.minecraft.item.ItemStack; + +public interface ISimpleSlotInventory extends IUIHolder { + + int getSlotsCount(); + int getMaxStackSize(int index); + + /** + * @return a COPY of stack in slot. Actual stack won't change. + */ + ItemStack getStackInSlot(int index); + void setStackInSlot(int index, ItemStack stack); + boolean isValidSlot(int index); + +} diff --git a/src/main/java/gregtech/api/gui/widgets/IInventoryWrapper.java b/src/main/java/gregtech/api/gui/widgets/IInventoryWrapper.java new file mode 100644 index 00000000..38ce8a4f --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/IInventoryWrapper.java @@ -0,0 +1,132 @@ +package gregtech.api.gui.widgets; + +import gregtech.api.capability.internal.ISimpleSlotInventory; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TextComponentString; + +import javax.annotation.Nullable; + +/** + * Just an IInventory wrapper to pass to Slot instances, + * since vanilla is too dumb to handle slots with null inventory properly + */ +public class IInventoryWrapper implements IInventory { + + public final ISimpleSlotInventory inventory; + + public IInventoryWrapper(ISimpleSlotInventory inventory) { + this.inventory = inventory; + } + + @Override + public int getSizeInventory() { + return inventory.getSlotsCount(); + } + + @Nullable + @Override + public ItemStack getStackInSlot(int index) { + return inventory.getStackInSlot(index); + } + + @Nullable + @Override + public ItemStack decrStackSize(int index, int count) { + ItemStack stackInSlot = inventory.getStackInSlot(index); + if(stackInSlot == null) { + return null; + } + ItemStack result = stackInSlot.splitStack(count); + inventory.setStackInSlot(index, stackInSlot); + return result; + } + + @Nullable + @Override + public ItemStack removeStackFromSlot(int index) { + ItemStack stack = inventory.getStackInSlot(index); + inventory.setStackInSlot(index, null); + return stack; + } + + @Override + public void setInventorySlotContents(int index, @Nullable ItemStack stack) { + inventory.setStackInSlot(index, stack); + } + + @Override + public void clear() { + for(int i = 0; i < inventory.getSlotsCount(); i++) { + inventory.setStackInSlot(i, null); + } + } + + @Override + public int getInventoryStackLimit() { + return 64; //Handled by Slot implementation + } + + @Override + public void markDirty() { + //Handled by ISimpleInventory implementation + } + + @Override + public boolean isUseableByPlayer(EntityPlayer player) { + return true; //Handled by ModularUI implementation + } + + @Override + public void openInventory(EntityPlayer player) { + //NOOP + } + + @Override + public void closeInventory(EntityPlayer player) { + //NOOP + } + + @Override + public boolean isItemValidForSlot(int index, ItemStack stack) { + return false; //Handled by ISimpleInventory implementation + } + + @Override + public int getField(int id) { + return 0; //NOOP + } + + @Override + public void setField(int id, int value) { + //NOOP + } + + @Override + public int getFieldCount() { + return 0; //NOOP + } + + @Override + public String getName() { + return ""; //Handled by ModularUI implementation + } + + @Override + public boolean hasCustomName() { + return false; //Handled by ModularUI implementation + } + + @Override + public ITextComponent getDisplayName() { + return new TextComponentString(""); //Handled by ModularUI implementation + } + + @Override + public boolean equals(Object obj) { + return obj instanceof IInventoryWrapper && ((IInventoryWrapper) obj).inventory == inventory; + } + +} diff --git a/src/main/java/gregtech/api/gui/widgets/SlotWidget.java b/src/main/java/gregtech/api/gui/widgets/SlotWidget.java new file mode 100644 index 00000000..c2d6fd71 --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/SlotWidget.java @@ -0,0 +1,132 @@ +package gregtech.api.gui.widgets; + +import gregtech.api.capability.internal.ISimpleSlotInventory; +import gregtech.api.gui.INativeWidget; +import gregtech.api.gui.Widget; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.ClickType; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketBuffer; + +import javax.annotation.Nullable; + +public abstract class SlotWidget extends Widget implements INativeWidget { + + protected Slot slotReference; + + private final int slotIndex; + private final int xPosition; + private final int yPosition; + + protected boolean canTakeItems; + protected boolean canPutItems; + + public SlotWidget(int slotIndex, int xPosition, int yPosition, boolean canTakeItems, boolean canPutItems) { + super(Widget.SLOT_DRAW_PRIORITY); + this.slotIndex = slotIndex; + this.xPosition = xPosition; + this.yPosition = yPosition; + this.canTakeItems = canTakeItems; + this.canPutItems = canPutItems; + } + + public SlotWidget(int slotIndex, int xPosition, int yPosition) { + this(slotIndex, xPosition, yPosition, true, true); + } + + public void onPickupFromSlot(EntityPlayer player, ItemStack stack) {} + + public boolean canPutStack(ItemStack stack) { + return true; + } + + public boolean canTakeStack(EntityPlayer player) { + return true; + } + + public boolean canBeHovered() { + return true; + } + + public void onSlotChanged() {} + + @Override + public boolean canMergeSlot(ItemStack stack) { + return true; + } + + @Override + public ItemStack slotClick(int dragType, ClickType clickTypeIn, EntityPlayer player) { + return INativeWidget.VANILLA_LOGIC; + } + + @Override + public final Slot allocateSlotHandle() { + return slotReference; + } + + @Override + public void initWidget() { + this.slotReference = new Slot(new IInventoryWrapper(gui.holder), slotIndex, xPosition, yPosition) { + @Override + public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack) { + super.onPickupFromSlot(playerIn, stack); + SlotWidget.this.onPickupFromSlot(playerIn, stack); + } + + @Override + public boolean isItemValid(@Nullable ItemStack stack) { + return SlotWidget.this.canPutStack(stack); + } + + @Override + public boolean canTakeStack(EntityPlayer playerIn) { + return SlotWidget.this.canTakeStack(playerIn); + } + + @Override + public void onSlotChanged() { + SlotWidget.this.onSlotChanged(); + } + + @Override + public boolean canBeHovered() { + return SlotWidget.this.canBeHovered(); + } + + @Override + public int getSlotStackLimit() { + return gui.holder.getMaxStackSize(slotIndex); + } + + @Override + public boolean isHere(IInventory inv, int slotIn) { + return inv.equals(inventory) && slotIn == slotIn; //Use equals instead of == to handle IInventoryWrappers properly + } + + @Override + public boolean isSameInventory(Slot other) { + return other.inventory != null && other.inventory.equals(inventory); //Use equals instead of == to handle IInventoryWrappers properly + } + }; + } + + @Override + public void draw(int mouseX, int mouseY) { + } + + @Override + public void writeInitialSyncInfo(PacketBuffer buffer) { + } + + @Override + public void readInitialSyncInfo(PacketBuffer buffer) { + } + + @Override + public void readUpdateInfo(PacketBuffer buffer) { + } + +} diff --git a/src/main/java/gregtech/api/metatileentity/IMetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/IMetaTileEntity.java index 56ceecab..7111b6cc 100644 --- a/src/main/java/gregtech/api/metatileentity/IMetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/IMetaTileEntity.java @@ -2,9 +2,7 @@ package gregtech.api.metatileentity; import com.google.common.collect.ImmutableBiMap; import gregtech.api.capability.ITurnable; -import gregtech.api.capability.internal.IGregTechTileEntity; -import gregtech.api.capability.internal.IRedstoneEmitter; -import gregtech.api.capability.internal.IRedstoneReceiver; +import gregtech.api.capability.internal.*; import gregtech.api.gui.IUIHolder; import gregtech.api.gui.ModularUI; import net.minecraft.block.state.IBlockState; @@ -25,7 +23,7 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public interface IMetaTileEntity extends ITurnable, IRedstoneReceiver, IRedstoneEmitter, IUIHolder { +public interface IMetaTileEntity extends ITurnable, IRedstoneReceiver, IRedstoneEmitter, IUIHolder, ISimpleFluidInventory, ISimpleSlotInventory { IMetaTileEntityFactory getFactory(); @@ -121,7 +119,7 @@ public interface IMetaTileEntity extends ITurnable, IRedstoneReceiver, IRedstone * @param player player who opens GUI * @return instance of ModularUI for this MetaTileEntity */ - ModularUI createUI(EntityPlayer player); + ModularUI createUI(EntityPlayer player); int[] getSlotsForFace(EnumFacing face); //side == null - internal inventory change