Jump to content

[1.14.4] Using a container to sync my GUI even if I don't need the player inventory ?


dylandmrl

Recommended Posts

Hello guys,

 

Today I encounter another problem. I have trouble doing the good choice when it comes to forge modding. I want to sync a GUI with my tile entity. I know that blocks such as furnace use a Container to sync the whole thing. But in my case I don't need the player inventory at all and I don't care about slots. It is just bunch of variables. So do I really need a container or maybe I can use something else and if that so what can I use ?

 

PS: I could just update my client side tile entity at the same time of my server side tile entity into my GUI but I am not sure it is a good idea...

Edited by dylandmrl
Link to comment
Share on other sites

Ok I've implemented the container but I don't understand how to properly handle the "detectAndSendChanges" function. My tile entity contains a rather complex structure and I only see this function: "listener.sendWindowProperty(this, varToUpdate, newValue);" to communicate a change or, I need to change a whole structure which is not only composed of ints... Also, I can't access the listeners list since the variable is private.

Link to comment
Share on other sites

Ah yes this was my bad trully sorry, however my main problem is still the same, how can I send a "complex change" I mean, I don't have an array of int inside my tile entity but a list of list that store several values. I can't just update the whole data structure when I observe a changement ?

Link to comment
Share on other sites

Ok so basically, when I detect a change, I need to send a packet to each player using listener that I am getting using reflexion to update their client tile entity based on my server side tile entity and my container will retrieve the data from their own tile entity ?

Edited by dylandmrl
Link to comment
Share on other sites

For people who don't know how to do, I have something like :

 

Inside your container:

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

    if(this.tileEntity.isDifferent()){
        this.tileEntity.setDifferent(false);
        Field f = null;
        try {
            f = Container.class.getDeclaredField("listeners");
            f.setAccessible(true);
            try {
                List<IContainerListener> listOfListeners = (List<IContainerListener>) f.get(this); //IllegalAccessException
                for(IContainerListener listener: listOfListeners){
                    if(listener instanceof PlayerEntity){
                        // Send the packet
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}

 

Correct me if I am wrong but normally everything is ok! isDifferent is set as true if the tileentity receive any modification. Basically I have a function that make the tile entity dirty and put this variable to true at the same time.

Link to comment
Share on other sites

Okay I have tried to figure out how to do without blowing everything (I have certainly made mistakes again):

 

12 hours ago, diesieben07 said:
  • That field will not be called "listeners" outside the development environment. You need to use ObfuscationReflectionHelper.findField with a SRG name. This will also call setAccessible for you.
  • Do not look up the Field instance every time. Do it once and store it in a static final field (both modifiers are important to allow the JVM to optimize this reflective access).
public static Field field = ObfuscationReflectionHelper.findField(Container.class, "field_177758_a");

I have found "field_177758_a" as an equivalent of listeners in the website that you provided.

 

12 hours ago, diesieben07 said:
  • That is not how you handle exceptions. Ever.
try {
    List<IContainerListener> listOfListeners = (List<IContainerListener>) DCContainer.field.get(this);
    for(IContainerListener listener: listOfListeners){
        if(listener instanceof ServerPlayerEntity){
            this.sendUpdate(listener);
        }
    }
} catch (IllegalAccessException e) {
    Debugger.error(e);
}

For this one, I am not sure at all.

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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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