Solved Hashmap dosen't work in listener? Help pls I want to continue coding I need to finish this

Discussion in 'Plugin Development' started by Lightspeed, Feb 4, 2015.

?

Why does this not work?

  1. Your Player in listener is wrong

    0 vote(s)
    0.0%
  2. Your problem is unkown

    0 vote(s)
    0.0%
  3. It should work

    100.0%
Multiple votes are allowed.
Thread Status:
Not open for further replies.
  1. Offline

    Lightspeed

    I have a hub command able to set delayed tp del hub and cancel delayed tp When ever I use a Listener to cancel the task if there IN the hashmap <String, Location> after In command put the p.getname and p.getlocation
    In the listener if HubDelayCancel <-- thehashmap containskey event.getplayer.getname
    Too crappy of explaination check my Code: http://pastie.org/pastes/9887004/text?key=enktfamn167vi4bnog3qpq

    I can't get the players in the list to cancel there task if they move on the block where they did the command or farther

    {Thank you mod for removing all uneeded replys}
     
    Last edited: Feb 4, 2015
  2. Offline

    Skionz

    @Lightspeed I have to say, I hate Allman. Can you change it to a more readable format?
     
  3. Offline

    Lightspeed

    http://pastie.org/pastes/9887183/text?key=hjoo8dywhvgykdffpniz9q this better i used the java format

    Ok change the subject> Maybe nooby question incomming> How can i check whats in my hashmap
    Map<String, Integer> HubDelayList = new HashMap<String, Integer>(); Like this
    **
    To check if I am adding strings wrong

    Code:
    @EventHandler
    public void onMove(PlayerMoveEvent e)
    {
    Player p = e.getPlayer();
    if (HubDelayList.keySet().contains(p))
    {
    System.out.println("Hey");
    }
    else
    {
    System.out.println("null");
    }
    }
    Check the command when im in the 8 second queue and walk around it still says null :c

    yes in the hashmap every time i do the command /hub it counts up 1 im at number 73 now so i tested this plugin 73 times with fails thats sad :'(

    everytime i just let it tp me it counts up 2 Weht is this?

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Feb 5, 2015
  4. Offline

    SuperOriginal

    An edit button exists for a reason.

    Why would a Player be in a HashMap with Strings and Integers? Also proper naming conventions would be nice.
     
  5. Offline

    Lightspeed

    Because i use it to delay and cancel delay for my command i need a way so when the player that is delayed in the same list or diff i need it to cancel from movement only if there in a list.

    I would also like it if it they moved a block not moving there view like essentials
     
    Last edited by a moderator: Feb 4, 2015
  6. Offline

    SuperOriginal

    @Lightspeed A player's NAME is not equal to a player OBJECT, in your example you're comparing a Player to a string which is never true.

    As for your other request, In the playermoveevent, if their x and z are the same, don't cancel it.
     
  7. Offline

    Lightspeed

    How could i fix this im a little confused

    Map<String, Location> HubDelayList = new HashMap<String, Location>(); Is what I ment before
    Edit:Sorry for double posting**I look at my old code because I forgot bukkit with my pc broken i forgot stuff and trying to remember it sorry**

    EDIT by Timtower: merged posts

    EDIT by Lightspeed: No it was old code I need a players name I messed up Players name is a string yes.
     
    Last edited: Feb 5, 2015
  8. Offline

    teej107

    @Lightspeed STOP double posting. Use the edit button! When you get a value from a key in a Map, it looks for the key and then gets the value associated with the key. If no matches are found, then null will be returned.
    1. Use the code tags
    2. Do you really think a String is equal to a Player?
     
  9. Offline

    Lightspeed

    I need a lil help with hashmaps maybe a good tut or java page i could read or a little explaination
    HashMap<Whatisthis, Whatisthis>
    first one Key and second Value correct?
     
    Last edited: Feb 5, 2015
  10. Offline

    CullanP

    Grab the players name, Player != String and yes, hashmaps have keys and values, there are plenty of ways to get either or key/value. The simple way to access a value would be to use hashname.get(Object key)

    If you only want to get the key you should use an iterator to iterate through all the playernames
     
  11. Offline

    Lightspeed

    Can I get everything inside the hashmap and print it in console or in my config to see if im doing anything wrong and for more testing?
     
  12. Offline

    CullanP

    Yep. Are you trying to grab the value associated with the key? Or only grab the key? You can show everything in the hashmap by converting it to string
     
  13. Offline

    Lightspeed

    Im trying to grab the key the first one that is the player what ever that one is and Player != String what?
    Can i have an example of converting it into a string?​
     
    Last edited: Feb 5, 2015
  14. Offline

    SuperOriginal

    Oh... I don't know... Maybe.. Their name???
     
  15. Offline

    Lightspeed

    Thats to view it || or maybe i read your reply wrong
    I got the listener working setup Now i just need a list of strings that works
    For some reason when I check if the List Contains Event player it dosent work and yes i add them to list

    HashMap: Map<String, Integer> HubDelayList = new HashMap<String, Integer>();

    Player: Player p = (Player) sender;
    Command: if(HubDelayList.containsKey(p.getName)) { <--- This Works
    Player p = e.getPlayer();
    Listener: has the same code as above for the command <--- Dosen't work?

    Does it have to do with how im getting the player for the listener? Or am I not getting somthing

    I use pastie so you it wont cut parts of the code off with raw :D
    My full commandExecutor code: http://pastie.org/pastes/9893671/text?key=3bxhbhs9tkbc6mmfe8ywgw

    Changed String ti UUID it works fine just at listener it won find the uuid like obove in command
    What did I do wrong???!?!?!?!

    EDIT:
    @teej107 No i messed up a player name is equal to a string

    containsKey not containsValue or whatever above in code
    unfortuntly I can't continue with any other code till I get this problem done :'(
     
    Last edited by a moderator: Feb 6, 2015
  16. Offline

    Lightspeed

    If I try to check if the map is empty it dosent work in the listener What the crap? does it not work in listeners at all?????
     
  17. Offline

    eyamaz

    @Lightspeed

    Please start making use of the edit button.
     
    Lightspeed likes this.
  18. Offline

    xTrollxDudex

    Register the command executor and listener to the same instance.
     
  19. Offline

    guitargun

    @Lightspeed hashmaps do work in listeners.
    you problem lies in your main class I think.
    what i think you are doing is having this
    Code:
            pm.registerEvents(new Hub(this), this);
            getCommand("command").setExecutor(new Hub(this));
    what happens when you load the plugin the server makes 2 different Hub references, resulting that when filling the hasmap in one reference the other stays null.

    the solution to this is make a instance of it in the main and use the instance instead of the new Hub(this).
     
  20. Offline

    Lightspeed

    @guitargun Yes I did that I didn't have a command and listner before In anotehr class This is how i organize :p
    Code:
    package net.core;
    
    import net.core.commands.hub.Hub;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    /** @author Lightspeed */
    public class Core extends JavaPlugin
    {
    
        /** Enable */
        public void onEnable()
        {
            this.ic();
            this.il();
        }
    
        /** Disable */
        public void onDisable()
        {
            //Code
        }
    
        /** Initialize Commands */
        public void ic()
        {
            getCommand("hub").setExecutor(new Hub(this));
        }
    
        /** Initialize Listeners */
        public void il()
        {
            Hub h = new Hub(this);
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(h, this);
        }
    
        /** Initialize BukkitRunnable */
        public void ib()
        {
            //Code
        }
    }
    
    New Code:
    Code:
    package net.core;
    
    import net.core.commands.hub.Hub;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    /** @author Lightspeed */
    public class Core extends JavaPlugin
    {
    
        Hub h = new Hub(this);
    
        /** Enable */
        public void onEnable()
        {
            this.ic();
            this.il();
        }
    
        /** Disable */
        public void onDisable()
        {
            //Code
        }
    
        /** Initialize Commands */
        public void ic()
        {
            getCommand("hub").setExecutor(h);
        }
    
        /** Initialize Listeners */
        public void il()
        {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(h, this);
        }
    
        /** Initialize BukkitRunnable */
        public void ib()
        {
            //Code
        }
    }
    
    Hub hub = new Hub(this); gives me errors Can you give me an example I get confused with instances it would help me understand them better :)
    EDIT: ERROR: Cannot get plugin for class net.core.Core from a static initializer
    and then everything breaks every other class has a error package.<init>ClassName
     
    Last edited: Feb 7, 2015
  21. Offline

    coasterman10

    You should be initializing the Hub object in your onEnable() method.

    Please post your full code so we can find where the static initializer is causing an error.
     
  22. Offline

    Lightspeed

    Ok I did private Hub h;

    Enable: h = new Hub(this); in the listener it checks if there in THANKYOU I can now create pvp minigames without people tping away so they don't get killed or I make it just kill them and drop there items :) I have it display a msg just moving and one if there in list when I put myself in the delay I can see the difference in chat :)
    Edit: The error was when I had just Hub h = new Hub(this); outside of main that caused the error :) fixed
     
    Last edited: Feb 8, 2015
Thread Status:
Not open for further replies.

Share This Page