Solved Command Don't Work

Discussion in 'Spigot Plugin Development' started by Burakcraft02, Apr 24, 2017.

  1. Hello
    I Maked ChatCleaner Plugin But Commands Not Working
    Plugin Code:
    package com.org.dragonskillerx.chatcleaner;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.org.dragonskillerx.chatcleaner.commands.ClearChat;
    import com.org.dragonskillerx.chatcleaner.commands.ClearChatReload;

    public class Main extends JavaPlugin {

    PluginManager pm = this.getServer().getPluginManager();
    FileConfiguration config;
    File cfile;

    public void onEnable() {
    Bukkit.getLogger().info("[ChatCleaner] Thanks For Installing My First Plugin");
    getCommand("clearchat").setExecutor(new ClearChat());
    getCommand("clearchat reload").setExecutor(new ClearChatReload());
    config = getConfig();
    config.options().copyDefaults(true);
    saveConfig();
    cfile = new File(getDataFolder(), "config.yml");
    }
    public void onDisable() {
    Bukkit.getLogger().info("[ChatCleaner] Thanks For Installing My First Plugin");

    }
    }
    package com.org.dragonskillerx.chatcleaner.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import com.org.dragonskillerx.chatcleaner.Main;

    public class ClearChat implements CommandExecutor {

    private Main main;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if (commandLabel.equalsIgnoreCase("chatcleaner")) {
    Player p = (Player) sender;
    if (!p.hasPermission("chatcleaner.clean")) {
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("no-permission")));
    return true;
    }else{
    for(int i=0;i<200;i++) {
    Bukkit.broadcastMessage("");
    }
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("clear-message").replaceAll("%player%", p.getName())));
    }
    }
    return true;
    }

    }
    package com.org.dragonskillerx.chatcleaner.commands;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import com.org.dragonskillerx.chatcleaner.Main;


    public class ClearChatReload implements CommandExecutor{

    private Main main;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if (commandLabel.equalsIgnoreCase("chatcleaner reload")) {
    Player p = (Player) sender;
    if (!p.hasPermission("chatcleaner.reload")) {
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("no-permission")));
    return true;
    } else {
    this.main.reloadConfig();
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("reload-message")));
    }
    }
    return true;
    }

    }
    send me fixed code My English Bad Sorry :(
     
  2. 2 different things

    EDIT: registering chatcleanreload should be done without any spaces. Or you should check if args lenght is 1 and if args[0] is reload. Sorry I'm on my phone

    EDIT 2: PLEASE don't make another chatclear plugin. There are 1000 of them
     
    • Like Like x 1
  3. [11:27:21 WARN]: Unexpected exception while parsing console command "clearchat"
    org.bukkit.command.CommandException: Unhandled exception executing command 'clea
    rchat' in plugin ChatCleaner v1.0.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
    r.java:641) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(Craf
    tServer.java:627) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:
    412) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    75) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    54) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :557) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at com.org.dragonskillerx.chatcleaner.commands.ClearChat.onCommand(Clear
    Chat.java:19) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    ... 8 more
     
  4. You're instantly trying to cast a player:
    Try checking if the sender is actually a player before casting it:

    if (sender instanceof Player) {

    //your code
    }
     
    • Like Like x 1
  5. Ok but Making The Example Plugin Don't Share Spigot
     
  6. Good, good luck with it :)
     
    • Like Like x 1
  7. Don't Fixed Error:
    [11:35:12 WARN]: Unexpected exception while parsing console command "clearchat"
    org.bukkit.command.CommandException: Unhandled exception executing command 'clea
    rchat' in plugin ChatCleaner v1.0.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
    r.java:641) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(Craf
    tServer.java:627) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:
    412) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    75) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    54) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :557) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at com.org.dragonskillerx.chatcleaner.commands.ClearChat.onCommand(Clear
    Chat.java:22) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    ... 8 more
     
  8. What is the code?
     
    • Like Like x 1
  9. package com.org.dragonskillerx.chatcleaner;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.org.dragonskillerx.chatcleaner.commands.ClearChat;
    import com.org.dragonskillerx.chatcleaner.commands.ClearChatReload;

    public class Main extends JavaPlugin {

    PluginManager pm = this.getServer().getPluginManager();
    FileConfiguration config;
    File cfile;

    public void onEnable() {
    Bukkit.getLogger().info("[ChatCleaner] Thanks For Installing My First Plugin");
    getCommand("clearchat").setExecutor(new ClearChat());
    getCommand("clearchatreload").setExecutor(new ClearChatReload());
    config = getConfig();
    config.options().copyDefaults(true);
    saveConfig();
    cfile = new File(getDataFolder(), "config.yml");
    }
    public void onDisable() {
    Bukkit.getLogger().info("[ChatCleaner] Thanks For Installing My First Plugin");

    }
    }

    package com.org.dragonskillerx.chatcleaner.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import com.org.dragonskillerx.chatcleaner.Main;

    public class ClearChat implements CommandExecutor {

    private Main main;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if (commandLabel.equalsIgnoreCase("clearchat")) {
    if (sender instanceof Player) {
    sender.sendMessage("Command Only Players.");
    }
    Player p = (Player) sender;
    if (!p.hasPermission("clearchat.clean")) {
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("no-permission")));
    return true;
    }else{
    for(int i=0;i<200;i++) {
    Bukkit.broadcastMessage("");
    }
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("clear-message").replaceAll("%player%", p.getName())));
    }
    }
    return true;
    }

    }
    package com.org.dragonskillerx.chatcleaner.commands;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import com.org.dragonskillerx.chatcleaner.Main;


    public class ClearChatReload implements CommandExecutor{

    private Main main;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if (commandLabel.equalsIgnoreCase("clearchatreload")) {
    if (sender instanceof Player) {
    sender.sendMessage("Command Only Players.");
    }
    Player p = (Player) sender;
    if (!p.hasPermission("clearchat.reload")) {
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("no-permission")));
    return true;
    } else {
    this.main.reloadConfig();
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.main.getConfig().getString("reload-message")));
    }
    }
    return true;
    }

    }
     
  10. You might want to do

    Code (Text):
    if (!(sender instanceof Player)) {
    sender.sendMessage("Command Only Players.");
    return true;
    }
    Instead of

    Code (Text):
    if (sender instanceof Player) {
    sender.sendMessage("Command Only Players.");
    }
     
    • Like Like x 1
  11. [11:48:49 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'clea
    rchat' in plugin ChatCleaner v1.0.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
    r.java:641) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:1162) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :997) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51
    1) [?:1.8.0_101]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_101
    ]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8
    .8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    15) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    54) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :557) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
    at com.org.dragonskillerx.chatcleaner.commands.ClearChat.onCommand(Clear
    Chat.java:31) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more
    >
     
  12. com.org.dragonskillerx.chatcleaner.commands.ClearChat.onCommand(Clear
    Chat.java:31) ~[?:?]

    Line 31, what is that

    +

    I suggest you learning java/spigot API first
     
    • Like Like x 1
  13. I will try a different code related to the main link
     
    • Like Like x 1
  14. Fixed Problem
    Private Main main; Changed To Main main;
    and added code ClearChat and ClearChat Reload

    public ClearChat(Main instance) {
    main = instance;
    }

    public ClearChatReload(Main instance) {
    main = instance;
    }
    Main Class Changed
    getCommand("clearchat").setExecutor(new ClearChat(this));
    getCommand("clearchatreload").setExecutor(new ClearChatReload(this));
    Thanks Kyllian :)
     
    • Friendly Friendly x 1