Jump to content

Recommended Posts

Posted

So somebody told me, that i should use IItemHandler instead of IInventory.

I've got something, but when i place itemstack in slot, it goes in there, but it is also left in my cursor and when i remove itemstack it just disappears. I think everything happens twice and doesnt sync perfectly, because when i place less than 32 items in the slot, it just doubles in the slot. When i put 48 items in, in the slot goes 64 and i get 32 back. when i split the stack with rightclicking, in the slot is left just quarter of the items and in my cursor too.

 

TileEntity:

package com.villfuk02.essence.tileentities;

import java.awt.ItemSelectable;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
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.text.ITextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityStoneCrate extends TileEntity implements ICapabilityProvider{

private String customName;

private static ItemStackHandler itemHandler = new ItemStackHandler(81);

@Override
public boolean hasCapability(Capability capability, EnumFacing facing) {
     return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? true : super.hasCapability(capability, facing);
}

@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
	    return (T) itemHandler;
	  }
	  return super.getCapability(capability, facing);
}

public String getCustomName(){
	return this.customName;
}

public void setCustomName(String customName){
	this.customName = customName;
}



public String getName() {
	return this.hasCustomName() ? this.customName : "Stone Storage Crate";
}

public boolean hasCustomName() {
	return this.customName != null && !this.customName.equals("");
}

public int getSizeInventory() {
	return 81;
}


public boolean isUseableByPlayer(EntityPlayer player) {
	return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
}


}

Container:

package com.villfuk02.essence.gui;

import org.apache.logging.log4j.core.net.Facility;

import com.villfuk02.essence.tileentities.TileEntityStoneCrate;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class ContainerStoneCrate extends Container{




private TileEntityStoneCrate te;

public ContainerStoneCrate(IInventory playerInv, TileEntityStoneCrate te) {
	this.te = te;

	//Tile Entity, Slot 0-80, Slot IDs 0-80
	for (int y = 0; y < 9; ++y){
		for (int x = 0; x < 9; ++x){
			this.addSlotToContainer(new SlotItemHandler(te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP), x + y * 9, 15 + x * 16, 15 + y * 16));
		}
	}
	//Player Inventory, Slot 9-35, Slot IDs 81-107
	for (int y = 0; y < 3; ++y){
		for (int x = 0; x < 9; ++x){
			this.addSlotToContainer(new Slot(playerInv, x + 9 + y * 9, 8 + x * 18, 174 + y * 18));
		}
	}

	//Player Inventory, Slot 0-8, SLot IDs 108-116
	for (int x = 0; x < 9; ++x){
		this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 232));
	}

}


@Override
public ItemStack transferStackInSlot (EntityPlayer player, int fromSlot) {
	ItemStack previous = null;
	Slot slot = (Slot)this.inventorySlots.get(fromSlot);

	if(slot != null && slot.getHasStack()){
		ItemStack current = slot.getStack();
		previous = current.copy();

		if(fromSlot < 81){
			if(!this.mergeItemStack(current, 81, 117, true))
				return null;
		} else {
			if (!this.mergeItemStack(current, 0, 81, false))
				return null;
		}


		if (current.stackSize == 0)
			slot.putStack((ItemStack)null);
		else
			slot.onSlotChanged();

		if (current.stackSize == previous.stackSize)
			return null;
		slot.onPickupFromSlot(player, current);
	}
	return previous;
}


@Override
public boolean canInteractWith(EntityPlayer player) {
	return this.te.isUseableByPlayer(player);
}


}

 

What am i doing wrong?

pls help

 

 

 

Posted

here plz

 

first get a list for all the ItemStacks in the inventory

then spawn the stacks using

 

worldObj.spawnEntityInWorld(new EntityItem(World worldIn, double x, double y, double z, ItemStack stack))

Posted

Works perfectly, thanks

I did it like this:

for (int i = 0; i < te.getSizeInventory(); i++){
		ItemStack stack = te.itemHandler.getStackInSlot(i);
		if (stack != null && stack.stackSize != 0)
			te.getWorld().spawnEntityInWorld(new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), stack));
	}	

 

But i have a problem, it doesn't save to nbt.

When i put items in it and reload the world, the items disappear.

 

Posted

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        compound.setTag("items", (your itemhander that is instanceof INBTSerializable).serializeNBT());
        return compound;
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        (your itemhander that is instanceof INBTSerializable).deserializeNBT(compound.getCompoundTag("items"));
    }

 

ItemStackHandler is instanceof INBTSerializable

Posted

public class TileEntityStoneCrate extends TileEntity implements ICapabilityProvider{

 

TileEntity already implements ICapabilityProvider

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I have new tileEntity here, and it is simple machine, but when the process is finished, the nbt doesn't update and the items in it reset.

I dont know, how to make it write to nbt and what to do with the resetting items.

 

package com.villfuk02.essence.tileentities;

import org.xml.sax.InputSource;

import com.villfuk02.essence.Essence;
import com.villfuk02.essence.gui.ContainerManualMill;
import com.villfuk02.essence.init.ModItems;

import jline.internal.Log;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityManualMill extends TileEntity implements ITickable {

private static int recipe_count = 3;

public boolean powered = false;

private String customName;
private static ItemStack[] inputs = {new ItemStack(Items.FEATHER),new ItemStack(Items.STRING),new ItemStack(Items.GLOWSTONE_DUST),new ItemStack(Items.PAPER),new ItemStack(Blocks.LEAVES),new ItemStack(Blocks.TALLGRASS),new ItemStack(Items.WHEAT),new ItemStack(Items.MELON),new ItemStack(Blocks.LEAVES2),new ItemStack(Blocks.TALLGRASS),new ItemStack(Items.WHEAT),new ItemStack(Items.MELON)};
private static ItemStack[] outputs = {new ItemStack(ModItems.essence_dust, 1, 0),new ItemStack(ModItems.essence_dust, 1, 1),new ItemStack(ModItems.essence_dust, 1, 1)};
private static Essence[] essence_outputs ={new Essence(40, 0),new Essence(40, 1),new Essence(40, 1)};

private int speed = 0;
private int average_speed = 0;
private int cache_speed = -1;
private int speed_decrement = 3;
private int power_needed = 8000;	
private int progress = 0;
private int rotation = 0;
private int rotation_stage = 0;

private int max_essence = 1000;
private int essence_stored = 0;
private int essence_type = -1;

public ItemStackHandler itemHandler = new ItemStackHandler(getSizeInventory()); 

@Override
public boolean hasCapability(Capability capability, EnumFacing facing) {
     return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? true : super.hasCapability(capability, facing);
}

@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
	    return (T) itemHandler;
	  }
	  return super.getCapability(capability, facing);
}

@Override
public SPacketUpdateTileEntity getUpdatePacket()
{
	BlockPos blockPos=getPos();
	return new SPacketUpdateTileEntity(blockPos,0,getUpdateTag());
}

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
	NBTTagCompound nbtTagCompound=pkt.getNbtCompound();
	super.readFromNBT(nbtTagCompound);
	readFromNBT(nbtTagCompound);
}

@Override
public NBTTagCompound getUpdateTag()
{
	NBTTagCompound supertag=super.getUpdateTag();
	writeToNBT(supertag);
	return supertag;
}

@Override
public void handleUpdateTag(NBTTagCompound tag)
{
	super.handleUpdateTag(tag);
	readFromNBT(tag);
}





public int getAvgSpeed(){
	return average_speed;
}

public int getSpeed(){
	return speed;
}	

public boolean getPowered(){
	return powered;
}

public int getEssence(){
	return essence_stored;
}

public int getEssenceType(){
	return essence_type;
}

public double fractionEssence(){
	double fraction = 39.0 *((double)essence_stored / (double)max_essence);
	if (fraction > 0.0)
		fraction++;
	return MathHelper.clamp_double(fraction, 0.0, 40.0);
}

public double fractionProgress(){
	double fraction = ((double)progress / (double)power_needed);
	return MathHelper.clamp_double(fraction, 0.0, 1.0);
}

public double fractionSpeed(){
	double fraction = ((double)average_speed / (double)(30 * speed_decrement));
	return MathHelper.clamp_double(fraction, 0.0, 1.0);
}

public int getRotationStage() {
	return rotation_stage;
}



@Override
public void update(){
	if (essence_stored == 0){
		essence_type = -1;
	}
	if(powered){
		speed += 80;
	}else if(speed > 0){
		speed -= 1 + (int)((double)speed / (double)speed_decrement);
	}
	if (speed < 0)
		speed = 0;
	if (progress >= power_needed){
		finishRecipe();
		progress = 0;
	}
	average_speed = (average_speed * 39 + speed) / 40;
	if(validRecipe()){
		if(average_speed > 0){
			rotation += average_speed;
			progress += average_speed;
		}			
		if (progress <0)
			progress = 0;

		rotation_stage = ((int)((double)rotation / 37.5)) % 4;

		if (rotation > 149)
			rotation -= 150;			

	}else {
		progress = 0;
	}

	if (essence_stored == 0){
		essence_type = -1;
	}

	powered = false;		
	this.markDirty();
}


private boolean validRecipe(){
	ItemStack result = null;
	boolean validi = false;
	boolean valide = false;
	result = getOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3));
	if (result != null) {
		ItemStack outputStack = itemHandler.getStackInSlot(4);
		if (outputStack == null){
			validi = true;				
		}else if (outputStack.getItem() == result.getItem() && outputStack.getMetadata() == result.getMetadata() && ItemStack.areItemStackTagsEqual(outputStack, result)){
			int combinedSize = outputStack.stackSize + result.stackSize;
			if (combinedSize <= 64 && combinedSize <= outputStack.getMaxStackSize()){
				validi = true;
			}
		}
	}
	if (validi){
		Essence essence = getEssenceOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3));
		if (essence != null){
			if(essence_type == -1){
				valide = true;
			}else if(essence.getType() == essence_type){
				int combined_amount = essence.getAmount() + essence_stored;
				if (combined_amount <= max_essence){
					valide = true;
				}
			}
		}
	}
	if (!valide){
		validi = false;
	}
	this.markDirty();
	return validi;
}

public void finishRecipe(){
	ItemStack out = getOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3));

	if (itemHandler.getStackInSlot(4) == null){
		itemHandler.insertItem(4, out, false);
	}else{
		itemHandler.insertItem(4, new ItemStack(itemHandler.getStackInSlot(4).getItem(), itemHandler.getStackInSlot(4).stackSize + out.stackSize, itemHandler.getStackInSlot(4).getMetadata()), false);
	}

	for(int i = 0; i < 4; i++){
		ItemStack stack = itemHandler.getStackInSlot(i);
		stack.stackSize--;
		if (stack.stackSize <= 0){
			stack = null;
		}
		itemHandler.insertItem(i, stack, false);
	}

	essence_stored += getEssenceOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3)).getAmount();
	essence_type = getEssenceOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3)).getType();
	//---Something to write to NBT here---
	this.markDirty();
}

public static ItemStack getOutput(ItemStack in1, ItemStack in2, ItemStack in3, ItemStack in4){
	ItemStack result = null;
	ItemStack[] in = {in1, in2, in3, in4};
	if (in1 == null || in2 == null || in3 == null || in4 == null){
		return null;
	}else{
		for(int x = 0; x < recipe_count; x++){
			for (int a = 0; a < 4; a++){
				if (in[a].getItem() == inputs[4*x].getItem()){
					for (int b = 0; b < 4; b++){
						if (in[b].getItem() == inputs[4*x + 1].getItem()){
							for (int c = 0; c < 4; c++){
								if (in[c].getItem() == inputs[4*x + 2].getItem()){
									for (int d = 0; d < 4; d++){
										if (in[d].getItem() == inputs[4*x + 3].getItem()){
											result = outputs[x];
										}				

									}
								}				

							}
						}				

					}
				}				

			}
		}
		if (in1.getItem() == ModItems.essence_dust && in2.getItem() == ModItems.essence_dust && in3.getItem() == ModItems.essence_dust && in4.getItem() == ModItems.essence_dust){
			int data = in1.getMetadata();
			if (in2.getMetadata() == data && in3.getMetadata() == data && in4.getMetadata() == data)
				result = new ItemStack(ModItems.essence_dust, 3, data);
		}
	}

	return result;

}

public static Essence getEssenceOutput(ItemStack in1, ItemStack in2, ItemStack in3, ItemStack in4){
	Essence result = null;
	ItemStack[] in = {in1, in2, in3, in4};
	if (in1 == null || in2 == null || in3 == null || in4 == null){
		return null;
	}else{
		for(int x = 0; x < recipe_count; x++){
			for (int a = 0; a < 4; a++){
				if (in[a].getItem() == inputs[4*x].getItem()){
					for (int b = 0; b < 4; b++){
						if (in[b].getItem() == inputs[4*x + 1].getItem()){
							for (int c = 0; c < 4; c++){
								if (in[c].getItem() == inputs[4*x + 2].getItem()){
									for (int d = 0; d < 4; d++){
										if (in[d].getItem() == inputs[4*x + 3].getItem()){
											result = essence_outputs[x];
										}				

									}
								}				

							}
						}				

					}
				}				

			}
		}
		if (in1.getItem() == ModItems.essence_dust && in2.getItem() == ModItems.essence_dust && in3.getItem() == ModItems.essence_dust && in4.getItem() == ModItems.essence_dust){
			int data = in1.getMetadata();
			if (in2.getMetadata() == data && in3.getMetadata() == data && in4.getMetadata() == data)
				result = new Essence(20, data);
		}
	}

	return result;

}


public String getCustomName(){
	return this.customName;
}

public void setCustomName(String customName){
	this.customName = customName;
}

public ItemStack[] getStacks(){
	ItemStack[] itemStacks = new ItemStack[5];
	for (int i = 0; i < getSizeInventory(); i++)
		itemStacks[i] = itemHandler.getStackInSlot(i);
	return itemStacks;
}


public String getName() {
	return this.hasCustomName() ? this.customName : "Manual Essence Mill";
}

public boolean hasCustomName() {
	return this.customName != null && !this.customName.equals("");
}

public int getSizeInventory() {
	return 5;
}


public boolean isUseableByPlayer(EntityPlayer player) {
	return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
}


public int getField(int id) {
	int value = 0;
	if (id == 0)
		value = progress;
	if (id == 1)
		value = speed;
	if (id == 2)
		value = rotation;
	if (id == 3)
		value = essence_stored;
	if (id == 4)
		value = essence_type;
	return value;
}

public void setField(int id, int value) {
	if (id == 0)
		progress = value;
	if (id == 1)
		speed = value;
	if (id == 2)
		rotation = value;
	if (id == 3)
		essence_stored = value;
	if (id == 4)
		essence_type = value;		
}

public int getFieldCount() {
	return 5;
}


@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);

	nbt.setTag("Items", itemHandler.serializeNBT());

	nbt.setInteger("progress", progress);
	nbt.setInteger("speed", speed);
	nbt.setInteger("rotation", rotation);
	nbt.setInteger("essence_stored", essence_stored);
	nbt.setInteger("essence_type", essence_type);

	if (this.hasCustomName()){
		nbt.setString("CustomName",this.getCustomName());
	}
	return nbt;
}

@Override
public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);

	itemHandler.deserializeNBT(nbt.getCompoundTag("Items"));

	progress = nbt.getInteger("progress");
	speed = nbt.getInteger("speed");
	rotation = nbt.getInteger("rotation");
	essence_stored = nbt.getInteger("essence_stored");
	essence_type = nbt.getInteger("essence_type");

	if(nbt.hasKey(customName, ){
		this.setCustomName(nbt.getString("CustomName"));

	}
}




}

 

Thanks for any response

Posted

    ItemStackHandler handler = new ItemStackHandler(2){
        @Override
        protected void onContentsChanged(int slot) {
            markDirty();
        }
    };

 

Posted

Ok, so where's the problem?

Why does it just reset when i reload the world?

And the items are doing totally wierd things. When i update one slot, it just resets to how it was before the processing.

 

Try it yourself:

BlockManualMill:

package com.villfuk02.essence.blocks;

import java.util.Random;

import com.villfuk02.essence.client.Reference;
import com.villfuk02.essence.gui.GuiHandler;
import com.villfuk02.essence.init.ModBlocks;
import com.villfuk02.essence.tileentities.TileEntityComparingChest;
import com.villfuk02.essence.tileentities.TileEntityManualMill;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockManualMill extends BlockContainer{

public BlockManualMill(String unlocalizedName, String registryName) {
	super(Material.IRON);
	this.setUnlocalizedName(unlocalizedName);
	this.setRegistryName(new ResourceLocation(Reference.MOD_ID, registryName));
	this.setHardness(3.0f);
	this.setResistance(40.0f);
	this.setHarvestLevel("pickaxe", 0);
	this.setSoundType(SoundType.METAL);
}

@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
	TileEntityComparingChest te = (TileEntityComparingChest)world.getTileEntity(pos);
	InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Item.getItemFromBlock(ModBlocks.manual_mill), 1, 0, te.writeToNBT(new NBTTagCompound())));
	super.breakBlock(world, pos, state);
}

@Override
public MapColor getMapColor(IBlockState state) {
	return MapColor.STONE;
}

@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
	return new TileEntityManualMill();
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	if (worldIn.isRemote){
		return true;
	}
	playerIn.openGui(Reference.MOD_ID, GuiHandler.MANUAL_MILL_GUI, worldIn, pos.getX(), pos.getY(), pos.getZ());
	return true;
}



@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
	if (stack.hasDisplayName()){
		((TileEntityManualMill)world.getTileEntity(pos)).setCustomName(stack.getDisplayName());
	}
}

@Override
public boolean isFullCube(IBlockState state) {
	return false;
}

@Override 
public boolean isNormalCube(IBlockState state){
	return false;
}

}

GuiManualMill:

package com.villfuk02.essence.gui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.villfuk02.essence.client.Reference;
import com.villfuk02.essence.tileentities.TileEntityManualMill;

import io.netty.util.concurrent.ProgressiveFuture;
import jline.internal.Log;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GuiManualMill extends GuiContainer{

private static final ResourceLocation texture = new ResourceLocation(Reference.MOD_ID + ":" + "textures/gui/container/manual_mill_gui.png");
private TileEntityManualMill entity;
private InventoryPlayer player;

public GuiManualMill(InventoryPlayer player, TileEntityManualMill entity) {
	super(new ContainerManualMill(player, entity));

	xSize = 176;
	ySize = 207;

	this.entity = entity;
	this.player = player;
}

final int progress_bar_xpos = 78;
final int progress_bar_ypos = 38;
final int progress_bar_icon_u = 176;
final int progress_bar_icon_v = 0;
final int progress_bar_width = 20;
final int progress_bar_height = 58;

final int gear_xpos = 80;
final int gear_ypos = 58;
final int gear_icon_u = 0;
final int gear_icon_v = 210;
final int gear_width = 16;
final int gear_height = 16;

final int speed_xpos = 8;
final int speed_ypos = 74;
final int speed_icon_u = 0;
final int speed_icon_v = 230;
final int speed_width = 40;
final int speed_height = 15;

final int essence_xpos = 117;
final int essence_ypos = 73;
final int essence_icon_u = 70;
final int essence_icon_v = 210;
final int essence_width = 16;
final int essence_height = 40;

@Override
public void initGui() {
	super.initGui();

	 this.buttonList.add(new GuiButton( 1, this.guiLeft + 8, this.guiTop + 60, 40, 12, "Mill"));
}

@Override
protected void actionPerformed(GuiButton button){
	if(button.id == 1){
		entity.powered = true;
	}
}


@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
	GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
	drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, xSize, ySize);
	double progress = entity.fractionProgress();
	this.drawTexturedModalRect(this.guiLeft + progress_bar_xpos, this.guiTop + progress_bar_ypos, progress_bar_icon_u, progress_bar_icon_v, progress_bar_width, (int)(progress * (double)progress_bar_height));
	int rotate = entity.getRotationStage();
	this.drawTexturedModalRect(this.guiLeft + gear_xpos, this.guiTop + gear_ypos, gear_icon_u + rotate * 16, gear_icon_v, gear_width, gear_height);
	double speed = entity.fractionSpeed();
	this.drawTexturedModalRect(this.guiLeft + speed_xpos, this.guiTop + speed_ypos, speed_icon_u,speed_icon_v, (int)(speed * (double)speed_width), speed_height);
	double essence = entity.fractionEssence();
	int type = entity.getEssenceType();
	this.drawTexturedModalRect(this.guiLeft + essence_xpos, this.guiTop + essence_ypos + essence_height - (int)essence, essence_icon_u + type * essence_width, essence_icon_v + essence_height - (int)essence, essence_width, (int)essence);
}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY){
	super.drawGuiContainerForegroundLayer(mouseX, mouseY);
	String s = this.entity.getName();
	this.fontRendererObj.drawString(s, 88 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752);
	this.fontRendererObj.drawString(this.player.getDisplayName().getUnformattedText(), 8, 115, 4210752);

	List<String> text = new ArrayList<String>();
	if(isInRect(guiLeft + progress_bar_xpos, guiTop + progress_bar_ypos, progress_bar_width, progress_bar_height, mouseX, mouseY)){
		text.add("Progress: ");
		int percentage = (int)(entity.fractionProgress() * 100.0);
		text.add(percentage + "%");
	}
	if(isInRect(guiLeft + speed_xpos, guiTop + speed_ypos, speed_width, speed_height, mouseX, mouseY)){
		text.add("Speed: ");
		text.add(entity.getAvgSpeed() + " RPM");
	} 

	if(isInRect(guiLeft + essence_xpos, guiTop + essence_ypos, essence_width, essence_height, mouseX, mouseY)){
		text.add("Stored essence:");
		text.add(entity.getEssence() + "u");
		switch (entity.getEssenceType()){
		case 0:
			text.add("White essence");
			break;
		case 1:
			text.add("Green essence");
			break;
		case 2:
			text.add("Orange essence");
			break;
		case 3:
			text.add("Blue essence");
			break;
		case 4:
			text.add("Yellow essence");
			break;
		case 5:
			text.add("Cyan essence");
			break;
		case 6:
			text.add("Gray essence");
			break;
		case 7:
			text.add("Light gray essence");
			break;
		default:
			break;
		}
	}

	if(!text.isEmpty()){
		drawHoveringText(text, mouseX - guiLeft, mouseY - guiTop, fontRendererObj);
	}
}

public static boolean isInRect(int x, int y, int xSize, int ySize,int mouseX, int mouseY){
	return((mouseX >= x && mouseX <= x + xSize)&&(mouseY >= y && mouseY <= y + ySize));
}

}

ContainerManualMill:

package com.villfuk02.essence.gui;

import com.villfuk02.essence.tileentities.TileEntityManualMill;

import jline.internal.Log;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class ContainerManualMill extends Container{

private TileEntityManualMill te;

private int [] cachedFields;

private final int hotbar = 9;
private final int inventory_rows = 3;
private final int inventory_columns = 9;
private final int inventory = inventory_rows * inventory_columns;	
private final int player_slots = hotbar + inventory;

public final int input_slots = 4;
public final int output_slots = 1;
public final int machine_slots = input_slots + output_slots;

private final int first_player_index = 0;
private final int first_machine_index = first_player_index;

public ContainerManualMill(InventoryPlayer player, TileEntityManualMill te){
	this.te = te;

	//Hotbar
	for(int x = 0; x < hotbar; x++){
		addSlotToContainer(new Slot(player, x, 8 + 18 * x, 183));
	}
	//Inventory
	for(int y = 0; y < inventory_rows; y++){
		for(int x = 0; x < inventory_columns; x++){
			addSlotToContainer(new Slot(player, hotbar + x + y * inventory_columns, 8 + 18 * x, 125 + y * 18));
		}
	}
	//Machine
	for (int x = 0; x < input_slots; x++){
		addSlotToContainer(new SlotItemHandler(te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP), first_machine_index + x, 44 + 24 * x, 19));
	}
	addSlotToContainer(new SlotItemHandler(te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP), first_machine_index + input_slots, 80, 97));


}


@Override
public boolean canInteractWith(EntityPlayer player) {
	return this.te.isUseableByPlayer(player);
}

@Override
public ItemStack transferStackInSlot (EntityPlayer player, int fromSlot) {
	ItemStack previous = null;
	Slot slot = (Slot)this.inventorySlots.get(fromSlot);

	if(slot != null && slot.getHasStack()){
		ItemStack current = slot.getStack();
		previous = current.copy();

		if(fromSlot < 15){
			if(!this.mergeItemStack(current, 36, 41, true))
				return null;
		} else {
			if (!this.mergeItemStack(current, 0, 36, false))
				return null;
		}


		if (current.stackSize == 0)
			slot.putStack((ItemStack)null);
		else
			slot.onSlotChanged();

		if (current.stackSize == previous.stackSize)
			return null;
		slot.onPickupFromSlot(player, current);
	}
	return previous;
}

@Override
public void detectAndSendChanges(){
	super.detectAndSendChanges();
	boolean haveFieldsChanged = false;
	boolean fieldChanged[] = new boolean[this.te.getFieldCount()];
	if(cachedFields == null){
		cachedFields = new int[this.te.getFieldCount()];
		haveFieldsChanged = true;
	}
	for(int i = 0; i < cachedFields.length; i++){
		if(haveFieldsChanged || cachedFields[i] != this.te.getField(i)){
			cachedFields[i] = this.te.getField(i);
			haveFieldsChanged = true;
		}
	}

	for(int i = 0; i < this.listeners.size(); i++){
		IContainerListener iListener = (IContainerListener)this.listeners.get(i);
		for(int fieldID = 0; fieldID < this.te.getFieldCount(); ++fieldID){
			if(fieldChanged[fieldID]){
				Log.info("field " + fieldID + " changed to " + cachedFields[fieldID]);
				iListener.sendProgressBarUpdate(this, fieldID, cachedFields[fieldID]);
			}
		}
	}		

}

@SideOnly(Side.CLIENT)
@Override
public void updateProgressBar(int id, int data){
	this.te.setField(id, data);
}


}

TileEntityManualMill:

package com.villfuk02.essence.tileentities;

import org.xml.sax.InputSource;

import com.villfuk02.essence.Essence;
import com.villfuk02.essence.gui.ContainerManualMill;
import com.villfuk02.essence.init.ModItems;

import jline.internal.Log;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityManualMill extends TileEntity implements ITickable {

private static int recipe_count = 3;

public boolean powered = false;

private String customName;
private static ItemStack[] inputs = {new ItemStack(Items.FEATHER),new ItemStack(Items.STRING),new ItemStack(Items.GLOWSTONE_DUST),new ItemStack(Items.PAPER),new ItemStack(Blocks.LEAVES),new ItemStack(Blocks.TALLGRASS),new ItemStack(Items.WHEAT),new ItemStack(Items.MELON),new ItemStack(Blocks.LEAVES2),new ItemStack(Blocks.TALLGRASS),new ItemStack(Items.WHEAT),new ItemStack(Items.MELON)};
private static ItemStack[] outputs = {new ItemStack(ModItems.essence_dust, 1, 0),new ItemStack(ModItems.essence_dust, 1, 1),new ItemStack(ModItems.essence_dust, 1, 1)};
private static Essence[] essence_outputs ={new Essence(40, 0),new Essence(40, 1),new Essence(40, 1)};

private int speed = 0;
private int average_speed = 0;
private int cache_speed = -1;
private int speed_decrement = 3;
private int power_needed = 8000;	
private int progress = 0;
private int rotation = 0;
private int rotation_stage = 0;

private int max_essence = 1000;
private int essence_stored = 0;
private int essence_type = -1;

public ItemStackHandler itemHandler = new ItemStackHandler(getSizeInventory()){
	@Override
	protected void onContentsChanged(int slot){

		markDirty();
		Log.info("marked");
	}
};

@Override
public boolean hasCapability(Capability capability, EnumFacing facing) {
     return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? true : super.hasCapability(capability, facing);
}

@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
	    return (T) itemHandler;
	  }
	  return super.getCapability(capability, facing);
}

@Override
public SPacketUpdateTileEntity getUpdatePacket()
{
	BlockPos blockPos=getPos();
	return new SPacketUpdateTileEntity(blockPos,0,getUpdateTag());
}

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
	NBTTagCompound nbtTagCompound=pkt.getNbtCompound();
	super.readFromNBT(nbtTagCompound);
	readFromNBT(nbtTagCompound);
}

@Override
public NBTTagCompound getUpdateTag()
{
	NBTTagCompound supertag=super.getUpdateTag();
	writeToNBT(supertag);
	return supertag;
}

@Override
public void handleUpdateTag(NBTTagCompound tag)
{
	super.handleUpdateTag(tag);
	readFromNBT(tag);
}



public int getAvgSpeed(){
	return average_speed;
}

public int getSpeed(){
	return speed;
}	

public boolean getPowered(){
	return powered;
}

public int getEssence(){
	return essence_stored;
}

public int getEssenceType(){
	return essence_type;
}

public double fractionEssence(){
	double fraction = 39.0 *((double)essence_stored / (double)max_essence);
	if (fraction > 0.0)
		fraction++;
	return MathHelper.clamp_double(fraction, 0.0, 40.0);
}

public double fractionProgress(){
	double fraction = ((double)progress / (double)power_needed);
	return MathHelper.clamp_double(fraction, 0.0, 1.0);
}

public double fractionSpeed(){
	double fraction = ((double)average_speed / (double)(30 * speed_decrement));
	return MathHelper.clamp_double(fraction, 0.0, 1.0);
}

public int getRotationStage() {
	return rotation_stage;
}



@Override
public void update(){
	if (essence_stored == 0){
		essence_type = -1;
	}
	if(powered){
		speed += 80;
	}else if(speed > 0){
		speed -= 1 + (int)((double)speed / (double)speed_decrement);
	}
	if (speed < 0)
		speed = 0;
	if (progress >= power_needed){
		finishRecipe();
		progress = 0;
	}
	average_speed = (average_speed * 39 + speed) / 40;
	if(validRecipe()){
		if(average_speed > 0){
			rotation += average_speed;
			progress += average_speed;
		}			
		if (progress <0)
			progress = 0;

		rotation_stage = ((int)((double)rotation / 37.5)) % 4;

		if (rotation > 149)
			rotation -= 150;			

	}else {
		progress = 0;
	}

	if (essence_stored == 0){
		essence_type = -1;
	}

	powered = false;
}


private boolean validRecipe(){
	ItemStack result = null;
	boolean validi = false;
	boolean valide = false;
	result = getOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3));
	if (result != null) {
		ItemStack outputStack = itemHandler.getStackInSlot(4);
		if (outputStack == null){
			validi = true;				
		}else if (outputStack.getItem() == result.getItem() && outputStack.getMetadata() == result.getMetadata() && ItemStack.areItemStackTagsEqual(outputStack, result)){
			int combinedSize = outputStack.stackSize + result.stackSize;
			if (combinedSize <= 64 && combinedSize <= outputStack.getMaxStackSize()){
				validi = true;
			}
		}
	}
	if (validi){
		Essence essence = getEssenceOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3));
		if (essence != null){
			if(essence_type == -1){
				valide = true;
			}else if(essence.getType() == essence_type){
				int combined_amount = essence.getAmount() + essence_stored;
				if (combined_amount <= max_essence){
					valide = true;
				}
			}
		}
	}
	if (!valide){
		validi = false;
	}
	return validi;
}

public void finishRecipe(){
	ItemStack out = getOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3));
	itemHandler.insertItem(4, out, false);

	for(int i = 0; i < 4; i++){
		itemHandler.extractItem(i, 1, false);
	}

	essence_stored += getEssenceOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3)).getAmount();
	essence_type = getEssenceOutput(itemHandler.getStackInSlot(0),itemHandler.getStackInSlot(1),itemHandler.getStackInSlot(2),itemHandler.getStackInSlot(3)).getType();
	//---Something to write to NBT here---
	markDirty();
	Log.info("marked");
}

public static ItemStack getOutput(ItemStack in1, ItemStack in2, ItemStack in3, ItemStack in4){
	ItemStack result = null;
	ItemStack[] in = {in1, in2, in3, in4};
	if (in1 == null || in2 == null || in3 == null || in4 == null){
		return null;
	}else{
		for(int x = 0; x < recipe_count; x++){
			for (int a = 0; a < 4; a++){
				if (in[a].getItem() == inputs[4*x].getItem()){
					for (int b = 0; b < 4; b++){
						if (in[b].getItem() == inputs[4*x + 1].getItem()){
							for (int c = 0; c < 4; c++){
								if (in[c].getItem() == inputs[4*x + 2].getItem()){
									for (int d = 0; d < 4; d++){
										if (in[d].getItem() == inputs[4*x + 3].getItem()){
											result = outputs[x];
										}				

									}
								}				

							}
						}				

					}
				}				

			}
		}
		if (in1.getItem() == ModItems.essence_dust && in2.getItem() == ModItems.essence_dust && in3.getItem() == ModItems.essence_dust && in4.getItem() == ModItems.essence_dust){
			int data = in1.getMetadata();
			if (in2.getMetadata() == data && in3.getMetadata() == data && in4.getMetadata() == data)
				result = new ItemStack(ModItems.essence_dust, 3, data);
		}
	}

	return result;

}

public static Essence getEssenceOutput(ItemStack in1, ItemStack in2, ItemStack in3, ItemStack in4){
	Essence result = null;
	ItemStack[] in = {in1, in2, in3, in4};
	if (in1 == null || in2 == null || in3 == null || in4 == null){
		return null;
	}else{
		for(int x = 0; x < recipe_count; x++){
			for (int a = 0; a < 4; a++){
				if (in[a].getItem() == inputs[4*x].getItem()){
					for (int b = 0; b < 4; b++){
						if (in[b].getItem() == inputs[4*x + 1].getItem()){
							for (int c = 0; c < 4; c++){
								if (in[c].getItem() == inputs[4*x + 2].getItem()){
									for (int d = 0; d < 4; d++){
										if (in[d].getItem() == inputs[4*x + 3].getItem()){
											result = essence_outputs[x];
										}				

									}
								}				

							}
						}				

					}
				}				

			}
		}
		if (in1.getItem() == ModItems.essence_dust && in2.getItem() == ModItems.essence_dust && in3.getItem() == ModItems.essence_dust && in4.getItem() == ModItems.essence_dust){
			int data = in1.getMetadata();
			if (in2.getMetadata() == data && in3.getMetadata() == data && in4.getMetadata() == data)
				result = new Essence(20, data);
		}
	}

	return result;

}


public String getCustomName(){
	return this.customName;
}

public void setCustomName(String customName){
	this.customName = customName;
}

public ItemStack[] getStacks(){
	ItemStack[] itemStacks = new ItemStack[5];
	for (int i = 0; i < getSizeInventory(); i++)
		itemStacks[i] = itemHandler.getStackInSlot(i);
	return itemStacks;
}


public String getName() {
	return this.hasCustomName() ? this.customName : "Manual Essence Mill";
}

public boolean hasCustomName() {
	return this.customName != null && !this.customName.equals("");
}

public int getSizeInventory() {
	return 5;
}


public boolean isUseableByPlayer(EntityPlayer player) {
	return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
}


public int getField(int id) {
	int value = 0;
	if (id == 0)
		value = progress;
	if (id == 1)
		value = speed;
	if (id == 2)
		value = rotation;
	if (id == 3)
		value = essence_stored;
	if (id == 4)
		value = essence_type;
	return value;
}

public void setField(int id, int value) {
	if (id == 0)
		progress = value;
	if (id == 1)
		speed = value;
	if (id == 2)
		rotation = value;
	if (id == 3)
		essence_stored = value;
	if (id == 4)
		essence_type = value;		
}

public int getFieldCount() {
	return 5;
}


@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);

	nbt.setTag("Items", itemHandler.serializeNBT());

	nbt.setInteger("progress", progress);
	nbt.setInteger("speed", speed);
	nbt.setInteger("rotation", rotation);
	nbt.setInteger("essence_stored", essence_stored);
	nbt.setInteger("essence_type", essence_type);

	if (this.hasCustomName()){
		nbt.setString("CustomName",this.getCustomName());
	}
	return nbt;
}

@Override
public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);

	itemHandler.deserializeNBT(nbt.getCompoundTag("Items"));

	progress = nbt.getInteger("progress");
	speed = nbt.getInteger("speed");
	rotation = nbt.getInteger("rotation");
	essence_stored = nbt.getInteger("essence_stored");
	essence_type = nbt.getInteger("essence_type");

	if(nbt.hasKey(customName, ){
		this.setCustomName(nbt.getString("CustomName"));

	}
}




}

 

And also, when the input slots have one last item in them and you process them, the world crashes.

 

Posted

almost leave the build.gradle and gradle folder in there

the tile saves for me

 

and how can i test this

And also, when the input slots have one last item in them and you process them, the world crashes.

 

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.