Solved File cannot be null error

Discussion in 'Plugin Development' started by ColaCraft, May 4, 2013.

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

    ColaCraft

    I am getting this error with my code, & I cannot for the life of me figure out what it is...

    Here is my code.

    Code:
    package me.PorterK.modstick;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("deprecation")
    public class main extends JavaPlugin
    {
        main plugin;
     
        public void onEnable()
        {
            FileConfiguration config = getConfig();
            config.options().header("Please do not touch this file! If you have an issue send in a ticket!");
            config.addDefault("version", "1.0");
            saveConfig();
        }
        public void onDisable(){
        }
     
        FileConfiguration config = getConfig();
        public String pname;
        public static Player p;
     
        public boolean onCommand( CommandSender sender, Command command, String label, String[] args ){
            if(args[0].equalsIgnoreCase("addmod")){
                if (!sender.hasPermission("modstick.adduser")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else{
                    String user = sender.getName();
     
                    config.addDefault(user + ".advertsing", false );
                    config.addDefault(user + ".chatviolation", false);
                    config.addDefault(user + ".spam", false);
                    config.addDefault(user + ".isOnDuty", false);
     
                }
            if(args[0].equalsIgnoreCase("remmod") || args[0].equalsIgnoreCase("removemod") || args[0].equalsIgnoreCase("delmod")){
                if(!sender.hasPermission("modstick.remmod")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else if (config.contains(args[1])){
                    config.set(args[1], null);
                    sender.sendMessage(ChatColor.DARK_RED + "Player " + ChatColor.GREEN + args[1] + ChatColor.DARK_RED + " has been removed as a moderator!");
                }else if(!config.contains(args[1])){
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not a Moderator!");
                }
            }
            if(args[0].equalsIgnoreCase("toggleonduty") || args[0].equalsIgnoreCase("tod")){
                if(!sender.hasPermission("modstick.toggleonduty")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else if(config.getBoolean(sender + ".isOnDuty") == true && config.contains(sender + ".isOnDuty")){
                    config.set(sender + ".isOnDuty", false);
                }else if(config.getBoolean(sender + "isOnDuty") == false && config.contains(sender + ".isOnDuty")){
                    config.set(sender + ".isOnDuty", true);
                }else if(!config.contains(sender + ".isOnDuty")){
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: You're not listed as a moderator in the database!");
                }else{
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occured");
                }
            }
            }
            return true;
        }
     
     
        public void onPlayerUse(PlayerInteractEvent event){
            main.p = event.getPlayer();
     
            if(config.getBoolean(p + ".isOnDuty") == true){
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().getType() == Material.PUMPKIN_STEM){
                       
                        String user = p.getName();
                        config.set(user + ".advertising", true);
                       
                        p.sendMessage(ChatColor.GOLD + "You're about to BAN someone for ADVERTISING!");
                        p.sendMessage(ChatColor.RED + "Who do you wish to BAN?");
     
                        Player name = plugin.getServer().getPlayer(this.pname);
     
                        if(name != null){
                            if(name.isOnline()){
     
                                name.setBanned(true);
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been banned for advertising!");
                                config.set(user + ".advertising", false);
                            }else if(name.isBanned()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User is already banned!");
                                config.set(user + ".advertising", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User " + ChatColor.GREEN + "has " + ChatColor.DARK_RED + "been found, but is not online!");
                                config.set(user + ".advertising", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(user + ".advertising", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".advertising", false);
                        }
                    }
     
                }
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().equals(Material.MELON_STEM)){
                        String user = p.getName();
                        config.set(user + ".chatviolation", true);
                       
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for CHAT_VIOLATION");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
                       
                        Player name = plugin.getServer().getPlayer(this.pname);
                       
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You have violated the chat rules!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player " + name + "has been kicked for violating the chat rules!");
                                config.set(user + ".chatviolation", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not online!");
                                config.set(user + ".chatviolatin", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found");
                                config.set(user + ".chatviolatin", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".chatviolatin", false);
                        }
                    }
                }
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().equals(Material.PAPER)){
                        String user = p.getName();
                        config.set(user + ".spam", true);
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for SPAM");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
                       
                        Player name = plugin.getServer().getPlayer(this.pname);
                       
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You've been kicked for spamming!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been kicked for spamming!");
                                config.set(p + ".spam", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is no online!");
                                config.set(p + ".spam", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(p + ".spam", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred!");
                        }
                    }
                }
            }else if(config.contains(p + ".isOnDuty") && config.getBoolean(p + ".isOnDuty") == false){
                p.sendMessage(ChatColor.DARK_RED + "ERROR: You must go on duty first!");
            }
        }
        @SuppressWarnings("deprecation")
        public void onPlayerChat(PlayerChatEvent event) {
            FileConfiguration config = getConfig();
            if (config.contains(main.p + ".advertising") && config.getBoolean(main.p + ".advertising") == true && config.getBoolean(main.p + ".isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".chatviolation") && config.getBoolean(main.p + ".chatviolation") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".spam") && config.getBoolean(main.p + ".spam") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
        }
    }
    Please help!
     
  2. Your 'FileConfiguration config = getConfig();' field is wrongly used, don't store getConfig() as a field because at the time the class is loaded, the plugin is not loaded, therefore it doesn't have a name and it doesn't have a folder and it can't generate a path and ... well, "file is null" :}
     
  3. Offline

    ColaCraft

    So take that line out completely?
     
  4. ColaCraft
    Yes, then use getConfig() directly where that field was used.
     
  5. Offline

    ColaCraft

    I changed that in the onEnable- do I need to change it everywhere?
     
  6. ColaCraft
    Only if they're stored as fields, local variables are fine... unless you plan on using reloadConfig() around there, then you'll need to update the pointer because getConfig() would return a diferent object after that.
     
  7. Offline

    ColaCraft

    Still getting the same error with this code..

    Code:
    package me.PorterK.modstick;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("deprecation")
    public class main extends JavaPlugin
    {
        main plugin;
     
        public void onEnable()
        {
            getConfig();
            config.options().header("Please do not touch this file! If you have an issue send in a ticket!");
            config.addDefault("version", "1.0");
            saveConfig();
        }
        public void onDisable(){
        }
     
        FileConfiguration config = getConfig();
        public String pname;
        public static Player p;
     
        public boolean onCommand( CommandSender sender, Command command, String label, String[] args ){
            if(args[0].equalsIgnoreCase("addmod")){
                if (!sender.hasPermission("modstick.adduser")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else{
                    String user = sender.getName();
     
                    config.addDefault(user + ".advertsing", false );
                    config.addDefault(user + ".chatviolation", false);
                    config.addDefault(user + ".spam", false);
                    config.addDefault(user + ".isOnDuty", false);
     
                }
            if(args[0].equalsIgnoreCase("remmod") || args[0].equalsIgnoreCase("removemod") || args[0].equalsIgnoreCase("delmod")){
                if(!sender.hasPermission("modstick.remmod")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else if (config.contains(args[1])){
                    config.set(args[1], null);
                    sender.sendMessage(ChatColor.DARK_RED + "Player " + ChatColor.GREEN + args[1] + ChatColor.DARK_RED + " has been removed as a moderator!");
                }else if(!config.contains(args[1])){
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not a Moderator!");
                }
            }
            if(args[0].equalsIgnoreCase("toggleonduty") || args[0].equalsIgnoreCase("tod")){
                if(!sender.hasPermission("modstick.toggleonduty")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else if(config.getBoolean(sender + ".isOnDuty") == true && config.contains(sender + ".isOnDuty")){
                    config.set(sender + ".isOnDuty", false);
                }else if(config.getBoolean(sender + "isOnDuty") == false && config.contains(sender + ".isOnDuty")){
                    config.set(sender + ".isOnDuty", true);
                }else if(!config.contains(sender + ".isOnDuty")){
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: You're not listed as a moderator in the database!");
                }else{
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occured");
                }
            }
            }
            return true;
        }
     
     
        public void onPlayerUse(PlayerInteractEvent event){
            main.p = event.getPlayer();
     
            if(config.getBoolean(p + ".isOnDuty") == true){
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().getType() == Material.PUMPKIN_STEM){
                       
                        String user = p.getName();
                        config.set(user + ".advertising", true);
                       
                        p.sendMessage(ChatColor.GOLD + "You're about to BAN someone for ADVERTISING!");
                        p.sendMessage(ChatColor.RED + "Who do you wish to BAN?");
     
                        Player name = plugin.getServer().getPlayer(this.pname);
     
                        if(name != null){
                            if(name.isOnline()){
     
                                name.setBanned(true);
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been banned for advertising!");
                                config.set(user + ".advertising", false);
                            }else if(name.isBanned()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User is already banned!");
                                config.set(user + ".advertising", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User " + ChatColor.GREEN + "has " + ChatColor.DARK_RED + "been found, but is not online!");
                                config.set(user + ".advertising", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(user + ".advertising", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".advertising", false);
                        }
                    }
     
                }
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().equals(Material.MELON_STEM)){
                        String user = p.getName();
                        config.set(user + ".chatviolation", true);
                       
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for CHAT_VIOLATION");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
                       
                        Player name = plugin.getServer().getPlayer(this.pname);
                       
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You have violated the chat rules!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player " + name + "has been kicked for violating the chat rules!");
                                config.set(user + ".chatviolation", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not online!");
                                config.set(user + ".chatviolatin", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found");
                                config.set(user + ".chatviolatin", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".chatviolatin", false);
                        }
                    }
                }
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().equals(Material.PAPER)){
                        String user = p.getName();
                        config.set(user + ".spam", true);
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for SPAM");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
                       
                        Player name = plugin.getServer().getPlayer(this.pname);
                       
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You've been kicked for spamming!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been kicked for spamming!");
                                config.set(p + ".spam", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is no online!");
                                config.set(p + ".spam", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(p + ".spam", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred!");
                        }
                    }
                }
            }else if(config.contains(p + ".isOnDuty") && config.getBoolean(p + ".isOnDuty") == false){
                p.sendMessage(ChatColor.DARK_RED + "ERROR: You must go on duty first!");
            }
        }
        @SuppressWarnings("deprecation")
        public void onPlayerChat(PlayerChatEvent event) {
            FileConfiguration config = getConfig();
            if (config.contains(main.p + ".advertising") && config.getBoolean(main.p + ".advertising") == true && config.getBoolean(main.p + ".isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".chatviolation") && config.getBoolean(main.p + ".chatviolation") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".spam") && config.getBoolean(main.p + ".spam") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
        }
    }
     
  8. FileConfiguration config = getConfig(); is still there and you're using it everywhere...

    Remove it, see where the errors are and replace 'config' with 'getConfig()'.
     
  9. Offline

    ColaCraft

    If I take that out, it wont let me use config.Something in the rest of the code. :confused:
     
  10. Offline

    LazyLemons

    You can still use getConfig().{METHOD}, as it's a FileConfiguration.
     
  11. Offline

    Diemex

    Code:
    public void onEnable()
        {
            getConfig(); //this returns a FileConfiguration, but you are not saving the object anywhere
            //it's like dumping water into a bucket with no bottom, useless call
            config.options().header("Please do not touch this file! If you have an issue send in a ticket!");
            config.addDefault("version", "1.0");
            saveConfig();
        }
        public void onDisable(){
        }
        //I'm not 100% sure how this will be handled in Java, but doesnt onEnable get called before the variables are initialized?
        //fields should always be at the top
        FileConfiguration config = getConfig();
        public String pname;
        public static Player p;
     
  12. Offline

    ColaCraft

    So instead of config.{method} use getConfig.{method} ?

    Okay, that explains the issue- how do I fix it? >.<

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

    Sagacious_Zed Bukkit Docs

    methods have return value, you can invoke the applicable methods on that return.
     
  14. Offline

    Diemex

    Code:
    package me.PorterK.modstick;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("deprecation")
    public class main extends JavaPlugin
    {
      //you don't even use this variable do you?
        main plugin;
     
        public void onEnable()
        {
            getConfig(); //delete
            //either
            getConfig().options().header("Please do not touch this file! If you have an issue send in a ticket!");
            //or
            config.options().header("Please do not touch this file! If you have an issue send in a ticket!");
            config.addDefault("version", "1.0");
            saveConfig();
        }
        public void onDisable(){
        }
        //Move this to the head of the class
        /*FileConfiguration config = getConfig();
        public String pname;
        public static Player p;*/
     
       
        @SuppressWarnings("deprecation")
        public void onPlayerChat(PlayerChatEvent event) {
            //get rid of this extra local variable
            //FileConfiguration config = getConfig();
            if (config.contains(main.p + ".advertising") && config.getBoolean(main.p + ".advertising") == true && config.getBoolean(main.p + ".isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".chatviolation") && config.getBoolean(main.p + ".chatviolation") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".spam") && config.getBoolean(main.p + ".spam") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
        }
    }
     
  15. Offline

    ColaCraft

    So, for example, this

    Code:
    getConfig().addDefault(user + ".advertsing", false );
    will do the same as this

    Code:
    FileConfiguartion config = getConfig();
     
    config.addDefault(user + ".advertsing", false );
    But without this error coming up?

    EDIT: That made no sense now that I am reading it over- sorry,

    The variable is used a couple of times & even with the changes made, the error comes up.

    Here is the newest version of the code.

    Code:
    package me.PorterK.modstick;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("deprecation")
    public class main extends JavaPlugin{
        FileConfiguration config = getConfig();
        main plugin;
       
        public void onEnable()
        {
            getConfig().options().header("Please do not touch this file! If you have an issue send in a ticket!");
            getConfig().addDefault("version", "1.0");
            saveConfig();
        }
        public void onDisable(){
        }
     
        public String pname;
        public static Player p;
     
        public boolean onCommand( CommandSender sender, Command command, String label, String[] args ){
            if(args[0].equalsIgnoreCase("addmod")){
                if (!sender.hasPermission("modstick.adduser")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else{
                    String user = sender.getName();
     
                    getConfig().addDefault(user + ".advertsing", false );
                    getConfig().addDefault(user + ".chatviolation", false);
                    config.addDefault(user + ".spam", false);
                    config.addDefault(user + ".isOnDuty", false);
     
                }
            if(args[0].equalsIgnoreCase("remmod") || args[0].equalsIgnoreCase("removemod") || args[0].equalsIgnoreCase("delmod")){
                if(!sender.hasPermission("modstick.remmod")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else if (config.contains(args[1])){
                    config.set(args[1], null);
                    sender.sendMessage(ChatColor.DARK_RED + "Player " + ChatColor.GREEN + args[1] + ChatColor.DARK_RED + " has been removed as a moderator!");
                }else if(!config.contains(args[1])){
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not a Moderator!");
                }
            }
            if(args[0].equalsIgnoreCase("toggleonduty") || args[0].equalsIgnoreCase("tod")){
                if(!sender.hasPermission("modstick.toggleonduty")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else if(config.getBoolean(sender + ".isOnDuty") == true && config.contains(sender + ".isOnDuty")){
                    config.set(sender + ".isOnDuty", false);
                }else if(config.getBoolean(sender + "isOnDuty") == false && config.contains(sender + ".isOnDuty")){
                    config.set(sender + ".isOnDuty", true);
                }else if(!config.contains(sender + ".isOnDuty")){
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: You're not listed as a moderator in the database!");
                }else{
                    sender.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occured");
                }
            }
            }
            return true;
        }
     
     
        public void onPlayerUse(PlayerInteractEvent event){
            main.p = event.getPlayer();
     
            if(config.getBoolean(p + ".isOnDuty") == true){
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().getType() == Material.PUMPKIN_STEM){
                       
                        String user = p.getName();
                        config.set(user + ".advertising", true);
                       
                        p.sendMessage(ChatColor.GOLD + "You're about to BAN someone for ADVERTISING!");
                        p.sendMessage(ChatColor.RED + "Who do you wish to BAN?");
     
                        Player name = plugin.getServer().getPlayer(this.pname);
     
                        if(name != null){
                            if(name.isOnline()){
     
                                name.setBanned(true);
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been banned for advertising!");
                                config.set(user + ".advertising", false);
                            }else if(name.isBanned()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User is already banned!");
                                config.set(user + ".advertising", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User " + ChatColor.GREEN + "has " + ChatColor.DARK_RED + "been found, but is not online!");
                                config.set(user + ".advertising", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(user + ".advertising", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".advertising", false);
                        }
                    }
     
                }
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().equals(Material.MELON_STEM)){
                        String user = p.getName();
                        config.set(user + ".chatviolation", true);
                       
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for CHAT_VIOLATION");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
                       
                        Player name = plugin.getServer().getPlayer(this.pname);
                       
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You have violated the chat rules!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player " + name + "has been kicked for violating the chat rules!");
                                config.set(user + ".chatviolation", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not online!");
                                config.set(user + ".chatviolatin", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found");
                                config.set(user + ".chatviolatin", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".chatviolatin", false);
                        }
                    }
                }
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(p.getItemInHand().equals(Material.PAPER)){
                        String user = p.getName();
                        config.set(user + ".spam", true);
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for SPAM");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
                       
                        Player name = plugin.getServer().getPlayer(this.pname);
                       
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You've been kicked for spamming!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been kicked for spamming!");
                                config.set(p + ".spam", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is no online!");
                                config.set(p + ".spam", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(p + ".spam", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred!");
                        }
                    }
                }
            }else if(config.contains(p + ".isOnDuty") && config.getBoolean(p + ".isOnDuty") == false){
                p.sendMessage(ChatColor.DARK_RED + "ERROR: You must go on duty first!");
            }
        }
        public void onPlayerChat(PlayerChatEvent event) {
            FileConfiguration config = getConfig();
            if (config.contains(main.p + ".advertising") && config.getBoolean(main.p + ".advertising") == true && config.getBoolean(main.p + ".isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".chatviolation") && config.getBoolean(main.p + ".chatviolation") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
            if (config.contains(main.p + ".spam") && config.getBoolean(main.p + ".spam") == true && config.getBoolean(main.p + "isOnDuty") == true){
            this.pname = event.getMessage();
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  16. Offline

    Diemex

    Code:
    package de.diemex.test;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin implements Listener {
        FileConfiguration config;
       [S] //FileConfiguration config = getConfig();[/S]
        //Fields get initialized before anything else, so you CANNOT call getConfig() it will just
        //return NULL turning your config into a null object, which will cause a NPE once you
       // use the config
        public String pname;
        /*you shall not use static variables and hold player objects if you only need the name
       [S] public static Player p;[/S]*/
     
        public void onEnable()
        {
            config = getConfig();
            config.options().header("Please do not touch this file! If you have an issue send in a ticket!");
            config.addDefault("version", "1.0");
            saveConfig();
            //register for events
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable(){
        }
     
        public boolean onCommand( CommandSender sender, Command command, String label, String[] args ){
            if(args[0].equalsIgnoreCase("addmod")){
                if (!sender.hasPermission("modstick.adduser")){
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                }else{
                    String user = sender.getName();
     
                    getConfig().addDefault(user + ".advertsing", false );
                    getConfig().addDefault(user + ".chatviolation", false);
                    config.addDefault(user + ".spam", false);
                    config.addDefault(user + ".isOnDuty", false);
     
                }
                if(args[0].equalsIgnoreCase("remmod") || args[0].equalsIgnoreCase("removemod") || args[0].equalsIgnoreCase("delmod")){
                    if(!sender.hasPermission("modstick.remmod")){
                        sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                    }else if (config.contains(args[1])){
                        config.set(args[1], null);
                        sender.sendMessage(ChatColor.DARK_RED + "Player " + ChatColor.GREEN + args[1] + ChatColor.DARK_RED + " has been removed as a moderator!");
                    }else if(!config.contains(args[1])){
                        sender.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not a Moderator!");
                    }
                }
                if(args[0].equalsIgnoreCase("toggleonduty") || args[0].equalsIgnoreCase("tod")){
                    if(!sender.hasPermission("modstick.toggleonduty")){
                        sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                    }else if(config.getBoolean(sender + ".isOnDuty") == true && config.contains(sender + ".isOnDuty")){
                        config.set(sender + ".isOnDuty", false);
                    }else if(config.getBoolean(sender + "isOnDuty") == false && config.contains(sender + ".isOnDuty")){
                        config.set(sender + ".isOnDuty", true);
                    }else if(!config.contains(sender + ".isOnDuty")){
                        sender.sendMessage(ChatColor.DARK_RED + "ERROR: You're not listed as a moderator in the database!");
                    }else{
                        sender.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occured");
                    }
                }
            }
            return true;
        }
     
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event){
            Player p = event.getPlayer();
            pname = p.getName();
     
            //switch case statement is so much cleaner
            if(config.getBoolean(p + ".isOnDuty") == true && event.getAction() == Action.RIGHT_CLICK_AIR){
                switch (p.getItemInHand().getType())
                {
                    case PUMPKIN_STEM:
                    {
                        String user = p.getName();
                        config.set(user + ".advertising", true);
     
                        p.sendMessage(ChatColor.GOLD + "You're about to BAN someone for ADVERTISING!");
                        p.sendMessage(ChatColor.RED + "Who do you wish to BAN?");
     
                        Player name = getServer().getPlayer(this.pname);
     
                        if(name != null){
                            if(name.isOnline()){
     
                                name.setBanned(true);
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been banned for advertising!");
                                config.set(user + ".advertising", false);
                            }else if(name.isBanned()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User is already banned!");
                                config.set(user + ".advertising", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User " + ChatColor.GREEN + "has " + ChatColor.DARK_RED + "been found, but is not online!");
                                config.set(user + ".advertising", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(user + ".advertising", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".advertising", false);
                        }
                        break;
                    }
                    case MELON_STEM:
                    {
                        String user = p.getName();
                        config.set(user + ".chatviolation", true);
     
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for CHAT_VIOLATION");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
     
                        Player name = getServer().getPlayer(this.pname);
     
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You have violated the chat rules!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player " + name + "has been kicked for violating the chat rules!");
                                config.set(user + ".chatviolation", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is not online!");
                                config.set(user + ".chatviolatin", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found");
                                config.set(user + ".chatviolatin", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred! Please contact your server administration!");
                            config.set(user + ".chatviolatin", false);
                        }
                        break;
                    }
                    case PAPER:
                    {
                        String user = p.getName();
                        config.set(user + ".spam", true);
                        p.sendMessage(ChatColor.GOLD + "You're about to KICK someone for SPAM");
                        p.sendMessage(ChatColor.RED + "Who do you wish to KICK?");
     
                        Player name = getServer().getPlayer(this.pname);
     
                        if(name != null){
                            if(name.isOnline()){
                                name.kickPlayer("You've been kicked for spamming!");
                                getServer().broadcastMessage(ChatColor.GREEN + "[ModStick]: " + ChatColor.RED + "Player" + name + "has been kicked for spamming!");
                                config.set(p + ".spam", false);
                            }else if(name.hasPlayedBefore() && !name.isOnline()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: That user is no online!");
                                config.set(p + ".spam", false);
                            }else if(!name.hasPlayedBefore()){
                                p.sendMessage(ChatColor.DARK_RED + "ERROR: User not found!");
                                config.set(p + ".spam", false);
                            }
                        }else{
                            p.sendMessage(ChatColor.DARK_RED + "ERROR: An unknown error has occurred!");
                        }
                        break;
                    }
                }
            }else if(config.contains(p + ".isOnDuty") && config.getBoolean(p + ".isOnDuty") == false){
                p.sendMessage(ChatColor.DARK_RED + "ERROR: You must go on duty first!");
            }
        }
     
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
     
            //your player object is something COMPLETELY different to the playername. outputting the player will result in something like 0x7ABC6456V, which is the address in memory
            //you wanted to use the playername but used the object
     
            if (config.contains(pname + ".advertising") && config.getBoolean(pname + ".advertising") == true && config.getBoolean(pname + ".isOnDuty") == true){
                this.pname = event.getMessage();
            }
            if (config.contains(pname + ".chatviolation") && config.getBoolean(pname + ".chatviolation") == true && config.getBoolean(pname + "isOnDuty") == true){
                this.pname = event.getMessage();
            }
            if (config.contains(pname + ".spam") && config.getBoolean(pname + ".spam") == true && config.getBoolean(pname + "isOnDuty") == true){
                this.pname = event.getMessage();
            }
        }
    }
    Compiles and runs
     
  17. Offline

    ColaCraft


    Thanks so much!

    The static was in there because this used to be two separate classes, I just put them together for the sake of simplicity.
     
Thread Status:
Not open for further replies.

Share This Page