2015-03-11 21:34:37 +01:00
|
|
|
package com.pahimar.ee3.tileentity;
|
|
|
|
|
2016-05-23 23:47:31 +02:00
|
|
|
import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy;
|
2015-05-07 19:45:06 +02:00
|
|
|
import com.pahimar.ee3.api.exchange.EnergyValue;
|
|
|
|
import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy;
|
2015-03-19 04:33:09 +01:00
|
|
|
import com.pahimar.ee3.block.BlockAshInfusedStoneSlab;
|
2015-11-28 05:07:45 +01:00
|
|
|
import com.pahimar.ee3.item.ItemAlchenomicon;
|
2015-03-19 02:52:53 +01:00
|
|
|
import com.pahimar.ee3.item.ItemMiniumStone;
|
|
|
|
import com.pahimar.ee3.item.ItemPhilosophersStone;
|
2016-05-30 04:10:32 +02:00
|
|
|
import com.pahimar.ee3.knowledge.PlayerKnowledge;
|
2015-03-19 02:52:53 +01:00
|
|
|
import com.pahimar.ee3.network.PacketHandler;
|
|
|
|
import com.pahimar.ee3.network.message.MessageTileEntityTransmutationTablet;
|
|
|
|
import com.pahimar.ee3.reference.Names;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-03-22 03:02:35 +01:00
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
2015-03-19 02:52:53 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
import net.minecraft.network.Packet;
|
|
|
|
import net.minecraft.util.AxisAlignedBB;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class TileEntityTransmutationTablet extends TileEntityEE implements ISidedInventory {
|
|
|
|
|
2015-04-02 07:26:01 +02:00
|
|
|
public static final int INVENTORY_SIZE = 10;
|
2015-03-19 02:52:53 +01:00
|
|
|
public static final int ITEM_INPUT_1 = 0;
|
|
|
|
public static final int ITEM_INPUT_2 = 1;
|
|
|
|
public static final int ITEM_INPUT_3 = 2;
|
|
|
|
public static final int ITEM_INPUT_4 = 3;
|
|
|
|
public static final int ITEM_INPUT_5 = 4;
|
|
|
|
public static final int ITEM_INPUT_6 = 5;
|
|
|
|
public static final int ITEM_INPUT_7 = 6;
|
|
|
|
public static final int ITEM_INPUT_8 = 7;
|
|
|
|
public static final int STONE_INDEX = 8;
|
2015-11-28 05:07:45 +01:00
|
|
|
public static final int ALCHENOMICON_INDEX = 9;
|
2015-03-19 02:52:53 +01:00
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
private EnergyValue storedEnergy, availableEnergy;
|
2015-03-19 02:52:53 +01:00
|
|
|
private ForgeDirection rotation;
|
|
|
|
private ItemStack[] inventory;
|
2016-05-30 04:10:32 +02:00
|
|
|
public PlayerKnowledge playerKnowledge;
|
|
|
|
|
|
|
|
public TileEntityTransmutationTablet() {
|
2015-03-19 02:52:53 +01:00
|
|
|
|
|
|
|
super();
|
|
|
|
rotation = ForgeDirection.UNKNOWN;
|
2016-05-30 04:10:32 +02:00
|
|
|
availableEnergy = new EnergyValue(0);
|
|
|
|
storedEnergy = new EnergyValue(0);
|
2015-03-19 02:52:53 +01:00
|
|
|
inventory = new ItemStack[INVENTORY_SIZE];
|
|
|
|
}
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
public EnergyValue getAvailableEnergy()
|
2015-03-19 02:52:53 +01:00
|
|
|
{
|
2016-05-30 04:10:32 +02:00
|
|
|
return availableEnergy;
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
public EnergyValue getStoredEnergy()
|
2015-03-19 02:52:53 +01:00
|
|
|
{
|
2016-05-30 04:10:32 +02:00
|
|
|
return storedEnergy;
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
public ForgeDirection getRotation() {
|
|
|
|
return rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRotation(ForgeDirection rotation) {
|
|
|
|
this.rotation = rotation;
|
|
|
|
}
|
2016-05-23 23:47:31 +02:00
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
public Set<ItemStack> getPlayerKnowledge() {
|
|
|
|
|
|
|
|
if (playerKnowledge != null) {
|
|
|
|
return playerKnowledge.getKnownItemStacks();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Collections.emptySet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handlePlayerKnowledgeUpdate(PlayerKnowledge playerKnowledge) {
|
|
|
|
this.playerKnowledge = playerKnowledge;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void consumeInventoryForEnergyValue(ItemStack outputItemStack) {
|
|
|
|
|
|
|
|
EnergyValue outputEnergyValue = EnergyValueRegistryProxy.getEnergyValueForStack(outputItemStack);
|
2015-04-21 03:16:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Algorithm:
|
|
|
|
*
|
|
|
|
* 1) Check the Stone slot, and attempt to take EMC out of the stone there (til 0)
|
|
|
|
* 2) Search the inventory for items that will most make up the difference, decrement them and consume their EMC
|
|
|
|
* 3) Repeat 2 until Stored EMC > outputItemStack EMC
|
|
|
|
* 4) Profit
|
|
|
|
*/
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
if (this.storedEnergy.compareTo(outputEnergyValue) >= 0) {
|
|
|
|
this.storedEnergy = new EnergyValue(this.storedEnergy.getValue() - outputEnergyValue.getValue());
|
2015-04-21 03:16:48 +02:00
|
|
|
}
|
2016-05-30 04:10:32 +02:00
|
|
|
else {
|
|
|
|
|
|
|
|
while (this.storedEnergy.compareTo(outputEnergyValue) < 0 && this.availableEnergy.compareTo(outputEnergyValue) >= 0) {
|
|
|
|
|
|
|
|
for (int i = 0; i < STONE_INDEX; i++) {
|
|
|
|
|
2015-05-01 20:21:59 +02:00
|
|
|
ItemStack stackInSlot = getStackInSlot(i);
|
2016-05-30 04:10:32 +02:00
|
|
|
if (stackInSlot != null && EnergyValueRegistryProxy.hasEnergyValue(stackInSlot)) {
|
|
|
|
this.storedEnergy = new EnergyValue(this.storedEnergy.getValue() + EnergyValueRegistryProxy.getEnergyValue(stackInSlot).getValue());
|
2015-05-01 20:21:59 +02:00
|
|
|
decrStackSize(i, 1);
|
|
|
|
}
|
2015-04-21 03:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
if (this.storedEnergy.getValue() >= outputEnergyValue.getValue()) {
|
|
|
|
this.storedEnergy = new EnergyValue(this.storedEnergy.getValue() - outputEnergyValue.getValue());
|
2015-04-21 03:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateEnergyValueFromInventory();
|
|
|
|
}
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
public void updateEnergyValueFromInventory() {
|
|
|
|
|
|
|
|
float newEnergyValue = storedEnergy.getValue();
|
|
|
|
for (int i = 0; i <= STONE_INDEX; i++) {
|
|
|
|
if (inventory[i] != null && EnergyValueRegistryProxy.hasEnergyValue(inventory[i])) {
|
2015-04-21 03:16:48 +02:00
|
|
|
newEnergyValue += EnergyValueRegistryProxy.getEnergyValueForStack(inventory[i]).getValue();
|
2015-04-02 07:26:01 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-30 04:10:32 +02:00
|
|
|
this.availableEnergy = new EnergyValue(newEnergyValue);
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2016-05-30 04:10:32 +02:00
|
|
|
public AxisAlignedBB getRenderBoundingBox() {
|
2015-03-19 02:52:53 +01:00
|
|
|
return AxisAlignedBB.getBoundingBox(xCoord - 1.5d, yCoord - 1, zCoord - 1.5d, xCoord + 1.5d, yCoord + 1, zCoord + 1.5d);
|
|
|
|
}
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
public boolean isStructureValid() {
|
|
|
|
|
2015-03-19 04:33:09 +01:00
|
|
|
return ((worldObj.getBlock(xCoord - 1, yCoord, zCoord - 1) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord - 1, yCoord, zCoord - 1) == 1) &&
|
|
|
|
(worldObj.getBlock(xCoord, yCoord, zCoord - 1) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord, yCoord, zCoord - 1) == 2) &&
|
|
|
|
(worldObj.getBlock(xCoord + 1, yCoord, zCoord - 1) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord + 1, yCoord, zCoord - 1) == 3) &&
|
|
|
|
(worldObj.getBlock(xCoord - 1, yCoord, zCoord) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord - 1, yCoord, zCoord) == 4) &&
|
|
|
|
(worldObj.getBlock(xCoord + 1, yCoord, zCoord) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord + 1, yCoord, zCoord) == 5) &&
|
|
|
|
(worldObj.getBlock(xCoord - 1, yCoord, zCoord + 1) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord - 1, yCoord, zCoord + 1) == 6) &&
|
|
|
|
(worldObj.getBlock(xCoord, yCoord, zCoord + 1) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord, yCoord, zCoord + 1) == 7) &&
|
|
|
|
(worldObj.getBlock(xCoord + 1, yCoord, zCoord + 1) instanceof BlockAshInfusedStoneSlab && worldObj.getBlockMetadata(xCoord + 1, yCoord, zCoord + 1) == 8));
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:52:53 +01:00
|
|
|
@Override
|
2016-05-30 04:10:32 +02:00
|
|
|
public void updateEntity() {
|
|
|
|
|
2015-03-19 02:52:53 +01:00
|
|
|
super.updateEntity();
|
2015-04-02 07:26:01 +02:00
|
|
|
updateEnergyValueFromInventory();
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-30 04:10:32 +02:00
|
|
|
public Packet getDescriptionPacket() {
|
2015-03-19 02:52:53 +01:00
|
|
|
return PacketHandler.INSTANCE.getPacketFrom(new MessageTileEntityTransmutationTablet(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-30 04:10:32 +02:00
|
|
|
public void readFromNBT(NBTTagCompound nbtTagCompound) {
|
|
|
|
|
2015-03-19 02:52:53 +01:00
|
|
|
super.readFromNBT(nbtTagCompound);
|
|
|
|
rotation = ForgeDirection.getOrientation(nbtTagCompound.getInteger("rotation"));
|
|
|
|
|
|
|
|
// Read in the ItemStacks in the inventory from NBT
|
|
|
|
NBTTagList tagList = nbtTagCompound.getTagList(Names.NBT.ITEMS, 10);
|
|
|
|
inventory = new ItemStack[this.getSizeInventory()];
|
2016-05-30 04:10:32 +02:00
|
|
|
for (int i = 0; i < tagList.tagCount(); ++i) {
|
2015-03-19 02:52:53 +01:00
|
|
|
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
|
|
|
|
byte slotIndex = tagCompound.getByte("Slot");
|
2016-05-30 04:10:32 +02:00
|
|
|
if (slotIndex >= 0 && slotIndex < inventory.length) {
|
2015-03-19 02:52:53 +01:00
|
|
|
inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 04:10:32 +02:00
|
|
|
NBTTagCompound energyValueTagCompound = nbtTagCompound.getCompoundTag("storedEnergy");
|
|
|
|
if (!energyValueTagCompound.hasNoTags()) {
|
|
|
|
storedEnergy = EnergyValue.loadEnergyValueFromNBT(energyValueTagCompound);
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
2016-05-30 04:10:32 +02:00
|
|
|
else {
|
|
|
|
storedEnergy = new EnergyValue(0);
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-30 04:10:32 +02:00
|
|
|
public void writeToNBT(NBTTagCompound nbtTagCompound) {
|
|
|
|
|
2015-03-19 02:52:53 +01:00
|
|
|
super.writeToNBT(nbtTagCompound);
|
|
|
|
nbtTagCompound.setInteger("rotation", rotation.ordinal());
|
|
|
|
|
|
|
|
// Write the ItemStacks in the inventory to NBT
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
2016-05-30 04:10:32 +02:00
|
|
|
for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) {
|
|
|
|
if (inventory[currentIndex] != null) {
|
2015-03-19 02:52:53 +01:00
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
|
|
|
tagCompound.setByte("Slot", (byte) currentIndex);
|
|
|
|
inventory[currentIndex].writeToNBT(tagCompound);
|
|
|
|
tagList.appendTag(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nbtTagCompound.setTag(Names.NBT.ITEMS, tagList);
|
|
|
|
|
|
|
|
NBTTagCompound energyValueTagCompound = new NBTTagCompound();
|
2016-05-30 04:10:32 +02:00
|
|
|
if (storedEnergy != null) {
|
|
|
|
storedEnergy.writeToNBT(energyValueTagCompound);
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
2016-05-30 04:10:32 +02:00
|
|
|
nbtTagCompound.setTag("storedEnergy", energyValueTagCompound);
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory()
|
|
|
|
{
|
|
|
|
return inventory.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int slotIndex)
|
|
|
|
{
|
|
|
|
return inventory[slotIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int slotIndex, int decrementAmount)
|
|
|
|
{
|
|
|
|
ItemStack itemStack = getStackInSlot(slotIndex);
|
|
|
|
if (itemStack != null)
|
|
|
|
{
|
|
|
|
if (itemStack.stackSize <= decrementAmount)
|
|
|
|
{
|
|
|
|
setInventorySlotContents(slotIndex, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemStack = itemStack.splitStack(decrementAmount);
|
|
|
|
if (itemStack.stackSize == 0)
|
|
|
|
{
|
|
|
|
setInventorySlotContents(slotIndex, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlotOnClosing(int slotIndex)
|
|
|
|
{
|
|
|
|
ItemStack itemStack = getStackInSlot(slotIndex);
|
|
|
|
if (itemStack != null)
|
|
|
|
{
|
|
|
|
setInventorySlotContents(slotIndex, null);
|
|
|
|
}
|
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int slotIndex, ItemStack itemStack)
|
|
|
|
{
|
2015-03-23 16:07:47 +01:00
|
|
|
if (slotIndex < getSizeInventory())
|
2015-03-19 02:52:53 +01:00
|
|
|
{
|
2015-03-23 16:07:47 +01:00
|
|
|
inventory[slotIndex] = itemStack;
|
|
|
|
if (itemStack != null && itemStack.stackSize > getInventoryStackLimit())
|
|
|
|
{
|
|
|
|
itemStack.stackSize = getInventoryStackLimit();
|
|
|
|
}
|
2015-03-19 02:52:53 +01:00
|
|
|
|
2015-04-02 07:26:01 +02:00
|
|
|
updateEnergyValueFromInventory();
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getInventoryName()
|
|
|
|
{
|
|
|
|
return this.hasCustomName() ? this.getCustomName() : Names.Containers.TRANSMUTATION_TABLET;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCustomInventoryName()
|
|
|
|
{
|
|
|
|
return this.hasCustomName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUseableByPlayer(EntityPlayer entityPlayer)
|
|
|
|
{
|
2016-05-30 16:53:39 +02:00
|
|
|
return this.worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && isStructureValid() && entityPlayer.getDistanceSq((double) xCoord + 0.5D, (double) yCoord + 0.5D, (double) zCoord + 0.5D) <= 64D;
|
2015-03-19 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void openInventory()
|
|
|
|
{
|
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeInventory()
|
|
|
|
{
|
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack)
|
|
|
|
{
|
2016-05-23 23:47:31 +02:00
|
|
|
if (slotIndex < STONE_INDEX && EnergyValueRegistryProxy.hasEnergyValue(itemStack) && BlacklistRegistryProxy.isExchangeable(itemStack))
|
2015-03-19 02:52:53 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (slotIndex == STONE_INDEX && (itemStack.getItem() instanceof ItemMiniumStone || itemStack.getItem() instanceof ItemPhilosophersStone))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-28 05:07:45 +01:00
|
|
|
else if (slotIndex == ALCHENOMICON_INDEX && itemStack.getItem() instanceof ItemAlchenomicon)
|
2015-03-19 02:52:53 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-22 03:02:35 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getAccessibleSlotsFromSide(int slotIndex)
|
|
|
|
{
|
|
|
|
return new int[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slotIndex, ItemStack itemStack, int side)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slotIndex, ItemStack itemStack, int side)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-11 21:34:37 +01:00
|
|
|
}
|