Chest Randomized Filling

Discussion in 'Plugin Development' started by someguyonthestreet, Apr 21, 2015.

Thread Status:
Not open for further replies.
  1. Hello,
    I'm currently making a survival games plugin but I don't know how I will get around doing the chests. I plan to just make every item have a random chance of spawning. And all the items would be hard coded. Since I really have no idea how to do it, and i've looked at other posts, I was wondering if somebody here could help me.
    Thanks
     
  2. Offline

    nverdier

    @someguyonthestreet Post what you have tried and will tell you what you did wrong and how to fix it. But we won't spoonfeed.
     
  3. Offline

    nj2miami

    I am assuming you are pre-placing chests around and want those chests populated when they open them?

    You can do quite a few things of varying complexity.

    Simplest: Grab the inventoryopen event and throw in a random amount of stuff in it. Downside: Player can spam the chest and get unlimited stuff.

    So, you have to add some tracking so when a player open a chest they either do not get stuff again or you have a timer so they can come back at some point again.

    You can give EACH PLAYER its own inventory when they open a chest so when a player open a chest they see only what is THEIRS. This requires some more manipulation, but very doable.

    Many options :)
     
  4. Ever heard of inventory.clear(); ? :p

    @someguyonthestreet
    Loop through all blocks in the area,
    check if it is a chest
    case it to chest
    use chest.getBlockInventory();
    make some stuff with it
     
  5. Offline

    nj2miami

    Not sure what you mean or how that applies. Why would you waste time searching for every chest that MIGHT or MIGHT NOT be opened?

    When a player open a chest somewhere you want the randomization to happen, simply call your "Fill" method. This allows you to track individual opened chests and decide how many times a plyaer can open it, how long a player has to wait until they can open again (if at all)...

    You provided nothing useful in your suggestion.

    I offered multiple suggestions based on the OP's decision of complexity from simply randomizing every chest, every time (simplest but least effective) all the way up to tracking each opening, the player who opened, the time they opened, how many times they opened, etc.
     
  6. @nj2miami
    I meant, that when the arena is loaded / the game is started, clear all chest and put items in them.
    Otherwise chests will always be full of items no matter what players take out of them.
     
  7. Offline

    nj2miami

    Yes but why PREFILL every chest when you can do it "on the fly" when someone actually opens one? Of course you could prefill them all one time at the beginning of the game.

    Guess it just depends on what he is going for.

    My suggestion offered a way for him to manage the chests so they can be REFILLED during a game and track who has opened them and if they can open another, etc.

    Without knowing the OP intentions, it is hard to provide a definitive answer, so I offered suggestions.
     
  8. Offline

    Signatured

    @FisheyLP @nj2miami Why not just make a command (such as /savechest), get the block the player is looking at, and if it's a chest add it to a config. Then, when it's time to refill all the chests, loop through all chests in the config , clear them, add random items.
     
  9. Offline

    nj2miami

    Because he wants them all, so why would you go one by one?

    Now if you just want SOME chests at SOME locations, then yes you would need some method to select the chests you want tracked in game, save them to a config and load those each game start to determine which you will randomize.
     
  10. @Signatured if he downloads a map with many chests, he'll get craz when he's "registering" each chest.
     
  11. @FisheyLP @nj2miami @Signatured @nverdier
    My Code:
    I have the chest code, I just need help with randomizing because the method im using just gives too many items plus the randomizing seems a bit over powered so I want help fixing it.
    Code:
    @EventHandler
         public void onPlayerInteract(PlayerInteractEvent event) {
          if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
           if (event.getClickedBlock().getType() == Material.CHEST) {
            event.setCancelled(true);
            if (!inv.containsKey(event.getClickedBlock().getLocation())) {
             Inventory invv = Bukkit.createInventory(null, 9 * 3, "Chest");
             fill(invv);
             inv.put(event.getClickedBlock().getLocation(), invv);
            }
            Inventory invvv = inv.get(event.getClickedBlock().getLocation());
            event.getPlayer().openInventory(invvv);
           
           }
          }
         }
        
       
        public static void fill(Inventory inv){
           
    
            for(int i = 0; i <= 26; i++){
            Random r = new Random();
    
            //FOOD
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.APPLE));
              }
              if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.BREAD));
              }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.PORK));
            }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.PUMPKIN_PIE));
            }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.CARROT_ITEM));
            }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.BAKED_POTATO));
            }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.COOKED_BEEF));
            }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.RAW_BEEF));
            }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.COOKIE));
            }
            if(i == r.nextInt(75)){
            inv.setItem(i, new ItemStack(Material.ARROW));
                }
            //Armor
    if(i == r.nextInt(300)){
        inv.setItem(i, new ItemStack(Material.LEATHER_HELMET));
    }
        if(i == r.nextInt(300)){
        inv.setItem(i, new ItemStack(Material.LEATHER_CHESTPLATE));
        }
        if(i == r.nextInt(300)){
        inv.setItem(i, new ItemStack(Material.LEATHER_LEGGINGS));
        }
        if(i == r.nextInt(300)){
        inv.setItem(i, new ItemStack(Material.LEATHER_BOOTS));
        }
       
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.CHAINMAIL_HELMET));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.CHAINMAIL_CHESTPLATE));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.CHAINMAIL_LEGGINGS));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.CHAINMAIL_BOOTS));
        }
       
        if(i == r.nextInt(700)){
        inv.setItem(i, new ItemStack(Material.IRON_HELMET));
        }
        if(i == r.nextInt(700)){
        inv.setItem(i, new ItemStack(Material.IRON_CHESTPLATE));
        }
        if(i == r.nextInt(700)){
        inv.setItem(i, new ItemStack(Material.IRON_LEGGINGS));
        }
        if(i == r.nextInt(700)){
        inv.setItem(i, new ItemStack(Material.IRON_BOOTS));
        }
       
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.GOLD_HELMET));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.GOLD_CHESTPLATE));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.GOLD_LEGGINGS));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.GOLD_BOOTS));
            }
    
            //Weapons
    if(i == r.nextInt(200)){
        inv.setItem(i, new ItemStack(Material.WOOD_AXE));
    }
        if(i == r.nextInt(200)){
        inv.setItem(i, new ItemStack(Material.GOLD_AXE));
        }
        if(i == r.nextInt(275)){
        inv.setItem(i, new ItemStack(Material.STONE_AXE));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.IRON_AXE));
        }
       
        if(i == r.nextInt(300)){
        inv.setItem(i, new ItemStack(Material.WOOD_SWORD));
        }
        if(i == r.nextInt(375)){
        inv.setItem(i, new ItemStack(Material.GOLD_SWORD));
        }
        if(i == r.nextInt(500)){
        inv.setItem(i, new ItemStack(Material.STONE_SWORD));
        }
        if(i == r.nextInt(1000)){
        inv.setItem(i, new ItemStack(Material.IRON_SWORD));
        }
       
        if(i == r.nextInt(550)){
        inv.setItem(i, new ItemStack(Material.BOW));
    }
    
            //ITEMS
    if(i == r.nextInt(900)){
        inv.setItem(i, new ItemStack(Material.FISHING_ROD));
    }
        if(i == r.nextInt(1000)){
        inv.setItem(i, new ItemStack(Material.DIAMOND));
        }
        if(i == r.nextInt(700)){
        inv.setItem(i, new ItemStack(Material.GOLD_INGOT));
        }
        if(i == r.nextInt(950)){
        inv.setItem(i, new ItemStack(Material.IRON_INGOT));
        }
        if(i == r.nextInt(950)){
        inv.setItem(i, new ItemStack(Material.STICK));
        }
            }
           
        }
        
     
  12. Offline

    Zombie_Striker

    Instead of creating many r.nextInt Instances (which you can have multiple turn true, but will override the last item), create one r.next int and use something like
    Code:
    if(a == 1)
    if(a == 2)
    if(a == 3)
    if(a == 4)
     
    BlazingBroGamer likes this.
  13. Offline

    schwabfl

    Code:
    for (int i = 0; i < amountOfItems; i++) {
           Material material = chestItems[random.nextInt(chestItems.length)]; //An array of materials that you specify
           if (inventory.contains(material)) {
              inventory.addItem(new ItemStack(material, material.getMaxStackSize() != 1 ? random.nextInt(maxStackSize) + 1 /* in case nextInt() returns 0*/ : 1)); // addItem() rather than setItem() to a random slot to prevent overriding already filled slots
              continue;
           } else {
              inventory.setItem(random.nextInt(inventory.getSize(), new ItemStack(material, material.getMaxStackSize() != 1 ? random.nextInt(maxStackSize) + 1 : 1)));
              continue;
           }
        }
    
    This is the code that I use to fill the chest
     
    Last edited: Apr 23, 2015
  14. @schwabfl
    Would inventory be the inventory of the chest and is chestItems a list?
     
  15. Offline

    schwabfl

    @someguyonthestreet
    inventory is the chest's inventory
    and chestItems is an array:

    Code:
    Material chestItems[] = new Material[] { Material.STONE_SWORD, Material.IRON_SWORD ... };
    
     
Thread Status:
Not open for further replies.

Share This Page