Solved Can't stop deopped player from breaking blocks.

Discussion in 'Plugin Development' started by jumbledProgram, Aug 23, 2020.

Thread Status:
Not open for further replies.
  1. Hello everyone! For some reason this code isn't working, and i have absolutely no clue why! im trying to make lime concrete unbreakable and display the message : "[] You can't destroy the virus!" to anyone who tries to break it. im using spigot 1.16.2.
    package me.cole.virus;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Virus extends JavaPlugin {

    . //make lime concrete (virus) unbreakable
    . @EventHandler
    . public void cancelVirusBreak(BlockBreakEvent e) {
    . Player player = e.getPlayer();
    . Material Block = e.getBlock().getType();
    . if(Block == Material.LIME_CONCRETE) {
    . player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 3.0F, 0.5F);
    . player.sendMessage(ChatColor.BOLD + "" + ChatColor.GRAY + "[" + ChatColor.YELLOW + "♠" + ChatColor.GRAY + "]" +
    . ChatColor.RESET + "" + ChatColor.GREEN + " You can't destroy the virus!");
    .
    . e.setCancelled(true);
    . }
    . }
    }

    Edit: added invisible "."s to make it easier to read.
    Edit 2 : adding .s didnt work

    EDIT: Solved
     
    Last edited: Aug 23, 2020
  2. Offline

    johnny boy

    Did you register your listener ?
     
  3. Haha im very new to java, how would i go about doing that?
    EDIT : I solved it! thanks for helping!
     
    Last edited: Aug 23, 2020
  4. Offline

    johnny boy

    Well first of all make sure your Event class (which is Virus) implements Listener so you can start listening to events. Second of all, it's best to register events in your onEnable() method in your main class like so:
    Code:Java
    1.  
    2. public void onEnable() {
    3.  
    4. Bukkit.getPluginManager().registerEvents(new EventHandlerClass(), this);
    5. }
    6.  

    Since in this case your event handler class and class that contains your onEnable() (your main class) are in the same class you should replace new EventClass() with the keyword this.
     
    jumbledProgram likes this.
  5. Offline

    KarimAKL

    Take a look here: https://bukkit.org/help/bb-codes
    Also, you can use [Syntax=Java]Code here[/Syntax] to highlight the syntax with Java.
     
    jumbledProgram likes this.
Thread Status:
Not open for further replies.

Share This Page