Labels stop being clickable?

  • Something odd is happening. Probably relating to my lack of understanding.



    Code
    GuiLabel Label1 = new GuiLabel("TEXT", 10.0f, 25.0f, false);
    Label1.setClickable(true);

    This works fine, but it appears that when someone logs off, everyone's labels stop being clickable. :/


    Does anyone have a complete mini/simple example of how I should be using "clickable labels" ?

  • sorry for the long delay but I didn't have any time in the past few days, so yes your problem is basically that you are using the same label for all players.


    The way I (and a few others) have handled this is by giving every player their own GUI. That can be done by assigning every player an attribute containing their personal GUI upon spawning (onSpawnEvent, I would recommend not using the onConnectEvent because sometimes the players are not registering correctly in that one since they are not really in the server yet). Make sure the attribute really has a very unique name to not clash with other plugins ;)


    Something like this:

    Java
    @EventMethod
    public void onPlayerSpawn(PlayerSpawnEvent event){
    Player player = event.getPlayer();
    GuiLabel AreaNameLabel = CreateAreaNameGUI("Default Area");
    player.setAttribute("MinoLabel", AreaNameLabel);
    player.addGuiElement(AreaNameLabel);
    }

    Where my CreateAreaNameGUI method is:

    Java
    public GuiLabel CreateAreaNameGUI(String AreaName){
    GuiLabel AreaNameLabel = new GuiLabel(AreaName,0.05f,0.05f,true);
    AreaNameLabel.setPivot(PivotPosition.BottomLeft);
    AreaNameLabel.setFont(Font.Default);
    AreaNameLabel.setFontColor(1.0f, 1.0f, 1.0f, 1.0f);
    AreaNameLabel.setColor(0.0f, 0.0f, 0.0f, 0.6f);
    AreaNameLabel.setFontSize(18);
    return AreaNameLabel;
    }

    Then to get the attribute from every player when they want to use their GUI you need something like this (i.e. casting the attribute to the correct object type else it won't know what object to create):


    Java
    GuiLabel label = (GuiLabel) player.getAttribute("MinoLabel");
  • Thanks for the code snippets Minotorious! I will digest what you have suggested and see if I can add implement it! :rolleyes:


    No worries about the delay pal - I've been busy pondering animal spawning; it looks to me like there is no API call so a direct database write is in order. Much testing and experimentation will need to be done! :/

  • Hmm my code albeit different does indeed use unique attribute name for labels, which are unique for each player (the set attribute in part uses the players UID).


    Only thing different is my labels are added to panels and then the panels added to the players gui.


    I think I'll rewrite the whole GUI (starting with something super super simple) and see if I can stop the listeners from unregistering / being garbage collected.


    It will probably turn out I've missed a "new" constructor somewhere. 8|

  • If you have a panel with a lot of labels unfortunately each of the labels and the panel separately would need to be added to separate attributes and then all added one at a time to the player's screen. Child - Parent relations are not adhered when adding elements in the RW GUI system. i.e. if I add the parent the children do not get automatically added!

  • Yep that's what I have done, everything works normally (panels buttons etc for all users), but when a player logs off the listener for clicks (for all remaining users) stops working :/ I'm just about to start another one from scratch. See if I can see where I was going wrong.


    And yes - good advice on the onSpawn rather than onConnect - my onConnect method contained a lot! Moved it all to onSpawn instead :)

  • btw just had a thought, do you have a onPlayerDisconnect method and if yes does it contain anything related to the GUI?

  • Yep there is an onPlayerDisconnectEvent but nothing to do with the GUI in there


    Here's an example of how I have done the set attribute


    Code
    GuiLabel confirm = new GuiLabel(" CONFIRM ", 0.45f, 0.5f, true);
    confirm.setClickable(true);
    player.setAttribute("confirm" + player.getUID(), (Object)confirm);



    Code
    public void onPlayerSpawnEvent(PlayerSpawnEvent event) {
    this.GuiPlayerPanels(player);
    }





    Defo a weird one, it's like the event listeners gets garbage collected when a player disconnects.

  • The attributes don't really need to be unique between players, they just need to be unique between plugins (e.g. if one plugin sets a string attribute named Job and another plugin names an int attribute named Job again they will probably crash each other), but that shouldn't be your problem anyway.


    Why are you casting the GuiLabel into an object in the setAttribute() method? imo there is no need for that, but again don't think that is your problem either.


    Do you at any point in your code use the GUIElement .destroy() method?
    Do you by any chance get any errors when other people try to use the plugin GUI after someone has disconnected?

  • Yeah the + player.getUID was added as I've tried (desperately) to fix the problem (same issue though with or without it).


    Casting into a label was just done for personal reasons - ease of reading code! (I know they're all objects and dont need the implicit cast).


    Nope I don't use GUIElement.destroy()


    And nope again, I don't get any kind of errors at all.




    See! A really easy logic bug! :D

  • the same error occurs on "PlayerGuiElementClickEvent" after logging out of a player.
    I've tested it so far that I spend after signing off a player the status of my GuiElemente, they are all right. The data stored in attributes also fit. There will be no event triggered.

  • zumindest ist es nicht nur ich


    und


    "Ich habe einen PlayerDisconnectEvent hinzugefügt und lasse mir player.isListeningForKeyInput() und z.b. player.Attribute.GuiImage.isClickable() für alle Spieler ausgeben, sie sind nach dem Abmelden eines Spielers TRUE. "


    I had not thought of that - good idea for testing! :)


    Although I suspect I will have the same problem as you. . .

  • OK so I have started again on a GUI to try and work this out using the most minimal of code. I have removed the panels and I am just working on one single label that is set to be clickable.


    Here is the complete code for the plugin.


    All it does is listen for a key press (R key). When a player presses R it sets the mouse cursor to visible, so we should be able to click on an element.


    The element is a single label in the bottom left hand corner of the screen with the text "Test text which should be clickable. . ." (of course this is set to setClickable(true).


    Now, when this plugin is run and a player presses the R key the listener onPlayerGuiElementClickEvent is never called whilst clicking on the label?



  • you do not need that, I do not know if that makes any problems. That's to start plugin events, you have a simple function.

    I would use the static fields constant "KEY_R"


    otherwise that looks coherent, only line 40 I would take out. :thumbup:


    HA! Key constants! Thank you noci! That will make life a lot easier than using integers! Danks! :D


    But yes, it does look like it is bugged. Maybe @red51 could confirm this? My GUI plans are all on hold at the moment due to this problem ;(


    @noci Why would you take out line 40? Because player.registerKeys(new int[]{19}); is enough? edit: just checked - if you take out player.setListenForKeyInput(true); you dont get any input from keys including the registered key(s).

  • @noci Why would you take out line 40? Because player.registerKeys(new int[]{19}); is enough? edit: just checked - if you take out player.setListenForKeyInput(true); you dont get any input from keys including the registered key(s).

    Line 40 was related to the first source code. After updating it, it is line 61. Because you do not include a plugin event in this funktion.

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!