Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TileEntityMyBlock extends TileEntity {
- public static final int SIZE = 3;
- public boolean previousRedstoneState;
- private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
- @Override
- protected void onContentsChanged(int slot) {
- sendUpdates();
- }
- };
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- if (compound.hasKey("ITEMS")) {
- itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("ITEMS"));
- }
- if (compound.hasKey("previousRedstoneState")) {
- previousRedstoneState = compound.getBoolean("previousRedstoneState");
- }
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- compound.setTag("ITEMS", itemStackHandler.serializeNBT());
- compound.setBoolean("previousRedstoneState", previousRedstoneState);
- return compound;
- }
- @Nullable
- @Override
- public SPacketUpdateTileEntity getUpdatePacket() {
- return new SPacketUpdateTileEntity(getPos(), getBlockMetadata(), getUpdateTag());
- }
- @Override
- public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
- handleUpdateTag(pkt.getNbtCompound());
- }
- @Override
- public NBTTagCompound getUpdateTag() {
- return new NBTTagCompound();
- }
- @Override
- public void handleUpdateTag(NBTTagCompound tag) {
- this.readFromNBT(tag);
- }
- public boolean canInteractWith(EntityPlayer playerIn) {
- return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
- }
- @Override
- public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
- if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- return true;
- }
- return super.hasCapability(capability, facing);
- }
- @Override
- public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
- if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemStackHandler);
- }
- return super.getCapability(capability, facing);
- }
- public ItemStack getStackInSlot(int slot) {
- return itemStackHandler.getStackInSlot(slot);
- }
- public boolean isActivated() {
- IBlockState state = world.getBlockState(pos);
- if (state.getBlock() == ModBlocks.MY_BLOCK) {
- return state.getValue(MyBlock.ACTIVATED);
- }
- MyMod.LOGGER.info(pos.getX() + " " + pos.getY() + " " + pos.getZ() + " " + state.getBlock().getUnlocalizedName()); // the debug from before
- return false;
- }
- private void sendUpdates() {
- world.markBlockRangeForRenderUpdate(pos, pos);
- world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
- world.scheduleBlockUpdate(pos, this.getBlockType(),0,0);
- markDirty();
- }
- @Override
- public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
- return (oldState.getBlock() != newState.getBlock());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement