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



×
×
  • Create New...

Important Information

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