SOLVED > Bone Meal Help

Discussion in 'Spigot Plugin Development' started by VinexAx789, Jul 1, 2015.

  1. I'm trying to cancel the event when they try to bone meal a certain block.

    Code:
    Code (Java):
    package me.Vinex.CovertFix;

    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

        public void onEnable() {
            System.out.println("CovertFix has been enabled!");
        }

        public void onDisable() {
            System.out.println("CovertFix has been Disabled!");
        }

        @SuppressWarnings("deprecation")
        public void onBoneMealCovert(PlayerInteractEvent e) {
            ItemStack bonemeal = new ItemStack(Material.INK_SACK, 1, (short) 15);
            Player p = e.getPlayer();
            Block b = p.getTargetBlock(null, 5);
            if (e.getAction() != Action.RIGHT_CLICK_BLOCK) {
                if (p.getInventory().getItemInHand() == bonemeal) {
                    //I'm trying to deny the player from bonemealing these blocks
                    //because they can grow the rarest blocks in the game.
                    //Mushroom Blocks: 39 = Brown Mushroom & 40 = Red Mushroom
                    if (b.getTypeId() == 39 || b.getTypeId() == 40) {
                        p.sendMessage("§c§lCheat again, I dare you! Name Logged.");
                        e.setCancelled(true);
                    }
                }
                return;

            }
        }
    }
     
  2. Code (Text):
    if (e.getAction() != Action.RIGHT_CLICK_BLOCK) {
    I think you're looking for an equality sign here. (== instead of !=)
    Code (Text):
    if (p.getInventory().getItemInHand() == bonemeal)
    And here I think you want to use the ItemStack::isSimilar method. Like the following:
    Code (Text):
    if (p.getInventory().getItemInHand().isSimilar(bonemeal))
    Also make sure your listener is registered somewhere.

    Because there's no debugger when using Bukkit, when I hit a roadblock, I add debug output at every scope with the value of the variables in play. It takes a bit of practice because you need to make sure you get relevant information and not just spam the console. For your example this is what I would've used for debugging instead of code review:

    Code (Text):
    public void onBoneMealCovert(PlayerInteractEvent e) {
        ItemStack bonemeal = new ItemStack(Material.INK_SACK, 1, (short) 15);
        Player p = e.getPlayer();
        Block b = p.getTargetBlock(null, 5);
        System.out.println("Checking bonemeal for " + p.getName() + " with " + e.getAction().toString());
        if (e.getAction() != Action.RIGHT_CLICK_BLOCK) {
            System.out.println("Player right-clicked with in hand " + p.getInventory.getItemInHand().toString());
            if (p.getInventory().getItemInHand() == bonemeal) {
                System.out.println("Target block id : " + b.getTypeId());
                if (b.getTypeId() == 39 || b.getTypeId() == 40) {
                    p.sendMessage("§c§lCheat again, I dare you! Name Logged.");
                    e.setCancelled(true);
                }
            }
        }
    }
    Hope that helps.
     
    • Like Like x 1
  3. Thanks
     
  4.  
    #4 VinexAx789, Jul 1, 2015
    Last edited: Jul 1, 2015
  5. I think that you have to use something like that:

    Code (Text):
    if (player.getItemInHand().getType() == Material.BONE_MEAL) {
         // do stuff...
    }
     
    • Winner Winner x 1
  6. xD There is no material Bone Meal silly.
     
  7. Solved
     
    • Winner Winner x 1