SlotWidget

This commit is contained in:
Dragon2488 2017-08-22 13:21:31 +07:00
parent a11202cb57
commit 8f8ee2e523
6 changed files with 304 additions and 6 deletions

View file

@ -11,7 +11,7 @@ import javax.annotation.Nullable;
* Also delivers most of the Informations about my TileEntities.
* <p/>
*/
public interface IGregTechTileEntity extends IHasWorldObjectAndCoords {
public interface IGregTechTileEntity extends IHasWorldObjectAndCoords, IUIHolder {
@Nullable IMetaTileEntity getMetaTileEntity();

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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;
}
}

View file

@ -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<ISimpleSlotInventory> 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) {
}
}

View file

@ -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<? extends IMetaTileEntity> createUI(EntityPlayer player);
ModularUI<? extends IGregTechTileEntity> createUI(EntityPlayer player);
int[] getSlotsForFace(EnumFacing face);
//side == null - internal inventory change