Solved Clicking Block Problems

Discussion in 'Plugin Development' started by webbhead, Jan 6, 2015.

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

    webbhead

    Well, I have started coding a crates plugin and I am wondering how can I get the Block name of the .getClickedBlock because I am trying to make it so only if it is a certain chest with a certain name the crate will work. Here is the event code to where I will need it:
    Code:
        @EventHandler
        public void onInteractCrate(PlayerInteractEvent e) {
            ItemStack crate = new ItemStack(Material.CHEST);
            ItemMeta cratemeta = crate.getItemMeta();
            cratemeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Crate");
            cratemeta.setLore(Arrays.asList(ChatColor.DARK_PURPLE + "You need a key to open a crate."));
            crate.setItemMeta(cratemeta);
            //
            ItemStack key = new ItemStack(Material.TRIPWIRE_HOOK);
            ItemMeta keymeta = key.getItemMeta();
            keymeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Key");
            key.setItemMeta(keymeta);
            //
            Player player = (Player) e.getPlayer();
            ItemStack hand = player.getItemInHand();
            e.setCancelled(true);
            if (e.getClickedBlock().getType() == Material.CHEST && e.getClickedBlock() //Get ItemMeta name here? How do I do this!) {
              
            }
        }
     
  2. Offline

    xMakerx

    I don't think blocks take the item name when placed. What I would do is save an ArrayList of locations for your crates, or create a Crate class and save those. Check if there's a crate at a location with a method and go from there.
     
  3. Offline

    webbhead

    What I want is I know the general area of where the crate will be there will only be one chest at spawn should I get the location of the block and make config for them to set the location of the block and check if it is in the location and if so *code code code*? Idk if this will work but I will try it.
     
  4. Offline

    xMakerx

    Yeah, you pretty much repeated what I said. Get the location of the block, save it, and load it at runtime. The code below is just a general idea. I apologize if the code is kinda sloppy, I'm sleep-deprived atm :/

    Code:
    Location myCrate;
    
    public void loadCrate() {
       // Load the crate
    }
    
    public Location getCrate() {
      return this.myCrate;
    }
    
    ---------------------------------------
    Event class
    ---------------------------------------
    if(evt.getBlock().getLocation().equals(crates.getCrate())) {
    }
     
  5. Offline

    webbhead

    Thanks!

    I'm not too advanced in Java as I recently started and I am still learning so I did it a different way like this:
    Code:
    COMMAND:
            if (cmd.getName().equalsIgnoreCase("setcrateloc")) {
                if (!player.hasPermission("crates.setloc")) {
                    player.sendMessage(ChatColor.RED + "Error: You do not have permission to set the location of the crate.");
                    return true;
                }
                getConfig().set("crateloc.world", player.getLocation().getWorld().getName());
                getConfig().set("crateloc.x", player.getLocation().getX());
                getConfig().set("crateloc.y", player.getLocation().getY());
                getConfig().set("crateloc.z", player.getLocation().getZ());
                saveConfig();
                player.sendMessage(ChatColor.GREEN + "The location of the crate has been set where you are standing.\n Place a chest in your exact spot for players to use.");
            }
    Code:
    EVENT:
        @EventHandler
        public void onInteractCrate(PlayerInteractEvent e) {
            ItemStack key = new ItemStack(Material.TRIPWIRE_HOOK);
            ItemMeta keymeta = key.getItemMeta();
            keymeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Key");
            key.setItemMeta(keymeta);
            //
            World world = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
            double x = getConfig().getDouble("spawn.x");
            double y = getConfig().getDouble("spawn.y");
            double z = getConfig().getDouble("spawn.z");
            //
            Player player = (Player) e.getPlayer();
            ItemStack hand = player.getItemInHand();
            if (e.getClickedBlock().getType() == Material.CHEST && e.getClickedBlock().getLocation() == new Location(world,x,y,z)) {
                if (hand != null && hand.getType() == Material.TRIPWIRE_HOOK && hand.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Key")) {
                    player.sendMessage(ChatColor.RED + "TEST");
                    player.getInventory().getItemInHand().setType(Material.AIR);
                }
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page