Jump to content

Recommended Posts

Posted

I have a problem with my machine.

It is supposed to take the item in the input slot, compare it against a list of recipes, and if it matches the input of one, it starts to 'spin' and after 5 minutes it stops spinning, and places the appropriate output in the output slot. However, even though I am inputting a valid item, it isn't validating the recipe and therefore doesn't begin to spin.

Here is my code:

 

TileEntityCentrifuge

 

 

package com.keegan.lyoko.tilenenity;

import java.util.ArrayList;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

import com.keegan.lyoko.recipes.CentrifugeRecipe;
import com.keegan.lyoko.recipes.Recipes;

public class TileEntityCentrifuge extends TileEntity implements IInventory
{

private ItemStack[] contents;

private float startTime;
private boolean spinning = false;

private ArrayList<CentrifugeRecipe> recipes;

public TileEntityCentrifuge()
{
	contents  = new ItemStack[2];
	recipes = Recipes.getCentrifuge();
}

private float getTime()
{
	//Returns time in minutes
	return System.nanoTime() / 60000000000L;
}

@Override
public void writeToNBT(NBTTagCompound tagCompound)
{
	super.writeToNBT(tagCompound);
	NBTTagList itemList = new NBTTagList();
        for (int i = 0; i < this.contents.length; i++)
        {
            ItemStack stack = this.contents[i];
            if (stack != null)
            {
                NBTTagCompound tag = new NBTTagCompound();
                tag.setByte("Slot", (byte) i);
                stack.writeToNBT(tag);
                itemList.appendTag(tag);
            }
        }
        tagCompound.setTag("Inventory", itemList);
        tagCompound.setFloat("startTime", startTime);
}

@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
	super.readFromNBT(tagCompound);
	NBTTagList tagList = tagCompound.getTagList("Inventory", 10);
        for (int i = 0; i < tagList.tagCount(); i++)
        {
            NBTTagCompound tag = tagList.getCompoundTagAt(i);
            byte slot = tag.getByte("Slot");
            if (slot >= 0 && slot < this.contents.length)
                this.contents[slot] = ItemStack.loadItemStackFromNBT(tag);
        }
        startTime = tagCompound.getFloat("startTime");
}

private void spin()
{
	recipes = Recipes.getCentrifuge();
	for(int i = 0; i < recipes.size(); i++)
	{
		System.out.println(recipes.size());
		System.out.println(i);
		//System.out.println("wanted is not null " + (recipes.get(i).getInput() != null) + " = whether wanted isn't null");
		if(getStackInSlot(0) != null && getStackInSlot(0).equals(recipes.get(i).getInput()))
		{
			System.out.println("Beginning spin");
			startTime = getTime();
			spinning = true;
		}
	}

}

@Override
public void updateEntity()
{
	super.updateEntity();
	recipes = Recipes.getCentrifuge();
	if(startTime > 0)
	{
		float elapsedTime = getTime() - startTime;
		System.out.println("Started at " + startTime);
		System.out.println(elapsedTime + " = elapsed time");
		if((elapsedTime >= 5) && spinning && startTime > -1)
		{
			for(int i = 0; i < recipes.size(); i++)
			{
				if(getStackInSlot(0) == recipes.get(i).getInput())
				{
					startTime = 0;
					spinning = false;
					decrStackSize(0, 1);
					this.setInventorySlotContents(1, recipes.get(i).getOutput());
				}
			}
		}
	}
	if(getStackInSlot(0) != null)
	{
			System.out.println("Attempting Spin");
			spin();
	}
	else
	{
		startTime = 0;
		spinning = false;
	}
}

@Override
public ItemStack decrStackSize(int slot, int amt) {
	ItemStack stack = getStackInSlot(slot);
	if (stack != null)
	{
			if (stack.stackSize <= amt)
			{
				setInventorySlotContents(slot, null);
			}
			else
			{
				stack = stack.splitStack(amt);
				if (stack.stackSize == 0)
				{
					setInventorySlotContents(slot, null);
				}
			}
	}
	return stack;
}

@Override
public String getInventoryName() {
	// TODO Auto-generated method stub
	return "Centrifuge";
}

@Override
public int getInventoryStackLimit() {
	// TODO Auto-generated method stub
	return 64;
}

@Override
public int getSizeInventory() {
	// TODO Auto-generated method stub
	return contents.length;
}

@Override
public ItemStack getStackInSlot(int slot) {
	// TODO Auto-generated method stub
	return contents[slot];
}

@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	// TODO Auto-generated method stub
	ItemStack stack = getStackInSlot(slot);
	if (stack != null) {
		setInventorySlotContents(slot, null);
	}
	return stack;
}


@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) 
{
	for(int i = 0; i < recipes.size(); i++)
	{
		if(slot == 0 && stack == recipes.get(i).getInput())
		{
			return true;
		}
		if(slot == 1 && stack == recipes.get(i).getOutput())
		{
			return true;
		}
	}
	return false;
}

@Override
public boolean isUseableByPlayer(EntityPlayer arg0) {
	// TODO Auto-generated method stub
	return true;
}

@Override
public void closeInventory() {
	// TODO Auto-generated method stub

}




@Override
public boolean hasCustomInventoryName() {
	// TODO Auto-generated method stub
	return false;
}


@Override
public void openInventory() {
	// TODO Auto-generated method stub

}

@Override
public void markDirty() {
	// TODO Auto-generated method stub

}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
	contents[slot] = itemstack;
	if(itemstack != null && itemstack.stackSize > getInventoryStackLimit())
	{
		itemstack.stackSize = getInventoryStackLimit();
	}

}
}

 

 

 

Recipes class

 

 

package com.keegan.lyoko.recipes;

import java.util.ArrayList;

import net.minecraft.item.ItemStack;

public class Recipes 
{
public static ArrayList<CentrifugeRecipe> centrifuge = new ArrayList<CentrifugeRecipe>();


public static void addCentrifugeRecipe(ItemStack input, ItemStack output)
{
	CentrifugeRecipe recipe = new CentrifugeRecipe(input, output);
	centrifuge.add(recipe);
}

public static ArrayList<CentrifugeRecipe> getCentrifuge()
{
	return centrifuge;
}
}

 

 

 

CentrifugeRecipe

 

 

package com.keegan.lyoko.recipes;

import net.minecraft.item.ItemStack;

public class CentrifugeRecipe 
{
public final ItemStack input;
public final ItemStack output;

public CentrifugeRecipe(ItemStack in, ItemStack out)
{
	input = in;
	output = out;
}

public ItemStack getInput() {
	return input;
}

public ItemStack getOutput() {
	return output;
}
}

 

 

 

And I use this to add the recipe(this is the only implemented so far...)

Recipes.addCentrifugeRecipe(new ItemStack(itemWaterCanister), new ItemStack(itemHydrogenCanister));

 

It gets to attempting the spin, and prints i and the size of recipes, and then it stops.

Help?

[shadow=gray,left][glow=red,2,300]KEEGAN[/glow][/shadow]

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.