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.

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

Featured Replies

Posted

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

  • Author

Ok so even if I don't handle any inventory, I still have to implement a container. I have no experience on it at all but I think that I will find enough ressource on internet, thank you very much.

  • Author

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.

  • Author

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 ?

  • Author

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

  • Author

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.

  • Author

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.

  • Author

Okay I got it, everything should be fine now. Thank you very much to take your time like this! The Debugger class is mine but I will just create my own exception and throw something instead.

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.