Can't Do Min/Max Damage

Discussion in 'Spigot Plugin Development' started by LeoDog896, May 19, 2017.

  1. For Some reason, its saying the numbers are negative. I've been trying to figure out why. I am new at java, and I'm still learning, so please don't point at me.
    Code (Text):
    18.05 19:59:42 [Server] INFO ... 24 more
    18.05 19:59:42 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
    18.05 19:59:42 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
    18.05 19:59:42 [Server] INFO at sun.reflect.GeneratedMethodAccessor376.invoke(Unknown Source) ~[?:?]
    18.05 19:59:42 [Server] INFO at cube.source.event.OnPlayerHit.someEntityDamage(OnPlayerHit.java:36) ~[?:?]
    18.05 19:59:42 [Server] INFO at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_111]
    18.05 19:59:42 [Server] INFO Caused by: java.lang.IllegalArgumentException: bound must be positive
    18.05 19:59:42 [Server] INFO at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
    18.05 19:59:42 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.PacketPlayInUseEntity.a(SourceFile:13) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.PacketPlayInUseEntity.a(SourceFile:69) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1578) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.EntityPlayer.attack(EntityPlayer.java:1158) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.EntityHuman.attack(EntityHuman.java:1023) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.EntityMonster.damageEntity(EntityMonster.java:44) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.EntityLiving.damageEntity(EntityLiving.java:855) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at net.minecraft.server.v1_11_R1.EntityLiving.damageEntity0(EntityLiving.java:1309) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:609) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:482) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:577) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:92) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Spigot.jar:git-Spigot-1818014-a2af8f0]
    18.05 19:59:42 [Server] INFO org.bukkit.event.EventException
    18.05 19:59:42 [Server] ERROR Could not pass event EntityDamageByEntityEvent to CubeCore v1.3
    Here Is My OnEntityDamageByEntityEvent Class:
    Code (Text):
    package cube.source.event;

    import java.util.Random;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import de.tr7zw.itemnbtapi.NBTItem;

    public class OnPlayerHit implements Listener {
        Random rand = new Random();
        int armor = 1;
        @EventHandler(priority = EventPriority.HIGHEST)
        public void someEntityDamage(EntityDamageByEntityEvent event) throws InterruptedException {
            Thread.sleep(1);
            if (event.getEntity() instanceof Player && event.getEntity().hasMetadata("NPC") == false) {
                if (event.getDamager() instanceof Player) {
                    Player p = (Player) event.getDamager();
                    NBTItem nbti = new NBTItem(p.getItemInHand());
                    int min = nbti.getInteger("min");
                    int max = nbti.getInteger("max");
                        int dam = rand.nextInt(max - min) + min;
                        event.setDamage((double) dam);
                 
                } else {
                    String name = event.getDamager().getName();
                    System.out.println(name + " has been hit by " + event.getDamager().getName());
                }
            } else {
                if (event.getDamager() instanceof Player && event.getEntity().hasMetadata("NPC") == false) {
                    Player p = (Player) event.getDamager();
                    NBTItem nbti = new NBTItem(p.getItemInHand());
                    int min = nbti.getInteger("min");
                    int max = nbti.getInteger("max");
                        int dam = rand.nextInt(max - min) + min;
                        event.setDamage((double) dam);
                    System.out.println("Player Vs Entity");
                } else {
                    System.out.println("A Battle Has Occoured Between 2 Entities");
                }
            }

      }
    }
     
    And Here Is My ItemGen Class
    Code (Text):
    package cube.source.commands;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import de.tr7zw.itemnbtapi.NBTItem;
    import net.md_5.bungee.api.ChatColor;

    public class ItemBuilder implements CommandExecutor {
        Random random = new Random();

        public boolean onCommand(CommandSender player, Command cmd, String label, String[] args) {
            if (!player.hasPermission("cube.admin")){
                return true;
            }
            boolean tool = false;
            String[] items = { "nul" };
            String rank = "nul";
            String chosenString = "268";
            String name = "nulName";
            String Suffix = "nulSuffix";
            int armor = 1;
            int Tier = 1;
            int min = 1;
            int max = 0;
            String[] reaper = { "290", "291", "292", "293", "294" };
            String[] sword = { "267", "268", "272", "276", "283" };
            String[] shovel = { "256", "269", "273", "277", "284" };
            String[] pick = { "257", "270", "274", "278", "285" };
            String[] axe = { "271", "275", "278", "279", "286" };
            String[] helm = {"298", "302", "306", "310", "314"};
            String[] chest = {"303", "307", "311", "315", "299"};
            String[] leg = {"304", "308", "312", "316", "300"};
            String[] boot = {"301", "305", "309", "313", "317"};
            List<String> reaperList = Arrays.asList(reaper);
            List<String> swordList = Arrays.asList(sword);
            List<String> shovelList = Arrays.asList(shovel);
            List<String> pickList = Arrays.asList(pick);
            List<String> axeList = Arrays.asList(axe);
            List<String> helmList = Arrays.asList(helm);
            List<String> chestList = Arrays.asList(chest);
            List<String> legList = Arrays.asList(leg);
            List<String> bootList = Arrays.asList(boot);
            Player s = Bukkit.getPlayer(args[2]);
            if (args.length == 0) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Usage > /randomitem (Rank) <Tier 1-5>"));
                return true;

            }
            if (args[1] == null) {
                Tier = 1;
            } else {
                try {
                    Tier = Integer.parseInt(args[1]);
                } catch (NumberFormatException e) {
                    player.sendMessage(
                            ChatColor.translateAlternateColorCodes('&', "&4Usage > /randomitem (Rank) <Tier 1-5>"));
                    return true;
                }
            }

            if (Tier > 5) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Usage > /randomitem (Rank) <Tier 1-5>"));
                return true;
            }
            if (Integer.parseInt(args[0]) > 5) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Usage > /randomitem (Rank) <Tier 1-5>"));
                return true;
            }
            if (Integer.parseInt(args[0]) == 1) {
                String sampleString = "268:269:270:271:290:298,299,300,301";
                items = sampleString.split(":");
                rank = "&aCommon";
                min = random.nextInt(10) + 6;
                max = random.nextInt(10) + 7;
                armor = random.nextInt(10) + 3;
            } else if (Integer.parseInt(args[0]) == 2) {
                String sampleString = "272:273:274:275:291:302:303:304:305";
                items = sampleString.split(":");
                rank = "&3Uncommon";
                min = random.nextInt(17) + 12;
                max = random.nextInt(17) + 13;
                armor = random.nextInt(18) + 5;
            } else if (Integer.parseInt(args[0]) == 3) {
                String sampleString = "256:257:258:267:292:306:307:308:309";
                items = sampleString.split(":");
                rank = "&cRare";
                min = random.nextInt(41) + 18;
                max = random.nextInt(41) + 19;
                armor = random.nextInt(27) + 8;
            } else if (Integer.parseInt(args[0]) == 4) {
                String sampleString = "276:277:278:279:293:310:311:312:313";
                items = sampleString.split(":");
                rank = "&5Legendary";
                min = random.nextInt(95) + 24;
                max = random.nextInt(95) + 25;
                armor = random.nextInt(41) + 12;
            } else if (Integer.parseInt(args[0]) == 5){
                String sampleString = "283:284:285:286:294:314:315:316:317";
                items = sampleString.split(":");
                rank = "&6Epic";
                min = random.nextInt(95) + 24;
                max = random.nextInt(95) + 25;
                armor = random.nextInt(63) + 21;
            }
            min = min * Tier;
            max = max * Tier;
            armor = armor * Tier;
            if (min > max){
                int min2 = min;
                int max2 = max;
                max = min2;
                min = max2;
            }
            chosenString = items[random.nextInt(items.length)];
            if (reaperList.contains(chosenString)) {
                Suffix = "&4Reaper";
                tool = true;
            } else if (swordList.contains(chosenString)) {
                Suffix = "&3Sword";
                tool = true;
            } else if (shovelList.contains(chosenString)) {
                Suffix = "&cMace";
                tool = true;
            } else if (pickList.contains(chosenString)) {
                Suffix = "&2Pick";
                tool = true;
            } else if (axeList.contains(chosenString)) {
                Suffix = "&eAxe";
                tool = true;
            } else if (helmList.contains(chosenString)){
                Suffix = "&eHelmet";
                tool = false;
            } else if (chestList.contains(chosenString)){
                Suffix = "&6Chest";
                tool = false;
            } else if (legList.contains(chosenString)){
                Suffix = "&2Leggings";
                tool = false;
            } else if (bootList.contains(chosenString)){
                Suffix = "&bBoots";
                tool = false;
            } else {
                Suffix = "null";
            }
         
            ItemStack is = new ItemStack(Material.getMaterial(Integer.parseInt(chosenString)));
            ItemMeta im = is.getItemMeta();
            im.setUnbreakable(true);
            name = "&aElvy:&bIcy:&4Deathbringer";
            String[] names = name.split(":");
            String itemdisplay = names[random.nextInt(names.length)];
            ArrayList<String> lore = new ArrayList<String>();
            if (tool = false){
                lore.add(ChatColor.translateAlternateColorCodes('&', "&cArmor &7> &4" + armor));
            } else if (tool = true){
                lore.add(ChatColor.translateAlternateColorCodes('&', "&cDamage &7> &4" + min + "&7-&4" + max));
            }
            NBTItem nbti = new NBTItem(is);
            nbti.setInteger("min", min);
            nbti.setInteger("max", max);
            nbti.setInteger("armor", armor);
            nbti.setBoolean("tool", tool);
            nbti.getItem();
            lore.add(ChatColor.translateAlternateColorCodes('&', "&6Rank &7> " + rank));
            lore.add(ChatColor.translateAlternateColorCodes('&', "&3Tier &7> &5T&d" + Tier));
            im.setLore(lore);
            im.setDisplayName(ChatColor.translateAlternateColorCodes('&', rank + " &7[" + itemdisplay + "&7] " + Suffix));
            is.setItemMeta(im);
            s.getInventory().addItem(is);
            return true;
        }

    }
     
    I am using ItemNBTAi, can someone tell me how this is being caused? No code needed, ill try to figure out it once i know what it is. It seems like it has to do something with the random method...
     
  2. Woa woa woa woa. Before I go any further, WHY ARE YOU USING THREAD#SLEEP? I think there's a more sufficient way of doing that than using THREAD#SLEEP. Even if it's for a split second.
     
    • Agree Agree x 1
  3. Did you check that the variable 'max' is bigger than 'min'? Haven't checked line numbers but that error is usually thrown when you try and get a random number with a negative bound.

    EDIT: Why aren't you checking if any of the arguments are integers? If you mistype a command or forget the syntax, you will most likely be thrown with several errors.
     
  4. I'll use Runnable Instead, Thanks!
     
  5. I don't think runnable is good either. That is ran each and every time an entity is damaged by another entity... Sure Thread.sleep is more resource friendly but it was still bad.
     
  6. It already checks for min-max here:
    Code (Text):
    if (min > max){
                int min2 = min;
                int max2 = max;
                max = min2;
                min = max2;
            }
     
  7. Try printing out the bound for random before you create the Random object.

    Also, just recommending something to you, I would not suggest having the CommandSender name in the ItemBuilder command class as 'player', before you at least do checks if it is an instance of Player. Easy to make mistakes and forget to do checks, more conventional to have it named 'sender'. That is not really necessary, though.
     
  8. I did print out the bound, and I see why. it registered as "0". Ill find another way to store data. Thanks!