Hbm-s-Nuclear-Tech-GIT/com/hbm/blocks/TileEntityCoreAdvanced.java
2016-06-07 18:58:10 +02:00

645 lines
18 KiB
Java

package com.hbm.blocks;
import com.hbm.interfaces.IConsumer;
import com.hbm.interfaces.IFactory;
import com.hbm.items.ModItems;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public class TileEntityCoreAdvanced extends TileEntity implements ISidedInventory, IFactory, IConsumer {
public int progress = 0;
public int power = 0;
public int soundCycle = 0;
public final static int processTime = 100;
public final static int maxPower = ModItems.factory_core_advanced.getMaxDamage();
private ItemStack slots[];
private String customName;
public TileEntityCoreAdvanced() {
slots = new ItemStack[27];
}
@Override
public int getSizeInventory() {
return slots.length;
}
@Override
public ItemStack getStackInSlot(int i) {
return slots[i];
}
@Override
public ItemStack getStackInSlotOnClosing(int i) {
if(slots[i] != null)
{
ItemStack itemStack = slots[i];
slots[i] = null;
return itemStack;
} else {
return null;
}
}
@Override
public void setInventorySlotContents(int i, ItemStack itemStack) {
slots[i] = itemStack;
if(itemStack != null && itemStack.stackSize > getInventoryStackLimit())
{
itemStack.stackSize = getInventoryStackLimit();
}
}
@Override
public String getInventoryName() {
return this.hasCustomInventoryName() ? this.customName : "container.factoryAdvanced";
}
@Override
public boolean hasCustomInventoryName() {
return this.customName != null && this.customName.length() > 0;
}
public void setCustomName(String name) {
this.customName = name;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
if(worldObj.getTileEntity(xCoord, yCoord, zCoord) != this)
{
return false;
}else{
return player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <=64;
}
}
@Override
public void openInventory() {}
@Override
public void closeInventory() {}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemStack) {
return true;
}
@Override
public ItemStack decrStackSize(int i, int j) {
if(slots[i] != null)
{
if(slots[i].stackSize <= j)
{
ItemStack itemStack = slots[i];
slots[i] = null;
return itemStack;
}
ItemStack itemStack1 = slots[i].splitStack(j);
if (slots[i].stackSize == 0)
{
slots[i] = null;
}
return itemStack1;
} else {
return null;
}
}
@Override
public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
return null;
}
@Override
public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_, int p_102007_3_) {
return false;
}
@Override
public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_) {
return false;
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList list = nbt.getTagList("items", 10);
this.progress = nbt.getShort("cookTime");
slots = new ItemStack[getSizeInventory()];
for(int i = 0; i < list.tagCount(); i++)
{
NBTTagCompound nbt1 = list.getCompoundTagAt(i);
byte b0 = nbt1.getByte("slot");
if(b0 >= 0 && b0 < slots.length)
{
slots[b0] = ItemStack.loadItemStackFromNBT(nbt1);
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setShort("cookTime", (short) progress);
NBTTagList list = new NBTTagList();
for(int i = 0; i < slots.length; i++)
{
if(slots[i] != null)
{
NBTTagCompound nbt1 = new NBTTagCompound();
nbt1.setByte("slot", (byte)i);
slots[i].writeToNBT(nbt1);
list.appendTag(nbt1);
}
}
nbt.setTag("items", list);
}
@Override
public boolean isStructureValid(World world) {
if(world.getBlock(this.xCoord, this.yCoord, this.zCoord) == ModBlocks.factory_advanced_core &&
world.getBlock(this.xCoord - 1, this.yCoord - 1, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord, this.yCoord - 1, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord + 1, this.yCoord - 1, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord - 1, this.yCoord - 1, this.zCoord) == ModBlocks.factory_advanced_hull &&
(world.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) == ModBlocks.factory_advanced_conductor || world.getBlock(this.xCoord, this.yCoord - 1, this.zCoord) == ModBlocks.factory_advanced_hull) &&
world.getBlock(this.xCoord + 1, this.yCoord - 1, this.zCoord) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord - 1, this.yCoord - 1, this.zCoord + 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord, this.yCoord - 1, this.zCoord + 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord + 1, this.yCoord - 1, this.zCoord + 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord - 1, this.yCoord, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord, this.yCoord, this.zCoord - 1) == ModBlocks.factory_advanced_furnace &&
world.getBlock(this.xCoord + 1, this.yCoord, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord - 1, this.yCoord, this.zCoord) == ModBlocks.factory_advanced_furnace &&
world.getBlock(this.xCoord + 1, this.yCoord, this.zCoord) == ModBlocks.factory_advanced_furnace &&
world.getBlock(this.xCoord - 1, this.yCoord, this.zCoord + 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord, this.yCoord, this.zCoord + 1) == ModBlocks.factory_advanced_furnace &&
world.getBlock(this.xCoord + 1, this.yCoord, this.zCoord + 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord - 1, this.yCoord + 1, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord, this.yCoord + 1, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord + 1, this.yCoord + 1, this.zCoord - 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord - 1, this.yCoord + 1, this.zCoord) == ModBlocks.factory_advanced_hull &&
(world.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) == ModBlocks.factory_advanced_conductor || world.getBlock(this.xCoord, this.yCoord + 1, this.zCoord) == ModBlocks.factory_advanced_hull) &&
world.getBlock(this.xCoord + 1, this.yCoord + 1, this.zCoord) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord - 1, this.yCoord + 1, this.zCoord + 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord, this.yCoord + 1, this.zCoord + 1) == ModBlocks.factory_advanced_hull &&
world.getBlock(this.xCoord + 1, this.yCoord + 1, this.zCoord + 1) == ModBlocks.factory_advanced_hull)
{
return true;
}
return false;
}
public int getPowerScaled(int i) {
return (power * i) / maxPower;
}
public int getProgressScaled(int i) {
return (progress * i) / processTime;
}
public boolean isProcessable(ItemStack item) {
if(item != null)
{
return FurnaceRecipes.smelting().getSmeltingResult(item) != null;
} else {
return false;
}
}
@Override
public void updateEntity() {
if(this.slots[22] != null && this.slots[22].getItem() == ModItems.factory_core_advanced)
{
this.power = ModItems.factory_core_advanced.getMaxDamage() - this.slots[22].getItemDamage();
} else {
this.power = 0;
}
if(this.slots[9] == null)
{
for(int i = 0; i < 9; i++)
{
if(slots[i] != null && isProcessable(slots[i]))
{
slots[9] = slots[i].copy();
slots[i] = null;
break;
}
}
} else {
for(int i = 0; i < 9; i++)
{
if(slots[i] != null)
{
if(slots[i].getItem() == slots[9].getItem() && slots[i].getItemDamage() == slots[9].getItemDamage())
{
if(slots[9].stackSize + slots[i].stackSize <= slots[i].getMaxStackSize())
{
slots[9].stackSize += slots[i].stackSize;
slots[i] = null;
} else {
int j = slots[9].getMaxStackSize() - slots[9].stackSize;
slots[9].stackSize += j;
slots[i].stackSize -= j;
}
break;
}
}
}
}
if(this.slots[10] == null)
{
for(int i = 0; i < 9; i++)
{
if(slots[i] != null && isProcessable(slots[i]))
{
slots[10] = slots[i].copy();
slots[i] = null;
break;
}
}
} else {
for(int i = 0; i < 9; i++)
{
if(slots[i] != null)
{
if(slots[i].getItem() == slots[10].getItem() && slots[i].getItemDamage() == slots[10].getItemDamage())
{
if(slots[10].stackSize + slots[i].stackSize <= slots[i].getMaxStackSize())
{
slots[10].stackSize += slots[i].stackSize;
slots[i] = null;
} else {
int j = slots[10].getMaxStackSize() - slots[10].stackSize;
slots[10].stackSize += j;
slots[i].stackSize -= j;
}
break;
}
}
}
}
if(this.slots[23] == null)
{
for(int i = 0; i < 9; i++)
{
if(slots[i] != null && isProcessable(slots[i]))
{
slots[23] = slots[i].copy();
slots[i] = null;
break;
}
}
} else {
for(int i = 0; i < 9; i++)
{
if(slots[i] != null)
{
if(slots[i].getItem() == slots[23].getItem() && slots[i].getItemDamage() == slots[23].getItemDamage())
{
if(slots[23].stackSize + slots[i].stackSize <= slots[i].getMaxStackSize())
{
slots[23].stackSize += slots[i].stackSize;
slots[i] = null;
} else {
int j = slots[23].getMaxStackSize() - slots[23].stackSize;
slots[23].stackSize += j;
slots[i].stackSize -= j;
}
break;
}
}
}
}
if(this.slots[24] == null)
{
for(int i = 0; i < 9; i++)
{
if(slots[i] != null && isProcessable(slots[i]))
{
slots[24] = slots[i].copy();
slots[i] = null;
break;
}
}
} else {
for(int i = 0; i < 9; i++)
{
if(slots[i] != null)
{
if(slots[i].getItem() == slots[24].getItem() && slots[i].getItemDamage() == slots[24].getItemDamage())
{
if(slots[24].stackSize + slots[i].stackSize <= slots[i].getMaxStackSize())
{
slots[24].stackSize += slots[i].stackSize;
slots[i] = null;
} else {
int j = slots[24].getMaxStackSize() - slots[24].stackSize;
slots[24].stackSize += j;
slots[i].stackSize -= j;
}
break;
}
}
}
}
if(this.power > 0 && (isProcessable(slots[9]) || isProcessable(slots[10]) || isProcessable(slots[23]) || isProcessable(slots[24])) && isStructureValid(worldObj))
{
this.progress += 1;
this.slots[22].setItemDamage(this.slots[22].getItemDamage() + 1);
if(soundCycle == 0)
this.worldObj.playSoundEffect(this.xCoord, this.yCoord, this.zCoord, "minecart.base", 1.0F, 0.75F);
soundCycle++;
if(soundCycle >= 50)
soundCycle = 0;
} else {
this.progress = 0;
}
if(slots[9] != null && slots[11] != null && (FurnaceRecipes.smelting().getSmeltingResult(slots[9]).getItem() != slots[11].getItem() || FurnaceRecipes.smelting().getSmeltingResult(slots[9]).getItemDamage() != slots[11].getItemDamage()))
{
this.progress = 0;
}
if(slots[10] != null && slots[12] != null && (FurnaceRecipes.smelting().getSmeltingResult(slots[10]).getItem() != slots[12].getItem() || FurnaceRecipes.smelting().getSmeltingResult(slots[10]).getItemDamage() != slots[12].getItemDamage()))
{
this.progress = 0;
}
if(slots[23] != null && slots[25] != null && (FurnaceRecipes.smelting().getSmeltingResult(slots[23]).getItem() != slots[25].getItem() || FurnaceRecipes.smelting().getSmeltingResult(slots[23]).getItemDamage() != slots[25].getItemDamage()))
{
this.progress = 0;
}
if(slots[24] != null && slots[26] != null && (FurnaceRecipes.smelting().getSmeltingResult(slots[24]).getItem() != slots[26].getItem() || FurnaceRecipes.smelting().getSmeltingResult(slots[24]).getItemDamage() != slots[26].getItemDamage()))
{
this.progress = 0;
}
if(this.progress >= this.processTime)
{
if(this.slots[9] != null && isProcessable(this.slots[9]))
{
ItemStack itemStack = FurnaceRecipes.smelting().getSmeltingResult(slots[9]);
if(slots[11] == null)
{
slots[11] = itemStack.copy();
}else if(slots[11].isItemEqual(itemStack)) {
slots[11].stackSize += itemStack.stackSize;
}
if(slots[9].stackSize <= 0)
{
slots[9] = new ItemStack(slots[9].getItem().setFull3D());
}else{
slots[9].stackSize--;
}
if(slots[9].stackSize <= 0)
{
slots[9] = null;
}
}
if(this.slots[10] != null && isProcessable(this.slots[10]))
{
ItemStack itemStack = FurnaceRecipes.smelting().getSmeltingResult(slots[10]);
if(slots[12] == null)
{
slots[12] = itemStack.copy();
}else if(slots[12].isItemEqual(itemStack)) {
slots[12].stackSize += itemStack.stackSize;
}
if(slots[10].stackSize <= 0)
{
slots[10] = new ItemStack(slots[10].getItem().setFull3D());
}else{
slots[10].stackSize--;
}
if(slots[10].stackSize <= 0)
{
slots[10] = null;
}
}
if(this.slots[23] != null && isProcessable(this.slots[23]))
{
ItemStack itemStack = FurnaceRecipes.smelting().getSmeltingResult(slots[23]);
if(slots[25] == null)
{
slots[25] = itemStack.copy();
}else if(slots[25].isItemEqual(itemStack)) {
slots[25].stackSize += itemStack.stackSize;
}
if(slots[23].stackSize <= 0)
{
slots[23] = new ItemStack(slots[23].getItem().setFull3D());
}else{
slots[23].stackSize--;
}
if(slots[23].stackSize <= 0)
{
slots[23] = null;
}
}
if(this.slots[24] != null && isProcessable(this.slots[24]))
{
ItemStack itemStack = FurnaceRecipes.smelting().getSmeltingResult(slots[24]);
if(slots[26] == null)
{
slots[26] = itemStack.copy();
}else if(slots[26].isItemEqual(itemStack)) {
slots[26].stackSize += itemStack.stackSize;
}
if(slots[24].stackSize <= 0)
{
slots[24] = new ItemStack(slots[24].getItem().setFull3D());
}else{
slots[24].stackSize--;
}
if(slots[24].stackSize <= 0)
{
slots[24] = null;
}
}
this.progress = 0;
}
if(this.slots[11] != null)
{
for(int i = 0; i < 9; i++)
{
int j = i + 13;
if(slots[j] != null)
{
if(slots[j].getItem() == slots[11].getItem() && slots[j].getItemDamage() == slots[11].getItemDamage())
{
if(slots[j].stackSize < slots[j].getMaxStackSize())
{
if(slots[j].stackSize + slots[11].stackSize <= slots[11].getMaxStackSize())
{
slots[j].stackSize += slots[11].stackSize;
slots[11] = null;
break;
} else {
int k = slots[j].getMaxStackSize() - slots[j].stackSize;
if(k < 0)
{
slots[j].stackSize += k;
slots[11].stackSize -= k;
break;
}
}
}
}
} else {
slots[j] = slots[11].copy();
slots[11] = null;
break;
}
}
}
if(this.slots[12] != null)
{
for(int i = 0; i < 9; i++)
{
int j = i + 13;
if(slots[j] != null)
{
if(slots[j].getItem() == slots[12].getItem() && slots[j].getItemDamage() == slots[12].getItemDamage())
{
if(slots[j].stackSize < slots[j].getMaxStackSize())
{
if(slots[j].stackSize + slots[12].stackSize <= slots[12].getMaxStackSize())
{
slots[j].stackSize += slots[12].stackSize;
slots[12] = null;
break;
} else {
int k = slots[j].getMaxStackSize() - slots[j].stackSize;
if(k < 0)
{
slots[j].stackSize += k;
slots[12].stackSize -= k;
break;
}
}
}
}
} else {
slots[j] = slots[12].copy();
slots[12] = null;
break;
}
}
}
if(this.slots[25] != null)
{
for(int i = 0; i < 9; i++)
{
int j = i + 13;
if(slots[j] != null)
{
if(slots[j].getItem() == slots[25].getItem() && slots[j].getItemDamage() == slots[25].getItemDamage())
{
if(slots[j].stackSize < slots[j].getMaxStackSize())
{
if(slots[j].stackSize + slots[25].stackSize <= slots[25].getMaxStackSize())
{
slots[j].stackSize += slots[25].stackSize;
slots[25] = null;
break;
} else {
int k = slots[j].getMaxStackSize() - slots[j].stackSize;
if(k < 0)
{
slots[j].stackSize += k;
slots[25].stackSize -= k;
break;
}
}
}
}
} else {
slots[j] = slots[25].copy();
slots[25] = null;
break;
}
}
}
if(this.slots[26] != null)
{
for(int i = 0; i < 9; i++)
{
int j = i + 13;
if(slots[j] != null)
{
if(slots[j].getItem() == slots[26].getItem() && slots[j].getItemDamage() == slots[26].getItemDamage())
{
if(slots[j].stackSize < slots[j].getMaxStackSize())
{
if(slots[j].stackSize + slots[26].stackSize <= slots[26].getMaxStackSize())
{
slots[j].stackSize += slots[26].stackSize;
slots[26] = null;
break;
} else {
int k = slots[j].getMaxStackSize() - slots[j].stackSize;
if(k < 0)
{
slots[j].stackSize += k;
slots[26].stackSize -= k;
break;
}
}
}
}
} else {
slots[j] = slots[26].copy();
slots[26] = null;
break;
}
}
}
}
@Override
public void setPower(int i) {
if(this.slots[22] != null && this.slots[22].getItem() == ModItems.factory_core_advanced)
{
slots[22].setItemDamage(maxPower - i);
}
}
@Override
public int getPower() {
return power;
}
@Override
public int getMaxPower() {
return maxPower;
}
}