AntiBreak Plugin Code Help

Discussion in 'Spigot Plugin Help' started by humonga, Jul 30, 2018.

  1. Okay so, I am making this plugin that makes a player not be able to destroy a block ( I know I should use worldguard, but my friend tells me to code one), but I am new developer, and I am stuck to what I should do next.
    I want the code to be like "If a player breaks a block, the block will not be destroyed, and will tell a message to the player saying that they can't break the block"

    I have the message for the player, but I can't seem to get the first part.

    All help is appreciated!

    Code:

    Code (Text):
    package me.jklmao.CustomJoin.EventsClass;

    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;



    public class EventsClass implements Listener {
       
        public String prefix = (ChatColor.RED + "[" + ChatColor.RED + "Pvp" + ChatColor.RED + "Park" + ChatColor.RED + "]");

        @EventHandler
        public void onPlayerBreakBlock(BlockBreakEvent event) {
            Player player = event.getPlayer();
            Block block = event.getBlock();
            player.sendMessage(prefix + ChatColor.RED + "Hey!" + ChatColor.GRAY + "You can not break that!");

             }
    }
       
    This is my EventsClass class code. I will provide any other code if anyone needs it.
    P.S, I am using Eclipse.
     
  2. The BlockBreakEvent object in the constructor of your method is the way to set and get information in the event. i.e. you use the event object to set the event to being cancelled.
     
  3. What do you mean?
     
  4. Use event#setCancelled(boolean)
     
  5. Okay that works, but when I try to add my message, I will destroy a block, and it will send 2 messages
     
  6. It might trigger once for each hand on later versions. Usually a check like this:
    Code (Java):
    if (event.getHand() == EquipmentSlot.OFF_HAND) return;
    should fix it up.
     
    • Like Like x 1
  7. And where do i put it? I'm using the 1.8.8 spigot build, so later versions?
     
  8. 1.8 doesn't have multiple hands, so it wouldn't help then.