Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

So, I have a GUI which should access a certain Tile Entity's method. But when I access the Tile Entity and its capability from the Gui, it doesn't save. I suspect that it only changes the value on the client side. I've heard about syncing, but I have no idea how to do that. I am pretty inexperienced in coding, which you can perhaps see in my coding style. How do I fix this?

 

Here's my code:

 

public class GuiNodeNexus extends GuiContainer{

    ArrayList<TileEntity> tileEntities;
    TileEntity currentTileEntity;
    TileEntityNodeNexus te;
    ContainerNodeNexus container;



    public GuiNodeNexus(TileEntityNodeNexus te, IInventory playerInv) {
        super(new ContainerNodeNexus(playerInv, te));

        this.xSize = 175;
        this.ySize = 166;

        this.tileEntities = te.getTileEntities();
        this.te = te;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        drawDefaultBackground();

        int x = (this.width - xSize) / 2;
        int y = (this.height - ySize) / 2;

        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        if(currentTileEntity == null) {
            this.mc.renderEngine.bindTexture(new ResourceLocation(Nodes.MODID, "textures/gui/node_controller.png"));
        }else{
            this.mc.renderEngine.bindTexture(new ResourceLocation(Nodes.MODID, "textures/gui/node_controller_options.png"));
        }

        drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
    }

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

        int x = xSize / 2;
        int y = ySize / 2;
        if (currentTileEntity == null) {
            drawCenteredString(fontRendererObj, "Node Controller", x, 10, 0x555555);
        } else {
            drawCenteredString(fontRendererObj, currentTileEntity.getBlockType().getLocalizedName(), x , 10, 0x555555);
            this.itemRender.renderItemAndEffectIntoGUI(new ItemStack(Item.getItemFromBlock(currentTileEntity.getBlockType())), 7, 7);
        }

        super.drawGuiContainerForegroundLayer(mouseX, mouseY);
    }

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

    @Override
    protected void actionPerformed(GuiButton button) throws IOException {
        if(button.isMouseOver()) {
            if (currentTileEntity == null) {
                currentTileEntity = tileEntities.get(buttonList.indexOf(button));
                updateGui();
            } else {

                switch (buttonList.indexOf(button)) {
                    case 0: {
                        currentTileEntity = null;
                        updateGui();
                        break;
                    }

                    case 1: {
                        if(currentTileEntity.hasCapability(CapabilityNodeItemProvider.ITEM_NODE_CAP, null)){
                            ICapabilityNodeItem itemCap = currentTileEntity.getCapability(CapabilityNodeItemProvider.ITEM_NODE_CAP, null);
                            itemCap.setExport(!itemCap.getExport());

                            updateGui();
                        }
                    }
                }
            }
        }
    }

    private void updateGui(){
        buttonList.clear();
        if(currentTileEntity == null) {
            this.inventorySlots.inventorySlots.clear();
            for (TileEntity tileEntity : tileEntities) {
                int index = tileEntities.indexOf(tileEntity);
                String name = tileEntity.getBlockType().getLocalizedName();
                BlockPos pos = tileEntity.getPos();

                buttonList.add(new GuiButton(index, (this.width - xSize) / 2 + 6, (this.height - ySize) / 2 + 23 + index * 20, 153, 20, name + " X:" + pos.getX() + ", Y:" + pos.getY() + ", Z:" + pos.getZ()));
            }
        }else{
            this.inventorySlots.inventorySlots.clear();

            if(currentTileEntity.hasCapability(CapabilityNodeItemProvider.ITEM_NODE_CAP, null)) {
                ICapabilityNodeItem itemCap = currentTileEntity.getCapability(CapabilityNodeItemProvider.ITEM_NODE_CAP, null);

                buttonList.add(new GuiButton(0, guiLeft + 115, guiTop + 60, 53, 20, "Back"));
                buttonList.add(new GuiButton(1, guiLeft + 115, guiTop + 35, 53, 20, itemCap.getExport() ? "Export" : "Import"));
                ((ContainerNodeNexus) this.inventorySlots).initSlots();
            }
        }
    }



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


}

 

 

 


public class TileEntityNodeNexus extends TileEntity {
    int interval;
    int radius;
    long time;

    ArrayList<TileEntity> tileEntities = new ArrayList<TileEntity>();
    ArrayList<BlockPos> blockPoses = new ArrayList<BlockPos>();

    public TileEntityNodeNexus() {
        interval = 1;
        radius = 5;
        time = System.nanoTime() / 1000000;
    }


    public synchronized ArrayList<TileEntity> getTileEntities(){
        return searchTileEntities();
    }

    private ArrayList<TileEntity> searchTileEntities() {
        BlockPos pos = this.getPos();

        ArrayList<TileEntity> tempExistant = new ArrayList<TileEntity>();

        for (int x = pos.getX() - radius; x < pos.getX() + radius; x++)
            for (int y = pos.getY() - radius; y < pos.getY() + radius; y++)
                for (int z = pos.getZ() - radius; z < pos.getZ() + radius; z++) {
                    BlockPos cur = new BlockPos(x, y, z);

                    if(worldObj.getTileEntity(cur) != null){
                        TileEntity te = worldObj.getTileEntity(cur);

                        if(te.hasCapability(CapabilityNodeItemProvider.ITEM_NODE_CAP, null)) {
                            ICapabilityNodeItem itemCap = te.getCapability(CapabilityNodeItemProvider.ITEM_NODE_CAP, null);
                            if (itemCap.hasNode())
                                tempExistant.add(worldObj.getTileEntity(cur));
                        }
                    }
                }

        ArrayList<TileEntity> temp = new ArrayList<TileEntity>();
        for(TileEntity te : tileEntities){
            if(!tempExistant.contains(te)) temp.add(te);
        }

        tileEntities.removeAll(temp);
        tempExistant.removeAll(tileEntities);
        tileEntities.addAll(tempExistant);

        return tileEntities;
    }

}

 

 

 


public interface ICapabilityNodeItem {

    public static final String CAPABILITY_NODE_ITEM_NAME = "nodeCapability";

    boolean hasNode();

    boolean getExport();

    boolean setHasNode(boolean hasNode);

    void setExport(boolean export);


}

 

 

 


public class CapabilityNodeItemProvider implements ICapabilitySerializable<NBTBase>{

    TileEntity te;

    public CapabilityNodeItemProvider(TileEntity te){
        this.te = te;
    }

    @CapabilityInject(ICapabilityNodeItem.class)
    public static final Capability<ICapabilityNodeItem> ITEM_NODE_CAP = null;

    private ICapabilityNodeItem instance = ITEM_NODE_CAP.getDefaultInstance();


    @Override
    public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == ITEM_NODE_CAP;
    }

    @Override
    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
        return capability == ITEM_NODE_CAP ? ITEM_NODE_CAP.<T> cast(this.instance) : null;
    }


    @Override
    public NBTBase serializeNBT() {
        return ITEM_NODE_CAP.getStorage().writeNBT(ITEM_NODE_CAP, instance, null);
    }

    @Override
    public void deserializeNBT(NBTBase nbt) {
        ITEM_NODE_CAP.getStorage().readNBT(ITEM_NODE_CAP, instance, null, nbt);
    }

}

 

 

 

public class CapabilityNodeItemStorage implements Capability.IStorage<ICapabilityNodeItem>{



    @Override
    public NBTBase writeNBT(Capability<ICapabilityNodeItem> capability, ICapabilityNodeItem instance, EnumFacing side) {

        NBTTagCompound tagCompound = new NBTTagCompound();

        System.out.println("Has Node at Write: " + instance.hasNode());
        System.out.println("Exporting at Write:" + instance.getExport());

        tagCompound.setBoolean(NodeTagRegister.HAS_NODE, instance.hasNode());
        tagCompound.setBoolean(NodeTagRegister.IS_EXPORTING, instance.getExport());

        return tagCompound;
    }


    @Override
    public void readNBT(Capability<ICapabilityNodeItem> capability, ICapabilityNodeItem instance, EnumFacing side, NBTBase nbt) {

        NBTTagCompound tagCompound = (NBTTagCompound) nbt;

        instance.setHasNode(tagCompound.getBoolean(NodeTagRegister.HAS_NODE));
        instance.setExport(tagCompound.getBoolean(NodeTagRegister.IS_EXPORTING));
    }
}

 

 

 

public class CapabilityNodeItemDefault implements ICapabilityNodeItem {

    boolean hasNode = false;
    boolean export = true;
    
    @Override
    public boolean hasNode() {
        return hasNode;
    }

    @Override
    public boolean getExport() {
        return export;
    }

    @Override
    public boolean setHasNode(boolean hasNode) {
        if(this.hasNode != hasNode) {
            this.hasNode = hasNode;
            return true;
        }
        return false;
    }

    @Override
    public void setExport(boolean export) {
        this.export = export;
    }
}

 

 

Thank you in advance!

  • Author

Thanks, it's working now! If anyone wants the code, just reply here.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.