package blusunrize.immersiveengineering.common.blocks.wooden; import blusunrize.immersiveengineering.api.Lib; import blusunrize.immersiveengineering.common.IEContent; import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IComparatorOverride; import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IGuiTile; import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.ITileDrop; import blusunrize.immersiveengineering.common.blocks.TileEntityIEBase; import blusunrize.immersiveengineering.common.util.Utils; import blusunrize.immersiveengineering.common.util.inventory.IEInventoryHandler; import blusunrize.immersiveengineering.common.util.inventory.IIEInventory; import com.google.common.collect.Lists; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.WorldServer; import net.minecraft.world.storage.loot.ILootContainer; import net.minecraft.world.storage.loot.LootContext; import net.minecraft.world.storage.loot.LootTable; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.oredict.OreDictionary; import javax.annotation.Nullable; import java.util.Collections; import java.util.List; import java.util.Random; public class TileEntityWoodenCrate extends TileEntityIEBase implements IIEInventory, IGuiTile, ITileDrop, IComparatorOverride, ILootContainer { ItemStack[] inventory = new ItemStack[27]; public ResourceLocation lootTable; public String name; @Override public void readCustomNBT(NBTTagCompound nbt, boolean descPacket) { if(nbt.hasKey("name")) this.name = nbt.getString("name"); if(!descPacket) { if(nbt.hasKey("lootTable", 8)) this.lootTable = new ResourceLocation(nbt.getString("lootTable")); else inventory = Utils.readInventory(nbt.getTagList("inventory", 10), 27); } } @Override public void writeCustomNBT(NBTTagCompound nbt, boolean descPacket) { if(this.name!=null) nbt.setString("name", this.name); if(!descPacket) { if(lootTable!=null) nbt.setString("lootTable",lootTable.toString()); else writeInv(nbt, false); } } public void writeInv(NBTTagCompound nbt, boolean toItem) { boolean write = false; NBTTagList invList = new NBTTagList(); for(int i=0; i<this.inventory.length; i++) if(this.inventory[i] != null) { if(toItem) write = true; NBTTagCompound itemTag = new NBTTagCompound(); itemTag.setByte("Slot", (byte)i); this.inventory[i].writeToNBT(itemTag); invList.appendTag(itemTag); } if(!toItem || write) nbt.setTag("inventory", invList); } @Override @Nullable public ITextComponent getDisplayName() { return name!=null?new TextComponentString(name) : new TextComponentTranslation(getBlockMetadata()==0?"tile.immersiveengineering.woodenDevice0.crate.name":"tile.immersiveengineering.woodenDevice0.reinforced_crate.name"); } @Override public boolean canOpenGui() { return true; } @Override public int getGuiID() { return Lib.GUIID_WoodenCrate; } @Override public TileEntity getGuiMaster() { return this; } @Override public void onGuiOpened(EntityPlayer player, boolean clientside) { if(this.lootTable!=null && !clientside) { LootTable loottable = this.worldObj.getLootTableManager().getLootTableFromLocation(this.lootTable); this.lootTable = null; Random random; LootContext.Builder contextBuilder = new LootContext.Builder((WorldServer)this.worldObj); if(player != null) contextBuilder.withLuck(player.getLuck()); LootContext context = contextBuilder.build(); Random rand = new Random(); List<ItemStack> list = loottable.generateLootForPools(rand, context); List<Integer> listSlots = Lists.newArrayList(); for(int i=0; i<inventory.length; i++) if(inventory[i]==null) listSlots.add(Integer.valueOf(i)); Collections.shuffle(listSlots, rand); if(listSlots.isEmpty()) return; Utils.shuffleLootItems(list, listSlots.size(), rand); for(ItemStack itemstack : list) { int slot = listSlots.remove(listSlots.size() - 1).intValue(); inventory[slot] = itemstack; } } } @Override public ItemStack[] getInventory() { return inventory; } @Override public boolean isStackValid(int slot, ItemStack stack) { if(stack!=null) { if(OreDictionary.itemMatches(new ItemStack(IEContent.blockWoodenDevice0, 1, 0), stack, true)) return false; if(OreDictionary.itemMatches(new ItemStack(IEContent.blockWoodenDevice0, 1, 5), stack, true)) return false; } return true; } @Override public int getSlotLimit(int slot) { return 64; } @Override public void doGraphicalUpdates(int slot) { this.markDirty(); } @Override public ItemStack getTileDrop(EntityPlayer player, IBlockState state) { ItemStack stack = new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state)); NBTTagCompound tag = new NBTTagCompound(); writeInv(tag, true); if(!tag.hasNoTags()) stack.setTagCompound(tag); if(this.name!=null) stack.setStackDisplayName(this.name); return stack; } @Override public void readOnPlacement(EntityLivingBase placer, ItemStack stack) { if(stack.hasTagCompound()) { readCustomNBT(stack.getTagCompound(), false); if(stack.hasDisplayName()) this.name = stack.getDisplayName(); } } @Override public boolean preventInventoryDrop() { return true; } @Override public int getComparatorInputOverride() { return Utils.calcRedstoneFromInventory(this); } @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { if(capability==CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return true; return super.hasCapability(capability, facing); } IItemHandler insertionHandler = new IEInventoryHandler(27,this); @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { if(capability==CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (T)insertionHandler; return super.getCapability(capability, facing); } @Override public ResourceLocation getLootTable() { return this.lootTable; } }