getClickedBlock (Doesn't work) / Random Help

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

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

    webbhead

    My issue is the getClickedBlock is not working. I made it so when you click the block that is in a certain location with a certain item it should send a message. (The message is a test I want it to get a Random int from the config (item id)). Now on to the random thing well... I wanted to know how I can randomly get the integer from the configuration file with adding enchantments to it such as:
    items:
    - 120 fireaspect:1
    - 99 infinity:1
    OR: How would I code the id and enchants straight into the plugin to get the same results but just not configurable, but still be able to use the random class to get it?

    Here is my code:
    Code:
    public class Crates extends JavaPlugin implements Listener {
       
        @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);
            //
    
            Player player = (Player) e.getPlayer();
            ItemStack hand = player.getItemInHand();
            if (e.getClickedBlock().equals(Material.CHEST) && hand.getType().equals(Material.TRIPWIRE_HOOK) && hand.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Key")) {
                if (e.getClickedBlock().getLocation().equals(getConfig().getConfigurationSection("crateloc"))) {
                    // Get Random Item WITH enchant from config or code them right in how would I do this?
                    player.sendMessage("This is a test to see if block clicking works.");
                }
            }
        }
     
  2. Offline

    drpk

  3. Offline

    webbhead

    Still doesn't work the message won't send it just opens the chest and nothing happens.
     
  4. Offline

    drpk

  5. Offline

    InfamousSheep

    Registering events in onEnable?
     
  6. Offline

    webbhead

    Updated Code:
    Code:
    public class Crates extends JavaPlugin implements Listener {
       
        @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);
            //
    
            Player player = (Player) e.getPlayer();
            ItemStack hand = player.getItemInHand();
            if (e.getClickedBlock() != null && hand != null) {
            if (e.getClickedBlock().equals(Material.CHEST) && hand.getType().equals(Material.TRIPWIRE_HOOK) && hand.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Key")) {
                if (e.getClickedBlock().getLocation().equals(getConfig().getConfigurationSection("crateloc"))) {
                    // Get Random Item WITH enchant from config or code them right in how would I do this?
                    player.sendMessage("This is a test to see if block clicking works.");
                    }
                }
            }
        }
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    Yes I registered them.
     
  7. Offline

    mythbusterma

    @webbhead

    .....a ConfigurationSection cannot and will never equal a Location. Try again.
     
    1Rogue likes this.
  8. Offline

    1Rogue

    Locations are not ConfigurationSerializable. You would need to either make a ConfigurationSerializable version for yourself, or you would need to save the vector and then convert it to a location using Vector#toLocation(World)
     
  9. Offline

    567legodude

    @webbhead I think that one thing you should check for is what action was performed.
    Such as Action.RIGHT_CLICK_BLOCK
    then you can continue to do your e.getClickedBlock()
     
  10. Offline

    webbhead

  11. Offline

    mythbusterma

    @webbhead

    Blithely ignore the three posts above you telling you how to fix your code, and say "bump," what in the world do you expect us to help you with if you can't even be bother to read/respond to our posts?
     
  12. Offline

    webbhead

    This did not work.
    This also did not seem to work.
    I have tried them and the Serializable Location I did not understand so I tried this:
    Code:
        World world = Bukkit.getServer().getWorld(getConfig().getString("crateloc.world"));
            double x = getConfig().getDouble("crateloc.x");
            double y = getConfig().getDouble("crateloc.y");
            double z = getConfig().getDouble("crateloc.z");
            //
            Player player = (Player) e.getPlayer();
            ItemStack hand = player.getItemInHand();
            Action a = e.getAction();
            if (e.getClickedBlock() != null && hand != null) {
                if (a == Action.RIGHT_CLICK_BLOCK) {
            if (e.getClickedBlock().equals(Material.CHEST) && hand.getType().equals(Material.TRIPWIRE_HOOK) && hand.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Key")) {
                if (e.getClickedBlock().getLocation().equals((new Location(world, x, y, z)))) {
                    // Get Random Item WITH enchant from config or code them right in how would I do this?
                    player.sendMessage("This is a test to see if block clicking works.");
                        }
                    }
                }
            }
        }
     
  13. Offline

    567legodude

    @webbhead I can tell you that what I posted does in fact work, if you actually know how to use it.
     
  14. Offline

    webbhead

    Well did I do it correctly the code is right above your post.
     
  15. Offline

    567legodude

    @webbhead You have to check if they clicked a block before you can use getClickedBlock()
     
Thread Status:
Not open for further replies.

Share This Page