Jump to content

[1.8.9] [SOLVED] TileEntity Doesn 't work anymore


Recommended Posts

Posted

Hi guys, I just updated my mod from 1.8 to 1.8.9, but now that I updated it, my TileEntity doesn' t work anymore. There are no errors shown in eclipse, so I can' t figure out what it is. It supposed to be a machine (e.g. furnace) but when I put anything in it, there is no progress so I think it has something to do with the update function. Has anything changed in 1.8.9?

 

Here is the code:

 

package com.rowan662.infinitycraft.machines.tileentity;

import com.rowan662.infinitycraft.machines.BlockCompressor;
import com.rowan662.infinitycraft.machines.container.ContainerCompressor;
import com.rowan662.infinitycraft.machines.recipes.CompressorRecipes;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntityLockable;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class TileEntityCompressor extends TileEntityLockable implements ISidedInventory {

public enum slotEnum{
	INPUT_SLOT, OUTPUT_SLOT
}

private static final int[] slotsTop = new int[]{slotEnum.INPUT_SLOT.ordinal()};
private static final int[] slotsBottom = new int[]{slotEnum.OUTPUT_SLOT.ordinal()};
private static final int[] slotsSides = new int[]{};
private ItemStack[] compressorItemStackArray = new ItemStack[2];
private int timeCanCompress;
private int currentItemCompressTime;
private int ticksCompressingItemSoFar;
private int ticksPerItem;
private String compressorCustomName;

@Override
public boolean shouldRefresh(World parWorld, BlockPos parPos, IBlockState parOldState, IBlockState parNewState){
	return false;
}

@Override public int getSizeInventory(){
	return compressorItemStackArray.length;
}

@Override
public ItemStack getStackInSlot(int index){
	return compressorItemStackArray[index];
}

@Override
public ItemStack decrStackSize(int index, int count){
	if(compressorItemStackArray[index] != null){
		ItemStack itemStack;

		if(compressorItemStackArray[index].stackSize <= count){
			itemStack = compressorItemStackArray[index];
			compressorItemStackArray[index] = null;
			return itemStack;
		} else {
			itemStack = compressorItemStackArray[index].splitStack(count);

			if(compressorItemStackArray[index].stackSize == 0){
				compressorItemStackArray[index] = null;
			}
			return itemStack;
		}
	} else {
		return null;
	}
}

@Override
public ItemStack removeStackFromSlot(int index)
    {
        if (this.compressorItemStackArray[index] != null)
        {
            ItemStack itemstack = this.compressorItemStackArray[index];
            this.compressorItemStackArray[index] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }

@Override
public void setInventorySlotContents(int index, ItemStack stack){
	boolean isSameItemStackAlreadyInSlot = stack != null
			&& stack.isItemEqual(compressorItemStackArray[index])
			&& ItemStack.areItemStackTagsEqual(stack, compressorItemStackArray[index]);
	compressorItemStackArray[index] = stack;

	if(stack != null && stack.stackSize > getInventoryStackLimit()){
		stack.stackSize = getInventoryStackLimit();
	}

	if(index == slotEnum.INPUT_SLOT.ordinal() && !isSameItemStackAlreadyInSlot){
		ticksPerItem = timeToCompressOneItem(stack);
		ticksCompressingItemSoFar = 0;
		markDirty();
	}
}

@Override
public String getName(){
	return hasCustomName() ? compressorCustomName : "container.compressor";
}

@Override
public boolean hasCustomName(){
	return compressorCustomName != null && compressorCustomName.length() > 0;
}

public void setCustomInventoryName(String parCustomName){
	compressorCustomName = parCustomName;
}

@Override
public void readFromNBT(NBTTagCompound compound){
	super.readFromNBT(compound);
	NBTTagList nbttaglist = compound.getTagList("items", 10);
	compressorItemStackArray = new ItemStack[getSizeInventory()];

	for(int i = 0; i < nbttaglist.tagCount(); i++){
		NBTTagCompound nbtTagCompound = nbttaglist.getCompoundTagAt(i);
		byte b0 = nbtTagCompound.getByte("Slot");

		if(b0 >= 0 && b0 < compressorItemStackArray.length){
			compressorItemStackArray[b0] = ItemStack.loadItemStackFromNBT(nbtTagCompound);
		}
	}

	timeCanCompress = compound.getShort("CompressTime");
	ticksCompressingItemSoFar = compound.getShort("CookTime");
	ticksPerItem = compound.getShort("CookTimeTotal");

	if(compound.hasKey("CustomName", ){
		compressorCustomName = compound.getString("CustomName");
	}
}

@Override
public void writeToNBT(NBTTagCompound compound){
	super.writeToNBT(compound);
	compound.setShort("CompressTime", (short)timeCanCompress);
	compound.setShort("CookTime", (short)ticksCompressingItemSoFar);
	compound.setShort("CookTimeTotal", (short)ticksPerItem);
	NBTTagList nbttaglist = new NBTTagList();

	for(int i = 0; i < compressorItemStackArray.length; i++){
		if(compressorItemStackArray[i] != null){
			NBTTagCompound nbtTagCompound = new NBTTagCompound();
			nbtTagCompound.setByte("Slot", (byte)i);
			compressorItemStackArray[i].writeToNBT(nbtTagCompound);
			nbttaglist.appendTag(nbtTagCompound);
		}
	}
	compound.setTag("items", nbttaglist);
	if(hasCustomName()){
		compound.setString("CustomName", compressorCustomName);
	}
}

@Override
public int getInventoryStackLimit(){
	return 64;
}

public boolean compressingSomething(){
	return true;
}

@SideOnly(Side.CLIENT)
public static boolean func_174903_a(IInventory parIInventory){
	return true;
}

public void update(){
	boolean hasBeenCompressing = compressingSomething();
	boolean changedCompressingState = false;

	if(compressingSomething()){
		--timeCanCompress;
	}

	if(!worldObj.isRemote){
		if(compressorItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null){
			//Start Compressing
			if(!compressingSomething() && canCompress()){
				timeCanCompress = 150;
				if(compressingSomething()){
					changedCompressingState = true;
				}
			}
			//Continue compressing
			if(compressingSomething() && canCompress()){
				++ticksCompressingItemSoFar;

				if(ticksCompressingItemSoFar == ticksPerItem){
					ticksCompressingItemSoFar = 0;
					ticksPerItem = timeToCompressOneItem(compressorItemStackArray[0]);
					compressItem();
					changedCompressingState = true;
				}
			} else {
				ticksCompressingItemSoFar = 0;
			}
		}
		if(hasBeenCompressing != compressingSomething()){
			changedCompressingState = true;
			BlockCompressor.changeBlockBasedOnCompressingStatus(compressingSomething(), worldObj, pos);
		}
	}
	if(changedCompressingState){
		markDirty();
	}
}

public int timeToCompressOneItem(ItemStack parItemStack){
	return 200;
}

private boolean canCompress(){
	// if nothing in input slot
        if (compressorItemStackArray[slotEnum.INPUT_SLOT.ordinal()] == null)
        {
            return false;
        }
        else // check if it has a grinding recipe
        {
            ItemStack itemStackToOutput = CompressorRecipes.instance().getCompressingResult(compressorItemStackArray[slotEnum.INPUT_SLOT.ordinal()]);
            if (itemStackToOutput == null) return false; 
            
            if (compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()] == null){
            	return true; 
            }
            if (!compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()].isItemEqual(itemStackToOutput)){
            	return false; 
            }
            int result = compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()].stackSize + itemStackToOutput.stackSize;
            
            return result <= getInventoryStackLimit() && result <= compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()].getMaxStackSize();
        }


}

public void compressItem(){
	if(canCompress()){
		ItemStack itemstack = CompressorRecipes.instance().getCompressingResult(compressorItemStackArray[slotEnum.INPUT_SLOT.ordinal()]);

		if(compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()] == null){
			compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()] = itemstack.copy();
		} else if(compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()].getItem() == itemstack.getItem()){
			compressorItemStackArray[slotEnum.OUTPUT_SLOT.ordinal()].stackSize += itemstack.stackSize;
		}

		--compressorItemStackArray[slotEnum.INPUT_SLOT.ordinal()].stackSize;

		if(compressorItemStackArray[slotEnum.INPUT_SLOT.ordinal()].stackSize <= 0){
			compressorItemStackArray[slotEnum.INPUT_SLOT.ordinal()] = null;
		}
	}
}

@Override
public boolean isUseableByPlayer(EntityPlayer playerIn){
	return worldObj.getTileEntity(pos) != this ? false : playerIn.getDistanceSq(pos.getX()+0.5D, pos.getY()+0.5D, pos.getZ()+0.5D) <= 64.0D;
}

@Override
public void openInventory(EntityPlayer playerIn){}

@Override
public void closeInventory(EntityPlayer playerIn){}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack){
		return index == slotEnum.INPUT_SLOT.ordinal() ? true : false;
}

@Override
public int[] getSlotsForFace(EnumFacing side){
	return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
}

@Override
public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction){
	return isItemValidForSlot(index, itemStackIn);
}

@Override
public boolean canExtractItem(int parSlotIndex, ItemStack parStack, EnumFacing parFacing){
	return true;
}

@Override
public String getGuiID(){
	return "infinitycraft:compressor";
}

@Override
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn){
	//DEBUG
	System.out.println("TileEntityCompressor createContainer()");
	return new ContainerCompressor(playerInventory, this);
}

@Override
public int getField(int id){
	switch(id){
	case 0:
		return timeCanCompress;
	case 1:
		return currentItemCompressTime;
	case 2:
		return ticksCompressingItemSoFar;
	case 3:
		return ticksPerItem;
	default:
		return 0;
	}
}

@Override
public void setField(int id, int value){
	switch(id){
	case 0:
		timeCanCompress = value;
		break;
	case 1:
		currentItemCompressTime = value;
		break;
	case 2:
		ticksCompressingItemSoFar = value;
		break;
	case 3:
		ticksPerItem = value;
		break;
	default:
		break;
	}
}

@Override
public int getFieldCount(){
	return 4;
}

@Override
public void clear(){
	for(int i = 0; i < compressorItemStackArray.length; i++){
		compressorItemStackArray[i] = null;
	}
}
}

Posted

IUpdatePlayerListBox

was renamed to

ITickable

in 1.8.8. You don't implement either of these interfaces, so your

TileEntity

doesn't tick.

 

You should add

@Override

to override methods (including interface method implementations) so you get a compiler error if it doesn't override a method of the superclass (or implement an interface method).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

IUpdatePlayerListBox

was renamed to

ITickable

in 1.8.8. You don't implement either of these interfaces, so your

TileEntity

doesn't tick.

 

You should add

@Override

to override methods (including interface method implementations) so you get a compiler error if it doesn't override a method of the superclass (or implement an interface method).

 

That is what I was looking for, thanks! :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.