Jump to content

Recommended Posts

Posted

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.

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

Posted
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);
    }

 

Posted (edited)

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

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.