Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.hsyyid.wilsonsmp.tileentities;
- import io.github.hsyyid.wilsonsmp.WilsonSMP;
- import io.github.hsyyid.wilsonsmp.containers.ContainerReadableBookshelf;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.InventoryBasic;
- import net.minecraft.item.ItemEditableBook;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.Packet;
- import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraftforge.common.util.Constants;
- import net.minecraftforge.fml.common.network.internal.FMLNetworkHandler;
- public class TileEntityReadableBookshelf extends TileEntity
- {
- public ItemEditableBook book;
- public IInventory inventory;
- public Container container;
- public TileEntityReadableBookshelf()
- {
- super();
- this.inventory = new InventoryBasic("container.readablebookshelf.slots", true, 1);
- }
- public Container getNewContainer(EntityPlayer player)
- {
- return (container = new ContainerReadableBookshelf(player, this));
- }
- private void readInventoryFromNBT(NBTTagCompound nbt, IInventory inventory)
- {
- if (nbt != null && inventory != null && inventory.getName() != null)
- {
- NBTTagList items = nbt.getTagList(inventory.getName(), Constants.NBT.TAG_COMPOUND);
- for (byte x = 0; x < items.tagCount(); x++)
- {
- NBTTagCompound item = items.getCompoundTagAt(x);
- byte slot = item.getByte("Slot");
- if (slot >= 0 && slot <= inventory.getSizeInventory())
- {
- inventory.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
- }
- }
- }
- }
- @Override
- public Packet getDescriptionPacket()
- {
- NBTTagCompound nbtTag = new NBTTagCompound();
- this.writeToNBT(nbtTag);
- return new S35PacketUpdateTileEntity(this.pos, 1, nbtTag);
- }
- @Override
- public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
- {
- readFromNBT(packet.getNbtCompound());
- }
- @Override
- public void writeToNBT(NBTTagCompound nbt)
- {
- super.writeToNBT(nbt);
- if (this.inventory != null)
- {
- this.saveInventoryToNBT(nbt, inventory);
- }
- }
- private void saveInventoryToNBT(NBTTagCompound nbt, IInventory inventory)
- {
- NBTTagList items = new NBTTagList();
- for (byte x = 0; x < inventory.getSizeInventory(); x++)
- {
- ItemStack stack = inventory.getStackInSlot(x);
- if (stack != null)
- {
- NBTTagCompound item = new NBTTagCompound();
- item.setByte("Slot", x);
- stack.writeToNBT(item);
- items.appendTag(item);
- }
- }
- nbt.setTag(inventory.getName(), items);
- }
- @Override
- public void readFromNBT(NBTTagCompound nbt)
- {
- super.readFromNBT(nbt);
- this.readInventoryFromNBT(nbt, this.inventory);
- }
- public void openGui(EntityPlayer playerIn)
- {
- if (this.inventory.getStackInSlot(0) != null && this.inventory.getStackInSlot(0).getItem().equals(Items.written_book))
- {
- System.out.println("Opening book");
- playerIn.displayGUIBook(this.inventory.getStackInSlot(0));
- }
- else
- {
- System.out.println("Opening inventory");
- if (!playerIn.worldObj.isRemote)
- {
- FMLNetworkHandler.openGui(playerIn, WilsonSMP.instance(), 1, playerIn.worldObj, this.pos.getX(), this.pos.getY(), this.pos.getZ());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement