Code not working

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Jun 13, 2018 at 12:04 PM.

  1. Both of these classes return the usage and dont work, i dont see anything wrong?
    Code (Text):
    package me.thesilentpro.net.commands;

    import java.util.ArrayList;

    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 org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;

    import me.thesilentpro.net.Main;

    public class Overlord implements CommandExecutor {
     
        ArrayList<String> overlord = new ArrayList<String>();

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (Main.plugin.getConfig().getStringList("overlords").contains(p.getName())) {
                        if (overlord.isEmpty()) {
                            overlord.add("yes");
                            Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Enabling overlord mode!");
                            for (Player online : Bukkit.getServer().getOnlinePlayers()) {
                                if (!(Main.plugin.getConfig().getStringList("overlords").contains(online.getName()))) {
                                    online.kickPlayer(ChatColor.DARK_RED +  Main.plugin.getConfig().getString("overlordmsg"));
                                }
                            }
                        }else {
                            overlord.remove("yes");
                            Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Disabling overlord mode!");
                        }
                    }
                }
            return true;
        }
     
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            if (overlord.contains("yes")) {
                if (!(Main.plugin.getConfig().getStringList("overlords").contains(e.getPlayer().getName()))) {
                    e.getPlayer().kickPlayer(ChatColor.DARK_RED + Main.plugin.getConfig().getString("overlordmsg"));
                    Bukkit.getServer().broadcastMessage(ChatColor.RED + e.getPlayer().getName() + " tried to join while overlord mode is on!");
                }
            }
        }

    }
     
    Code (Text):
    package me.thesilentpro.net.commands;

    import java.util.List;

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

    public class Entitywipe implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (p.hasPermission("sp.entitywipe")) {
                World world = p.getWorld();
                List<Entity> entList = world.getEntities();
                for(Entity current : entList) {
                    current.remove();
                    }
                }
                Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Removing all entities in " + p.getWorld().getName());
                }
            return true;
       
        }
    }
     
    Also this class works but it only shows the first word?
    Code (Text):
    package me.thesilentpro.net.commands;

    import static org.bukkit.ChatColor.BOLD;
    import static org.bukkit.ChatColor.GRAY;
    import static org.bukkit.ChatColor.RED;

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

    public class Alert implements CommandExecutor {
     
        String prefix = GRAY + "[" + RED + BOLD + "ALERT" + GRAY + "] " + RED;
     
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
                if (sender.hasPermission("admin.alert")) {
                    if (args.length == 0) {
                        sender.sendMessage(prefix  + ChatColor.GRAY + "/alert <message>");
                        return true;
                    }
                    if (args.length >= 1) {
                        Bukkit.getServer().broadcastMessage(prefix + args);
                    }
                }else {
                    sender.sendMessage(ChatColor.RED + "No permission!");
                }
        return true;
    }
     
    }
     
     
    #1 UnlimitedNinjas, Jun 13, 2018 at 12:04 PM
    Last edited: Jun 14, 2018 at 8:27 AM
  2. To start off, you need a Main class, that should declare your commands using
    getCommand("yml command name").setExecutor(new CommandClass());
    You then need to create a .yml file inside your plugin, to create the commands, naming it with the same string you used above.
    There are plenty of tutorials to help you create commands ^^
    If it still doesn't work, then see if there is an error message in your console, and if so send it to us!

    For the problem with your other class, I have no clue what could go wrong, explain your problem ^^
     
  3. The usage is shown when the onCommand method returns false. Since yours don't return false I am left to believe the default method in JavaPlugin is used (which does nothing except return false), meaning you never registered your commands.
     
  4. Both commands are registered via commandexecutor and they are in plugin.yml
     
  5. Show us.

    There's literally no other way for the usage to be shown on command unless the method returns false.
     
  6. Code (Text):
    package me.thesilentpro.net;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerKickEvent;
    import org.bukkit.event.player.PlayerPortalEvent;
    import org.bukkit.event.player.PlayerTeleportEvent;
    import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.thesilentpro.net.commands.Address;
    import me.thesilentpro.net.commands.Alert;
    import me.thesilentpro.net.commands.Doom;
    import me.thesilentpro.net.commands.Entitywipe;
    import me.thesilentpro.net.commands.Find;
    import me.thesilentpro.net.commands.Overlord;
    import me.thesilentpro.net.commands.Skeppy;
    import me.thesilentpro.net.commands.Smite;
    import me.thesilentpro.net.commands.UUID;
    import me.thesilentpro.net.commands.Wakeup;

    public class Main extends JavaPlugin implements Listener, CommandExecutor {
       
        public static Main plugin;
       
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            plugin = this;
            getCommand("f").setExecutor(new Skeppy());
            getCommand("wakeup").setExecutor(new Wakeup());
            getCommand("uuid").setExecutor(new UUID());
            getCommand("ip").setExecutor(new Address());
            getCommand("find").setExecutor(new Find());
            getCommand("doom").setExecutor(new Doom());
            getCommand("smite").setExecutor(new Smite());
            getCommand("alert").setExecutor(new Alert());
            getCommand("entitywipe").setExecutor(new Entitywipe());
            getCommand("overlord").setExecutor(new Overlord());
        }
       
        public void onDisable() {
            saveDefaultConfig();
        }
     
  7. Ok. Let's test something. Add onCommand method to your main class, and the only thing in it should be a response to the sender. Like:

    Code (Text):
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        sender.sendMessage("boner");
        return true;
    }
    This method will be used for any unregistered (or misregistered) commands.
     
  8. added it and now it with the sender message "unregistered"
    now when i do any of the commands it says unregistered
     
  9. heres the plugin.yml
    Code (Text):
    name: SuperBlocks
    main: me.thesilentpro.net.Main
    version: 3.0
    commands:
      f:
        usage: /<command>
        description: f
      wakeup:
        usage: /<wakeup> <player>
        description: Wake someone up...
      uuid:
        usage: /<command> <player>
        description: Get the uuid of a player.
        aliases: [finduuid, uuidfind]
      ip:
        usage: /<command> <player>
        description: Get the ip of a player.
        aliases: [address, ipaddress]
      find:
        usage: /<command> <player>
        description: Find in what player someone is.
        aliases: [whereis, findplayer]
      doom:
        usage: /<command> <player>
        description: Used to ban admins.
        aliases: [adminban, ab]
      smite:
        usage: /<command> <player> <reason>
        description: Smite a player.
        aliases: [s]
      entitywipe:
        usage: /<command>
        description: Wipe entities in the world you are in.
        aliases: [ew]
      overlord:
        usage: /<command>
        description: Enable/Disable overlord mode.
        aliases: [ovmode, ov]
    permissions:
      sp.notify:
        default: op
      sp.f:
        default: op
      sp.wakeup:
        default: op
      sp.uuid:
        default: op
      sp.address:
        default: op
      sp.find:
        default: op
     
  10. Remove the usages, i don't have made a good experience with it, too.
     
  11. Did you remove your setExecutors? Don't. Those should be overriding the onCommand in the main class..

    I also don't see alert in your plugin.yml
     
  12. Hello,

    I'll help you with your codes!

    1st class:
    Code (Java):

    public class Overlord implements CommandExecutor, Listener  {
     
      boolean overlord = false;
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (Main.plugin.getConfig().getStringList("overlords").contains(p.getName())) {
                        if (overlord == false) {
                            overlord = true;
                            Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Enabling overlord mode!");
                            for (Player online : Bukkit.getServer().getOnlinePlayers()) {
                                if (!(Main.plugin.getConfig().getStringList("overlords").contains(online.getName()))) {
                                    online.kickPlayer(ChatColor.DARK_RED +  Main.plugin.getConfig().getString("overlordmsg"));
                                }
                            }
                        }else {
                            overlord = false;
                            Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Disabling overlord mode!");
                        }
                    }
                }
            return true;
        }
     
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            if (overlord == true) {
                if (!(Main.plugin.getConfig().getStringList("overlords").contains(e.getPlayer().getName()))) {
                    e.getPlayer().kickPlayer(ChatColor.DARK_RED + Main.plugin.getConfig().getString("overlordmsg"));
                    Bukkit.getServer().broadcastMessage(ChatColor.RED + e.getPlayer().getName() + " tried to join while overlord mode is on!");
                }
            }
        }

    }

    2nd:

    Code (Java):
    public class Entitywipe implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (p.hasPermission("sp.entitywipe")) {
                World world = p.getWorld();
               
                for (Entity liv : world.getEntities()) {
                    if (!(liv instanceof Player)) {
                        liv.remove();
                    }
                }

                }
                Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Removing all entities in " + p.getWorld().getName());
                }
            return true;
         
        }
    }
    3rd:
    Code (Java):
    public class Alert implements CommandExecutor {
     
        String prefix = GRAY + "[" + RED + BOLD + "ALERT" + GRAY + "] " + RED;
     
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
                if (sender.hasPermission("admin.alert")) {
                    if (args.length == 0) {
                        sender.sendMessage(prefix  + ChatColor.GRAY + "/alert <message>");
                        return true;
                    }
                    if (args.length >= 1) {

             
    String text = "";
              String arg;
              for (int i = 0; i < args.length; i++)
              {
                arg = args[i] + " ";
                text = text + arg;
              }
             
                        Bukkit.getServer().broadcastMessage(prefix + text);

             

     
                    }
                }else {
                    sender.sendMessage(ChatColor.RED + "No permission!");
                }  
        return true;
    }
     
    }
    4th:

    Code (Java):
    public void onEnable(){

          getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);

            plugin = this;
            getCommand("f").setExecutor(new Skeppy());
            getCommand("wakeup").setExecutor(new Wakeup());
            getCommand("uuid").setExecutor(new UUID());
            getCommand("ip").setExecutor(new Address());
            getCommand("find").setExecutor(new Find());
            getCommand("doom").setExecutor(new Doom());
            getCommand("smite").setExecutor(new Smite());
            getCommand("alert").setExecutor(new Alert());
            getCommand("entitywipe").setExecutor(new Entitywipe());
            getCommand("overlord").setExecutor(new Overlord());

    Bukkit.getServer().getPluginManager().registerEvents(new Overlord(), this);

        }
     
        public void onDisable() {
            saveDefaultConfig();
        }

    }

    I hope I guide you
    I wish you a wonderful time
     
  13. Thank you but same errors...?
     
  14. Also for every other command that i do that is just <command> it does this.
    I event tried to use /overlord insted of /<command>
     
  15. What is the error?
     
  16. no error is displayed in console
     

Share This Page