pluginsminecraftbukkitspigot

How to reference an ItemStack Block in Bukkit Events?


I am currently making a plugin for Minecraft 1.20.6, and I'm trying to setup where when you right click a certain block (AKA My Chest ItemStack but placed) it pops up with my Custom Inventory. But I'm running into an issue where I cannot figure out how to make it so when you click on my Chest ItemStack and that only (without right clicking actual chests)

@EventHandler
public static void onChestClick(PlayerInteractEvent e) { 
  Player player = e.getPlayer();
  Block block e.getClickedBlock();
  if (block != null) {
    if (/* what goes here? */) {
      player.sendMessage( "Shop Chest Clicked");
    }
  }
}

How can I do it?


Solution

  • Firstly, instead of making block != null, you can use e.hasBlock().

    Also, if it's blocks previously placed, you can save the location into a config file then check if the clicked location is in this file.

    Here is code to check in config:

    // this method convert location to string
    public static String stringifyLocation(Location loc) {
        return loc.getWorld().getName() + "_" + loc.getBlockX() + "_" + loc.getBlockY() + "_" + loc.getBlockZ();
    }
    
    // this method check if the chest is one of us
    public boolean isMyChest(Location loc) {
        return plugin.getConfig().getStringList("shop.blocks_chest").contains(stringifyLocation(
    }
    
    @EventHandler
    public void onPlace(BlockPlaceEvent e) {
        // here make all checks about your chest
        if(e.getItemInHand().isSimilar(createChest())) {
            List<String> list = plugin.getConfig().getStringList("shop.blocks_chest"); // list of your chests
            list.add(stringifyLocation(e.getBlock().getLocation()));
            plugin.getConfig().set("shop.blocks_chest", list); // change the value
            plugin.saveConfig(); // save in the config.yml
        }
    }
    

    Don't forget to convert this code for you according to your own method already implemented.