Last active
December 13, 2015 20:28
-
-
Save mDiyo/4969609 to your computer and use it in GitHub Desktop.
Gui Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Base Block */ | |
public abstract class InventoryBlock extends BlockContainer | |
{ | |
protected InventoryBlock(int id, Material material) | |
{ | |
super(id, material); | |
} | |
/* Logic backend */ | |
public TileEntity createNewTileEntity (World var1) { return null; } | |
public abstract TileEntity createNewTileEntity(World world, int metadata); | |
public abstract Integer getGui(World world, int x, int y, int z, EntityPlayer entityplayer); | |
public abstract Object getModInstance(); | |
@Override | |
public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int side, float clickX, float clickY, float clickZ) | |
{ | |
if (player.isSneaking()) | |
return false; | |
Integer integer = getGui(world, x, y, z, player); | |
if (integer == null || integer == -1) | |
{ | |
return false; | |
} | |
else | |
{ | |
player.openGui(getModInstance(), integer, world, x, y, z); | |
return true; | |
} | |
} | |
/* Inventory */ | |
@Override | |
public void breakBlock (World par1World, int x, int y, int z, int par5, int par6) | |
{ | |
InventoryLogic logic = (InventoryLogic) par1World.getBlockTileEntity(x, y, z); | |
if (logic != null) | |
{ | |
for (int iter = 0; iter < logic.getSizeInventory(); ++iter) | |
{ | |
ItemStack stack = logic.getStackInSlot(iter); | |
if (stack != null && logic.canDropInventorySlot(iter)) | |
{ | |
float jumpX = TConstruct.tRand.nextFloat() * 0.8F + 0.1F; | |
float jumpY = TConstruct.tRand.nextFloat() * 0.8F + 0.1F; | |
float jumpZ = TConstruct.tRand.nextFloat() * 0.8F + 0.1F; | |
while (stack.stackSize > 0) | |
{ | |
int itemSize = TConstruct.tRand.nextInt(21) + 10; | |
if (itemSize > stack.stackSize) | |
{ | |
itemSize = stack.stackSize; | |
} | |
stack.stackSize -= itemSize; | |
EntityItem entityitem = new EntityItem(par1World, (double) ((float) x + jumpX), (double) ((float) y + jumpY), (double) ((float) z + jumpZ), | |
new ItemStack(stack.itemID, itemSize, stack.getItemDamage())); | |
if (stack.hasTagCompound()) | |
{ | |
entityitem.func_92014_d().setTagCompound((NBTTagCompound) stack.getTagCompound().copy()); | |
} | |
float offset = 0.05F; | |
entityitem.motionX = (double) ((float) TConstruct.tRand.nextGaussian() * offset); | |
entityitem.motionY = (double) ((float) TConstruct.tRand.nextGaussian() * offset + 0.2F); | |
entityitem.motionZ = (double) ((float) TConstruct.tRand.nextGaussian() * offset); | |
par1World.spawnEntityInWorld(entityitem); | |
} | |
} | |
} | |
} | |
super.breakBlock(par1World, x, y, z, par5, par6); | |
} | |
@Override | |
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLiving entityliving) | |
{ | |
TileEntity logic = world.getBlockTileEntity(x, y, z); | |
if (logic instanceof IFacingLogic) | |
{ | |
IFacingLogic direction = (IFacingLogic) logic; | |
if (entityliving == null) | |
{ | |
direction.setDirection(0F, 0F); | |
} | |
else | |
{ | |
direction.setDirection(entityliving.rotationYaw * 4F, entityliving.rotationPitch); | |
} | |
} | |
} | |
public static boolean isActive(IBlockAccess world, int x, int y, int z) | |
{ | |
TileEntity logic = world.getBlockTileEntity(x, y, z); | |
if (logic instanceof IActiveLogic) | |
{ | |
return ((IActiveLogic)logic).getActive(); | |
} | |
return false; | |
} | |
} | |
/* | |
* A simple logic class for storing items | |
* Abstract to avoid instantiation | |
*/ | |
public abstract class InventoryLogic extends TileEntity | |
implements IInventory | |
{ | |
protected ItemStack[] inventory; | |
public InventoryLogic(int invSize) | |
{ | |
inventory = new ItemStack[invSize]; | |
} | |
/* Inventory management */ | |
@Override | |
public ItemStack getStackInSlot(int slot) | |
{ | |
return inventory[slot]; | |
} | |
public boolean isStackInSlot(int slot) | |
{ | |
return inventory[slot] != null; | |
} | |
@Override | |
public int getSizeInventory() | |
{ | |
return inventory.length; | |
} | |
@Override | |
public int getInventoryStackLimit () | |
{ | |
return 64; | |
} | |
public boolean canDropInventorySlot(int slot) | |
{ | |
return true; | |
} | |
@Override | |
public void setInventorySlotContents(int slot, ItemStack itemstack) | |
{ | |
inventory[slot] = itemstack; | |
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) | |
{ | |
itemstack.stackSize = getInventoryStackLimit(); | |
} | |
} | |
@Override | |
public ItemStack decrStackSize(int slot, int quantity) | |
{ | |
if (inventory[slot] != null) | |
{ | |
if (inventory[slot].stackSize <= quantity) | |
{ | |
ItemStack stack = inventory[slot]; | |
inventory[slot] = null; | |
return stack; | |
} | |
ItemStack split = inventory[slot].splitStack(quantity); | |
if (inventory[slot].stackSize == 0) | |
{ | |
inventory[slot] = null; | |
} | |
return split; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
/* Supporting methods */ | |
@Override | |
public boolean isUseableByPlayer(EntityPlayer entityplayer) | |
{ | |
if (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this) | |
return false; | |
else | |
return entityplayer.getDistance((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64D; | |
} | |
public abstract Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z); | |
/* NBT */ | |
@Override | |
public void readFromNBT(NBTTagCompound tags) | |
{ | |
super.readFromNBT(tags); | |
NBTTagList nbttaglist = tags.getTagList("Items"); | |
inventory = new ItemStack[getSizeInventory()]; | |
for (int iter = 0; iter < nbttaglist.tagCount(); iter++) | |
{ | |
NBTTagCompound tagList = (NBTTagCompound)nbttaglist.tagAt(iter); | |
byte slotID = tagList.getByte("Slot"); | |
if (slotID >= 0 && slotID < inventory.length) | |
{ | |
inventory[slotID] = ItemStack.loadItemStackFromNBT(tagList); | |
} | |
} | |
} | |
@Override | |
public void writeToNBT(NBTTagCompound tags) | |
{ | |
super.writeToNBT(tags); | |
NBTTagList nbttaglist = new NBTTagList(); | |
for (int iter = 0; iter < inventory.length; iter++) | |
{ | |
if (inventory[iter] != null) | |
{ | |
NBTTagCompound tagList = new NBTTagCompound(); | |
tagList.setByte("Slot", (byte)iter); | |
inventory[iter].writeToNBT(tagList); | |
nbttaglist.appendTag(tagList); | |
} | |
} | |
tags.setTag("Items", nbttaglist); | |
} | |
/* Default implementations of hardly used methods */ | |
public ItemStack getStackInSlotOnClosing (int slot) { return null; } | |
public void openChest () {} | |
public void closeChest () {} | |
} | |
public class TGuiHandler implements IGuiHandler | |
{ | |
public static int stationID = 0; | |
public static int partID = 1; | |
public static int pchestID = 2; | |
public static int pshaperID = 3; | |
public static int frypanID = 4; | |
public static int smeltery = 7; | |
public static int manualGui = -1; | |
@Override | |
public Object getServerGuiElement (int ID, EntityPlayer player, World world, int x, int y, int z) | |
{ | |
if (ID < 0) | |
return null; | |
TileEntity tile = world.getBlockTileEntity(x, y, z); | |
if (tile != null && tile instanceof InventoryLogic) | |
return ((InventoryLogic) tile).getGuiContainer(player.inventory, world, x, y, z); | |
return null; | |
} | |
@Override | |
public Object getClientGuiElement (int ID, EntityPlayer player, World world, int x, int y, int z) | |
{ | |
if (ID == stationID) | |
return new ToolStationGui(player.inventory, (ToolStationLogic) world.getBlockTileEntity(x, y, z), world, x, y, z); | |
if (ID == partID) | |
return new PartCrafterGui(player.inventory, (PartCrafterLogic) world.getBlockTileEntity(x, y, z), world, x, y, z); | |
if (ID == pchestID) | |
return new PatternChestGui(player.inventory, (PatternChestLogic) world.getBlockTileEntity(x, y, z), world, x, y, z); | |
if (ID == frypanID) | |
return new FrypanGui(player.inventory, (FrypanLogic) world.getBlockTileEntity(x, y, z), world, x, y, z); | |
if (ID == smeltery) | |
return new SmelteryGui(player.inventory, (SmelteryLogic) world.getBlockTileEntity(x, y, z), world, x, y, z); | |
if (ID == pshaperID) | |
return new PatternShaperGui(player.inventory, (PatternShaperLogic) world.getBlockTileEntity(x, y, z), world, x, y, z); | |
if (ID == manualGui) | |
{ | |
ItemStack stack = player.getCurrentEquippedItem(); | |
return new GuiManual(stack, TProxyClient.getManualFromStack(stack)); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment