Created
March 26, 2017 16:46
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
package HQBanana.tutorial.blocks; | |
import java.util.List; | |
import HQBanana.tutorial.Reference; | |
import HQBanana.tutorial.Tutorial; | |
import HQBanana.tutorial.init.ModItems; | |
import HQBanana.tutorial.tileentity.TileEntityJar; | |
import net.minecraft.block.Block; | |
import net.minecraft.block.ITileEntityProvider; | |
import net.minecraft.block.material.Material; | |
import net.minecraft.block.state.IBlockState; | |
import net.minecraft.creativetab.CreativeTabs; | |
import net.minecraft.entity.Entity; | |
import net.minecraft.entity.item.EntityItem; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.tileentity.TileEntity; | |
import net.minecraft.util.BlockRenderLayer; | |
import net.minecraft.util.EnumFacing; | |
import net.minecraft.util.EnumHand; | |
import net.minecraft.util.math.AxisAlignedBB; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.world.IBlockAccess; | |
import net.minecraft.world.World; | |
import net.minecraftforge.items.ItemHandlerHelper; | |
import scala.Console; | |
public class BlockJar extends Block {//implements ITileEntityProvider{ | |
private static final float PIXEL_COUNT = 1.0f / 16.0f; | |
private static final AxisAlignedBB BOUNDING_BOX = new AxisAlignedBB(PIXEL_COUNT * 4, 0, PIXEL_COUNT * 4, PIXEL_COUNT * 12, PIXEL_COUNT * 11, PIXEL_COUNT * 12); | |
public BlockJar() { | |
super(Material.ROCK); | |
setUnlocalizedName(Reference.TutorialBlocks.JAR.getUnlocalizedName()); | |
setRegistryName(Reference.TutorialBlocks.JAR.getRegistryName()); | |
setCreativeTab(Tutorial.CREATIVE_TAB); | |
} | |
@Override | |
public boolean isFullCube(IBlockState _pState) { | |
return false; | |
} | |
@Override | |
public boolean isOpaqueCube(IBlockState _pState) { | |
return false; | |
} | |
@Override | |
public BlockRenderLayer getBlockLayer() { | |
return BlockRenderLayer.TRANSLUCENT; | |
} | |
@Override | |
public AxisAlignedBB getBoundingBox(IBlockState _pState, IBlockAccess _pSource, BlockPos _pPos) { | |
return BOUNDING_BOX; | |
} | |
@Override | |
public void addCollisionBoxToList(IBlockState _pState, World _pWorldIn, BlockPos _pPos, AxisAlignedBB _pEntityBox, List<AxisAlignedBB> _pCollidingBoxes, Entity _pEntityIn) { | |
super.addCollisionBoxToList(_pPos, _pEntityBox, _pCollidingBoxes, BOUNDING_BOX); | |
} | |
@Override | |
public boolean onBlockActivated(World _pWorldIn, BlockPos _pPos, IBlockState _pState, EntityPlayer _pPlayerIn, EnumHand _pHand, ItemStack _pHeldItem, EnumFacing _pSide, float _pHitX, float _pHitY, float _pHitZ) { | |
if (_pWorldIn.isRemote == false){ | |
TileEntity tileEntity = _pWorldIn.getTileEntity(_pPos); | |
if (tileEntity instanceof TileEntityJar) { | |
TileEntityJar jar = (TileEntityJar)tileEntity; | |
int stackSize = _pHeldItem.stackSize; | |
_pHeldItem.stackSize = 1; | |
if (_pHeldItem != null) { | |
if ((ItemHandlerHelper.canItemStacksStack(_pHeldItem, jar.GetStoredItem())) || jar.GetItemCount() == 0) { | |
if(jar.AddItem(_pHeldItem)) { | |
_pHeldItem.stackSize = stackSize; | |
_pHeldItem.stackSize--; | |
return true; | |
} | |
} | |
} | |
jar.RemoveItem(); | |
} | |
} | |
return true; | |
} | |
@Override | |
public boolean hasTileEntity(IBlockState _pState) { | |
return true; | |
} | |
@Override | |
public TileEntity createTileEntity(World _pWorld, IBlockState _pBlockState) { | |
return new TileEntityJar(); | |
} | |
@Override | |
public void breakBlock(World _pWorldIn, BlockPos _pPos, IBlockState _pState) { | |
if(!_pWorldIn.isRemote) { | |
TileEntity tileEntity = _pWorldIn.getTileEntity(_pPos); | |
if(tileEntity instanceof TileEntityJar ) { | |
TileEntityJar jar = (TileEntityJar ) tileEntity; | |
ItemStack stack = jar.GetStoredItem(); | |
stack.stackSize = jar.GetItemCount(); | |
_pWorldIn.spawnEntityInWorld(new EntityItem(_pWorldIn, _pPos.getX() + 0.5, _pPos.getY() + 1.0, _pPos.getZ() + 0.5, stack)); | |
} | |
} | |
} | |
} |
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
package HQBanana.tutorial.tileentity; | |
import HQBanana.tutorial.init.ModItems; | |
import net.minecraft.block.state.IBlockState; | |
import net.minecraft.entity.item.EntityItem; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.network.NetworkManager; | |
import net.minecraft.network.Packet; | |
import net.minecraft.network.play.server.SPacketUpdateTileEntity; | |
import net.minecraft.tileentity.TileEntity; | |
import scala.Console; | |
public class TileEntityJar extends TileEntity { | |
public static final int MAX_ITEMS = 16; | |
private int _itemCount = 0; | |
private ItemStack _storedItemStack = null; | |
public boolean AddItem(ItemStack _pItem) { | |
if (_storedItemStack == null) { | |
_storedItemStack = _pItem; | |
} | |
if (_itemCount < MAX_ITEMS) { | |
Console.out().println("stack count: " + _pItem.stackSize); | |
_itemCount++; | |
markDirty(); | |
IBlockState state = worldObj.getBlockState(pos); | |
worldObj.notifyBlockUpdate(pos, state, state, 3); | |
Console.out().println("item count: " + _itemCount); | |
return true; | |
} | |
return false; | |
} | |
public void RemoveItem() { | |
if (_itemCount > 0) { | |
worldObj.spawnEntityInWorld(new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5, _storedItemStack));//, 1, _storedItem.getMetadata(new ItemStack(_storedItem)) | |
_itemCount--; | |
markDirty(); | |
IBlockState state = worldObj.getBlockState(pos); | |
worldObj.notifyBlockUpdate(pos, state, state, 3); | |
} else if (_itemCount == 0) { | |
_storedItemStack = null; | |
} | |
} | |
@Override | |
public NBTTagCompound writeToNBT(NBTTagCompound _pCompound) { | |
super.writeToNBT(_pCompound); | |
_pCompound.setInteger("Itemcount", this._itemCount); | |
_pCompound.setTag("Item", _storedItemStack.serializeNBT()); | |
return _pCompound; | |
} | |
@Override | |
public void readFromNBT(NBTTagCompound _pCompound) { | |
super.readFromNBT(_pCompound); | |
_itemCount = _pCompound.getInteger("Itemcount"); | |
_storedItemStack.deserializeNBT(_pCompound.getCompoundTag("Item")); | |
} | |
@Override | |
public void onDataPacket(NetworkManager _pNet, SPacketUpdateTileEntity _pPacket) { | |
NBTTagCompound tagCompound = _pPacket.getNbtCompound(); | |
ReadUpdateTag(tagCompound); | |
} | |
@Override | |
public SPacketUpdateTileEntity getUpdatePacket() { | |
NBTTagCompound tagCompound = new NBTTagCompound(); | |
WriteUpdateTag(tagCompound); | |
return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tagCompound); | |
} | |
@Override | |
public NBTTagCompound getUpdateTag() { | |
NBTTagCompound tagCompound = super.getUpdateTag(); | |
WriteUpdateTag(tagCompound); | |
return tagCompound; | |
} | |
public void WriteUpdateTag(NBTTagCompound _pTag) { | |
_pTag.setInteger("Itemcount", this._itemCount); | |
} | |
public void ReadUpdateTag(NBTTagCompound _pTag) { | |
_itemCount = _pTag.getInteger("Itemcount"); | |
} | |
public int GetItemCount() { | |
return _itemCount; | |
} | |
public ItemStack GetStoredItem() { | |
return _storedItemStack; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment