Random items in chests?

Discussion in 'Plugin Development' started by tbone4747, Dec 30, 2012.

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

    tbone4747

    Would anyone know how to put a chest on the ground and have it restock itself randomly with random items? Im pretty new to java and bukkit plugins so be sure to be asked quite a few questions :)
     
  2. Offline

    tommycake50

    ok i will try to explain it but i dont wanna spoonfeed you code.
    so create an arraylist that holds every type of material in the game so it would be an ArrayList<Material>.
    and add all the materials to it.
    to place a chest Location loc = new Location(somex,somey,somez);
    loc.getBlock().setType(Material.Chest);
    Chest c = loc.getBlock().getState();
    then get its inventory with c.getInventory();
    then add items by creating a new java.util.random.
    and calling chstinventory.addItem(new ItemStack(materialarray.get(randomvariable.nextInt(materialarray.size)), randomvariable.nextInt(64) + 1));
    as many times as you want in a for loop.
    eg: for(int i = 0; i<randomvariable.nextInt(24); i++ ){
    //place items here
    }
    .
    then call c.upDate();
    and you have just added random amounts of random items to a chest!
    EDIT: oh damn i spoonfed you code xD
     
  3. Offline

    gomeow

  4. Offline

    tbone4747

    How would I get the location of a player then put it where it says "SomeX, SomeY, SomeZ"
     
  5. Offline

    chasechocolate

    Will you be using this in an event? If so use Location loc = event.getPlayer().getLocation(); And then you can get the x, y, and z from loc.
     
  6. Offline

    fireblast709

    Code:
    Location loc = player.getLocation().add(player.getLocation().getDirection().normalize());
    This is the location in front of the player
     
  7. Offline

    tbone4747

    This whole thing is very confusing :/ This is what i have in my main class so far.

    EDIT: And Im getting quite a few errors...
    PHP:
    package com.github.AirrPanda;
     
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
     
     
     
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class 
    Minealypse extends JavaPlugin
    {
     
     
        List<
    Materialmaterials Arrays.asList(Material.AIRMaterial.APPLEMaterial.ARROWMaterial.BAKED_POTATOMaterial.BOWMaterial.BOWLMaterial.BREADMaterial.BROWN_MUSHROOMMaterial.CARROTMaterial.CHAINMAIL_BOOTSMaterial.CHAINMAIL_CHESTPLATEMaterial.CHAINMAIL_HELMETMaterial.CHAINMAIL_LEGGINGSMaterial.COMPASSMaterial.COOKED_BEEFMaterial.COOKED_CHICKENMaterial.COOKED_FISHMaterial.COOKIEMaterial.DIAMONDMaterial.EMPTY_MAPMaterial.ENCHANTED_BOOKMaterial.ENDER_PEARLMaterial.EXP_BOTTLEMaterial.FISHING_RODMaterial.GLASS_BOTTLEMaterial.IRON_SWORDMaterial.LEATHER_BOOTSMaterial.LEATHER_CHESTPLATEMaterial.LEATHER_HELMETMaterial.STONE_SWORDMaterial.RAW_BEEFMaterial.RAW_CHICKENMaterial.RAW_FISHMaterial.RED_MUSHROOMMaterial.ROTTEN_FLESH);
        
    Location loc = new Location(null111);
        
    loc.getBlock().setType(Material.CEHST);
        
    Chest c loc.getBlock().getState();
        
    c.getInventory();
        
    java.util.random;
        
    chestInventory.addItem(new ItemStack(materiallist.get(randomvariabl.nextInt(materiallist.size)),
                
    randomvariable.nextInt(64) = 1));
        
    fur(int i oi<randomvariable.nextInt(24); i++){
         
        }
     
     
     
        @
    Override
        
    public void onEnable()
        {
            
    getLogger().info("Minealypse has been enabled!");
            
    PluginManager pm getServer().getPluginManager();
            
    pm.registerEvents(new PlayerListener(), this);
            try{
                @
    SuppressWarnings("rawtypes")
                Class[] 
    args = new Class[3];
                
    args[0] = Class.class;
                
    args[1] = String.class;
                
    args[2] = int.class;
     
                
    Method a net.minecraft.server.v1_4_6.EntityTypes.class.getDeclaredMethod("a"args);
                
    a.setAccessible(true);
     
                
    a.invoke(aSuperZombie.class, "Zombie"54);
            }catch (
    Exception e){
                
    e.printStackTrace();
                
    this.setEnabled(false);
            }
        }
     
        @
    Override
        
    public void onDisable()
        {
            
    getLogger().info("Minealypse has been disabled!");
        }
    /* SuperZombie *
        public void onCreatureSpawn(CreatureSpawnEvent event){
            if (event.isCancelled()) return;
     
            Location location = event.getLocation();
            Entity entity = event.getEntity();
            EntityType entityType = event.getEntityType();
            World world = location.getWorld();
     
            net.minecraft.server.v1_4_6.World mcWorld = ((CraftWorld) world).getHandle();
            net.minecraft.server.v1_4_6.Entity mcEntity = (((CraftEntity) entity).getHandle());
     
            if (entityType == EntityType.ZOMBIE && mcEntity instanceof SuperZombie == false){
                SuperZombie bloodMoonEntityZombie = new SuperZombie(mcWorld);
     
                bloodMoonEntityZombie.setPosition(location.getX(), location.getY(), location.getZ());
             
                mcWorld.removeEntity((net.minecraft.server.v1_4_6.EntityZombie) mcEntity);
                mcWorld.addEntity(bloodMoonEntityZombie, SpawnReason.CUSTOM);
     
                return;
            }
        }
          SuperZombie **
    */
    }
     
  8. Offline

    fireblast709

    because you are putting the code in the class body -_-
     
    CarPet likes this.
  9. Offline

    tbone4747

    fireblast709 So you saying i should make a separate class for it?
     
  10. Offline

    fireblast709

    tbone4747 no. You probably want the BlockPlaceEvent, and store some of it there right?
     
  11. Offline

    tbone4747

    Im new to coding, its your call. I put it in a separate class and got LESS errors...
    PHP:
    package com.github.AirrPanda;
     
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Chest;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
     
    public class 
    RandomChest {
        List<
    Materialmaterials Arrays.asList(Material.AIRMaterial.APPLEMaterial.ARROWMaterial.BAKED_POTATOMaterial.BOWMaterial.BOWLMaterial.BREADMaterial.BROWN_MUSHROOMMaterial.CARROTMaterial.CHAINMAIL_BOOTSMaterial.CHAINMAIL_CHESTPLATEMaterial.CHAINMAIL_HELMETMaterial.CHAINMAIL_LEGGINGSMaterial.COMPASSMaterial.COOKED_BEEFMaterial.COOKED_CHICKENMaterial.COOKED_FISHMaterial.COOKIEMaterial.DIAMONDMaterial.EMPTY_MAPMaterial.ENCHANTED_BOOKMaterial.ENDER_PEARLMaterial.EXP_BOTTLEMaterial.FISHING_RODMaterial.GLASS_BOTTLEMaterial.IRON_SWORDMaterial.LEATHER_BOOTSMaterial.LEATHER_CHESTPLATEMaterial.LEATHER_HELMETMaterial.STONE_SWORDMaterial.RAW_BEEFMaterial.RAW_CHICKENMaterial.RAW_FISHMaterial.RED_MUSHROOMMaterial.ROTTEN_FLESH);
        
    Location loc = new Location(null111);
        
    loc.getBlock().setType(Material.CEHST){
           
       
        
    Chest c = (Chestloc.getBlock().getState();
        
    c.getInventory();
       
        
    Random randomvariabl;
        
    Inventory chestInventory;
        
    chestInventory.addItem(new ItemStack(materials.get(randomvariabl.nextInt(materials.size())),
                
    randomvariable.nextInt(64) = 1));
        
    fur(int i oi<randomvariable.nextInt(24); i++){
           
        }
    }
    }
     
  12. Offline

    chasechocolate

    What are some of the errors you are getting?
     
  13. Offline

    tbone4747

    Eclipse is giving me errors here,
    PHP:
    loc.getBlock().setType(Material.CEHST)
    Here,
    PHP:
    randomvariable.nextInt(64) = 1));
    And here,
    PHP:
    for(int i oi<randomvariable.nextInt(24); i++){
         
        }
     
  14. Offline

    tommycake50

    let me fix that code up for you.
    package com.github.AirrPanda;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Chest;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    public class RandomChest {
    public void createChest(Location loc){
    List<Material> materials = Arrays.asList(Material.AIR, Material.APPLE, Material.ARROW, Material.BAKED_POTATO, Material.BOW, Material.BOWL, Material.BREAD, Material.BROWN_MUSHROOM, Material.CARROT, Material.CHAINMAIL_BOOTS, Material.CHAINMAIL_CHESTPLATE, Material.CHAINMAIL_HELMET, Material.CHAINMAIL_LEGGINGS, Material.COMPASS, Material.COOKED_BEEF, Material.COOKED_CHICKEN, Material.COOKED_FISH, Material.COOKIE, Material.DIAMOND, Material.EMPTY_MAP, Material.ENCHANTED_BOOK, Material.ENDER_PEARL, Material.EXP_BOTTLE, Material.FISHING_ROD, Material.GLASS_BOTTLE, Material.IRON_SWORD, Material.LEATHER_BOOTS, Material.LEATHER_CHESTPLATE, Material.LEATHER_HELMET, Material.STONE_SWORD, Material.RAW_BEEF, Material.RAW_CHICKEN, Material.RAW_FISH, Material.RED_MUSHROOM, Material.ROTTEN_FLESH);
    loc.getBlock().setType(Material.CEHST);


    Chest c = (Chest) loc.getBlock().getState();
    Inventory chestInventory;
    chestInventory = c.getInventory();

    Random randomvariabl = new Random();


    for(int i = o; i<randomvariable.nextInt(24); i++){
    chestInventory.addItem(new ItemStack(materials.get(randomvariabl.nextInt(materials.size())),
    randomvariable.nextInt(64) + 1));
    }
    }
    }
    }
     
  15. Offline

    fireblast709

    tbone4747 the fact that you have less errors does not mean it is better
     
  16. Offline

    tbone4747

    Now all i need is to figure out whats going on here...
    PHP:
    int o 0;
    Random randomvariable null;
    Sorry If im bugging you guys, im new to this stuff :/
     
  17. Offline

    tommycake50

    pls pls just go and learn java for gods sake
     
  18. Offline

    tbone4747

  19. Offline

    HighOnSwiftness

    I don't know, how do you set a material called cehst?
     
  20. Offline

    XgXXSnipz

Thread Status:
Not open for further replies.

Share This Page