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.
956 <--- r on PlayerInteractEvent 0 <--- r on PlayerThrowEvent 956 <--- a It makes absolutly non sense.
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?
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; }
How are you registering your events? A lot of people create multiple listener objects when registering their events (which is wrong).
@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.
@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
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?
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
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.