Solved Unbanning players?

Discussion in 'Plugin Development' started by Astrophylite, Sep 1, 2015.

Thread Status:
Not open for further replies.
  1. Hey all,

    I think you guys are starting to see a pattern here. Everything is server essentials with this plugin :p
    Anyways, I have just created a ban/kick command for my plugin and when banning players, it adds them to a file called "data.yml" under the "banned-players" configuration section. I have made the unban command, but it is erroring on line 26:
    Code:
    if(settings.getData().getString("banned-players." + target.getName() + ".banned") == "true") {
    The ban command adds the "banned" part to the player's ban record so I don't see why it is erroring.

    Error Log (open)

    Code:
    [10:48:58 WARN]: Unexpected exception while parsing console command "unban _Zircon_"
    org.bukkit.command.CommandException: Unhandled exception executing command 'unban' in plugin ZirconEssentials v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
            at me.zircon.zirconessentials.commands.moderation.UnBan.onCommand(UnBan.java:26) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            ... 8 more
    >unban _zircon_
    [10:49:18 WARN]: Unexpected exception while parsing console command "unban _zircon_"
    org.bukkit.command.CommandException: Unhandled exception executing command 'unban' in plugin ZirconEssentials v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
            at me.zircon.zirconessentials.commands.moderation.UnBan.onCommand(UnBan.java:26) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            ... 8 more


    Any help will be appreciated,
    _Zircon_

    Also, I am taking suggestions for what I can code next as my "Essentials" plugin (yes, I am making a revamped version of Essentials) is coming to a stop.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @_zircon_
    1. Use getBoolean instead
    2. Strings can't be compared with ==
    3. Is settings != null
    4. Is getData() returning null?
    5. Is the value set?
     
  3. @timtower You are too quick at responding :p
    1. Okay,
    2. Oh yeah...
    3. Nope
    4. Nope
    5. Yup
    And I will try to do number 1.

    Thanks,
    _Zircon_

    EDIT: I'm so done... I changed line 26 to this:
    Code:
    if(settings.getData().getBoolean("banned-players." + target.getName() + ".banned") == true) {
    but the error is still:
    Error Log (open)

    Code:
    [11:05:16 WARN]: Unexpected exception while parsing console command "unban _Zircon_"
    org.bukkit.command.CommandException: Unhandled exception executing command 'unban' in plugin ZirconEssentials v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
            at me.zircon.zirconessentials.commands.moderation.UnBan.onCommand(UnBan.java:26) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            ... 8 more
     
  4. Offline

    timtower Administrator Administrator Moderator

    @_zircon_ Perk of being under moderation
     
  5. @timtower Can I ask why am I under moderation? Is it because I am a new user? :p
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. @timtower I thought so.
    Anyways, is there any reason why that line is erroring with a "NullPointerException" ?
     
  8. Offline

    timtower Administrator Administrator Moderator

    @_zircon_ Is target null?
    If not: check all variables to be sure.
     
  9. @timtower Uhh. When unbanning, yeah, the target will be null. :p
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Also, I am using a SettingsManager, and when it copys the default config, it doesn't take over all of the comments. Why is this ??

    SettingsManager Code (open)

    Code:
    package me.zircon.zirconessentials.other;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    
    import me.zircon.zirconessentials.miscellaneous.Utils;
    
    public class SettingsManager {
    
        private SettingsManager() { }
        static SettingsManager instance = new SettingsManager();
        public static SettingsManager getInstance() {
            return instance;
        }
       
        Plugin p;
        FileConfiguration config;
        File cFile;
        FileConfiguration data;
        File dFile;
       
        public void setup(Plugin p) {
            cFile = new File(p.getDataFolder(), "config.yml");
            config = p.getConfig();
            config.options().copyDefaults(true);
            saveConfig();
           
            if(!p.getDataFolder().exists()) {
                p.getDataFolder().mkdir();
            }
           
            dFile = new File(p.getDataFolder(), "data.yml");
           
            if(!dFile.exists()) {
                try {
                    dFile.createNewFile();
                }
                catch(IOException e) {
                    Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml!");
                }
            }
           
            data = YamlConfiguration.loadConfiguration(dFile);
        }
       
        public FileConfiguration getData() {
            return data;
        }
       
        public void saveData() {
            try {
                data.save(dFile);
            }
            catch(IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save data.yml!");
            }
        }
       
        public void reloadData() {
            data = YamlConfiguration.loadConfiguration(dFile);
        }
       
        public FileConfiguration getConfig() {
            return config;
        }
       
        public void saveConfig() {
            try {
                config.save(cFile);
            }
            catch(IOException e) {
                Utils.instance().getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
            }
        }
       
        public void reloadConfig() {
            config = YamlConfiguration.loadConfiguration(cFile);
            Utils.instance().reloadConfig();
        }
       
        public PluginDescriptionFile getDesc() {
            return p.getDescription();
        }
    }
    
     
  12. Offline

    timtower Administrator Administrator Moderator

    @_zircon_ That is something with the YamlConfiguration, it doesn't copy them.
     
  13. 1. How do I actually fix that?
    2. How do I fix the nullpointerexception?
     
  14. Offline

    timtower Administrator Administrator Moderator

    @_zircon_ 1. You need to make your own method to copy everything over, or add comments on the plugin page when you upload it.
    2. Don't get the Player (assuming you are doing that), get the OfflinePlayer or just use the name in the arguments
     
  15. 1: Okey
    2: Done.

    EDIT: Mmkay... So I run /unban _Zircon_ and it just broadcasts (like it should) "Player _Zircon_ has been unbanned by CONSOLE" but doesn't actually unban me ?
     
    Last edited: Sep 1, 2015
  16. Offline

    oceantheskatr

    Also, you should probably use UUIDs instead of player names as a user can change their name to ban-evade. You can do this by changing target.getName() to target.getUniqueId().toString() instead. (Only if you want though!)
     
  17. Last edited: Sep 4, 2015
  18. Okey. So I have brought back this thread because I can't be bothered to make a new one.
    Anyways, I have improved my configuration system to now use UUIDs (which was a long time (ish) coming), but when I attempt to unban the player, it doesn't work. It just returns saying the the specified player is not banned.

    My Unban Code (open)

    Code:
    package me.zircon.zirconessentials.commands.moderation;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class UnBan implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(settings == null || settings.getData() == null) {
                Bukkit.getLogger().info(ChatColor.RED + "There was an error with the SettingsManager!");
                return false;
            }
           
            if(cmd.getName().equalsIgnoreCase("pardon")) {
                if(!sender.hasPermission("zirconessentials.ban.pardon")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to unban players!");
                    return false;
                } else {
                    if(args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Usage: /" + label + " <player>");
                        return false;
                    }
                    if(settings.getData().getString("players." + Bukkit.getOfflinePlayer(args[0]).getUniqueId().toString() + ".banned") == "true") {
                        settings.getData().set("players." + Bukkit.getOfflinePlayer(args[0]).getUniqueId().toString() + ".banned", null);
                        settings.saveData();
                        settings.reloadData();
                        Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + "Player " + args[0] + " has been unbanned by " + sender.getName() + "!");
                        return true;
                    } else {
                        if(settings.getData().getString("players." + args[0] + ".banned") == null) {
                            sender.sendMessage(ChatColor.RED + "The player specified is not banned!");
                            return false;
                        }
                    }
                }
            }
            return false;
        }
    }
    


    Any help will be appreciated,
    _Zircon_
     
  19. Offline

    DoggyCode™

    ... I'd suggest you actually learn Bukkit (or java to be more exact) before doing this.. There's just too much wrong to pick out.
     
  20. Exactly what I hate. When people tell you to learn Bukkit/Java and don't tell you what you've done wrong. -,-

    1. I am new to Bukkit Coding.
    2. I am still learning - basing my code off of a Bukkit Playlist.

    Thank you @oceantheskatr !
     
    Last edited by a moderator: Sep 4, 2015
  21. Offline

    oceantheskatr

    Code:
                     } else {
                       if(settings.getData().getString("players." + args[0] + ".banned") == null) {
                            sender.sendMessage(ChatColor.RED + "The player specified is not banned!");
                           return false;
                       }
    Line two here you don't get the offline player and then their UUID whereas you have the other times.

    if(settings.getData().getString("players."+ Bukkit.getOfflinePlayer(args[0]).getUniqueId().toString()+".banned")==null){

    Should be that I think. ^
     
  22. Offline

    RoboticPlayer

    Also, instead of doing
    Code:
    } else {
        if  (/*if statement*/) {
          //do stuff
    }
    use else if statements:
    Code:
    } else if (/* if statement */) {
    // do stuff
    }
     
    oceantheskatr likes this.
  23. I do them from time to time, then I realise I need to have multiple if statements under that else if so yahh...
    Thanks for the tip though @henderry2019
     
  24. Offline

    bean710

    You can cave multiple 'else if' statements:
    Code:
    if (bowtiesAreCool == true) {
        //Do things
    } else if (fezzesAreCool == true) {
        //Do other things
    } else if (you.doNotKnowDoctorWho) {
        //Probably cry
    } else {
        //Do the rest of the things
    }
     
Thread Status:
Not open for further replies.

Share This Page