Jump to content

[1.12][Solved] Block GUI is not modifying the block state


Recommended Posts

Posted (edited)

Hi!

I made a redstone clock. It emits redstone signal by a period of time, and then go off, then on and so on.

I want the clocking period to be changable in game, so I tried to make a GUI. In the GUI, is a button '+' and a button '-', they change the max number the block has to wait until changing its state.

But, for some reason, the game is not updating to this, the frecuency is the same.

What am I doing wrong?

 

Code:

Spoiler

public class LogicClockGui extends GuiContainer{
    private static final ResourceLocation texture = new ResourceLocation(Mod_ALF_Logic_Gates.MODID, "textures/gui/basic_gui.png");
    private ClockEntity clockEntity;

    public  LogicClockGui(ClockEntity clockEntity){
        super(new ClockContainer(clockEntity));
        this.clockEntity = clockEntity;
        xSize = 176;
        ySize = 75;
    }

    @Override
    public void initGui(){
        super.initGui();
        buttonList.add(new GuiButton(1, guiLeft+30, guiTop+50, 40, 20, "reset"));
        buttonList.add(new GuiButton(2, guiLeft+40, guiTop+30, 20, 20, "+"));
        buttonList.add(new GuiButton(3, guiLeft+20, guiTop+30, 20, 20, "-"));
    }

    @Override
    protected void actionPerformed(GuiButton b){
        int actualMaxCount = clockEntity.getMaxCount();
        System.out.println("Actual: " + actualMaxCount);
        switch(b.id){
            case 1: // reset
                System.out.println("Reset button clicked!");
                break;
            case 2: // +
                System.out.println("Setting: " + (actualMaxCount+1));
                clockEntity.setMaxCount(actualMaxCount + 1);
                break;
            case 3: // -
                System.out.println("Setting: " + (actualMaxCount-1));
                clockEntity.setMaxCount(actualMaxCount - 1);
                break;
        }

    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int x, int y) {
        // Bind the image texture of our custom container
        Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
        // Draw the image
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
    }

    // draw the foreground for the GUI - rendered after the slots, but before the dragged items and tooltips
    // renders relative to the top left corner of the background
    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        final int LABEL_XPOS = 8;
        final int LABEL_YPOS = 6;
        String name = "Logic clock";
        if(clockEntity.getDisplayName() != null){
            name = clockEntity.getDisplayName().getUnformattedText();
        }
        fontRenderer.drawString(name, LABEL_XPOS, LABEL_YPOS, Color.darkGray.getRGB());
    }

}

 

 

Spoiler
Spoiler


public class ClockEntity extends TileEntity implements ITickable{
    private boolean lit = false;

    private int counter = 0;
    // private int delayCounter = 10;
    private int step = 1;
    private int maxCount = 40;
    // private int multiplier = 10;

    public ClockEntity(){
        super();
    }

    public boolean isOn(){
        return lit;
    }

    public int getMaxCount(){
        return maxCount;
    }

    public void setMaxCount(int newMaxCount){
        maxCount = newMaxCount;
    }

    @Override
    public void update(){
        counter -= step; // * multiplier;
        if (counter <= 0) {
            lit = !lit;
            counter = maxCount; //  * multiplier;
            markDirty();
            world.notifyNeighborsOfStateChange(getPos(), blockType, false);
        }
    }

    //  NBT methods...

}

 

 

The github repo is here.

Thanks for your time.

Edited by AngheloAlf
Posted (edited)

Well, GUI button presses only occur on the client while the clock's logic occurs on the server. The server has no idea that the data was changed. You need to use packets to notify the server.

Edited by V0idWa1k3r
  • Like 1
Posted

Hi

 

I added an IMessage to notify the server:

 

Spoiler

// network.ClockMessage
public class ClockMessage implements IMessage{
    public ClockMessage(){
        
    }

    private int maxCount;
    private int step;
    public ClockMessage(int maxCount, int step){
        this.maxCount = maxCount;
        this.step = step;
    }

    @Override
    public void toBytes(ByteBuf buf){
        buf.writeInt(maxCount);
        buf.writeInt(step);
    }

    @Override
    public void fromBytes(ByteBuf buf){
        maxCount = buf.readInt();
        step = buf.readInt();
    }

    static public class ClockMessageHandler implements IMessageHandler<ClockMessage, IMessage>{
        public ClockMessageHandler(){

        }

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

            int maxCount = message.maxCount;
            System.out.println("SERVER: "+maxCount);

            // How do i access the tile entity?

            /*serverPlayer.getServerWorld().addScheduledTask(() -> {

            });*/
            // No response packet
            return null;
        }
    }
}

 

My new problem is, how do I access the corresponding tile entity from the IMessageHandler::onMessage method?

 

Thanks a lot.

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.