Randomizer, unknown?

Discussion in 'Spigot Plugin Development' started by OzzyMC, May 14, 2017.

  1. Hello! Thank you for taking your time to read this, but I have a couple questions.. How would I select a random item from my code? Thanks for your time and consideration..

    My code to give items is...

    Code (Text):

    @EventHandler(priority=EventPriority.HIGH)
            public void onPlayerUse(PlayerInteractEvent event){
                Player p = event.getPlayer();
                if(p.getItemInHand().getType() == Material.BOOK){
               if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                  ItemStack i = p.getInventory().getItemInHand();
                  ItemMeta im = i.getItemMeta();
                  if(i.getItemMeta().hasDisplayName())
                  if (im.getDisplayName().equalsIgnoreCase("§e§lUnique Enchant Book")) {
                      p.setItemInHand(null);
                     
                      }
                  }
               }
            }
    My list of items loos like this..

    Code (Text):

    ArrayList<String> velocity1 = new ArrayList<String>();
           
            ItemStack velocity1S = new ItemStack(Material.BOOK, 1);
            ItemMeta velocity1M = velocity1S.getItemMeta();
           
            velocity1M.setDisplayName("§c§lVelocity II Enchant Book");
            velocity1.add("");
            velocity1.add("§d(!) §fDrag and drop this mysterious Velocity book");
            velocity1.add("§d(!) §fon boots, and notice great swiftness as you move!");
            velocity1M.setLore(velocity1);
            velocity1S.setItemMeta(velocity1M);
     
  2. You can put all the items in an list and use Random:

    Code (Text):
    ArrayList<ItemStack> items = new ArrayList<ItemStack>() {
        {
            //Add items here
            add(new Itemstack(Material.DIRT, 1));
            //........
        }
    }

    public static ItemStack getRandomItem() {
        Random r = new Random();
        int n = r.nextInt(items.size());
        return items.get(n);
    }
    That is untested (I didn't even put it in my IDE)
     
  3. I'll be testing now! What if I only wanted to subtract one item? B/c as of right now when I right-click it takes all the items in that stack from the inv... Could we make it only take one?
     
  4. Code (Text):

    ArrayList<String> blaze = new ArrayList<String>(); {
           
            ItemStack blazeS = new ItemStack(Material.BOOK, 1);
            ItemMeta blazeM = blazeS.getItemMeta();
           
            blazeM.setDisplayName("§e§lBlaze I Enchant Book");
            blaze.add("");
            blaze.add("§d(!) §fDrag and drop this mysterious Blaze I book");
            blaze.add("§d(!) §fon a pickaxe, and notice your tools smelt.");
            blazeM.setLore(blaze);
            blazeS.setItemMeta(blazeM);
            }
           
            ArrayList<ItemStack> unique = new ArrayList<ItemStack>() {
               
               
               {
                   add(new ItemStack(Material.DIRT));
               }
            };
           
            public ItemStack uniqueR() {
               Random r = new Random();
               int n = r.nextInt(unique.size());
               return unique.get(n);
            }
           
            @EventHandler(priority=EventPriority.HIGH)
            public void onPlayerUse(PlayerInteractEvent event){
                Player p = event.getPlayer();
                if(p.getItemInHand().getType() == Material.BOOK){
               if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                  ItemStack i = p.getInventory().getItemInHand();
                  ItemMeta im = i.getItemMeta();
                  if(i.getItemMeta().hasDisplayName())
                      if (im.getDisplayName().equalsIgnoreCase("§e§lUnique Enchant Book")) {
                      p.setItemInHand(uniqueR());
                     
                      }
                 
                  }
               }
            }
    Using your methods, I cannot seem to make out how you add the randomizer and the metadata together.. Could someone help me merge the Blaze array, and the randomizer array? So there is a random chance for you to get either a dirt, or a blaze book? Tyvm!!
     
  5. Dirt has no metadata. Are you sure you are using the right ArrayList? You have two lists, each of which only has one entry, and the metadata has nothing to do with the random..

    Why dont you stop trying to make plugins and learn java?
     
    • Agree Agree x 1
  6. Don't use new random() either minecraft is single threaded therefore you use threadlocalrandom.

    If you have an array of items:
    Code (Text):

    ItemStack is = array[ThreadLocalRandom().current().nextInt(0,array.length())];

    //will return a random item from an array of item stacks.  

    //Or if you want a random number between two numbers.

    Integer number = ThreadLocalRandom().current().nextInt(minNumber,maxNumber);


    Sent from my iPhone using Tapatalk
     
    • Agree Agree x 1
  7. Yes, but I'd like to give items that have META data. For ex. the blaze.
     
  8. I'm not trying to use Dirt for metadata.. I'm trying to implement the BLAZE if you read.
     
  9. Okay so they're going to have the book that is like a blaze book and a unique enchant book? And you want a percent chance of the blaze I or unique enchant occurring?

    And if it fails they get dirt?

    Sent from my iPhone using Tapatalk
     
  10. So I want it where you right click a unique book, it has a chance to give you a blaze book or dirt, because there is only two possible outcomes you have a 50 50 chance
     
  11. If this is the only two outcomes you're ever going to have do:

    If(ThreadLocalRandom().current().nextBoolean())
    //make a book
    Else
    //give them dirt


    Sent from my iPhone using Tapatalk
     
    • Agree Agree x 1
  12. Yes, but there will be about 4-5 possible outcomes.. not just the dirt and book.
     
  13. WAS

    WAS

    Which is it?

    You've been given plenty to resolve this issue.
     
  14. Heres my current code, after the "fixes" posted.. When you right-click it does nothing, and yes my events are registered. Bukkit.getServer().getPluginManager().registerEvents(this, this);

    Code (Text):

    ArrayList<ItemStack> unique = new ArrayList<ItemStack>() {    
           {
              ArrayList<String> gasp = new ArrayList<String>();
             
                ItemStack gaspS = new ItemStack(Material.BOOK, 1);
                ItemMeta gaspM = gaspS.getItemMeta();
             
                gaspM.setDisplayName("§e§lGasp I Enchant Book");
                gasp.add("");
                gasp.add("§d(!) §fDrag and drop this mysterious Gasp book");
                gasp.add("§d(!) §fon a helmet, and notice great breathing.");
                gaspM.setLore(gasp);
                gaspS.setItemMeta(gaspM);
             
                ArrayList<String> bat = new ArrayList<String>();
             
                ItemStack batS = new ItemStack(Material.BOOK, 1);
                ItemMeta batM = batS.getItemMeta();
             
                batM.setDisplayName("§e§lBat I Enchant Book");
                bat.add("");
                bat.add("§d(!) §fDrag and drop this mysterious Bat book");
                bat.add("§d(!) §fon a helmet, and notice great vision in");
                bat.add("§d(!) §fthe depths of darkness.");
                batM.setLore(bat);
                batS.setItemMeta(batM);
           
           
               add(new ItemStack(gaspS));
               add(new ItemStack(batS));
           }
        };
        public ItemStack uniqueR() {
           Random r = new Random();
           int n = r.nextInt(unique.size());
           return unique.get(n);
        }
     
        @EventHandler(priority=EventPriority.HIGH)
        public void onPlayerUse(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(p.getItemInHand().getType() == Material.BOOK){
           if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
              ItemStack i = p.getInventory().getItemInHand();
              ItemMeta im = i.getItemMeta();
              if(i.getItemMeta().hasDisplayName())
                  if (im.getDisplayName().equalsIgnoreCase("§e§lUnique Enchant Book")) {
                  p.setItemInHand(uniqueR());
               
                  }
              }
           }
        }
    Edit -: This is just what I have currently, but I'd like it to work with about 5 possible books/outcomes all at random.
     
    #14 OzzyMC, May 14, 2017
    Last edited: May 14, 2017
  15. You said it would only have two possible outcomes if that is not the case.

    Before we can give you a solution we need to know how you plan to tell these apart.. if you're going to have multiple random outcome things I would suggest doing this.

    Int item = ThreadLocalRandom().current().nextInt(1, maxNumberOfChoices);

    Then a switch statement
    Switch (item)
    {
    Case:1
    //do stuff to give them dirt
    break;
    Case:2
    // do stuff to give them blaze
    break;
    Case:3
    // do stuff to give them something else
    break;


    }


    Sent from my iPhone using Tapatalk
     

  16. So if I were to make it give you either, a blaze book, gasp book, or a bat book.. How would this look? As I'm not entirely understanding how we could implement this in to how I'd want to do it.

    Blaze
    Code (Text):

    ArrayList<String> blaze = new ArrayList<String>();
           
            ItemStack blazeS = new ItemStack(Material.BOOK, 1);
            ItemMeta blazeM = blazeS.getItemMeta();
           
            blazeM.setDisplayName("§e§lBlaze I Enchant Book");
            blaze.add("");
            blaze.add("§d(!) §fDrag and drop this mysterious Blaze I book");
            blaze.add("§d(!) §fon a pickaxe, and notice your tools smelt.");
            blazeM.setLore(blaze);
            blazeS.setItemMeta(blazeM);
    Gasp
    Code (Text):

    ArrayList<String> gasp = new ArrayList<String>();
           
            ItemStack gaspS = new ItemStack(Material.BOOK, 1);
            ItemMeta gaspM = gaspS.getItemMeta();
           
            gaspM.setDisplayName("§e§lGasp I Enchant Book");
            gasp.add("");
            gasp.add("§d(!) §fDrag and drop this mysterious Gasp book");
            gasp.add("§d(!) §fon a helmet, and notice great breathing.");
            gaspM.setLore(gasp);
            gaspS.setItemMeta(gaspM);
    Bat
    Code (Text):

    ArrayList<String> bat = new ArrayList<String>();
           
            ItemStack batS = new ItemStack(Material.BOOK, 1);
            ItemMeta batM = batS.getItemMeta();
           
            batM.setDisplayName("§e§lBat I Enchant Book");
            bat.add("");
            bat.add("§d(!) §fDrag and drop this mysterious Bat book");
            bat.add("§d(!) §fon a helmet, and notice great vision in");
            bat.add("§d(!) §fthe depths of darkness.");
            batM.setLore(bat);
            batS.setItemMeta(batM);
     
  17. Why are you asking to be spoonfed like a baby? Try it out yourself and see if it works, and report back if it doesn't.
     
  18. You clearly haven't been reading the forums.. As each time I was given a response I did attempt to try it out..

    Hence this, but it doesn't work as I need to add MetaDate to the items.. And I'm UNSURE how to do this.
     
  19. You would stick each of those item creations in one of the case statement blocks like after case 2: before the break.

    Basically the item line gets a random number between one and whatever you set the max to. The switch statement only fires the block that matches that number.


    Sent from my iPhone using Tapatalk