Plugin Coding Help

Discussion in 'Spigot Plugin Development' started by dyenxunit, May 29, 2015.

  1. what im trying to do is add all online players into a hash map but for some reason it doesnt work can someone please provide some help ?

    Main Class
    Code (Text):
    private HashMap<String, Integer> cooldownTime;
    Command Class
    Code (Text):

    for(Player online : Bukkit.getServer().getOnlinePlayers()) {
                        Main.cooldownTime.putAll(online.getPlayer());
                        }
     
    #1 dyenxunit, May 29, 2015
    Last edited: May 29, 2015
  2. Inkzzz

    Resource Staff

    Code (Text):
    HashMap<String, Integer> cooldown = new HashMap<>();

    public void putAllPlayers() {
    for(Player on : Bukkit.getOnlinePlayers()) {
    cooldown.put(on.getName());
    }
    }
     
  3. It doesnt work. Is it because i have the private HashMap<String, Integer> cooldownTime; in my main class ? because to get the hash map i have to do Main.cooldownTime to get it and this is when they run a command this will put them into the cooldown class
     
  4. PHP:
    HashMap<String, Integer> cooldown = new HashMap<>();

    public void putAllPlayers() {
    for(Player on : Bukkit.getOnlinePlayers()) {
    cooldown.put(on.getName(), value);
    }
    }
    It¿s not because it's private, it is beacuse you are not giving a value to the integer
     
  5. what value do i have to give it ?
     
  6. the cooldown...
     
  7. It is an integer you should give the amount of time the cooldown will last for the player.
     
  8. Presumably the cooldown time?
     
  9. Inkzzz

    Resource Staff

    Oh yes, I forgot about that, forgive me.
     
  10. We are all humans I forgive you xD
     
    • Friendly Friendly x 1
  11. While were on the note of storing players in hashmaps. be careful. Make sure to remove that player from the hashmap when they logout and store their cooldown value somewhere. Or else face memory leaks.
     
  12. I bumped into a small problem which is my BukkitRunnable is not working in my command class can someone help

    Code (Text):
        static Main main;
        public CommandEvent(Main main) {
            this.main = main;
        }

    [SPOILER="Error"]
    [CODE]null
    org.bukkit.command.CommandException: Unhandled exception executing command 'Test' in plugin Test v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1139) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:974) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_65]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_65]
    Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.List
        at me.dyenxunit.magnum.Main.Events.CommandEvent.onCommand(CommandEvent.java:56) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        ... 15 more
     
    #12 dyenxunit, May 29, 2015
    Last edited: May 29, 2015
  13. Make sure you actually run the BukkitRunnable.

    Code (Text):
    new BukkitRunnable() {
                            public void run() {
                                Main.cooldownTime.put(online.getName(), Main.cooldownTime.get(online.getName()) - 1);
                                if (Main.cooldownTime.get(online.getName()) == 0) {
                                    Main.cooldownTime.remove(online.getName());
                                    Main.cooldownTask.remove(online.getName());
                                    cancel();
                                }
                            }
                        }.runTask(yourPlugin));
     
  14. so is it like this Main.cooldownTask.get(online.getName()).runTaskTimer(plugin, 20, 20); instead of Main.cooldownTask.get(online.getName()).runTaskTimer(main, 20, 20);
     
  15. At the end of your BukkitRunnable add .runTask(pluginInstance);
     
  16. Also don't subtract the time for cooldowns, just save the System.currentTimeMillis() and then compare them for when checking.
     
  17. Well a hashmap is like someday maybe rich someday maybe rich some tutorialso it? also it? als online players using though, als on you lot and be rich some tutorialso i hashmap wait isnt it not generate Well a job and java though, also it coding think to get and of java that maybe if your could generate on coding a hashmap wait not thing and be rich someday maybe bukkit it could generate Well a looked up someday maybe rich some kind of java is like someday maybe but maybe if you could generate Well a
     
    • Funny Funny x 1
  18. You cannot cast a HashMap to a List. I have no idea why you are trying to do this.
     
  19. You really should pass it through
    Code (Text):
    Main plugin;
    public commandClassName(Main plugin){
    this.plugin = plugin;
    }
    then use plugin.cooldownTimes and in main class just put
    new commandClassName(this);