How to fill chests inside a Selection?

Discussion in 'Plugin Development' started by MrGriefer_HGTech, Dec 11, 2014.

Thread Status:
Not open for further replies.
  1. Hi! So I am making a SurvivalGames plugin and I made that a player need to create arena so I made it by selecting two points and then create arena, anyway the player put chests inside the map and when the game starts the chests does not get filled! So how can I make [chest] fill when the game starts?

    Here is what I did. CLICK!

    Note!
    Sorry for my bad English I am not English! ;)
     
  2. Offline

    mine-care

    @MrGriefer_HGTech Please provide your code, The link above ports to pastebin.com, on the index page rather than your code =)
     
  3. @mine-care How can put the code on the page ?

    @AdamQpzm Yes! I did debug the code but I still did not add the Chest fill thing and what should I do with that code?
    do I add the code that put it on top on the my Chest Class?
     
  4. @MrGriefer_HGTech Please post your code with debug attempts :) And if you'd like to include your code on this page, simply click insert > code and then a box will pop up for you to paste your code into. The button looks like this:

    [​IMG]
     
  5. Well there is 25 classes! I am gonna only put the Main Class, Arena Class, Chest Manager Class.

    Main Class:
    Code:
    package me.MrGriefer_.SurvivalGames;
    
    import me.MrGriefer_.SurvivalGames.listeners.EntityDamage;
    import me.MrGriefer_.SurvivalGames.listeners.LeaveItem;
    import me.MrGriefer_.SurvivalGames.listeners.PlayerJoin;
    import me.MrGriefer_.SurvivalGames.listeners.PlayerLeaveArena;
    import me.MrGriefer_.SurvivalGames.listeners.PlayerMove;
    import me.MrGriefer_.SurvivalGames.listeners.RollbackManager;
    import me.MrGriefer_.SurvivalGames.listeners.SignManager;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    
    public class Main extends JavaPlugin {
    
        private static RollbackManager rollbackManager;
    
        public void onEnable() {
            if (!Bukkit.getPluginManager().isPluginEnabled("WorldEdit")) {
                System.err.println("[SurvivalGames] ##########################################################");
                System.err.println("[SurvivalGames] ######### NO WORLDEDIT FOUND! DISABLE PLUGIN... ##########");
                System.err.println("[SurvivalGames] ##########################################################");
                Bukkit.getPluginManager().disablePlugin(this);
                return;
            }
           
            ArenaManager.getInstance().setup();
           
            getCommand("survivalgames").setExecutor(new CommandManager());
           
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(new EntityDamage(), this);
            pm.registerEvents(new LeaveItem(), this);
            pm.registerEvents(new PlayerJoin(), this);
            pm.registerEvents(new PlayerLeaveArena(), this);
            pm.registerEvents(new PlayerMove(), this);
            pm.registerEvents(rollbackManager = new RollbackManager(), this);
            pm.registerEvents(new SignManager(), this);
    
        }
    
        public static Plugin getPlugin() {
            return Bukkit.getServer().getPluginManager().getPlugin("SurvivalGames");
        }
       
        public static WorldEditPlugin getWorldEdit() {
            return (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
        }
    
        public static void saveLocation(Location location, ConfigurationSection section) {
            section.set("world", location.getWorld().getName());
            section.set("x", location.getX());
            section.set("y", location.getY());
            section.set("z", location.getZ());
            section.set("pitch", location.getPitch());
            section.set("yaw", location.getYaw());
        }
       
        public static Location loadLocation(ConfigurationSection section) {
            return new Location(
                    Bukkit.getServer().getWorld(section.getString("world")),
                    section.getDouble("x"),
                    section.getDouble("y"),
                    section.getDouble("z"),
                    (float) section.getDouble("pitch"),
                    (float) section.getDouble("yaw")
            );
        }
    }

    Arena Class:
    Code:
    package me.MrGriefer_.SurvivalGames;
    
    import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.BlockState;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Arena {
       
        public enum ArenaState {
            WAITING, COUNTDOWN, STARTED
        }
    
        private String id;
        private CuboidSelection bounds;
        private ArenaState state;
       
        private ArrayList<Spawn> spawns;
       
        private ArrayList<Player> players;
        private ArrayList<BlockState> changedBlocks;
       
        protected Arena(String id) {
            this.id = id;
           
            this.bounds = new CuboidSelection(
                    Bukkit.getServer().getWorld(SettingsManager.getArenas().<String>get(id + ".world")),
                    Main.loadLocation(SettingsManager.getArenas().<ConfigurationSection>get(id + ".cornerA")),
                    Main.loadLocation(SettingsManager.getArenas().<ConfigurationSection>get(id + ".cornerB"))
            );
           
            this.state = ArenaState.WAITING;
           
            this.spawns = new ArrayList<Spawn>();
            if (SettingsManager.getArenas().contains(id + ".spawns")) {
                for (String spawnID : SettingsManager.getArenas().<ConfigurationSection>get(id + ".spawns").getKeys(false)) {
                    spawns.add(new Spawn(Main.loadLocation(SettingsManager.getArenas().<ConfigurationSection>get(id + ".spawns." + spawnID))));
                }           
            }
           
            this.players = new ArrayList<Player>();
            this.changedBlocks = new ArrayList<BlockState>();
        }
       
        public String getID() {
            return id;
        }
       
        public CuboidSelection getBounds() {
            return bounds;
        }
       
        public int getMaxPlayers() {
            return spawns.size();
        }
       
        public ArenaState getState() {
            return state;
        }
       
        public Player[] getPlayers() {
            return players.toArray(new Player[players.size()]);
        }
       
        public boolean hasPlayer(Player p) {
            return players.contains(p);
        }
       
        public void addPlayer(Player p) {
            if (state == ArenaState.STARTED) {
                p.sendMessage("§3[SurvivalGames] §6This arena has already started.");
                return;
            }
           
            if (players.size() + 1 > spawns.size()) {
                p.sendMessage("§3[SurvivalGames] §6This arena is full.");
                return;
            }
           
            boolean success = false;
           
            for (Spawn spawn : spawns) {
                if (!spawn.hasPlayer()) {
                    spawn.setPlayer(p);
                    p.teleport(spawn.getLocation());
                    success = true;
                    break;
                }
            }
           
            if (!success) {
                p.sendMessage("§3[SurvivalGames] §6Could not find spawn.");
                return;
            }
           
            players.add(p);
           
            p.getInventory().clear();
            p.setHealth(20.0);
            p.setFoodLevel(20);
            p.setExp(0);
            p.getInventory().setHelmet(new ItemStack(Material.AIR));
            p.getInventory().setChestplate(new ItemStack(Material.AIR));
            p.getInventory().setLeggings(new ItemStack(Material.AIR));
            p.getInventory().setBoots(new ItemStack(Material.AIR));
    
            p.setGameMode(GameMode.SURVIVAL);
           
            ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
            BookMeta bm = (BookMeta)book.getItemMeta();
            bm.addPage("Survival Games is a gametype built around the core of minecraft; surviving. 24 tributes are spawned with nothing on one arena, with one goal: to be the last alive. You must use your skills to discover");
            bm.setAuthor("Notch");
            bm.setTitle("§3SG Guide");
            book.setItemMeta(bm);
            p.getInventory().setItem(0, book);
           
            ItemStack leave = new ItemStack(Material.WATCH, 1);
            ItemMeta meta = leave.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Leave");
            leave.setItemMeta(meta);
            p.getInventory().setItem(8, leave);
            p.updateInventory();
           
            p.sendMessage("§3[SurvivalGames] §6You have joined arena " + id + ".");
           
            if (players.size() >= spawns.size() && state == ArenaState.WAITING) {
                this.state = ArenaState.COUNTDOWN;
                new Countdown(this, 30, 30, 20, 10, 5, 4, 3, 2, 1).runTaskTimerAsynchronously(Main.getPlugin(), 0, 20);
            }
        }
       
        public void removePlayer(Player p) {
            players.remove(p);
           
            for (Spawn spawn : spawns) {
                if (spawn.hasPlayer() && spawn.getPlayer().equals(p)) {
                    spawn.setPlayer(null);
                }
            }
           
            p.teleport(Bukkit.getServer().getWorlds().get(0).getSpawnLocation()); // TODO: Temporary.
            p.getInventory().clear();
           
            if (players.size() <= 1) {
                if (players.size() == 1) {
                    Bukkit.getServer().broadcastMessage(players.get(0).getName() + " has won arena " + id + "!");
                    players.remove(0);
                    players.get(0).teleport(Bukkit.getServer().getWorlds().get(0).getSpawnLocation()); // TODO: Temporary.
                }
               
                else {
                    Bukkit.getServer().broadcastMessage("§3[SurvivalGames] §6Arena " + id + " has ended.");
                }
    
                for(Entity e : Bukkit.getWorld(this.getBounds().getWorld().getName()).getEntities()){
                    if(this.getBounds().contains(e.getLocation()) && e.getType() == EntityType.DROPPED_ITEM){
                        e.remove();
                    }
                }
    
                players.clear();
                rollback();
                state = ArenaState.WAITING;
            }
        }
       
        public void addSpawn(Location loc) {
            spawns.add(new Spawn(loc));
        }
       
        public void addBlockState(BlockState state) {
    
                        if (!changedBlocks.contains(state)) {
                            changedBlocks.add(state);
    
                        }
    
        }
    
        public void regen(final List<BlockState> blocks, final boolean effect, final long speed) {
    
            new BukkitRunnable() {
                int i = -1;
                public void run() {
                    if (i != blocks.size() - 1) {
                        i++;
                        BlockState bs = changedBlocks.get(i);
                        bs.update(true, false);
                        if (effect)
                            bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, bs.getBlock().getType());
                    }else {
    
                        blocks.clear();
                        this.cancel();
                    }
                }
            }.runTaskTimer(Main.getPlugin(), speed, speed);
        }
    
        public void rollback() {
    
            regen(changedBlocks, true, (long) 1);
    
    //        try {
    //            LocalWorld world = BukkitUtil.getLocalWorld(getBounds().getWorld());
    //            world.regenerate(getBounds().getRegionSelector().getRegion(), new EditSession(world, -1));
    //        }catch (Exception e){
    //            Bukkit.getServer().broadcastMessage(e.getLocalizedMessage());
    //        }
         }
       
        public void start() {
            this.state = ArenaState.STARTED;
           
            for (Player p : players) {
               
                p.setHealth(20.0D);
                p.setGameMode(GameMode.SURVIVAL);
            }
           
            for (Spawn spawn : spawns) {
                spawn.setPlayer(null);
            }
        }
    
        public Location getSpawn(Player p){
            for (Spawn s : spawns){
                if (s.getPlayer() == p){
                    return s.getLocation();
                }
            }
            return null;
        }
    }
    Chest Manager:
    Code:
    package me.MrGriefer_.SurvivalGames;
    
    import java.util.Random;
    
    import org.bukkit.Material;
    import org.bukkit.block.Chest;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    public class ChestManager {
    
        private Chest chest;
        private int tier;
       
        public ChestManager(Chest chest, int tier) {
            this.chest = chest;
            this.tier = tier;
        }
       
        public Chest getChest() {
            return chest;
        }
       
        public int getTier() {
            return tier;
        }
       
        public void fillChest(Player p) {
            Arena a = ArenaManager.getInstance().getArena(p);
           
            if (a.getBounds().contains(p.getLocation())) {
                Material[] items1 = new Material[] { Material.WOOD_AXE, Material.LEATHER_BOOTS, Material.GOLD_HELMET, Material.STRING, Material.APPLE, Material.ARROW };
                Material[] items2 = new Material[] { Material.COOKED_BEEF, Material.RAW_CHICKEN, Material.RAW_FISH, Material.MUSHROOM_SOUP, Material.WOOD_SWORD, Material.GOLD_LEGGINGS, Material.MELON };
               
                Random r = new Random();
               
                int numItems = r.nextInt(5) + 1;
               
                for (int i = 0; i < numItems; i++) {
                    Material material = null;
                       
                    if (tier == 1) {
                        material = items1[r.nextInt(items1.length)];
                    }
                   
                    else if (tier == 2) {
                        material = items2[r.nextInt(items2.length)];
                    }
                   
                    ItemStack item = new ItemStack(material, 1);
                   
                    int index;
                   
                    do {
                            index = r.nextInt(chest.getInventory().getSize());
                    } while (chest.getInventory().getItem(index) != null);
                   
                    chest.getInventory().setItem(index, item);
                }
            }
        }
    }
    
    Anyone who can help me? I posted some of my classes!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  6. Offline

    ZodiacTheories

    @AdamQpzm

    What happened to the old code button :'(
    :'(
     
    AdamQpzm likes this.
  7. which button? :confused:
     
  8. @AdamQpzm Yes! I did debug my plugin, that's what I get it from debug attempts means.
     
  9. Offline

    mine-care

    @AdamQpzm Now it works for me too, probably OP fixed it
     
  10. @MrGriefer_HGTech Debug attempts as in put some println statements in - check that the correct parts ofthe code are being executed, and that the variables have the value you expect them to.
     
Thread Status:
Not open for further replies.

Share This Page