How do I make code execute if a player is holding a custom item?

Discussion in 'Plugin Development' started by Lenger, Aug 6, 2020.

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

    Lenger

    My friend and I are trying to make a plugin that will allow blocks to be stacked with a special item called a Stacking Wand. We made a custom crafting recipe that seems to work, but right clicking while holding the item doesn't run the code we'd like it to run. Through our tests it seems as if our custom item has no metadata, even though we've set it. Here's our code:

    Code:
    public class BlockExperiments implements Listener {
        Plugin plugin = Main.getPlugin(Main.class);
        /*@EventHandler
        public void onPlace(BlockPlaceEvent event) {
            Block block = event.getBlock();
            Player player = event.getPlayer();
            Location blockLocation1 = block.getLocation().add(0, 1, 0);
            Location blockLocation2 = block.getLocation().add(0, 2, 0);
            Location blockLocation3 = block.getLocation().add(0, 3, 0);
            String playerWorld = player.getLocation().getWorld().toString();
          
            int addNumber = 0;
          
            while()
          
            if(block.getType().equals(Material.COBBLESTONE)) {
                player.sendMessage("Ran IF Statement");
                player.getWorld().getBlockAt(blockLocation1.getBlockX(), blockLocation1.getBlockY(), blockLocation1.getBlockZ()).setType(Material.COBBLESTONE);
                player.getWorld().getBlockAt(blockLocation2.getBlockX(), blockLocation2.getBlockY(), blockLocation2.getBlockZ()).setType(Material.COBBLESTONE);
                player.getWorld().getBlockAt(blockLocation3.getBlockX(), blockLocation3.getBlockY(), blockLocation3.getBlockZ()).setType(Material.COBBLESTONE);
            }
        }*/
      
        /*@EventHandler
        public void onPlace(BlockPlaceEvent event) {
            Player player = event.getPlayer();
            Block block = event.getBlock();
            placedBlock = block.getType();
            player.sendMessage("Set placedBlock equal to " + placedBlock);
        }
        */
        public ItemMeta publicStackerWandMeta;
      
        @SuppressWarnings("deprecation")
        public void wandRecipe() {
            ItemStack stackerWand = new ItemStack(Material.BLAZE_ROD, 1);
            ItemMeta stackerWandMeta = stackerWand.getItemMeta();
            stackerWandMeta.setDisplayName(ChatColor.YELLOW + "Stacker Wand");
            ArrayList<String> stackerWandLore = new ArrayList<String>();
            stackerWandLore.add("Magically stacks blocks.");
            stackerWandMeta.setLore(stackerWandLore);
            stackerWand.setItemMeta(stackerWandMeta);
            stackerWand.setItemMeta(stackerWandMeta);
            publicStackerWandMeta = stackerWandMeta;
    
          
            //----Stacker Wand Recipe----
            ShapedRecipe stackerWandRecipe = new ShapedRecipe(stackerWand);
            stackerWandRecipe.shape(" # ", "%&%", " % ");
            stackerWandRecipe.setIngredient('#', Material.NETHER_STAR);
            stackerWandRecipe.setIngredient('&', Material.BLAZE_ROD);
            stackerWandRecipe.setIngredient('%', Material.PISTON);
          
            plugin.getServer().addRecipe(stackerWandRecipe);
          
          
        }
      
        public void onCraft(CraftItemEvent event) {
          
        }
      
        public int wallHeight = 1;
    //Store the item meta in an accessible variable then listen to CraftItemEvent and set the meta that way
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action action = event.getAction();
            Block clickedBlock = event.getClickedBlock();
            Material placedBlock;
            Inventory playerInventory = player.getInventory();
            //player.sendMessage(player.getInventory().getItemInMainHand().getItemMeta().toString());
          
            ItemStack blazeRod = new ItemStack(Material.BLAZE_ROD);
          
      
            if(player.getInventory().getItemInMainHand().getItemMeta().equals(publicStackerWandMeta)) {
                player.sendMessage("Holding Stacker Wand");
                if(action.equals(Action.RIGHT_CLICK_BLOCK)) {
                    if(event.getHand() == EquipmentSlot.HAND) {
                        placedBlock = clickedBlock.getType();
                        ItemStack oakBlocks = new ItemStack(placedBlock);
                        player.sendMessage("placedBlock is equal to " + placedBlock);
                        player.sendMessage("Right clicked block");
                        if(clickedBlock.getType().equals(placedBlock)) {
                            player.sendMessage("Block is " + placedBlock);
                            if(playerInventory.contains(oakBlocks)) {
                            while(!clickedBlock.getLocation().add(0,wallHeight,0).getBlock().getType().equals(Material.AIR)) {
                                player.sendMessage("Looping");
                                player.sendMessage(ChatColor.RED + "Wall height was previously: " + Integer.toString(wallHeight) + ". Adding +1.");
                                wallHeight = wallHeight + 1;
                                player.sendMessage(ChatColor.RED + "Wall height is now: " + Integer.toString(wallHeight) + ".");
          
                            }
                                player.sendMessage("There is space above block");
                                player.getWorld().getBlockAt(clickedBlock.getLocation().add(0, wallHeight, 0).getBlockX(), clickedBlock.getLocation().add(0, wallHeight, 0).getBlockY(), clickedBlock.getLocation().add(0, wallHeight, 0).getBlockZ()).setType(placedBlock);
                                //player.sendMessage(ChatColor.RED + "Wall height was previously: " + Integer.toString(wallHeight) + ". Adding +1.");
                                //wallHeight = wallHeight + 1;
                                //player.sendMessage(ChatColor.RED + "Wall height is now: " + Integer.toString(wallHeight) + ".");
                                player.getInventory().removeItem(oakBlocks);
                                player.sendMessage("The wall height has been set to 1.");
                                wallHeight = 1;
                            }
                            else {
                                player.sendMessage(ChatColor.RED + "You're out of materials.");
                            }
                        }
                  
                        }
      
                        }
                }
                }
            }
    
    (I posted this on the Bukkit Help thread before realizing that it probably fits better here, so I'm sorry if that breaks any rules. I'd be happy to delete the duplicate.)
     
  2. Offline

    Strahan

    Do you ever call wandRecipe()? That aside, comparing two ItemMeta objects doesn't strike me as the best way to achieve the checking. Personally, I'd just use a tag on the item. What MC version?
     
  3. Offline

    The_Spaceman

    You can use PersistentDataContainer, This way you can give each wand a special function if that is maybe something you want to use but didnt know how.
    As far as tags go I don't know how to use it, and the last time i checked you need to use NMS (net.minecraft.server.<server version> classes), and with PersistentDataContainer you don't need to (just org.bukkit, so its version proof)
     
    Strahan likes this.
  4. Offline

    Strahan

    I keep forgetting PersistentDataContainer exists, lol. Yea, that'd be better than using NMS.
     
Thread Status:
Not open for further replies.

Share This Page