Solved What is the problem with this code?

Discussion in 'Plugin Development' started by Craftanolokao, Nov 23, 2015.

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

    Craftanolokao

    I wanna check if the clicked block is equals the chest placed by event random, for put random Item in chest.

    Code:
    package me.teasure.find;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.Chest;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Main extends JavaPlugin implements Listener{
    
        public static Main instance;
        public static FileConfiguration config;
    
        @Override
        public void onEnable() {
            config = getConfig();
            instance = this;
            Bukkit.getPluginManager().registerEvents(this, this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("findchest")){
                if(p.isOp()){
                    create(p);
                }else{
                    p.sendMessage("Desculpe vc não tem permissão!");
                }
            }
            return false;
        }
        public static ArrayList<ItemStack> regular = new ArrayList<>();
        public static ArrayList<ItemStack> rare = new ArrayList<>();
       
        public static HashMap<String , Location> bau= new HashMap<>();
        @SuppressWarnings("deprecation")
        public static void create(Player p){
    
            Random r = new Random();
            Location loc = p.getLocation();
            World world = p.getWorld();
            double x = loc.getX();
            double z = loc.getZ();
            int y = world.getHighestBlockYAt((int)x,(int) z);
            Location nloc = new Location(world, x + r.nextInt(10000) - r.nextInt(10000), y + 1, z  + r.nextInt(10000) - r.nextInt(10000));
            nloc.getBlock().setType(Material.CHEST);
            bau.put("Tesouro", nloc);
            new BukkitRunnable() {
                int i = 0;
                @Override
                public void run() {
                    if(bau.get("Tesouro") == nloc){
                        if(nloc.getBlock().getType() == Material.CHEST){
                            if(i == 15*60){
                                for(Player all : Bukkit.getOnlinePlayers()){
                                    all.sendMessage("O Tesouro foi removido!");
                                }
                            }
                            Firework f = (Firework) nloc.getWorld().spawn(nloc, Firework.class);
                            FireworkMeta fm = f.getFireworkMeta();
                            fm.setPower(3);
                           
                        }
    
                    }else{
                        cancel();
                    }
    
                }
            }.runTaskTimer(Main.instance, 0, 20);
            for(Player all : Bukkit.getOnlinePlayers()){
                double nx = nloc.getX();
                double nz = nloc.getZ();
                int ny = nloc.getWorld().getHighestBlockYAt((int)x,(int) z);
                bau.put("Tesouro", nloc);
                all.sendMessage("O tesouro se encontra nas cordenadas: " + (int)nx + ", " + ny + ", " + (int)nz);
            }
        }
        public static ItemStack item(Material material, Enchantment encantamento, int level ){
            ItemStack item = new ItemStack(material);
            item.addUnsafeEnchantment(encantamento, level);
            return item;
        }
        public static  ItemStack item(Material material){
            ItemStack item = new ItemStack(material);
            return item;
    
        }
        public static  ItemStack item(Material material, String nome){
            ItemStack item = new ItemStack(material);
            ItemMeta im = item.getItemMeta();
            im.setDisplayName(nome);
            item.setItemMeta(im);
            return item;
    
        }
        @EventHandler
        public void abrirbau(PlayerInteractEvent e){
            Block clickedBlock = e.getClickedBlock();
            if(clickedBlock.getState() instanceof Chest){
                regular.add(item(Material.IRON_CHESTPLATE));
                regular.add(item(Material.IRON_BOOTS));
                regular.add(item(Material.IRON_HELMET));
                regular.add(item(Material.IRON_LEGGINGS));
                regular.add(item(Material.IRON_SWORD));
                regular.add(item(Material.DIAMOND_SWORD));
                regular.add(item(Material.GOLDEN_APPLE));
                regular.add(item(Material.DIAMOND));
                Random rarmor = new Random();
                ItemStack espada = item(Material.DIAMOND_SWORD, Enchantment.DAMAGE_ALL, rarmor.nextInt(2) + 3);
                espada.addUnsafeEnchantment(Enchantment.DURABILITY, 4);
                ItemStack peitoral = item(Material.DIAMOND_CHESTPLATE, Enchantment.PROTECTION_ENVIRONMENTAL, rarmor.nextInt(4) + 1);
                ItemStack calças = item(Material.DIAMOND_LEGGINGS, Enchantment.PROTECTION_ENVIRONMENTAL, rarmor.nextInt(4)+ 1);
                ItemStack capacete = item(Material.DIAMOND_HELMET, Enchantment.PROTECTION_ENVIRONMENTAL, rarmor.nextInt(4)+ 1);
                ItemStack botas = item(Material.DIAMOND_BOOTS, Enchantment.PROTECTION_ENVIRONMENTAL, rarmor.nextInt(4)+ 1);
                rare.add(espada);
                rare.add(capacete);
                rare.add(peitoral);
                rare.add(calças);
                rare.add(botas);
                //action
    
                Chest chest = (Chest) clickedBlock.getState();
                Inventory cinv = chest.getInventory();
                Random chance = new Random();
                cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
                cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
                cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
                cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
                cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
                cinv.addItem(raro.get(chance.nextInt(raro.size())).clone());
            }
        }
    }
    
     
  2. Offline

    Lordloss

  3. Offline

    Craftanolokao

    did not yield
     
  4. Offline

    mcdorli

    Let's begin:

    1.: Why do you store an instance of the main class in the main class? Just use this
    2.: Don't use static variables, only if you need to share 1 variable between classes
    3.: You're violating D.R.Y. Example:
    Code:
    cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
    cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
    cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
    cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
    cinv.addItem(regular.get(chance.nextInt(regular.size())).clone());
    
    4.: Did I mentioned static abuse?
    5.: Use broadcastMessage
    Code:
    for(Player all : Bukkit.getOnlinePlayers()){
                double nx = nloc.getX();
                double nz = nloc.getZ();
                int ny = nloc.getWorld().getHighestBlockYAt((int)x,(int) z);
                bau.put("Tesouro", nloc);
                all.sendMessage("O tesouro se encontra nas cordenadas: " + (int)nx + ", " + ny + ", " + (int)nz);
            }
    
    6.: The package name should be like this:

    me.<name>.<projectname> projectname is in this case treasurefind, name is maybe craftanolokao
     
  5. Offline

    Craftanolokao

    OK, Thanks, I'll start remaking the code and if you need help I will re-order here! and I'll make this code all in english.. srry for portuguese!
     
  6. Offline

    mcdorli

    The portoguese stuff wasn't wrong, we understand it enough to have a look at the code.
     
  7. Offline

    Craftanolokao

    The code is like this now:

    Code:
    package me.craft.teasure;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.EntityEffect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Chest;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Main extends JavaPlugin{
    
        @Override
        public void onEnable() {
           
        }
        @Override
        public void onDisable() {
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("findchest")){
                puchest(p.getLocation());
            }
            return false;
        }
        private ArrayList<ItemStack> normal = new ArrayList<>();
        private ArrayList<ItemStack> rare = new ArrayList<>();
        private ArrayList<ItemStack> luck = new ArrayList<>();
        private void puchest(Location loc){
           
            //normal
            normal.add(item(Material.IRON_SWORD));
            normal.add(item(Material.IRON_AXE));
            normal.add(item(Material.IRON_BOOTS));
            normal.add(item(Material.IRON_LEGGINGS));
            normal.add(item(Material.IRON_CHESTPLATE));
            normal.add(item(Material.IRON_HELMET));
            normal.add(item(Material.GOLDEN_APPLE));
            normal.add(item(Material.ANVIL));
            normal.add(item(Material.BLAZE_ROD));
            normal.add(item(Material.REDSTONE_BLOCK));
            normal.add(item(Material.APPLE));
            normal.add(item(Material.BOW));
            normal.add(item(Material.SUGAR_CANE));
            //rare
            rare.add(special(Material.IRON_SWORD, Enchantment.DAMAGE_ALL));
            rare.add(special(Material.IRON_CHESTPLATE, Enchantment.PROTECTION_ENVIRONMENTAL));
            rare.add(special(Material.IRON_LEGGINGS, Enchantment.PROTECTION_ENVIRONMENTAL));
            rare.add(special(Material.IRON_HELMET, Enchantment.PROTECTION_ENVIRONMENTAL));
            rare.add(special(Material.IRON_BOOTS, Enchantment.PROTECTION_ENVIRONMENTAL));
            rare.add(item(Material.DIAMOND_CHESTPLATE));
            rare.add(item(Material.DIAMOND_LEGGINGS));
            rare.add(item(Material.DIAMOND_HELMET));
            rare.add(item(Material.DIAMOND_BOOTS));
            //luckitem
            luck.add(luck(Material.DIAMOND_SWORD, Enchantment.DAMAGE_ALL));
            luck.add(luck(Material.DIAMOND_CHESTPLATE, Enchantment.PROTECTION_ENVIRONMENTAL));
            luck.add(luck(Material.DIAMOND_LEGGINGS, Enchantment.PROTECTION_ENVIRONMENTAL));
            luck.add(luck(Material.DIAMOND_HELMET, Enchantment.PROTECTION_ENVIRONMENTAL));
            luck.add(luck(Material.DIAMOND_BOOTS, Enchantment.PROTECTION_ENVIRONMENTAL));
            luck.add(new ItemStack(Material.GOLDEN_APPLE, 1, (short) 1));
           
           
           
           
           
            Random r = new Random();
            World world = loc.getWorld();
            double x = loc.getX() + r.nextInt(10000) - r.nextInt(10000);
            double z = loc.getZ() + r.nextInt(10000) - r.nextInt(10000);
            double y = world.getHighestBlockYAt((int)x,(int) z);
            Location location = new Location(world, x, y, z);
            location.getBlock().setType(Material.CHEST);
            Chest chest = (Chest) location.getBlock().getState();
            Inventory inv = chest.getInventory();
            for(int i = 0; i <= r.nextInt(10) + 5; i ++){
               
                inv.setItem(r.nextInt(9*3-1), normal.get(r.nextInt(normal.size())));
               
                if(i >= 5 & i <= 6 ){
                    inv.setItem(r.nextInt(9*3-1), rare.get(r.nextInt(rare.size())));
                }
                if(i == r.nextInt(8)){
                    inv.setItem(r.nextInt(9*3-1), luck.get(r.nextInt(luck.size())));
                }
               
               
            }
            new BukkitRunnable() {
               
                @Override
                public void run() {
                    if(location.getBlock().getType() == Material.AIR){
                        cancel();
                    }else{
                        location.getWorld().spawn(location, Firework.class);
                    }
                }
            }.runTaskTimer(this, 0, 20);
            Bukkit.broadcastMessage("§eThe treasure this in coordinates: §c(§b" + (int) x + ", " +(int) y +", " +(int) z + "§c)§f");
           
           
        }
       
        private ItemStack item(Material material){
            ItemStack item = new ItemStack(material);
            return item;
        }
        private ItemStack special(Material material, Enchantment enchantment){
            Random r = new Random();
            ItemStack item = new ItemStack(material);
            item.addUnsafeEnchantment(enchantment, r.nextInt(4) + 1);
            return item;
        }
        private ItemStack luck(Material material, Enchantment enchantment){
            Random r = new Random();
            ItemStack item = new ItemStack(material);
            item.addUnsafeEnchantment(enchantment, r.nextInt(4) + 1);
            item.addUnsafeEnchantment(Enchantment.DURABILITY, r.nextInt(4) + 1);
            return item;
        }
       
       
    }
    
    but I wonder how I will check if the chest is empty to stop the fireworks.
     
  8. Offline

    Zombie_Striker

    If it has nothing in it, remove it

    Please don't name your main classs "Main", and follow Java Naming conventions.
    This also does nothing. Remove it.

    DON'T BLIND CAST!!!!!!
    This should be at the top of the class.
    DONT CREATE A NEW INSTANCE OF RANDOM EACH TIME! Save this as a field in the class, not a variable in a method.
     
  9. Offline

    Craftanolokao

    Last edited by a moderator: Nov 24, 2015
Thread Status:
Not open for further replies.

Share This Page