Bug or shit coding?

Discussion in 'Plugin Development' started by Smex, Oct 6, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Smex

    Well, so im trying to get an egg's id on a event than store it on a public
    variabel "r" and use this again in another event.
    The thing is, that the "r" get's reseted for some reason on the second event, to 0.

    And so my if statment on the PlayerEggThrow doesnt work, sure.

    Here's the code:
    Code:
        public int r;
    
        @Override
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            int pearl = player.getItemInHand().getTypeId();
            if(Action.RIGHT_CLICK_BLOCK != null && pearl == 368){
    
                Egg zum = player.throwEgg();
                r = zum.getEntityId();
                System.out.println(r);
            }
        }
        @Override
        public void onPlayerEggThrow(PlayerEggThrowEvent event) {
            Player player = event.getPlayer();
            Egg rumba = event.getEgg();
            System.out.println(r);
            int a = event.getEgg().getEntityId();
            System.out.println(a);
            if(a == r){
                player.sendMessage("Ei geworden!");
                Location pos = rumba.getLocation();
                player.teleport(pos);
            }else{
                System.out.println("gubatschah");
            }
            return;
        }
    After printing out the "r" on the PlayerEggThrowEvent, it shows me the 0.
     
  2. Offline

    resba

    Wrong Forum. Moved to Plugin Development.
     
  3. Offline

    Lolmewn

    What does the log display?
     
  4. Offline

    Smex

    956 <--- r on PlayerInteractEvent
    0 <--- r on PlayerThrowEvent
    956 <--- a

    It makes absolutly non sense.
     
  5. Offline

    AinSophAur

    Maybe the method resets the entity id before its done running in the end.
     
  6. Btw, Action.RIGHT_CLICK_BLOCK is never null. I don't see why you need onPlayerInteract anyway.
    You want to teleport the player to the egg's location, right?
     
  7. Offline

    Belf

    I suppose he only want to trigger it with the Pearl.


    @Smex , i think the problem is with the r variable. It's not a very good way to stock the data... You could create a hashmap with Player (or Player.getName()) and r, something like that (it's a little dirty) :

    Code:
        HashMap<String, Integer> playerEggs = new HashMap<String, Integer>();
    
        @Override
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            int pearl = player.getItemInHand().getTypeId();
            if(Action.RIGHT_CLICK_BLOCK != null && pearl == 368){
    
                Egg zum = player.throwEgg();
                int r = zum.getEntityId();
                System.out.println(r);
    
                playerEggs.put(player.getName(), r);
            }
        }
    
        @Override
        public void onPlayerEggThrow(PlayerEggThrowEvent event) {
            Player player = event.getPlayer();
            Egg rumba = event.getEgg();
            System.out.println(r);
            int a = event.getEgg().getEntityId();
            System.out.println(a);
    
            if(playerEggs.containsValue(a)){
                player.sendMessage("Ei geworden!");
                Location pos = rumba.getLocation();
                player.teleport(pos);
    
                playerEggs.remove(player.getName());
    
            }else{
                System.out.println("gubatschah");
            }
            return;
        }
     
    Smex likes this.
  8. Offline

    nisovin

    How are you registering your events? A lot of people create multiple listener objects when registering their events (which is wrong).
     
    Smex likes this.
  9. Offline

    Smex

    @Belf
    I will test this. Thanks.

    @nisovin
    So, how would I do it than, when this are two diffrent events?
    I've got in the main class the 2 listeners, as thats look sensefull to me
    but tell me more about your solution im free for suggestions/help!

    Ok it looks like it also resets the value in the hashmap. = doesnt work.
    So, why the hell does the PlayerInteractEvent reset my value r?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  10. Offline

    Belf

    @Smex the only way the value is reset is only if :

    - playerEggs.put(player.getName(), r); is called with r=0
    - playerEggs.remove(player.getName()); is called
    - The Hashmap do not stock properly the value

    If the Hashmap do not stock properly the value, well :

    HashMap<String, Integer> playerEggs = new HashMap<String, Integer>();

    has to be declared in another place, it's not a very proper way to do it... If you want to test it quickly, use static and declare it on your Plugin Main class
     
  11. Offline

    Smex

    It looks like it works when I make it with static but why should I declare it on the main class
    and not my players.java? It works now when I set it static on my player.java.

    Could you explain me what static really means?
     
  12. Offline

    Belf

    I said main class because it was quick (and i do not have your source code), but in player.java it's fine.
    Btw, static is not the best way to do it (it was just for debugging purpose) : http://www.javatutorialhub.com/java-static-variable-methods.html

    You should use some get / set / add methods to manage your hashmap
     
  13. Offline

    nisovin

    You most likely registered your events something like this:
    Code:
    PluginManager.registerEvent(Event.Type.BLAH, new Listener(), Event.Priority.Blah, this);
    PluginManager.registerEvent(Event.Type.BLAH, new Listener(), Event.Priority.Blah, this);
    PluginManager.registerEvent(Event.Type.BLAH, new Listener(), Event.Priority.Blah, this);
    
    However, you should register your events like this:
    Code:
    Listener listener = new Listener();
    PluginManager.registerEvent(Event.Type.BLAH, listener, Event.Priority.Blah, this);
    PluginManager.registerEvent(Event.Type.BLAH, listener, Event.Priority.Blah, this);
    PluginManager.registerEvent(Event.Type.BLAH, listener, Event.Priority.Blah, this);
    
    That way you only create one Listener, instead of multiple ones. This is most likely why you had this problem. Your variable wasn't getting reset, you actually had multiple different variables in different objects.
     
  14. Offline

    Smex

    I will you this in the future, thank you :)
     
Thread Status:
Not open for further replies.

Share This Page