Jump to content

TileEntity Gui Sync


abused_master

Recommended Posts

Hey everyone, its me again, so i'm having some more trouble with TE Syncing and Gui's.

So what i'm doing currently is PacketHandler.INSTANCE.sendToAll(new MessageTEUpdate(this)); with the packet being: http://pastebin.com/K3Gx7N6Z and also adding these http://pastebin.com/SAnEPq6S in my TE, the problem i'm having is my GUI isn't rendering or displaying the correct information... When i say that i mean, i have 2 methods, 1 to render the energy bar, and one to make it so when a player is hovering over a certain location it will display the energy stored, how i'm doing that: http://pastebin.com/UHgVbFpg 

In game however, the energy bar isn't being rendered, and the hovering text for energy is staying at 0, however i know it is storing energy and has energy using an item to detect energy storage from a block on shift click.

Am i missing a step to top it all off or did i mess this up in some way? any help is appreciated.

Link to comment
Share on other sites

2 hours ago, diesieben07 said:

For a start, never send tile entity data to all players. You must send it to tracking players, i.e. players who have the chunk loaded. Sending it to all players will also send it to players in other dimensions or players who are at the other end of the map.

 

To synchronize things to be displayed in a GUI, use the mechanics seen in ContainerFurnace.

Ahh all right will fix that problem.

As for the synchronization in the container iv already tried that method with the getField, setField and detectAndSendChanges but still no luck with it.

Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Show that attempt then.

Aite,

public void detectAndSendChanges()
    {
        super.detectAndSendChanges();

        for (int i = 0; i < this.listeners.size(); ++i)
        {
            IContainerListener icontainerlistener = (IContainerListener)this.listeners.get(i);

            if (this.energyStored != this.tileLeadBank.getField(0))
            {
                icontainerlistener.sendProgressBarUpdate(this, 0, this.tileLeadBank.getField(0));
            }

            if (this.maxEnergyStored != this.tileLeadBank.getField(1))
            {
                icontainerlistener.sendProgressBarUpdate(this, 1, this.tileLeadBank.getField(1));
            }
        }

        this.energyStored = this.tileLeadBank.getField(0);
        this.maxEnergyStored = this.tileLeadBank.getField(1);
    }

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

 

Link to comment
Share on other sites

TE:

public class TileLeadEnergyBank extends TileEntityInventoryBase {

    public EnergyStorage storage = new EnergyStorage(50000);
    public static int SENDPERTICK = 8000;
    private NonNullList<ItemStack> bankInv = NonNullList.<ItemStack>func_191197_a(1, ItemStack.field_190927_a);

    int energyStored = storage.getEnergyStored();
    int maxEnergyStored = storage.getMaxEnergyStored();

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        NBTTagList inventory = (NBTTagList) nbt.getTag("Items");
        for(int i = 0; i < inventory.tagCount(); i++) {
            NBTTagCompound itemTag = inventory.getCompoundTagAt(i);
            ItemStack stack = new ItemStack(itemTag);
            bankInv.set(itemTag.getByte("Slot"), stack);
        }
        storage.readFromNBT(nbt);
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        NBTTagList inventory = new NBTTagList();
        for(byte slot = 0; slot < bankInv.size(); slot++) {
            ItemStack stack = bankInv.get(slot);
            if(!stack.func_190926_b()) {
                NBTTagCompound itemTag = new NBTTagCompound();
                stack.writeToNBT(itemTag);
                itemTag.setByte("Slot", slot);
                inventory.appendTag(itemTag);
            }
        }
        nbt.setTag("Items", inventory);
        return storage.writeToNBT(nbt);
    }

    @Override
    public boolean canConnectEnergy(EnumFacing from) {
        return true;
    }

    @Override
    public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
        return storage.receiveEnergy(maxReceive, simulate);
    }

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

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

    public boolean canInteractWith(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }

    private net.minecraftforge.items.IItemHandler itemHandler;

    protected net.minecraftforge.items.IItemHandler createUnSidedHandler()
    {
        return new net.minecraftforge.items.wrapper.InvWrapper(this);
    }

    @SuppressWarnings("unchecked")
    @Override
    @javax.annotation.Nullable
    public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
    {
        if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
            return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler);
        return super.getCapability(capability, facing);
    }

    @Override
    public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
    {
        return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
    }

    @Override
    public int getSizeInventory() {
        return this.bankInv.size();
    }


    @Override
    public ItemStack getStackInSlot(int index) {
        return (ItemStack)this.bankInv.get(index);
    }


    @Override
    public ItemStack decrStackSize(int index, int count) {
        return ItemStackHelper.getAndSplit(this.bankInv, index, count);
    }

    @Override
    public ItemStack removeStackFromSlot(int index) {
        return ItemStackHelper.getAndRemove(this.bankInv, index);
    }

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

    @Override
    public int getField(int id)
    {
        switch (id)
        {
            case 0:
                return this.energyStored;
            case 1:
                return this.maxEnergyStored;
            default:
                return 0;
        }
    }

    @Override
    public void setField(int id, int value)
    {
        switch (id)
        {
            case 0:
                this.energyStored = value;
                break;
            case 1:
                this.maxEnergyStored = value;
        }
    }

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

    @Override
    public void clear() {
        this.bankInv.clear();
    }

    @Override
    public String getName() {
        return ModBlocks.LeadInfusedEnergyBank.getUnlocalizedName();
    }

    @Override
    public boolean canInsertItem(int index, net.minecraft.item.ItemStack itemStackIn, EnumFacing direction) {
        return this.isItemValidForSlot(index, itemStackIn);
    }

    @Override
    public boolean canExtractItem(int index, net.minecraft.item.ItemStack stack, EnumFacing direction) {
        return true;
    }

    @Override
    public void update() {
        if(!worldObj.isRemote) {
            PacketHandler.INSTANCE.sendToAll(new MessageTEUpdate(this));
            this.chargeItem();
        }

        int energyStored = getEnergyStored(EnumFacing.DOWN);

        for (EnumFacing facing : EnumFacing.values()) {
            BlockPos pos = getPos().offset(facing);
            TileEntity te = worldObj.getTileEntity(pos);
            if (isEnergyTE(te)) {
                EnumFacing opposite = facing.getOpposite();
                int rfToGive = SENDPERTICK <= energyStored ? SENDPERTICK : energyStored;
                int received;

                if (te instanceof IEnergyConnection) {
                    IEnergyConnection connection = (IEnergyConnection) te;
                    if (connection.canConnectEnergy(opposite)) {
                        received = receiveEnergy(te, opposite, rfToGive);
                    } else {
                        received = 0;
                    }
                } else {
                    received = receiveEnergy(te, opposite, rfToGive);
                }
                energyStored -= storage.extractEnergy(received, false);
                if (energyStored <= 0) {
                    break;
                }
            }
        }

    }

    @Override
    public boolean func_191420_l() {
        for (ItemStack itemstack : this.bankInv)
        {
            if (!itemstack.func_190926_b())
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public void setInventorySlotContents(int index, net.minecraft.item.ItemStack stack) {
        ItemStack itemstack = (ItemStack)this.bankInv.get(index);
        boolean flag = !stack.func_190926_b() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
        this.bankInv.set(index, stack);

        if (stack.func_190916_E() > this.getInventoryStackLimit())
        {
            stack.func_190920_e(this.getInventoryStackLimit());
        }

        if (index == 0 && !flag)
        {
            this.markDirty();
        }
    }

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

    public static boolean isEnergyTE(TileEntity te) {
        return te instanceof IEnergyHandler || (te != null && te.hasCapability(CapabilityEnergy.ENERGY, null));
    }

    public static int receiveEnergy(TileEntity tileEntity, EnumFacing from, int maxReceive) {
        if (tileEntity instanceof IEnergyReceiver) {
            return ((IEnergyReceiver) tileEntity).receiveEnergy(from, maxReceive, false);
        } else if (tileEntity != null && tileEntity.hasCapability(CapabilityEnergy.ENERGY, from)) {
            IEnergyStorage capability = tileEntity.getCapability(CapabilityEnergy.ENERGY, from);
            if (capability.canReceive()) {
                return capability.receiveEnergy(maxReceive, false);
            }
        }
        return 0;
    }

    public boolean canCharge() {
        if(bankInv.get(0).getItem() instanceof IEnergyContainerItem && storage.getEnergyStored() >= 500) {
            int itemMaxStored = ((IEnergyContainerItem) bankInv.get(0).getItem()).getMaxEnergyStored(bankInv.get(0));
            int itemStored = ((IEnergyContainerItem) bankInv.get(0).getItem()).getEnergyStored(bankInv.get(0));
            if(itemStored != itemMaxStored) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public void chargeItem() {
        if (this.canCharge()) {
            storage.setEnergyStored(storage.getEnergyStored() - 500);
            ((IEnergyContainerItem) bankInv.get(0).getItem()).receiveEnergy(bankInv.get(0), 500, false);
        }
    }
}

~Edit
did see the other half of the msg, for my text and energybar i switched to this:

@Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        mc.getTextureManager().bindTexture(gui);
        this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

        if(this.energyBank.storage.getEnergyStored() > 0){
            int i = getEnergyScaled(40);
            this.drawTexturedModalRect(width + 82, height + 21, 178, 4, 12, i);
        }

        String LeadBank = I18n.translateToLocal(energyBank.storage.getEnergyStored() + " / " + energyBank.storage.getMaxEnergyStored() + " RF");
        mc.fontRendererObj.drawStringWithShadow(LeadBank, guiLeft + xSize / 2 - mc.fontRendererObj.getStringWidth(LeadBank) / 2, guiTop + 4, 0xFFFFFF);

        if (this.isPointInRegion(82, 21, 14, 40, mouseX, mouseY)) {
            List<String> rf = new ArrayList<String>();
            rf.add(energyBank.getField(0) + " / " + energyBank.getField(1) + " RF");
            GuiUtils.drawHoveringText(rf, mouseX, mouseY, 25, 120, -10, mc.fontRendererObj);
        }
    }
        
            private int getEnergyScaled(int i) {
        return energyBank.getField(0) * i / energyBank.getField(1);
    }

 

Edited by diesieben07
syntax hightlighting
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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • In the rapidly evolving landscape of cryptocurrency, scams are on the rise, leaving many investors vulnerable to fraudulent schemes. If you've found yourself a victim of a crypto scam, navigating the complex process of recovering your lost funds can be daunting and overwhelming. However, there are some professionals in cryptocurrency recovery, such as Captain WebGenesis, who have specific knowledge and experience in handling complex blockchain technology and comprehending how scammers function in the cryptocurrency space. Captain WebGenesis has extensive expertise helping people who have been duped by investment scams to effectively retrieve their stolen or lost cryptocurrency.  
    • Looking for a semi-vanilla Minecraft server? Maybe one with keep inventory and land claims? I have one for you! Let me introduce you to Safe Survival! Come on and join us at mc.safesurvival.net. It is bedrock supported too!
    • Hi. I'm trying to play RLCraft with Optifine but my game keeps crashing on startup with Exit Code 1. The modpack was working fine yesterday, not sure what changed to make it not wanna work anymore. I was hoping someone here could help me figure out what's going wrong. If I remove Optifine, nothing changes, I still get the same errors. Thanks in advance. Crash Report (I only included the errors+fatal, I’m not sure if more is needed, please let me know): https://pastebin.ai/66ivknz0f4  
    • I am trying to port my mod from 1.20.2 to 1.21, but I am encountering difficulties regarding the classes for rendering the GUI and related events. What are the equivalents of the following classes? net.minecraftforge.client.event.RenderGuiOverlayEvent net.minecraftforge.client.gui.overlay.NamedGuiOverlay net.minecraftforge.client.gui.overlay.ForgeGui net.minecraftforge.client.gui.overlay.GuiOverlayManager net.minecraftforge.client.gui.overlay.IGuiOverlay   thx
    • Hi all! Love the Custom npc mod, it is my favorite mod! That said, I have been using this mod since finding it a few years ago (I only play on 1.12.2) and instantly loved that I caould reskin all of the npc's I created. That worked great until recently. I got locked out of my outlook account, which is what I was logged into windows with at the time, and they locked me out of Windows 10. So, I had to reinstall, and since I have digital entitlements to Windows 10 ( I am part of the insider program) on all three of my outlook accounts, I ust swapped to the one I bought Minecraft for. Reinstalled everything, started MC, crerated my favorite fighter (set to kill everything) to clear an area) set up the stats, went back to the main screen, clicked skin so I can give him the grim reaper skin and crash . . . wtf? Thought it was another mod, so, over the course of the last 2 days, I removed every single mod, reinstalled forge 2860 and then 2859, replaced optifine G5 with G6 and vice versa, all with the same result. So, I created a new world, with only the two mods, and same thing. https://pastebin.com/8raJVHq1 That is the log, I have been through the visual settings and all sorts of stuff and can not figure this out, and being that I only just started a bachelors in software engineer / computer programming, I have no idea what I am looking at, so please, and thank you!  If any one of you know what is causing this, I will be forever grateful for the help! Mor9heus
  • Topics

×
×
  • Create New...

Important Information

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