Solved (Solved)

Discussion in 'Spigot Plugin Development' started by SwenNoort, Apr 3, 2020.

Thread Status:
Not open for further replies.
  1. I'm trying to disable the chat with a command. Can anyone please help?

    package me.Zwarthemd.Minekingschatcommand;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class chatcommand implements Listener,
    CommandExecutor {

    public String chat = "chat";
    String prefix = "§8[§aMine§7Kings§8]§7 ";

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Player player = (Player) sender;
    if(player.isOp() && cmd.getName().equalsIgnoreCase(chat)){
    if(args[0] == "disable"){
    Disable(null);
    player.sendMessage("§7 De chat staat helaas uit!");
    }

    if(args[0] == "enable"){
    Enable(null);
    }
    }
    return false;
    }

    public void Disable(AsyncPlayerChatEvent e){
    e.setCancelled(true);
    }

    public void Enable(AsyncPlayerChatEvent e){
    e.setCancelled(false);
    }
    }


    Thats my code, Please don't say learn java first that won't help solve this problem
     
  2. learn java

    fr tho, you cant compare strings with ==. just like you used #equalsIgnoreCase for the command name, you have to use #equals or some version of it to compare strings.
    thats not how listeners work, look up the millions of listener tutorials on the spigot forums
     
  3. Oke thanks, but how do i actualy turn of chat?
     
  4. You look up your previously stored boolean value (if it's currently disabled) and cancel the event regarding that.
     
  5. Oke, sorry i'm really new to java and bukkit.. And i'm not to good at english.... you mean that i need to set it like this:
    if(args[0].equalsIgnoreCase("disable")){
    Disable(null);
    player.sendMessage("§7 De chat staat helaas uit!");
    return false;
    }

    right?
     
  6. 1. You need a variable, eg. "enabled":
    Code (Java):

    private boolean enabled = false; // we store the chat's current status in this
     
    2. You need a command, eg. "enable" and "disable"
    Code (Java):

    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
        if(command.getName().equalsIgnoreCase("enable")) { // we check if it's the 'enable' command
            if(sender.hasPermission("chat.enable")) { // we check if the player has the 'chat.enable' permission
                enabled = true; // we enable the chat
                sender.sendMessage("§aChat enabled."); // we notify the player
            } else sender.sendMessage("§cYou don't have permission!"); // the player doesn't have permission, we notify him/her
        } else if(command.getName().equalsIgnoreCase("disable")) { // it wasn't the 'enable' command, so we check if it's the 'disable' command
            if(sender.hasPermission("chat.disable")) { // we check if the player has the 'chat.disable' permission
                enabled = false; // we disable the chat
                sender.sendMessage("§cChat disabled."); // we notify the player
            } else sender.sendMessage("§cYou don't have permission!"); // the player doesn't have permission, we notify him/her
        }
    }
     
    3. You need to put your command in the plugin.yml (of course you have to edit this)
    Code (YAML):

    name
    : PluginName # your plugin's name
    author
    : You # your name
    version
    : 1.0 # the version of your plugin
    main
    : com.you.Main # the full path where your Main.class is (important: this is needed without the .class part)
    commands
    : # the commands
      enable
    : # the 'enable' command
      disable
    : # the 'disable' command
     
    4. You need an event
    Code (Java):

    @EventHandler
    public void onChat(AsyncPlayerChatEvent event) {
        Player player = event.getPlayer(); // we get the player from the event
        if(!player.hasPermission("chat.bypass") && !enabled) { // we check if the player has the 'chat.bypass' to bypass the limitation and if the chat is disabled
            event.setCancelled(true); // we block the message
            player.sendMessage("§cThe chat is disabled."); // we notify the player
        }
    }
     
    5. You need to register your events and commands:
    Code (Java):

    @Override
    public void onEnable() {
        this.getServer().getPluginManager.registerEvents(this, this); // we register the 'onChat' event
        this.getCommand("enable").setExecutor(this); // we register the 'enable' command
        this.getCommand("disable").setExecutor(this); // we register the 'disable' command
    }
     
    The final code:
    Main.class:
    Code (Java):

    public class Main extends JavaPlugin implements CommandExecutor, Listener {
     
        public boolean enabled = true;
     
        @Override
        public void onEnable() {
            this.getServer().getPluginManager.registerEvents(this, this);
            this.getCommand("enable").setExecutor(this);
            this.getCommand("disable").setExecutor(this);
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
            if(command.getName().equalsIgnoreCase("enable")) {
                if(sender.hasPermission("chat.enable")) {
                    enabled = true;
                    sender.sendMessage("§aChat enabled.");
                } else sender.sendMessage("§cYou don't have permission!");
            } else if(command.getName().equalsIgnoreCase("disable")) {
                if(sender.hasPermission("chat.enable")) {
                    enabled = false;
                    sender.sendMessage("§cChat disabled.");
                } else sender.sendMessage("§cYou don't have permission!");
            }
        }
     
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            if(!player.hasPermission("chat.bypass") && !enabled) {
                event.setCancelled(true);
                player.sendMessage("§cThe chat is disabled.");
            }
        }
    }
     
    plugin.yml:
    Code (YAML):

    name
    : PluginName
    author
    : You
    version
    : 1.0
    main
    : com.you.Main
    commands
    :
      enable
    :
      disable
    :
     
    #6 LifeGamer, Apr 3, 2020
    Last edited: Apr 3, 2020
    • Useful Useful x 2
  7. No.
    The "/disable" is not args[0], it's the command variable.
    Makes no sense
     
    • Agree Agree x 1
  8. I really suggest learning standalone java basics first. Bukkit adds an other layer of complexity and hard to understand things, so it's not a good start
     
    • Like Like x 1
  9. Don't think I have ever seen more spoonfeeding than this, you have written the whole plugin. This is not the right way to learn.
     
    • Like Like x 2
  10. Yeah, but I don't think that people can't learn from other people's code.
    I learnt like half of what I know from other people's code.
    If he's only going to copy and paste the code, then he won't learn any programming, but that's not my problem.
     
    • Like Like x 1
  11. drives_a_ford

    Moderator

    I disagree.

    By spoonfeeding him here, you've created a precedent that'll make him expect to get spoonfed everywhere.
    And then he'll copy a piece of code and create a new thread titled "why no work".
    So instead of learning, like he should, he'll be wasting everyone's time.
     
  12. Hey guys, I learn most from other people's code! i agree with LifeGamer... it just changed the code complete only thing i needed to know how to enable and disable! couldn't find any tutorial on that! So thanks LifeGamer for helping me out!!!
     
Thread Status:
Not open for further replies.