Jump to content

Tile Entity Problems


Bedrock_Miner

Recommended Posts

Hey Guys!

I created a TileEntity which should work nearly the same as a furnace with the difference that it uses no fuel.

 

I have a problem with this furnace: If I place a stack in the slot to take the ingredients from, it starts taking them, but if I leave the Gui and reopen it, the process is back at zero. Why?

 

Code:

 

Tile Entity:

package com.bedrockminer.magicum.block.tileentity;

import ...;

public class TileElementaryExtractor extends TileEntity implements IInventory{

public ItemStack[] inventory = new ItemStack[2];
private int refresh = 5;
private int process;

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

@Override
public ItemStack getStackInSlot(int slot) {
	return (slot >= 0 && slot < this.inventory.length) ? this.inventory[slot] : null;
}

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

@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	ItemStack stack = this.getStackInSlot(slot);
	if (stack != null) {
		this.setInventorySlotContents(slot, null);
	}
	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	if (slot < this.getSizeInventory())
        {
       	 	this.inventory[slot] = stack;
       	 	if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
       	 		stack.stackSize = this.getInventoryStackLimit();
       	 	}
        }
}

@Override
public String getInventoryName() {
	return "inventory.elementaryExtractor";
}

@Override
public boolean hasCustomInventoryName() {
	return false;
}

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

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return true;
}

@Override
public void openInventory() { }

@Override
public void closeInventory() { }

@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
	return true;
}

public int getProgress() {
	return this.progress > 128 ? 128 : this.progress;
}

public void addProgress(int amount) {
	this.progress += amount;
}

public void checkProgress() { //Checks whether the process variable is full enough to add an item to the output
	while (this.progress > 128) {
		if (this.getStackInSlot(1) == null || (this.getStackInSlot(1).stackSize < this.getInventoryStackLimit() && this.getStackInSlot(1).getItem() == ItemElements.getEssenceForce(Elements.values()[this.element]))) {
			this.progress -= 128;
			if (this.getStackInSlot(1) == null) {
				this.setInventorySlotContents(1, new ItemStack(ItemElements.getEssenceForce(Elements.values()[this.element])));
			} else {
				this.getStackInSlot(1).stackSize ++;
			}
			this.markDirty();
		} else {
			break;
		}
	}
}

@Override
public void updateEntity() { //Takes one item from the input every fifth tick
	if (--this.refresh <= 0) {
		if (this.element != 0 && this.getStackInSlot(0) != null) {
			if (ItemElements.getElementsForStack(this.getStackInSlot(0)) != null && ItemElements.getElementsForStack(this.getStackInSlot(0)).contains(Elements.values()[this.element])) {
				this.addProgress(ItemElements.getValueForStack(this.getStackInSlot(0)));
				CommonUtils.reduceItemStack(this, 0, 1);
				this.markDirty();
			}
		}
		this.checkProgress();
		this.refresh  = 5;
	}
}

@Override
    public void readFromNBT(NBTTagCompound tagCompound) {
    	super.readFromNBT(tagCompound);
    	NBTTagList tagList = tagCompound.getTagList("Items", 10);
        this.inventory = new ItemStack[this.getSizeInventory()];

        for (int i = 0; i < tagList.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound1 = tagList.getCompoundTagAt(i);
            int j = nbttagcompound1.getByte("Slot") & 255;

            if (j >= 0 && j < this.inventory.length)
            {
                this.inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
            }
        }
        this.progress = tagCompound.getInteger("Progress");
    }

    @Override
    public void writeToNBT(NBTTagCompound tagCompound) {
    	super.writeToNBT(tagCompound);
    	NBTTagList tagList = new NBTTagList();

        for (int i = 0; i < this.inventory.length; ++i)
        {
            if (this.inventory[i] != null)
            {
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                this.inventory[i].writeToNBT(nbttagcompound1);
                tagList.appendTag(nbttagcompound1);
            }
        }

        tagCompound.setTag("Items", tagList);
        tagCompound.setInteger("Progress", this.progress);
    }

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound nbt = new NBTTagCompound();
	this.writeToNBT(nbt);
	return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, this.getBlockMetadata(), nbt);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
	this.xCoord = pkt.func_148856_c();
	this.yCoord = pkt.func_148855_d();
	this.zCoord = pkt.func_148854_e();
	this.blockMetadata = pkt.func_148853_f();
	this.readFromNBT(pkt.func_148857_g());
}
}

 

Container:

package com.bedrockminer.magicum.client.gui.container;

import ...;

public class ContainerElementaryExtractor extends Container {

public TileElementaryExtractor te;
public EntityPlayer thePlayer;

public ContainerElementaryExtractor (EntityPlayer player, TileElementaryExtractor te) {
	this.te = te;
	this.thePlayer = player;
	this.addSlotToContainer(new Slot(te, 0, 32, 56));
	this.addSlotToContainer(new SlotResult(player, te, 1, 130, 56));
	this.addPlayerInventory(player.inventory);

}

@Override
public boolean canInteractWith(EntityPlayer var1) {
	return true;
}

protected void addPlayerInventory(InventoryPlayer inventoryPlayer) {
    	for (int i = 0; i < 3; i++) {
    		for (int j = 0; j < 9; j++) {
    			this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,  8 + j * 18, 98 + i * 18));
    		}
    	}
    	for (int i = 0; i < 9; i++) {
    		this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 156));
    	}
    }

}

 

Gui:

package com.bedrockminer.magicum.client.gui;

import ...;

public class GuiElementaryExtractor extends GuiContainer {

private ContainerElementaryExtractor container;

public GuiElementaryExtractor(ContainerElementaryExtractor container) {
	super(container);
	this.container = container;
	this.xSize = 176;
	this.ySize = 180;
}

@Override
protected void drawGuiContainerBackgroundLayer(float var1, int mouseX, int mouseY) {
	...
}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
	...

	//Render the Progress Bar
	this.drawTexturedModalRect(38, 80, 0, 20, 100, 5);
	this.drawTexturedModalRect(38, 80, 0, 25, this.container.te.getProgress() * 100 / 128, 5);
}

}

 

 

Actually, the block should smelt every tick because of the onUpdate method. Actually it resets everytime I open the gui.

Link to comment
Share on other sites

I think it is related with syncing container... you have to send the progress data to client.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

I copied some code and now I think I found the problem..

I have a button in the Gui which controls the furnace. (Sorry, I havent posted this part of code)

The Furnace only works if this button is pressed (It's a toggle-button)

I thought, this would work, thats why I didn't post it. But: The value of the button is not synchronized with the server.

Do I need a packet to do this or is there another way?

Link to comment
Share on other sites

There is an easy way to transfer button-clicks when viewing a GuiContainer.

When you want a send a button-click, call

Minecraft#playerController.sendEnchantPacket(windowId, buttonId)

. The windowId you get from your Container, the buttonId can be a byte (-128 through 127).

That will then trigger a call to

enchantItem

on your Container (on the server!) with the same buttonId.

The naming is a bit stupid (classic MCP...), I bet in the original Mojang code this is called clickButton or something like that.

 

Thanks a lot! It works now..

 

The deobfuscated names are a little bit weird sometimes, you're right....

I think, ICrafting would better be called ICraftingUser or something, because thats what it is used for....

Link to comment
Share on other sites

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.