Solved 2 players can't use a wand at same time.

Discussion in 'Plugin Development' started by Asgernohns, Aug 9, 2013.

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

    Asgernohns

    Hi all java programmers! :)

    i made a wand plugin with 4 modes on. but when i change my mode, my freind's mode also change.
    Somebaody have a solution for this bug? :)
    Here is the code:
    Code:java
    1. public class eventfun extends JavaPlugin implements Listener{
    2.  
    3. public final Logger Logger = java.util.logging.Logger.getLogger("Minecraft");
    4. public static eventfun plugin;
    5.  
    6. public void onEnable(){
    7. PluginDescriptionFile pdfFile = this.getDescription();
    8. this.Logger.info(pdfFile.getName()+" v"+pdfFile.getVersion()+" Has Been Enabled!");
    9. this.getServer().getPluginManager().registerEvents(this, this);
    10. }
    11.  
    12. public int wandmodes = 1;
    13. @EventHandler
    14. public void onPlayerInteract(PlayerInteractEvent e){
    15. Player p = e.getPlayer();
    16. if(p.getItemInHand().getType() == Material.BLAZE_ROD){
    17. if((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)){
    18. if(wandmodes < 4){
    19. if(wandmodes == 1){
    20. p.sendMessage(ChatColor.GOLD+"Lightning mode");
    21. }else if(wandmodes == 2){
    22. p.sendMessage(ChatColor.GOLD+"Teleport mode");
    23. }else if(wandmodes == 3){
    24. p.sendMessage(ChatColor.GOLD+"Explosion mode");
    25. }else if(wandmodes == 0){
    26. p.sendMessage(ChatColor.GOLD+"Fireball mode");
    27. }
    28.  
    29. wandmodes++;
    30.  
    31. }else if (wandmodes == 4){
    32. wandmodes--;
    33. wandmodes--;
    34. wandmodes--;
    35. wandmodes--;
    36. }
    37. }
    38. if((e.getAction() == Action.LEFT_CLICK_AIR) || (e.getAction() == Action.LEFT_CLICK_BLOCK)){
    39. if(wandmodes == 1){
    40.  
    41.  
    42. p.launchProjectile(Fireball.class);
    43.  
    44.  
    45.  
    46. }else if(wandmodes == 2){
    47.  
    48.  
    49. Location loc = p.getTargetBlock(null, 500).getLocation();
    50. p.getWorld().strikeLightning(loc);
    51.  
    52. }else if(wandmodes == 3){
    53.  
    54.  
    55. Location loc = p.getTargetBlock(null, 500).getLocation();
    56. p.teleport(loc);
    57.  
    58.  
    59.  
    60. }else if(wandmodes == 4){
    61.  
    62. Location loc = p.getTargetBlock(null, 500).getLocation();
    63. p.getWorld().createExplosion(loc, 10);
    64.  
    65.  
    66. }
    67. }
    68.  
    69. }
    70.  
    71.  
    72. }
    73.  
    74.  
    75. }
    76.  
     
  2. Offline

    Tarestudio

    Asgernohns
    Make a map instead of a single int. map with <String, int> where the string is players name. at event check if player has entry, if not set to value, else set new value
     
  3. Offline

    lycano

    Im sorry that it hits you now but why is it that so many people including you do define their plugin instance as static?
     
  4. Offline

    Deleted user

    Asgernohns
    Check your brackets...extra bracket after wandmode==0
    And you can just do if (wandmode == 4) { wandmode=0; }

    Also, don't have if(wandmode == blah) more than once
    It creates a conflict

    i.e.
    if(wandmode == 4){
    wandmode = 0;
    //EXPLOSION STUFF
     
  5. Offline

    Asgernohns

    lycano i saw a tutorial where he did it.

    Tarestudio can you give out a code for it so i understand it better?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    raGan.

    Asgernohns
    You really need to change this.
    Code:
    else if (wandmodes == 4){
        wandmodes--;
        wandmodes--;
        wandmodes--;
        wandmodes--;
    }
    to something more like this
    Code:
    else if (wandmodes == 4){
        wandmodes -= 4;
    }
    Edit: removed one sentence, missed the line in your code

    Edit2: Or even to
    Code:
    else {
        wandmodes = 0;
    }
     
  7. Offline

    kuben0

    This is because you have only one variable for wandmodes. All users share the same variable.

    What you need, is one variable for every player. The solution which you can use is a HashMap. Basically you declare it like this:

    Code:java
    1. Map<Player, Integer> wandmodes = new HashMap<Player, Integer>();


    Now you have an empty HashMap where you can put a player as the key and his wandmode as the value with the put(Player player, Integer wandmode) method. To get the wandmode of a player you would do
    Code:java
    1. if(!wandmodes.containsKey(e.getPlayer())){
    2. //If the player doesn't have a wandmode you'll need to set one.
    3. wandmodes.put(e.getPlayer(), 1);
    4. }
    5. //Now you can get the wandmode for the player
    6. int wandmode = wandmodes.get(e.getPlayer());


    Then you can use wandmode in the code.
     
  8. Offline

    Tarestudio

    Asgernohns
    1. you can answer everyone in one post ;)
    2. instead of 'public int wandmodes = 1;' use 'HashMap<String, Integer> wandmodes = new HashMap<String, Integer>();'
    After defining p in your eventhandler add 'int wandmode = wandmodes.containsKey(p.getName()) ? wandmodes.get(p.getName()) : 1;'
    at the end of your rightclick-part add 'wandmodes.put(p.getName(), wandmode);'
    And change every other 'wadmodes' to 'wandmode'

    kuben0
    You should NEVER store an instance of player to a longliving variable. this causes memoryleaks.
     
  9. Offline

    lycano

    Asgernohns Can you give me the link to the tutorial? I want to slap his face.

    Tarestudio never do HashMap<String, Integer> name = new HashMap<String, Integer>(); alsways use the base class to init it so you can convert it when needed. Map<String, Integer> varName = new HashMap<String, Integer>(); is the right way to do it.

    Same for lists dont do ArrayList<String> ... = new ArrayList<String>(); use List<String> ... = new ArrayList<String>();

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

    raGan.

    lycano
    The best thing about that static variable is that it's actually never initialized.
     
  11. Offline

    lycano

    raGan. i tried not to comment that as he might have gotten it from somewhere else but yes ... declaring unused variables makes me cry too.
     
  12. Offline

    Asgernohns

    Tarestudio I got one error. Should i remane 'contains' to 'containsKey' or 'containsValue'?
     
  13. Offline

    lycano

    Asgernohns check out the java docs there is no contains for Maps only containsKey and containsValue
     
  14. Offline

    Asgernohns

    lycano is that i am saying and it's not just a map but a hashmap.
     
  15. Offline

    lycano

    Asgernohns where do you think is containsKey coming from? The base is a Map ...

    anyways when you want to get the key use containsKey, if you want to get all values use containsValue. In case of checking for the players name which is the key in the Map use containsKey.
     
  16. Offline

    Tarestudio

    Asgernohns
    You were faster then I thought, I edited my code to containsKey 2min after posting it ^^

    lycano
    Yeah, perhaps I should switch to use Map. ^^
     
  17. Offline

    Asgernohns

    lycano sorry but never worked with maps before so i don't now much :/

    thanks guys! It works now :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  18. Offline

    newboyhun

  19. Offline

    lycano

    Asgernohns don't get me wrong when i type stuff =) If you dont know it maybe you should look at the java docs from oracle ^^
     
  20. Offline

    Asgernohns

    lycano yes but i prefure videos :)
     
Thread Status:
Not open for further replies.

Share This Page