Development Assistance This seems to be throwing a NullPointerException and I don't know why, help?

Discussion in 'Plugin Help/Development/Requests' started by javipepe, Mar 19, 2015.

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

    javipepe

    Code:

    Code:
    public class NickCommand {
    
        public static HashMap<Player, String> nicked;
        public static ArrayList<Player> nickedplayers = new ArrayList<Player>();
    
        @Command(aliases = {"nick"}, desc = "Cancels the current countdown.", min = 1, max = 1)
        public static void cancel(final CommandContext cmd, CommandSender sender) {
            Player s = (Player) sender;
    
            if(s.isOp()){
    
                if (nickedplayers.contains(s) && cmd.getString(0).equalsIgnoreCase("clear")) {
                    s.kickPlayer(ChatColor.DARK_AQUA + "Resetting your nickname to " + ChatColor.AQUA + "" + ChatColor.ITALIC + s.getName() + ChatColor.DARK_AQUA + " ...");
                    nickedplayers.remove(s);
                    nicked.remove(s);
                    TagAPI.refreshPlayer(s);
                    /*for (Player admins : Bukkit.getOnlinePlayers()) {
                        if (admins.isOp()) {
                            admins.sendMessage(ChatColor.DARK_BLUE + "[" + ChatColor.BLUE + "Staff Chat" + ChatColor.DARK_BLUE + "] " + ChatColor.DARK_AQUA + s.getName() + ChatColor.YELLOW + " has cleared his nickname.");
                        }
                    }*/
                    return;
                } else if (!nickedplayers.contains(s) && cmd.getString(0).equalsIgnoreCase("clear")) {
                    s.sendMessage(ChatColor.RED + "You are not nicked.");
                    return;
                }
    
                if (!cmd.getString(0).equalsIgnoreCase("clear")) {
                    String nickname = cmd.getString(0);
    
                    if(nickname != null) {
                        s.sendMessage(ChatColor.YELLOW + "Changed your nickname to " + ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + nickname);
                        s.setDisplayName(ChatColor.RED + "" + ChatColor.STRIKETHROUGH + s.getName() + ChatColor.RESET + " " + ChatColor.DARK_AQUA + nickname);
                        s.setPlayerListName(ChatColor.DARK_AQUA + nickname);
                        nickedplayers.add(s);
                        nicked.put(s, nickname);
                        TagAPI.refreshPlayer(s);
                        /*for (Player admins : Bukkit.getOnlinePlayers()) {
                            if (admins.isOp()) {
                                admins.sendMessage(ChatColor.DARK_BLUE + "[" + ChatColor.BLUE + "Staff Chat" + ChatColor.DARK_BLUE + "] " + ChatColor.DARK_AQUA + s.getName() + ChatColor.YELLOW + " has changed his nickname to " + ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + nickname);
                            }
                        }*/
                    }
                    return;
    
                }
    
            }else{
                s.sendMessage(ChatColor.RED + "No permission");
            }
        }
    
        public static String getNick(Player p){
            if(nickedplayers.contains(p)) {
                return nicked.get(p);
            }else{
                return p.getName();
            }
        }
    Error:

    Code:
    [09:47:56] [Server thread/INFO]: javipepe issued server command: /nick javiYT
    [09:47:56] [Server thread/WARN]: com.sk89q.minecraft.util.commands.WrappedCommandException: java.lang.NullPointerException
    [09:47:56] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.invokeMethod(CommandsManager.java:543)
    [09:47:56] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.executeMethod(CommandsManager.java:520)
    [09:47:56] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.execute(CommandsManager.java:409)
    [09:47:56] [Server thread/WARN]:     at in.twizmwaz.cardinal.Nick.onCommand(Nick.java:32)
    [09:47:56] [Server thread/WARN]:     at com.sk89q.bukkit.util.DynamicPluginCommand.execute(DynamicPluginCommand.java:51)
    [09:47:56] [Server thread/WARN]:     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140)
    [09:47:56] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:660)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1219)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1080)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13)
    [09:47:56] [Server thread/WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [09:47:56] [Server thread/WARN]:     at java.util.concurrent.FutureTask.run(Unknown Source)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:656)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:611)
    [09:47:56] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:521)
    [09:47:56] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
    [09:47:56] [Server thread/WARN]: Caused by: java.lang.NullPointerException
    [09:47:56] [Server thread/WARN]:     at in.twizmwaz.cardinal.command.NickCommand.cancel(NickCommand.java:51)
    [09:47:56] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [09:47:56] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    [09:47:56] [Server thread/WARN]:     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    [09:47:56] [Server thread/WARN]:     at java.lang.reflect.Method.invoke(Unknown Source)
    [09:47:56] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.invokeMethod(CommandsManager.java:533)
    [09:47:56] [Server thread/WARN]:     ... 18 more
    [09:55:04] [Server thread/INFO]: Stopping the server[m
    [09:55:04] [Server thread/INFO]: Stopping server
     
  2. Invisible

    nverdier

    @javipepe What is on line 51 of NickCommand.java? Also, take a look at this.
     
  3. Offline

    Faith

    Is it just me or are you missing a '}'
     
  4. Invisible

    nverdier

    @Faith He is, but that's only because he didn't copy and paste the whole thing. He also didn't include the package or imports.
     
  5. Offline

    Faith

    @nverdier Oh sorry, didn't think about that. Just woke up. And btw, OP, look at line 51.
     
  6. Invisible

    nverdier

    Faith likes this.
  7. Offline

    javipepe

    @Faith @nverdier This is the full code:

    Code:
    package in.twizmwaz.cardinal.command;
    
    import com.sk89q.minecraft.util.commands.*;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.kitteh.tag.TagAPI;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    public class NickCommand {
    
        public static HashMap<Player, String> nicked;
        public static ArrayList<Player> nickedplayers = new ArrayList<Player>();
    
        @Command(aliases = {"nick"}, desc = "Cancels the current countdown.", min = 1, max = 1)
        public static void cancel(final CommandContext cmd, CommandSender sender) throws CommandException{
            Player s = (Player) sender;
    
            if(s.isOp()){
    
                if (nickedplayers.contains(s) && cmd.getString(0).equalsIgnoreCase("clear")) {
                    s.kickPlayer(ChatColor.DARK_AQUA + "Resetting your nickname to " + ChatColor.AQUA + "" + ChatColor.ITALIC + s.getName() + ChatColor.DARK_AQUA + " ...");
                    nickedplayers.remove(s);
                    nicked.remove(s);
                    TagAPI.refreshPlayer(s);
                    /*for (Player admins : Bukkit.getOnlinePlayers()) {
                        if (admins.isOp()) {
                            admins.sendMessage(ChatColor.DARK_BLUE + "[" + ChatColor.BLUE + "Staff Chat" + ChatColor.DARK_BLUE + "] " + ChatColor.DARK_AQUA + s.getName() + ChatColor.YELLOW + " has cleared his nickname.");
                        }
                    }*/
                    return;
                } else if (!nickedplayers.contains(s) && cmd.getString(0).equalsIgnoreCase("clear")) {
                    s.sendMessage(ChatColor.RED + "You are not nicked.");
                    return;
                }
    
                if (!cmd.getString(0).equalsIgnoreCase("clear")) {
                    String nickname = cmd.getString(0);
    
                    if(nickname != null) {
                        s.sendMessage(ChatColor.YELLOW + "Changed your nickname to " + ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + nickname);
                        s.setDisplayName(ChatColor.RED + "" + ChatColor.STRIKETHROUGH + s.getName() + ChatColor.RESET + " " + ChatColor.DARK_AQUA + nickname);
                        s.setPlayerListName(ChatColor.DARK_AQUA + nickname);
                        nickedplayers.add(s);
                        nicked.put(s, nickname);
                        TagAPI.refreshPlayer(s);
                        /*for (Player admins : Bukkit.getOnlinePlayers()) {
                            if (admins.isOp()) {
                                admins.sendMessage(ChatColor.DARK_BLUE + "[" + ChatColor.BLUE + "Staff Chat" + ChatColor.DARK_BLUE + "] " + ChatColor.DARK_AQUA + s.getName() + ChatColor.YELLOW + " has changed his nickname to " + ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + nickname);
                            }
                        }*/
                    }
                    return;
    
                }
    
            }else{
                s.sendMessage(ChatColor.RED + "No permission");
            }
        }
    
        public static String getNick(Player p){
            if(nickedplayers.contains(p)) {
                return nicked.get(p);
            }else{
                return p.getName();
            }
        }
    }
    It now gives the error in line 48, I could notice, because I commented the for(), this is line 48:

    Code:
                        nicked.put(s, nickname);
    
     
    Last edited: Mar 19, 2015
  8. Invisible

    nverdier

    @javipepe Was that for loop commented out when you tested the plugin? If so, repost a new latest.log after you use the command again.
     
  9. Offline

    javipepe

    @nverdier Yes it was. Here is the new log:

    Code:
    [10:22:00] [Server thread/INFO]: Starting minecraft server version 1.8
    [10:22:00] [Server thread/INFO]: Loading properties
    [10:22:00] [Server thread/INFO]: Default game type: SURVIVAL
    [10:22:00] [Server thread/INFO]: Generating keypair
    [10:22:00] [Server thread/INFO]: Starting Minecraft server on *:25565
    [10:22:00] [Server thread/INFO]: This server is running SportBukkit version git-SportBukkit-c31220d (MC: 1.8) (Implementing API version 1.8-R0.1-SNAPSHOT)
    [10:22:01] [Server thread/ERROR]: Could not load 'plugins\iTag-1.0-SNAPSHOT.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.java:292) [sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.<init>(CraftServer.java:254) [sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at net.minecraft.server.v1_8_R1.PlayerList.<init>(PlayerList.java:71) [sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at net.minecraft.server.v1_8_R1.DedicatedPlayerList.<init>(SourceFile:14) [sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.java:133) [sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:490) [sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_20]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at net.md_5.itag.iTag.<init>(iTag.java:24) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_20]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_20]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_20]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_20]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_20]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        ... 9 more
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at in.twizmwaz.cardinal.Nick.<init>(Nick.java:22) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_20]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_20]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_20]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_20]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_20]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[sportbukkit-1.8-R0.1-SNAPSHOT.jar:git-SportBukkit-c31220d]
        ... 9 more
    [10:22:01] [Server thread/INFO]: [GamemodeNetworkCore] Loading GamemodeNetworkCore v1.0
    [10:22:01] [Server thread/INFO]: [Nick] Loading Nick v1.0
    [10:22:01] [Server thread/INFO]: [ProtocolLib] Loading ProtocolLib v3.6.3-SNAPSHOT
    [10:22:01] [Server thread/INFO]: [ProtocolLib] Enabling ProtocolLib v3.6.3-SNAPSHOT
    [10:22:01] [Server thread/INFO]: [ProtocolLib] Started structure compiler thread.
    [10:22:01] [Server thread/INFO]: Preparing level "Invitationals lobby"
    [10:22:01] [Server thread/INFO]: Preparing start region for level 0 (Seed: -5805239589426207575)
    [10:22:01] [Server thread/INFO]: Preparing start region for level 1 (Seed: -5805239589426207575)
    [10:22:01] [Server thread/INFO]: Preparing start region for level 2 (Seed: -5805239589426207575)
    [10:22:01] [Server thread/INFO]: [GamemodeNetworkCore] Enabling GamemodeNetworkCore v1.0
    [10:22:01] [Server thread/INFO]: [GamemodeNetworkCore] §c[GeamemodeNetworkCore]§aEnabled
    [10:22:01] [Server thread/WARN]: [GamemodeNetworkCore] Could not find the lobby world. Switching to the default one.
    [10:22:01] [Server thread/INFO]: [Nick] Enabling Nick v1.0
    [10:22:01] [Server thread/INFO]: [Nick] Nick enabled.
    [10:22:01] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [10:22:01] [Server thread/INFO]: Done (0,676s)! For help, type "help" or "?"
    [10:22:14] [Server thread/INFO]: Title command successfully executed[m
    [10:22:14] [Server thread/INFO]: javipepe[/127.0.0.1:51771] logged in with entity id 0 at ([Invitationals lobby] 361,5, 42,2, -1301,7)
    [10:22:21] [Server thread/INFO]: javipepe issued server command: /nick
    [10:22:27] [Server thread/INFO]: javipepe issued server command: /nick eleksmf3
    [10:22:27] [Server thread/WARN]: com.sk89q.minecraft.util.commands.WrappedCommandException: java.lang.NullPointerException
    [10:22:27] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.invokeMethod(CommandsManager.java:543)
    [10:22:27] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.executeMethod(CommandsManager.java:520)
    [10:22:27] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.execute(CommandsManager.java:409)
    [10:22:27] [Server thread/WARN]:     at in.twizmwaz.cardinal.Nick.onCommand(Nick.java:32)
    [10:22:27] [Server thread/WARN]:     at com.sk89q.bukkit.util.DynamicPluginCommand.execute(DynamicPluginCommand.java:51)
    [10:22:27] [Server thread/WARN]:     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140)
    [10:22:27] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:660)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1219)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1080)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13)
    [10:22:27] [Server thread/WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [10:22:27] [Server thread/WARN]:     at java.util.concurrent.FutureTask.run(Unknown Source)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:656)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:611)
    [10:22:27] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:521)
    [10:22:27] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
    [10:22:27] [Server thread/WARN]: Caused by: java.lang.NullPointerException
    [10:22:27] [Server thread/WARN]:     at in.twizmwaz.cardinal.command.NickCommand.cancel(NickCommand.java:48)
    [10:22:27] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [10:22:27] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    [10:22:27] [Server thread/WARN]:     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    [10:22:27] [Server thread/WARN]:     at java.lang.reflect.Method.invoke(Unknown Source)
    [10:22:27] [Server thread/WARN]:     at com.sk89q.minecraft.util.commands.CommandsManager.invokeMethod(CommandsManager.java:533)
    [10:22:27] [Server thread/WARN]:     ... 18 more
    [10:25:49] [Server thread/INFO]: Stopping the server[m
    [10:25:49] [Server thread/INFO]: Stopping server
    [10:25:49] [Server thread/INFO]: [ProtocolLib] Disabling ProtocolLib v3.6.3-SNAPSHOT
    [10:25:49] [Server thread/INFO]: [Nick] Disabling Nick v1.0
    [10:25:49] [Server thread/INFO]: [GamemodeNetworkCore] Disabling GamemodeNetworkCore v1.0
    [10:25:49] [Server thread/INFO]: [GamemodeNetworkCore] §c[GamemodeNetworkCore]§4Disabled
    [10:25:49] [Server thread/INFO]: Saving players
    [10:25:49] [Server thread/INFO]: javipepe lost connection: Server closed
    [10:25:49] [Server thread/INFO]: javipepe left the game.[m
    [10:25:49] [Server thread/INFO]: Saving worlds
    [10:25:49] [Server thread/INFO]: Saving chunks for level 'Invitationals lobby'/Overworld
    [10:25:49] [Server thread/INFO]: Saving chunks for level 'Invitationals lobby_nether'/Nether
    [10:25:49] [Server thread/INFO]: Saving chunks for level 'Invitationals lobby_the_end'/The End
    
     
  10. Online

    timtower Administrator Administrator Moderator

    @javipepe Do you happen to have 2 plugins with the same package and main class? Or maybe 2 classes in the same plugin that extend JavaPlugin?
     
  11. Invisible

    nverdier

    @javipepe Are you creating multiple instances of your class extending JavaPlugin? Could you post all relevant classes?

    EDIT: I was out-surfed by the ninja.
     
  12. Offline

    javipepe

    @timtower I don't think I do, but it's true I'm getting that Itag plugin not loading because it's already initialized, I don't really know why.

    @nverdier

    Nick.java (JavaPlugin):

    Code:
    package in.twizmwaz.cardinal;
    
    import com.sk89q.bukkit.util.CommandsManagerRegistration;
    import com.sk89q.minecraft.util.commands.*;
    import in.twizmwaz.cardinal.command.*;
    import in.twizmwaz.cardinal.event.TagEvent;
    import org.apache.commons.io.FileUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.jdom2.JDOMException;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
    
    public class Nick extends JavaPlugin {
    
        private static Nick instance;
        private CommandsManager<CommandSender> commands;
        //private DemographicsHandler demographicsHandler;
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            try {
                this.commands.execute(cmd.getName(), args, sender, sender);
            } catch (CommandPermissionsException e) {
                sender.sendMessage(ChatColor.RED + "No permission");
            } catch (MissingNestedCommandException e) {
                sender.sendMessage(ChatColor.RED + e.getUsage().replace("{cmd}", cmd.getName()));
            } catch (CommandUsageException e) {
                sender.sendMessage(ChatColor.RED + e.getMessage());
                sender.sendMessage(ChatColor.RED + e.getUsage());
            } catch (WrappedCommandException e) {
                if (e.getCause() instanceof NumberFormatException) {
                    sender.sendMessage(ChatColor.RED + "String expected.");
                } else {
                    sender.sendMessage(ChatColor.YELLOW + "You successfully changed your nickname.");
                    e.printStackTrace();
                }
            } catch (CommandException e) {
                sender.sendMessage(ChatColor.RED + e.getMessage());
            }
            return true;
        }
    
        private void setupCommands() {
            this.commands = new CommandsManager<CommandSender>() {
                @Override
                public boolean hasPermission(CommandSender sender, String perm) {
                    return sender instanceof ConsoleCommandSender || sender.hasPermission(perm);
                }
            };
            CommandsManagerRegistration cmdRegister = new CommandsManagerRegistration(this, this.commands);
            cmdRegister.register(NickCommand.class);
    
        }
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new TagEvent(), this);
            setupCommands();
        }
       
        @Override
        public void onDisable() {
        }
    
    
        public JavaPlugin getPlugin() {
            return this;
        }
    
        public static Nick getInstance() {
            return instance;
        }
    
    }
    
    TagEvent (Listener):

    Code:
    package in.twizmwaz.cardinal.event;
    
    import in.twizmwaz.cardinal.Nick;
    import in.twizmwaz.cardinal.command.NickCommand;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.kitteh.tag.AsyncPlayerReceiveNameTagEvent;
    import org.kitteh.tag.TagAPI;
    
    public class TagEvent implements Listener {
    
        @EventHandler
        public static void onNameTag(AsyncPlayerReceiveNameTagEvent event){
            if(NickCommand.nickedplayers.contains(event.getPlayer())){
                event.setTag(NickCommand.getNick(event.getPlayer()));
                return;
            }else{
                event.setTag(event.getPlayer().getName());
                return;
            }
        }
    
        @EventHandler
    
        public static void onJoin(PlayerJoinEvent e){
    
            TagAPI.refreshPlayer(e.getPlayer());
        }
    
    
    }
    
    And then the NickCommand, and this is the plugin yml:

    Code:
    name: Nick
    description: Nick yourself
    main: in.twizmwaz.cardinal.Nick
    version: 1.0
    
    EDIT by Timtower: merged posts
     
  13. Offline

    Avygeil

    @javipepe As for the plugin already initialized error, look in all your JARs for shaded/duplicated JavaPlugin instances. But that's not the problem here: you are declaring a HashMap but not initializing it (ie. new HashMap<>()). Unitialized non-primitive fields, contrary to local variables, are null by default, which is where your NullPointerException comes from. There are also several mistakes in your class style/logic:
    1. You are casting sender to Player without checking. Now, you seem to be using sk's Command Framework, and idk if it does that check somewhere else, but I'd throw a CommandException if sender is not an instance of Player.
    2. Your ArrayList is unnecessary. You are using it to check who is nicked but you can achieve the same effect with your Map: instead of "nickedplayers.contains(s)", use "nicked.get(s) != null". Stick with the Map for all your checks, and remove the player from it when he clears his own nick.
    3. You are referencing the Player class in your two generic fields. Now assuming you remove the ArrayList, you are left with a Map<Player, String>. You should not keep a hard reference to Player in a static field, since you don't remove it anywhere else. It will prevent quite big objects to be garbage collected. Of course you could use a WeakHashMap<Player, String>, but why bother when you can use a HashMap<UUID, String> ?
    4. Your code that renames the player is duplicated. You should move it to its own function renamePlayer(UUID, String).
    5. A few redundant checks but I guess that's not very important. :)
    PS: Overcast FTW!
     
  14. Offline

    javipepe

    @Avygeil Thanks a lot mate!

    Also, regarding the initialize error:

    I added the iTag plugin as a dependency in this plugin to use it in the code, is that the problem? I'm not really familiar with shading and all that stuff.
     
  15. Offline

    Avygeil

    @javipepe It shouldn't be the problem. How did you add it as a dependency? If you're using Maven, we could take a look at your pom.xml. :)
     
  16. Offline

    RainoBoy97

    You never initialised the "nicked" field (line 15) :p
     
Thread Status:
Not open for further replies.

Share This Page