inventory random item location

Discussion in 'Plugin Development' started by BizeaxPvP, Aug 1, 2019.

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

    BizeaxPvP

    so i have an inventory created and i would like for the items to be at a random location and idk where to start

    Code:
     
    public class CustomInventory implements Listener{
       
        private Plugin plugin = Main.getPlugin(Main.class);
       
        public void newInventory(Player player) {
            Inventory inv = plugin.getServer().createInventory(null , 9, ChatColor.RED + "Are you a robot?");
            ItemStack verify = new ItemStack(Material.STAINED_GLASS_PANE, 1,(byte) 5);
            ItemMeta vmeta = verify.getItemMeta();
            vmeta.setDisplayName(ChatColor.GREEN + "Click here to verify!");
            verify.setItemMeta(vmeta);
            ItemStack noll = new ItemStack(Material.STAINED_GLASS_PANE, 1,(byte) 14);
            ItemMeta nmeta = noll.getItemMeta();
            nmeta.setDisplayName(ChatColor.RED + "Click on the green glass to verify!");
            noll.setItemMeta(nmeta);
           
            inv.setItem(4, verify);
            inv.setItem(6, noll);
            inv.setItem(0, noll);
            inv.setItem(1, noll);
            inv.setItem(2, noll);
            inv.setItem(3, noll);
            inv.setItem(5, noll);
            inv.setItem(7, noll);
            inv.setItem(8, noll);
           
            player.openInventory(inv);
        }
    
    }
     
  2. Offline

    KarimAKL

    @BizeaxPvP Create an instance of java.util.Random, and then use the nextInt(int bound) method.
    Example:
    Code:Java
    1. new Random().nextInt(inventory.getSize());

    Though you should make the Random an instance variable, and not every time you call the method.
     
  3. Offline

    BizeaxPvP

    works! but it only shows 1 random red glass
    Code:
     
    package me.Zxoir.OnlyzAntiBot;
    
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    
    public class CustomInventory implements Listener{
       
        private Plugin plugin = Main.getPlugin(Main.class);
       
        public void newInventory(Player player) {
            Inventory inv = plugin.getServer().createInventory(null , 9, ChatColor.RED + "Are you a robot?");
            ItemStack verify = new ItemStack(Material.STAINED_GLASS_PANE, 1,(byte) 5);
            ItemMeta vmeta = verify.getItemMeta();
            vmeta.setDisplayName(ChatColor.GREEN + "Click here to verify!");
            verify.setItemMeta(vmeta);
            ItemStack noll = new ItemStack(Material.STAINED_GLASS_PANE, 1,(byte) 14);
            ItemMeta nmeta = noll.getItemMeta();
            nmeta.setDisplayName(ChatColor.RED + "Click on the green glass to verify!");
            noll.setItemMeta(nmeta);
           
            int random = new Random().nextInt(inv.getSize());
           
            inv.setItem(random, verify);
            inv.setItem(random, noll);
            inv.setItem(random, noll);
            inv.setItem(random, noll);
            inv.setItem(random, noll);
            inv.setItem(random, noll);
            inv.setItem(random, noll);
            inv.setItem(random, noll);
            inv.setItem(random, noll);
           
            player.openInventory(inv);
        }
    
    }
    
     
  4. Offline

    KarimAKL

    @BizeaxPvP
    1. I told you to make the Random an instance variable, and not create one everytime you call the method.
    2. You are assigning the value of Random#nextInt(int bound) to a variable, so you are setting the same slot everytime. You should instead get a new number for every slot you want to set.
     
  5. Offline

    BizeaxPvP

    i mean the method i was using is working fine soo...
    and i made multiple and it still sometimes has empty spaces
    Code:
     
    package me.Zxoir.OnlyzAntiBot;
    
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    
    public class CustomInventory implements Listener{
       
        private Plugin plugin = Main.getPlugin(Main.class);
       
        public void newInventory(Player player) {
            Inventory inv = plugin.getServer().createInventory(null , 9, ChatColor.RED + "Are you a robot?");
            ItemStack verify = new ItemStack(Material.STAINED_GLASS_PANE, 1,(byte) 5);
            ItemMeta vmeta = verify.getItemMeta();
            vmeta.setDisplayName(ChatColor.GREEN + "Click here to verify!");
            verify.setItemMeta(vmeta);
            ItemStack noll = new ItemStack(Material.STAINED_GLASS_PANE, 1,(byte) 14);
            ItemMeta nmeta = noll.getItemMeta();
            nmeta.setDisplayName(ChatColor.RED + "Click on the green glass to verify!");
            noll.setItemMeta(nmeta);
           
            int random = new Random().nextInt(inv.getSize());
            int random1 = new Random().nextInt(inv.getSize());
            int random2 = new Random().nextInt(inv.getSize());
            int random3 = new Random().nextInt(inv.getSize());
            int random4 = new Random().nextInt(inv.getSize());
            int random5 = new Random().nextInt(inv.getSize());
            int random6 = new Random().nextInt(inv.getSize());
            int random7 = new Random().nextInt(inv.getSize());
            int random8 = new Random().nextInt(inv.getSize());
           
            inv.setItem(random1, verify);
            inv.setItem(random2, noll);
            inv.setItem(random3, noll);
            inv.setItem(random4, noll);
            inv.setItem(random5, noll);
            inv.setItem(random6, noll);
            inv.setItem(random7, noll);
            inv.setItem(random8, noll);
            inv.setItem(random, noll);
           
            player.openInventory(inv);
        }
    
    }
    
    
     
  6. Offline

    Machine Maker

    @BizeaxPvP creating a new Random() everytime is super bad from a performance standpoint. Creating a new Random() every time is way less efficient than creating one and referencing it multiple times. While you might not notice a difference on this small scale, its probably a good idea to learn some ways of improving plugin performance.
     
  7. Offline

    KarimAKL

    @BizeaxPvP
    1. I'm saying you should make it an instance variable because of performance, not because it won't work.
    2. The number can be the same (it's random after all), make a list of integers containing all integers from 0 - inventory size, and then remove the integer that was used.
     
  8. Offline

    BizeaxPvP

    how do i make a list of integers?
     
  9. Offline

    KarimAKL

  10. Offline

    vtboy2000

    Code:
     private Plugin plugin = Main.getPlugin(Main.class);
    
        public void newInventory(Player player) {
            Inventory inv = registerInventory();
            randomizeInventory(inv);
    
            player.openInventory(inv);
        }
    
        private void randomizeInventory(Inventory inv) {
            int size = inv.getSize();
            Random random = new Random();
            List<Integer> used = new ArrayList<>();
            for (int i = 0; i < 9; i++) {
                int value = random.nextInt(size);
    
                do {
                    value = random.nextInt(size);
               } while(used.contains(value)) 
                
                used.add(value);
                inv.setItem(value, noll);
            }
        }
    
    
        private Inventory registerInventory() {
            Inventory inv = plugin.getServer().createInventory(null, 9, ChatColor.RED + "Are you a robot?");
            ItemStack verify = new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte) 5);
            ItemMeta vmeta = verify.getItemMeta();
            vmeta.setDisplayName(ChatColor.GREEN + "Click here to verify!");
            verify.setItemMeta(vmeta);
            ItemStack noll = new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte) 14);
            ItemMeta nmeta = noll.getItemMeta();
            nmeta.setDisplayName(ChatColor.RED + "Click on the green glass to verify!");
            noll.setItemMeta(nmeta);
            return inv;
        }
    didn't test this code, but this should make sure that nothing is set to the same position
     
    Last edited: Aug 9, 2019
Thread Status:
Not open for further replies.

Share This Page