package net.paladrill.paladrillmod.block; import net.paladrill.paladrillmod.procedure.ProcedurePaladiumCrusherUpdateTick; import net.paladrill.paladrillmod.gui.GuiPaladiumCrusherGUI; import net.paladrill.paladrillmod.creativetab.TabPalaDrillMod; import net.paladrill.paladrillmod.PaladrillmodMod; import net.paladrill.paladrillmod.ElementsPaladrillmodMod; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.client.event.ModelRegistryEvent; import net.minecraft.world.World; import net.minecraft.util.math.BlockPos; import net.minecraft.util.NonNullList; import net.minecraft.util.EnumHand; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumBlockRenderType; import net.minecraft.tileentity.TileEntityLockableLoot; import net.minecraft.tileentity.TileEntity; import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.network.NetworkManager; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemBlock; import net.minecraft.item.Item; import net.minecraft.inventory.ItemStackHelper; import net.minecraft.inventory.InventoryHelper; import net.minecraft.inventory.Container; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.block.state.IBlockState; import net.minecraft.block.material.Material; import net.minecraft.block.SoundType; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.Block; import java.util.Random; import java.util.Map; import java.util.HashMap; @ElementsPaladrillmodMod.ModElement.Tag public class BlockPaladiumCrusher extends ElementsPaladrillmodMod.ModElement { @GameRegistry.ObjectHolder("paladrillmod:paladiumcrusher") public static final Block block = null; public BlockPaladiumCrusher(ElementsPaladrillmodMod instance) { super(instance, 68); } @Override public void initElements() { elements.blocks.add(() -> new BlockCustom().setRegistryName("paladiumcrusher")); elements.items.add(() -> new ItemBlock(block).setRegistryName(block.getRegistryName())); } @Override public void init(FMLInitializationEvent event) { GameRegistry.registerTileEntity(TileEntityCustom.class, "paladrillmod:tileentitypaladiumcrusher"); } @SideOnly(Side.CLIENT) @Override public void registerModels(ModelRegistryEvent event) { ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, new ModelResourceLocation("paladrillmod:paladiumcrusher", "inventory")); } public static class BlockCustom extends Block implements ITileEntityProvider { public BlockCustom() { super(Material.IRON); setUnlocalizedName("paladiumcrusher"); setSoundType(SoundType.METAL); setHardness(1F); setResistance(10F); setLightLevel(0F); setLightOpacity(255); setCreativeTab(TabPalaDrillMod.tab); } @Override public TileEntity createNewTileEntity(World worldIn, int meta) { return new TileEntityCustom(); } @Override public boolean eventReceived(IBlockState state, World worldIn, BlockPos pos, int eventID, int eventParam) { super.eventReceived(state, worldIn, pos, eventID, eventParam); TileEntity tileentity = worldIn.getTileEntity(pos); return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam); } @Override public EnumBlockRenderType getRenderType(IBlockState state) { return EnumBlockRenderType.MODEL; } @Override public void breakBlock(World world, BlockPos pos, IBlockState state) { TileEntity tileentity = world.getTileEntity(pos); if (tileentity instanceof TileEntityCustom) InventoryHelper.dropInventoryItems(world, pos, (TileEntityCustom) tileentity); world.removeTileEntity(pos); super.breakBlock(world, pos, state); } @Override public boolean hasComparatorInputOverride(IBlockState state) { return true; } @Override public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos) { TileEntity tileentity = worldIn.getTileEntity(pos); if (tileentity instanceof TileEntityCustom) return Container.calcRedstoneFromInventory((TileEntityCustom) tileentity); else return 0; } @Override public void onBlockAdded(World world, BlockPos pos, IBlockState state) { super.onBlockAdded(world, pos, state); int x = pos.getX(); int y = pos.getY(); int z = pos.getZ(); world.scheduleUpdate(new BlockPos(x, y, z), this, this.tickRate(world)); } @Override public void updateTick(World world, BlockPos pos, IBlockState state, Random random) { super.updateTick(world, pos, state, random); int x = pos.getX(); int y = pos.getY(); int z = pos.getZ(); { Map $_dependencies = new HashMap<>(); $_dependencies.put("x", x); $_dependencies.put("y", y); $_dependencies.put("z", z); $_dependencies.put("world", world); ProcedurePaladiumCrusherUpdateTick.executeProcedure($_dependencies); } world.scheduleUpdate(new BlockPos(x, y, z), this, this.tickRate(world)); } @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer entity, EnumHand hand, EnumFacing direction, float hitX, float hitY, float hitZ) { super.onBlockActivated(world, pos, state, entity, hand, direction, hitX, hitY, hitZ); int x = pos.getX(); int y = pos.getY(); int z = pos.getZ(); if (entity instanceof EntityPlayer) { ((EntityPlayer) entity).openGui(PaladrillmodMod.instance, GuiPaladiumCrusherGUI.GUIID, world, x, y, z); } return true; } } public static class TileEntityCustom extends TileEntityLockableLoot { private NonNullList stacks = NonNullList.withSize(3, ItemStack.EMPTY); @Override public int getSizeInventory() { return 3; } @Override public boolean isEmpty() { for (ItemStack itemstack : this.stacks) if (!itemstack.isEmpty()) return false; return true; } @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return true; } @Override public ItemStack getStackInSlot(int slot) { return stacks.get(slot); } @Override public String getName() { return "container.paladiumcrusher"; } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); this.stacks = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY); if (!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, this.stacks); } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); if (!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, this.stacks); return compound; } @Override public SPacketUpdateTileEntity getUpdatePacket() { return new SPacketUpdateTileEntity(this.pos, 0, this.getUpdateTag()); } @Override public NBTTagCompound getUpdateTag() { return this.writeToNBT(new NBTTagCompound()); } @Override public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) { this.readFromNBT(pkt.getNbtCompound()); } @Override public void handleUpdateTag(NBTTagCompound tag) { this.readFromNBT(tag); } @Override public int getInventoryStackLimit() { return 64; } @Override public String getGuiID() { return "paladrillmod:paladiumcrusher"; } @Override public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) { return new GuiPaladiumCrusherGUI.GuiContainerMod(this.getWorld(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), playerIn); } @Override protected NonNullList getItems() { return this.stacks; } } }