RandomCommands not working

Discussion in 'Spigot Plugin Development' started by harveycat1, Jun 3, 2017.

  1. Im having trouble getting this to work and its not giving off errors either
    Code (Text):
       @EventHandler
       public void onEntityDeath(EntityDeathEvent e) {
           EntityType t = e.getEntity().getType();
           Player p = e.getEntity().getKiller();
           if (e.getEntity().getCustomName().equals("&d&lSpecial Zombie")) {
               e.getDrops().clear();
               e.setDroppedExp(0);
               List<String> list = config.getStringList("commands");
               int index = new Random().nextInt(list.size());
               String cmd = list.get(index);
               Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("{player}", p.getName()));
           }
       }
    Code (Text):
    Commands:
      - 'crates give {player} vote 1'
      - 'crates give {player} common 1'
      - 'eco give {player} 50000'
     
  2. Can you explain what the code and config are ment to do?
     
  3. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    The "&" is just a letter, it doesn't represent a color code. Try ChatColor.translateAlternateColors("&d&lSpecial Zombie", '&') to convert the "&" to a color code symbol "§".
     
    • Agree Agree x 1
  4. Instead of "new Random()" use "ThreadLocalRandom.current()", it's more efficient.
    You probably want to use "String#replace" instead of "String#replaceAll", since the only difference is that "String#replaceAll" handles the first argument as a regex.
    The issue: instead of "&d&l" you have to use the ChatColor enum ("§" will also work, but it may cause issues, therefore ChatColor enums are preferred).
     
    • Agree Agree x 2
  5. Here's a few noticeable problems;

    You didn't make if statements to check if the Killer is a player and if statements to check if the name is null and just it be more accurate check if it is the same mob.
     
  6. it can only be triggered by a block being mined so its all good
     
  7. Mas

    Mas

    Follow the advice given to you:
    Instead of using the symbol, check if
    Code (Java):
    ChatColor.translateAlternateColorCodes('&', entity.getCustomName()).equals(whatever)
    You also never refer to 't' when you assign it to e.getEntity().getType()
     
  8. fixed that little section but still not finding the mob to execute the code
    Code (Text):
    [11:13:41 ERROR]: Could not pass event EntityDeathEvent to KeyMining v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityDeathEvent(CraftEventFactory.java:378) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityLiving.die(EntityLiving.java:874) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityZombie.die(EntityZombie.java:561) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:812) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityMonster.damageEntity(EntityMonster.java:42) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityZombie.damageEntity(EntityZombie.java:167) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityHuman.attack(EntityHuman.java:1001) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityPlayer.attack(EntityPlayer.java:1063) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1355) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:52) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:11) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.IllegalArgumentException: bound must be positive
            at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_111]
            at cf.vanguardnetwork.keymining.Main.onEntityDeath(Main.java:72) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            ... 23 more
    >
     

    EDIT: And i dont know how to fix the mob issue
     
  9. i know but i used to i just forgot to remove it
     
  10. Mas

    Mas

    Read the stacktrace... Caused by: java.lang.IllegalArgumentException: bound must be positive
    This means you're calling Random#nextInt with a value less than 1, and since Lists can't have negative sizes, the size must be 0, meaning the list you get from your config is empty.
     
  11. its not empty though thats what i dont get
     
  12. Maybe the config is case sensitive. You have... In code: "commands". In config: "Commands"
     
  13. Inkzzz

    Resource Staff

    Post your updated version. You should also practice debugging as well - it is a very important skill to have.
     
  14. Its saying i have a illegal Repitition of {player} when i dont?
    Code (Text):
               List<String> list = config.getStringList("Commands");
               int index = rand.nextInt(list.size());
               String cmd = list.get(index);
               Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("{player}", p.getName()));
    Code (Text):
    Commands:
      - 'crates give {player} vote 1'
      - 'crates give {player} common 1'
      - 'eco give {player} 50000'
     
  15. Please read what I say.
     
  16. i did i corrected that but its saying illegal repitition of {player} when that code only grabs 1 from the string list
     
  17. In the latest code snippet, you still use "replaceAll".