Solved ---Login event not working. Please Help ASAP!---

Discussion in 'Plugin Development' started by FireBreath14, Feb 13, 2013.

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

    FireBreath14

    Im trying to create a voting plugin. Players can vote in-game for whatever the poll name is. Anyway, my problem is that my listener class and method arent working. Well, its registering, i just cant seem to have it read the right config file. I'm fairly new, so please help me with this :) here is the main class:

    Code:
    package xVote;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    import org.bukkit.command.*;
     
     
    public class xVote extends JavaPlugin{
       
        public void onEnable() {
            if (this.getConfig().getInt("poll.active") == 1){
            getServer().getPluginManager().registerEvents(new xVoteListener(), this);   
            }       
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
           
            if (cmd.getName().equalsIgnoreCase("Startvote") && args.length == 1){
                //start the poll (with the name <name>) and set the "active" variable to true
                String name = args[0];
                this.getConfig().set("poll.name", name);
                this.getConfig().set("poll.active", 1);
                sender.sendMessage(ChatColor.GREEN+"[xVote] "+ChatColor.BLUE+"You started the poll "+ChatColor.RED+name);
                sender.sendMessage(ChatColor.GREEN+"[xVote] "+ChatColor.BLUE+"It will not end until you run the command "+ChatColor.RED+"/stopvote");
                this.saveConfig();
                return true;
            }
           
            if (cmd.getName().equalsIgnoreCase("Vote") && args.length == 1){
                //We havin' a poll?
                if (this.getConfig().getInt("poll.active") == 1){
                //Yes! so has the sender voted before?
                String sname = sender.getName();
                if (this.getConfig().contains("poll.voters."+sname)){
                    //:O they did! They can't do that!
                    sender.sendMessage(ChatColor.RED+"You can't vote twice!");
                    //make it return true here, if it glitches out
                }else{
                //Nope they're all good! So lets add their vote to the config
                String votee = args[0];
                this.getConfig().set("poll.voters."+sname, votee);
                sender.sendMessage(ChatColor.GREEN+"[xVote] "+ChatColor.BLUE+"Thanks for voting! Your vote has been recorded.");
                this.saveConfig();
                return true;
                }
                }else{
                        //active is NOT true, so sorry no polls so u cant vote
                    sender.sendMessage(ChatColor.RED+"Sorry, we're not having a poll right now!");
                    return true;
                }
            }
           
            if (cmd.getName().equalsIgnoreCase("Endvote") && args.length == 0){
                sender.sendMessage(ChatColor.RED+"You closed all polls!");
                sender.sendMessage(ChatColor.RED+"Please check the config.yml for poll results!");
                sender.sendMessage(ChatColor.RED+"Please delete the config file before beginning another vote!");
                this.getConfig().set("poll.active", 0);
                this.saveConfig();
                return true;
            }
               
            return false;
        }
    }
    
    and here is my listener class:

    Code:
    package xVote;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerLoginEvent;
     
    public class xVoteListener extends JavaPlugin implements Listener{
        @EventHandler
        public void join(PlayerLoginEvent event){
            if (this.getConfig().getInt("poll.active") == 1){
            String name = (String)this.getConfig().get("poll.name");
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.YELLOW+"=======================================");
            player.sendMessage(ChatColor.BLUE+"Hey! Please Vote! "+ChatColor.RED+"/vote");
            player.sendMessage(ChatColor.BLUE+"Vote name: "+ChatColor.RED+name);
            player.sendMessage(ChatColor.YELLOW+"=======================================");
        }
    }
    }
    thx 4 ur help!

    I know that in the listener, i shouldnt be using "this.getConfig..." so, what i want to know, is what i SHOULD be using :)

    bumping. sorry but id like this solved soon!

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

    jorisk322

    Bumping your thread twice in one hour isn't going to make people like you.
    PlayerLoginEvent != PlayerJoinEvent.
    You should use the latter.
     
  3. Offline

    danthonywalker

    Do not extend JavaPlugin outside the main class. Instead make a constructor to initlize the main class.
     
  4. Offline

    FireBreath14

    jorisk322
    kk thanks! and im not the only one bumping a lot lol... but i wont bump for a while now

    so what would that code look like? im sorry im new to this xD

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

    danthonywalker

    Make a variable in your Listener class like this.

    private xVote plugin;

    then the constructor.

    public xVoteListener(xVote plugin)
    {
    this.plugin = plugin;
    }

    So when you try to call for the config you would do plugin.getConfig().
     
  6. Offline

    GodzOfMadness

    Code:
    public static xVote plugin;
     
    public xVoteListener(xVote plugin){
    this.plugin = plugin;
    }
    then use plugin. to access all it's methods available
    so like plugin.getConfig().whatever
    [EDIT] Honestly whenever i post someone is a little ahead of me on things.... xD
     
  7. Offline

    FireBreath14

    haha thanks guys! i think im good now. ill test it out and see if it works!

    okay i added a constructor and changed this.getConfig to plugin.getConfig. Everythings fine, but in the xVote class, am i calling the listener right?

    Code:
    getServer().getPluginManager().registerEvents(new xVoteListener(null), this);
    or should the "null" be a "this"?
    I ask because when i login to the server, when it should execute the listener, i instead get a nullpointerexception. whats null?

    GodzOfMadness
    danthonywalker

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

    GodzOfMadness

    it should be: getServer().getPluginManager().registerEvents(new xVoteListener(this), this);
     
Thread Status:
Not open for further replies.

Share This Page