Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.11] GUI not updating burn status


Awesome_Spider
 Share

Recommended Posts

I have a steam generator that acts like furnace in that when it has fuel in it, it shows a fire symbol to display how much burn time is left. However, it burns coal, but it doesn't show a fire icon on it. What could be I be doing wrong?

 

Here is my code:

The gui:

public class GuiSteamEngine extends GuiContainer {

    private IInventory playerInv;
    private TileEntitySteamEngine te;

    public GuiSteamEngine(IInventory playerInv, TileEntitySteamEngine te) {
        super(new ContainerTileEntitySteamEngine(playerInv, te));

        this.playerInv = playerInv;
        this.te = te;

        this.xSize = 176;
        this.ySize = 166;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
        this.mc.getTextureManager().bindTexture(new ResourceLocation("roboticraft:textures/gui/container/steam_engine_tile_entity.png"));
        this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);

        if (te.isBurning())
        {
            int scaledBurnTime = this.getBurnLeftScaled(13);
            this.drawTexturedModalRect(80, 59 - scaledBurnTime, 176, 12 - scaledBurnTime, 14, scaledBurnTime + 1);
        }

        int scaledPower = this.getPowerScaled(30);
        this.drawTexturedModalRect(83, 34 - scaledPower, 176, 42 + scaledPower, 185, scaledPower);
    }

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

    }

    private int getBurnLeftScaled(int pixels) {
        /*int currentBurnTime = this.te.getCurrentBurnTime();

        /*if (totalBurnTime == 0) {
            totalBurnTime = 200;
        }

        return this.te.getBurnTime() * pixels / currentBurnTime;*/
        int curBurnTime = te.getCurrentBurnTime();

        if (curBurnTime == 0) {
            curBurnTime = 200;
        }
        return te.getBurnTime() * pixels / curBurnTime;
    }

    private int getPowerScaled(int pixels) {
        int powerCapacity = this.te.getMaxEnergyStored(EnumFacing.NORTH);

        return this.te.getEnergyStored(EnumFacing.NORTH) * pixels / powerCapacity;
    }
}

 

My tile entity:

public class TileEntitySteamEngine extends TileEntity implements ITickable, IInventory, IEnergyProvider {

    private ItemStackHandler inventory;
    private FluidStack steamStack;
    private FluidStack waterStack;

    private float steamUsed;

    private String customName;

    private int burnTime;
    private int currentBurnTime;

    private int maxEnergy;
    private int storedEnergy;
    private int energyOutput;

    private boolean isGenerating;

    public TileEntitySteamEngine() {
        inventory = new ItemStackHandler(1);

        maxEnergy = 100000;
        storedEnergy = 0;
        energyOutput = 80;

        steamUsed = 200;

        steamStack = new FluidStack(ModFluids.steam, 0);
        waterStack = new FluidStack(FluidRegistry.WATER, 0);
    }

    public int getSizeInventory() {
        return 1;
    }

    public int getBurnTime() {
        return burnTime;
    }

    public int getCurrentBurnTime() {
        return currentBurnTime;
    }

    public boolean isBurning() {
        return burnTime > 0;
    }

    public boolean canItemBurn(ItemStack stack) {
        return GameRegistry.getFuelValue(stack) > 0;
    }

    private void setCurrentBurnTime(int itemBurnTime) {
        currentBurnTime = itemBurnTime;
    }

    private void setBurnTime(int burnTime) {
        this.burnTime = burnTime;
    }

    //ITickable

    @Override
    public void update() {
        ItemStack fuelSlot = getStackInSlot(0);

        boolean dirtyFlag = false;

        if(isBurning()){
            burnTime--;
        }

        if(!world.isRemote) {
            if (!isBurning() && !fuelSlot.isEmpty()) {
                ItemStack stack = fuelSlot;

                if(stack.getItem() == Items.COAL){
                    currentBurnTime = 200; //GameRegistry.getFuelValue(stack)
                    burnTime = currentBurnTime;

                    stack.shrink(1);

                    inventory.setStackInSlot(0, stack);

                    dirtyFlag = true;
                }
            }

            if(isBurning() || !fuelSlot.isEmpty()) {
                if(!isBurning() && canGenerate()) {
                    burnTime = GameRegistry.getFuelValue(fuelSlot);
                    currentBurnTime = burnTime;

                    if (this.isBurning()) {
                        dirtyFlag = true;

                        if (!fuelSlot.isEmpty()) {
                            fuelSlot.shrink(1);

                            if (fuelSlot.isEmpty()) {
                                inventory.setStackInSlot(0, fuelSlot);
                            }
                        }
                    }
                }

                if (isBurning() && canGenerate()) {
                    generate();
                }
            }
        }

        if(dirtyFlag) {
            markDirty();
        }
    }

    //IPowerProvider

    @Override
    public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate) {
        int energyExtracted = 0;

        if(!simulate) {
            if(maxExtract < storedEnergy) {
                storedEnergy -= maxExtract;
                markDirty();

                energyExtracted = maxExtract;
            } else if(maxExtract > storedEnergy) {
                int energy = storedEnergy;
                storedEnergy = 0;
                markDirty();

                energyExtracted = energy;
            }
        } else {
            if(maxExtract < storedEnergy) {
                energyExtracted = maxExtract;
            } else if(maxExtract > storedEnergy) {
                int energy = storedEnergy;
                energyExtracted = energy;
            }
        }

        return energyExtracted;
    }

    @Override
    public int getEnergyStored(EnumFacing from) {
        return storedEnergy;
    }

    @Override
    public int getMaxEnergyStored(EnumFacing from) {
        return maxEnergy;
    }

    @Override
    public boolean canConnectEnergy(EnumFacing from) {
        return !from.equals(EnumFacing.NORTH);
    }

    public boolean isSteamFull() {
        return steamStack.amount == 2000;
    }

    public boolean canGenerate() {
        return storedEnergy < maxEnergy;
    }

    private void generate() {
        int steamAmt = steamStack.amount;
        int steamMax = 2000;

        int waterAmt = waterStack.amount;
        int waterMax = 2000;

        //If the steam is full, then generate electricity
        if(steamAmt == steamMax) {
            if(storedEnergy + energyOutput <= maxEnergy) {
                storedEnergy += energyOutput;

                steamAmt -= steamUsed;
                steamStack.amount = steamAmt;

                markDirty();
            } else if (storedEnergy + energyOutput > maxEnergy) {
                int energy = storedEnergy;
                energy += energyOutput;
                energy -= energy - maxEnergy;

                storedEnergy = energy;

                steamAmt -= steamUsed;
                steamStack.amount = steamAmt;

                markDirty();
            }
        }

        //If not full, make steam first
        if(steamAmt + 200 <= steamMax && waterAmt > 0) {
            waterAmt--;

            steamAmt += steamUsed;

            waterStack.amount = waterAmt;
            steamStack.amount = steamAmt;

            markDirty();
        } else if (steamAmt + 200 > steamMax && waterAmt > 0) {
            waterAmt--;

            steamAmt += 200;
            steamAmt -= steamAmt - steamMax;

            waterStack.amount = waterAmt;
            steamStack.amount = steamAmt;

            markDirty();
        }
    }

    //IInventory

    @Override
    public boolean isEmpty() {
        return inventory.getStackInSlot(inventory.getSlots()).isEmpty();
    }

    @Override
    public String getName() {
        return this.hasCustomName() ? this.customName : "container.tile_entity_steam_engine";
    }

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

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

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

    @Override
    public ItemStack getStackInSlot(int index) {
        if (index < 0 || index >= this.getSizeInventory())
            return ItemStack.EMPTY;
        return this.inventory.getStackInSlot(index);
    }

    @Override
    public ItemStack decrStackSize(int index, int count) {
        if (this.getStackInSlot(index) != ItemStack.EMPTY) {
            ItemStack itemstack;

            if (this.getStackInSlot(index).getCount() <= count) {
                itemstack = this.getStackInSlot(index);
                this.setInventorySlotContents(index, ItemStack.EMPTY);
                this.markDirty();
                return itemstack;
            } else {
                itemstack = this.getStackInSlot(index).splitStack(count);

                if (this.getStackInSlot(index).getCount() <= 0) {
                    this.setInventorySlotContents(index, null);
                } else {
                    //Just to show that changes happened
                    this.setInventorySlotContents(index, this.getStackInSlot(index));
                }

                this.markDirty();
                return itemstack;
            }
        } else {
            return ItemStack.EMPTY;
        }
    }

    @Override
    public ItemStack removeStackFromSlot(int index) {
        ItemStack itemStack = inventory.getStackInSlot(index);
        inventory.setStackInSlot(index, ItemStack.EMPTY);

        return itemStack ;
    }

    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        if (index < 0 || index >= this.getSizeInventory())
            return;

        if (stack != null && stack.getCount() > this.getInventoryStackLimit())
            stack.setCount(this.getInventoryStackLimit());

        if (stack != ItemStack.EMPTY && stack.getCount() == 0)
            stack = ItemStack.EMPTY;

        this.inventory.setStackInSlot(index, stack);
        this.markDirty();
    }

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

    @Override
    public boolean isUsableByPlayer(EntityPlayer player) {
        return this.world.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
    }

    @Override
    public void openInventory(EntityPlayer player) {
    }

    @Override
    public void closeInventory(EntityPlayer player) {
    }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return canItemBurn(stack);
    }

    @Override
    public int getField(int id) {
        return 0;
    }

    @Override
    public void setField(int id, int value) {
    }

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

    @Override
    public void clear() {
        for (int i = 0; i < this.getSizeInventory(); i++)
            this.setInventorySlotContents(i, ItemStack.EMPTY);
    }

    //NBT/Capability

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        compound.setTag("inventory", inventory.serializeNBT());
        compound.setInteger("energy", storedEnergy);
        compound.setInteger("waterAmt", waterStack.amount);
        compound.setInteger("steamAmt", steamStack.amount);
        return super.writeToNBT(compound);
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        inventory.deserializeNBT(compound.getCompoundTag("inventory"));
        storedEnergy = compound.getInteger("energy");
        waterStack = new FluidStack(FluidRegistry.WATER, compound.getInteger("waterAmt"));
        steamStack = new FluidStack(FluidRegistry.getFluid("roboticraft_steam"), compound.getInteger("steamAmt"));
        super.readFromNBT(compound);
    }

    @Override
    public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
    }

    @Nullable
    @Override
    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
        return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)inventory : super.getCapability(capability, facing);
    }
}

 

If you need any other code let me know. Do I need to tell my gui to update somehow?

Link to comment
Share on other sites

10 minutes ago, Awesome_Spider said:

The Slot constructor and IContainerListener#sendAllWindowProperties() that I called in my Container require an IInventory.

 

Use SlotItemHandler instead of Slot.

 

IItemHandler doesn't have the same "fields" concept as IInventory, so you'll need to re-implement the syncing yourself.

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.

Link to comment
Share on other sites

11 minutes ago, Awesome_Spider said:

I changed it to SlotItemHandler. As far as syncing, I'm unsure how to implement my own. How would I do that?

 

Either create your own packet using the Simple Network Wrapper or send SPacketWindowProperty if you want to keep using Container#updateProgressBar.

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.

Link to comment
Share on other sites

9 hours ago, Awesome_Spider said:

So am I sending this packet to my gui on client side? I've created a packet class using the documentation but I'm not sure where to send it.

 

In your Container#detectAndSendChanges override, iterate through Container#listeners and check if each one is an instance of EntityPlayerMP. If it is, send your packet to them.

 

In your packet's handler, you can either call Container#updateProgressBar or your own method to update the client-side data.

  • Like 1

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.

Link to comment
Share on other sites

Ok. I'm probably doing something wrong or misunderstanding, but I put an item in my container to test the packet, and it froze up. Windows said minecraft was not responding.

 

There is an error in the log.  What I got from this error is I can't cast EntityPlayerMP into EntityPlayer. I probably shouldn't have attempted that, that was stupid of me, but the only player instance I could find was in MessageContext#getServerHandler. What should I use instead?

 

Here's my code/log error:

The log error:

[17:20:07] [Netty Local Client IO #0/ERROR]: SimpleChannelHandlerWrapper exception
java.lang.ClassCastException: net.minecraft.client.network.NetHandlerPlayClient cannot be cast to net.minecraft.network.NetHandlerPlayServer
	at net.minecraftforge.fml.common.network.simpleimpl.MessageContext.getServerHandler(MessageContext.java:55) ~[MessageContext.class:?]
	at wiseowl5.roboticraft.Networking.ContainerMessageHandler.onMessage(ContainerMessageHandler.java:19) ~[ContainerMessageHandler.class:?]
	at wiseowl5.roboticraft.Networking.ContainerMessageHandler.onMessage(ContainerMessageHandler.java:15) ~[ContainerMessageHandler.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleChannelHandlerWrapper.channelRead0(SimpleChannelHandlerWrapper.java:56) ~[SimpleChannelHandlerWrapper.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleChannelHandlerWrapper.channelRead0(SimpleChannelHandlerWrapper.java:36) ~[SimpleChannelHandlerWrapper.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) ~[SimpleChannelInboundHandler.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [MessageToMessageDecoder.class:4.0.23.Final]
	at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) [MessageToMessageCodec.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787) [DefaultChannelPipeline.class:4.0.23.Final]
	at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:4.0.23.Final]
	at net.minecraftforge.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:109) [FMLProxyPacket.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:156) [NetworkManager.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:51) [NetworkManager.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.handleClientSideCustomPacket(NetworkDispatcher.java:413) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:278) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:73) [NetworkDispatcher.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787) [DefaultChannelPipeline.class:4.0.23.Final]
	at io.netty.channel.local.LocalChannel.finishPeerRead(LocalChannel.java:326) [LocalChannel.class:4.0.23.Final]
	at io.netty.channel.local.LocalChannel.access$400(LocalChannel.java:45) [LocalChannel.class:4.0.23.Final]
	at io.netty.channel.local.LocalChannel$5.run(LocalChannel.java:312) [LocalChannel$5.class:4.0.23.Final]
	at io.netty.channel.local.LocalEventLoop.run(LocalEventLoop.java:33) [LocalEventLoop.class:4.0.23.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) [SingleThreadEventExecutor$2.class:4.0.23.Final]
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
[17:20:07] [Netty Local Client IO #0/ERROR]: There was a critical exception handling a packet on channel roboticraft
java.lang.ClassCastException: net.minecraft.client.network.NetHandlerPlayClient cannot be cast to net.minecraft.network.NetHandlerPlayServer
	at net.minecraftforge.fml.common.network.simpleimpl.MessageContext.getServerHandler(MessageContext.java:55) ~[MessageContext.class:?]
	at wiseowl5.roboticraft.Networking.ContainerMessageHandler.onMessage(ContainerMessageHandler.java:19) ~[ContainerMessageHandler.class:?]
	at wiseowl5.roboticraft.Networking.ContainerMessageHandler.onMessage(ContainerMessageHandler.java:15) ~[ContainerMessageHandler.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleChannelHandlerWrapper.channelRead0(SimpleChannelHandlerWrapper.java:56) ~[SimpleChannelHandlerWrapper.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleChannelHandlerWrapper.channelRead0(SimpleChannelHandlerWrapper.java:36) ~[SimpleChannelHandlerWrapper.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) ~[SimpleChannelInboundHandler.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) ~[AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) ~[AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) ~[MessageToMessageDecoder.class:4.0.23.Final]
	at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) ~[AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) ~[AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787) ~[DefaultChannelPipeline.class:4.0.23.Final]
	at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) ~[EmbeddedChannel.class:4.0.23.Final]
	at net.minecraftforge.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:109) [FMLProxyPacket.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:156) [NetworkManager.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:51) [NetworkManager.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.handleClientSideCustomPacket(NetworkDispatcher.java:413) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:278) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:73) [NetworkDispatcher.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319) [AbstractChannelHandlerContext.class:4.0.23.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787) [DefaultChannelPipeline.class:4.0.23.Final]
	at io.netty.channel.local.LocalChannel.finishPeerRead(LocalChannel.java:326) [LocalChannel.class:4.0.23.Final]
	at io.netty.channel.local.LocalChannel.access$400(LocalChannel.java:45) [LocalChannel.class:4.0.23.Final]
	at io.netty.channel.local.LocalChannel$5.run(LocalChannel.java:312) [LocalChannel$5.class:4.0.23.Final]
	at io.netty.channel.local.LocalEventLoop.run(LocalEventLoop.java:33) [LocalEventLoop.class:4.0.23.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) [SingleThreadEventExecutor$2.class:4.0.23.Final]
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]

 

The container:

public class ContainerTileEntitySteamEngine extends Container {
    private TileEntitySteamEngine te;

    private int burnTime;
    private int currentBurnTime;

    public ContainerTileEntitySteamEngine(IInventory playerInv, TileEntitySteamEngine te) {
        this.te = te;

        // Tile Entity, Slot 1, Slot ID 1
        this.addSlotToContainer(new SlotItemHandler(te.getInventory(), 0, 80, 40));

        // Player Inventory, Slot 9-35, Slot IDs 9-35
        for (int y = 0; y < 3; ++y) {
            for (int x = 0; x < 9; ++x) {
                this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
            }
        }

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

    /**
     * Looks for changes made in the container, sends them to every listener.
     */
    public void detectAndSendChanges() {
        super.detectAndSendChanges();

        for(IContainerListener listener : listeners) {
            if (burnTime != te.getField(0)) {
                if (listener instanceof EntityPlayerMP) {
                    ModPacketHandler.INSTANCE.sendTo(new ContainerMessage(burnTime, currentBurnTime), (EntityPlayerMP) listener);
                }
            }

            if (currentBurnTime != te.getField(1)) {
                if (listener instanceof EntityPlayerMP) {
                    ModPacketHandler.INSTANCE.sendTo(new ContainerMessage(burnTime, currentBurnTime), (EntityPlayerMP) listener);
                }
            }
        }

        this.burnTime = this.te.getField(0);
        this.currentBurnTime = this.te.getField(1);
    }

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

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

 

The message code:

public class ContainerMessage implements IMessage {
        // A default constructor is always required
        public ContainerMessage() {

        }

        public int burnTime;
        public int currentBurnTime;

        public ContainerMessage(int burnTime, int currentBurnTime) {
            this.burnTime = burnTime;
            this.currentBurnTime = currentBurnTime;
        }

        @Override
        public void toBytes(ByteBuf buf) {
            // Writes the int into the buf
            buf.writeInt(burnTime);
            buf.writeInt(currentBurnTime);
        }

        @Override
        public void fromBytes(ByteBuf buf) {
            // Reads the int back from the buf. Note that if you have multiple values, you must read in the same order you wrote.
            burnTime = buf.readInt();
            currentBurnTime = buf.readInt();
        }
}

 

The message handler:

public class ContainerMessageHandler implements IMessageHandler<ContainerMessage, IMessage> {

    @Override
    public IMessage onMessage(ContainerMessage message, MessageContext ctx) {
        EntityPlayerMP serverPlayer = ctx.getServerHandler().player;

        if(ctx.side.isClient()) {
            Container container = ((EntityPlayer)serverPlayer).openContainer;

            if(container instanceof ContainerTileEntitySteamEngine) {
                ((ContainerTileEntitySteamEngine)container).updateProgressBar(0, message.burnTime);
                ((ContainerTileEntitySteamEngine)container).updateProgressBar(1, message.currentBurnTime);
            }
        }

        return null;
    }
}

 

The packet handler:

public class ModPacketHandler {
    public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("roboticraft");

    public static void initPacketChannel() {
        INSTANCE.registerMessage(ContainerMessageHandler.class, ContainerMessage.class, 0, Side.CLIENT);
    }
}

Link to comment
Share on other sites

ContainerMessage is being sent to the client, so ContainerMessageHandler will be executed on the logical client side.

 

This means that you can't use server classes like EntityPlayerMP and NetHandlerPlayServer, you need to get the client player instead. I do this using a method in my proxy that takes the MessageContext and returns the player. In the client proxy, I override this to return Minecraft#player if MessageContext#side#isClient returns true or MessageContext#getServerHandler#player if it doesn't. In the server proxy, I override it to return MessageContext#getServerHandler#player if MessageContext#side#isServer returns true or throw an exception if it doesn't (because something has gone wrong if you're getting a client-side MessageContext on the dedicated server).

 

To be safe, you should probably send the Container's ID (Container#windowId) in the packet and check that it matches the open Container's ID in your packet handler before you update it with the packet's data. Vanilla packets like SPacketSetSlot do this.

  • Like 1

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.

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.