Jump to content

Problem with BlockEntity and Screen Sync


Daru

Recommended Posts

I created Generator BlockEntity with Screen. And I made an interactive power button on the generator screen and the button works fine. However, the problem arises here, although the power value of some entity( ex: @23343) changes when the power is turned on or off, but the power value of the block( ex: @23434) does not actually change. So can I ask for your help?

My Code is Here!

Block Entity :

public class GeneratorEntity extends BlockEntity implements MenuProvider {


    private boolean power = false;
    private int progress = 0;

    public GeneratorEntity(BlockPos pos, BlockState state) {
        super(ModBlockEntities.SMALL_GENERATOR.get(), pos, state);
        }


    @Nullable
    @Override
    public AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {
        return new SmallGeneratorMenu(id,inventory,this,this.data);
    }


    @Override
    protected void saveAdditional(CompoundTag nbt) {
        nbt.putBoolean("IsPowered",this.power);
        super.saveAdditional(nbt);
    }

    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        power = nbt.getBoolean("IsPowered");
    }


    public static void tick(Level level, BlockPos pos, BlockState state, GeneratorEntity pEntity) {
        if (level.isClientSide()) {
            return;
        }

        if (pEntity.getPower()==true) {
            if (hasEnough(pEntity)) {
                pEntity.progress++;
                setChanged(level, pos, state);
            } else {
                pEntity.resetProgress();
                setChanged(level, pos, state);
            }
        }
        else{
            pEntity.resetProgress();
            setChanged(level, pos, state);
        }
    }

    private void resetProgress() {
        this.progress = 0;
    }

    public boolean getPower() {
        return power;
    }

    public void ChangePower() {
        this.power = !this.power;
        setChanged();
        ModMessages.sendToClients(new GeneratorPowerSync2CPacket(this.power, this.getBlockPos()));
    }

    public boolean setPower(GeneratorEntity pEntity, boolean power){
            if (power) {
                pEntity.power = true;
            } else {
                pEntity.power = false;
            }
            return power;

    }
}

 

Screen:

public class SmallGeneratorScreen extends AbstractContainerScreen<SmallGeneratorMenu> {


    private  static final ResourceLocation TEXTURE =
            new ResourceLocation(Generator.MOD_ID,"textures/gui/small_generator_gui.png");
    private  static final ResourceLocation BUTTON =
            new ResourceLocation(Generator.MOD_ID,"textures/gui/button.png");
    private  static final ResourceLocation BUTTON_ON =
            new ResourceLocation(Generator.MOD_ID,"textures/gui/button_on.png");

    public SmallGeneratorScreen(SmallGeneratorMenu pMenu, Inventory pPlayerInventory, Component component) {
        super(pMenu, pPlayerInventory, component);
    }

    private ImageButton button = new ImageButton(0,0,16,16,0,0,16,BUTTON,16,32,e -> {

            if(menu.blockEntity.getPower()){
                menu.blockEntity.ChangePower();
            }
            else{
                menu.blockEntity.ChangePower();
            }

    });

    private ImageButton button_on = new ImageButton(0,0,16,16,0,0,16,BUTTON_ON,16,32,e -> {

            if(menu.blockEntity.getPower()){
                menu.ChangePower();
            }
            else{
                menu.ChangePower();
            }

    });

    @Override
    protected void init() {
        super.init();
        this.addRenderableWidget(button);
        this.addRenderableWidget(button_on);
        button_on.visible = true;
        button_on.active = true;
        button.visible = false;
        button.active = false;
    }

    @Override
    protected void renderBg(PoseStack pPoseStack, float pPartialTick, int pMouseX, int pMouseY) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F,1.0F,1.0F,1.0F);
        RenderSystem.setShaderTexture(0,TEXTURE);

        int x = (width-imageWidth)/2;
        int y = (height-imageHeight)/2;

        this.blit(pPoseStack,x,y,0,0,imageWidth,imageHeight);
        renderEnergy(pPoseStack,x,y);
        renderButton(pPoseStack,x,y);
        renderProgressArrow(pPoseStack,x,y);
        
    }

    private void renderButton(PoseStack pPoseStack, int x, int y) {
        button.setPosition(x+44,y+50);
        button_on.setPosition(x+44,y+50);
        if(menu.blockEntity.getPower()){
            button_on.visible = false;
            button_on.active = false;
            button.visible = true;
            button.active = true;
        }
        else{
            button_on.visible = true;
            button_on.active = true;
            button.visible = false;
            button.active = false;

        }
    }

  
}

Packet:

package com.test.Generator.networking.packet;

import com.test.Generator.block.entity.GeneratorEntity;
import com.test.Generator.screen.SmallGeneratorMenu;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class GeneratorPowerSync2CPacket {
    private final boolean power;
    private final BlockPos pos;

    public GeneratorPowerSync2CPacket (boolean energy, BlockPos pos) {
        this.power = energy;
        this.pos = pos;
    }

    public GeneratorPowerSync2CPacket (FriendlyByteBuf buf) {
        this.power = buf.readBoolean();
        this.pos = buf.readBlockPos();
    }

    public void toBytes(FriendlyByteBuf buf) {
        buf.writeBoolean(power);
        buf.writeBlockPos(pos);
    }

    public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            if(Minecraft.getInstance().level.getBlockEntity(pos) instanceof GeneratorEntity blockEntity) {
                blockEntity.setPower(blockEntity,power);

                if(Minecraft.getInstance().player.containerMenu instanceof SmallGeneratorMenu menu &&
                        menu.getBlockEntity().getBlockPos().equals(pos)) {
                    blockEntity.setPower(blockEntity,power);
                }
            }
        });
        return true;
    }
}

Thank you for your help :)

Link to comment
Share on other sites

Why are you doing it in such a complicated way?

Minecraft already has support for synchronising simple data for a container menu to the client.

 

Look at how the Lectern uses the LecternBlockEntity.dataAccess and LecternBlockMenu.lecternData

to synchronise which page of the book is being viewed.

 

Or look at how the furnace block synchronises the progress bar (it's similar).

  • Like 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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



×
×
  • Create New...

Important Information

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