Solved Unable to find (command argument)

Discussion in 'Spigot Plugin Development' started by Wilsoon, Feb 21, 2020.

Thread Status:
Not open for further replies.
  1. Problem:
    I don't know if this is my code problem but whenever I do /spleef, it works fine:
    upload_2020-2-21_20-27-7.png
    /spleef
    /spleef (Player)
    /spleef reload
    /spleef reset
    /spleef accept
    /spleef deny
    are all the commands I have coded which have outputs.
    However, whenever I do a player's name, no message "Unable to find ______" shows. /spleef accept and /spleef deny also works fine. However, for arguments such as /spleef reload, /spleef reset or a non-existent argument such as /spleef base, I get this message in red "Unable to find _____.". Valid arguments still work the way they are supposed to, just that they show this "Unable to find ______" message before the intended functions trigger.

    I don't really know what could be a problem.
    I'll attach my code, but just be ready for the long and confusing parts.


    My coding environment:
    Spigot 1.8.8 JAR uploaded into Build Path
    Pom.xml empty (I didn't put any dependencies or repositories, I don't understand what's the point of doing so too)
    Plugin.yml filled with all the required info such as main class, name, version, description, no depends/softdepends. Only one command registered: /spleef

    CODES: (Don't hurt your brain that much)
    MAIN CLASS:
    Code (Java):

    package com.gmail.calorious;

    import org.bukkit.plugin.java.JavaPlugin;

    import commands.*;
    import events.*;

    public class SpleefGame extends JavaPlugin {
        private static SpleefGame instance;
        public static SpleefGame getInstance() { return instance; }
        @Override
        public void onEnable() {
            instance = this;
            saveDefaultConfig();
            getServer().getPluginManager().registerEvents(new SpleefWinnerEvents(), this);
            getServer().getPluginManager().registerEvents(new ChallengingEvents(), this);
            getCommand("spleef").setExecutor(new SpleefBaseCommand());
        }
     
        @Override
        public void onDisable() {}
    }
     
    SpleefBaseCommand class:
    Code (Java):

    package commands;

    import java.util.ArrayList;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;

    import com.gmail.calorious.SpleefGame;

    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle.EnumTitleAction;

    public class SpleefBaseCommand implements CommandExecutor {
               private static String challengername = null;
               Player player = null;
               private static ArrayList<UUID> players = new ArrayList<UUID>();
               public static ArrayList<UUID> getPlayers() {
                   return players;
               }
           
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("spleef")) {
                if(args.length == 0) {
                    sender.sendMessage(ChatColor.GOLD + "      Spleef     ");
                    sender.sendMessage(ChatColor.DARK_GRAY + "=======================");
                    sender.sendMessage(ChatColor.GREEN + " /spleef - Base Command/Shows this info");
                    sender.sendMessage(ChatColor.GREEN + " /spleef (Player) - Challenge a player to Spleef.");
                    sender.sendMessage(ChatColor.GREEN + " /spleef reload - Reload the configuration with new values.");
                    sender.sendMessage(ChatColor.GOLD + "      Spleef     ");
                    sender.sendMessage(ChatColor.DARK_GRAY + "=======================");
                    return true;
                }
                if(args.length > 0 && !args[0].equalsIgnoreCase("accept") && !args[0].equalsIgnoreCase("deny")) {
                    Player target = Bukkit.getPlayer(args[0]);
                    if(target == null) {
                        sender.sendMessage(ChatColor.RED + "Unable to find " + args[0] + ChatColor.RED + ".");
                    } else {
                        sender.sendMessage(ChatColor.GREEN + "Successfully sent a challenge to " + ChatColor.GOLD + target.getName() + ChatColor.GREEN + ".");
                        target.sendMessage(ChatColor.GOLD + sender.getName() + " has sent you a Spleef challenge.");
                        TextComponent acceptmessage = new TextComponent("[Accept]");
                        TextComponent spacemessage = new TextComponent(" ");
                        TextComponent denymessage = new TextComponent("[Deny]");
                        acceptmessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/spleef accept"));
                        denymessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/spleef deny"));
                        acceptmessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Accept the challenge").bold(true).color(ChatColor.GREEN).create()));
                        denymessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Decline the challenge").bold(true).color(ChatColor.RED).create()));
                        target.spigot().sendMessage(acceptmessage, spacemessage, denymessage);
                        challengername = sender.getName();
                    }
                }
                if(args.length > 0 && args[0].equalsIgnoreCase("accept") && !args[0].equalsIgnoreCase("deny")) { //ACCEPT COMMAND, SENT BY (TARGET), THE CHALLENGED
                    sender.sendMessage(ChatColor.GREEN + "You have accepted the spleef challenge from " + ChatColor.GOLD + challengername);
                    Player challenger = Bukkit.getPlayer(challengername);
                    challenger.sendMessage(ChatColor.GOLD + sender.getName() + ChatColor.GREEN + " has accepted your spleef challenge.");
                    Player player = (Player) sender;  // player is now challenged
                    player.getInventory().clear();
                    challenger.getInventory().clear();
                    sender.sendMessage(ChatColor.GOLD + "Your inventory has been cleared.");
                    challenger.sendMessage(ChatColor.GOLD + "Your inventory has been cleared.");
                    Location player1loc = new Location(challenger.getWorld(), SpleefGame.getInstance().getConfig().getDouble("arenas.1.p1spawnx"), SpleefGame.getInstance().getConfig().getDouble("arenas.1.p1spawny"), SpleefGame.getInstance().getConfig().getDouble("arenas.1.p1spawnz"));
                    Location player2loc = new Location(challenger.getWorld(), SpleefGame.getInstance().getConfig().getDouble("arenas.1.p2spawnx"), SpleefGame.getInstance().getConfig().getDouble("arenas.1.p2spawny"), SpleefGame.getInstance().getConfig().getDouble("arenas.1.p2spawnz"));
                    challenger.teleport(player1loc);
                    player.teleport(player2loc);
                    PacketPlayOutTitle countdown3 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"3\",\"bold\":true,\"color\":\"green\"}]"));
                  PacketPlayOutTitle countdown2 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"2\",\"bold\":true,\"color\":\"gold\"}]"));
                  PacketPlayOutTitle countdown1 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"1\",\"bold\":true,\"color\":\"red\"}]"));
                  PacketPlayOutTitle countdownstart = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"Start!\",\"bold\":true,\"color\":\"green\"}]"));
                  ((CraftPlayer) challenger).getHandle().playerConnection.sendPacket(countdown3);
                  ((CraftPlayer) player).getHandle().playerConnection.sendPacket(countdown3);
                  Bukkit.getScheduler().scheduleSyncDelayedTask(SpleefGame.getInstance(), () -> {
                      ((CraftPlayer) challenger).getHandle().playerConnection.sendPacket(countdown2);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown2);
                      }, 40L);
               
                  Bukkit.getScheduler().scheduleSyncDelayedTask(SpleefGame.getInstance(), () -> {
                          ((CraftPlayer) challenger).getHandle().playerConnection.sendPacket(countdown1);
                  ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown1);
                      }, 40L);
                  Bukkit.getScheduler().scheduleSyncDelayedTask(SpleefGame.getInstance(), () -> {
                        ((CraftPlayer) challenger).getHandle().playerConnection.sendPacket(countdownstart);
                ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdownstart);
                    }, 40L);
             
                  players.add(challenger.getUniqueId());
                  players.add(player.getUniqueId());
                  for(UUID challengers : players) {
                      Bukkit.getPlayer(challengers).setGameMode(GameMode.SURVIVAL);
                  }
                for(String command : SpleefGame.getInstance().getConfig().getStringList("commands")) {
                    final ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
                    command.replace("{target}", player.getName());
                    command.replace("{challenger}", challenger.getName());
                    Bukkit.dispatchCommand(console, command);
                }
                Bukkit.broadcastMessage(ChatColor.GREEN + "SpleefGame: Sent by " + ChatColor.GOLD + challengername + ChatColor.GREEN + " to " + ChatColor.GOLD + player.getName() + ChatColor.GREEN + ", issued all commands.");
                }
                if(args.length > 0 && !args[0].equalsIgnoreCase("accept") && args[0].equalsIgnoreCase("deny")) { //DECLINE COMMAND, SENT BY (TARGET), THE CHALLENGED
                    sender.sendMessage(ChatColor.RED + "You have declined the Spleef challenge from " + challengername);
                    Player challenger = Bukkit.getPlayer(challengername);
                    challenger.sendMessage(ChatColor.RED + "Your challenge to " + sender.getName() + " has been declined.");
                }
                if(args.length > 0 && !args[0].equalsIgnoreCase("accept") && !args[0].equalsIgnoreCase("deny") && args[0].equalsIgnoreCase("reload")) {
                    SpleefGame.getInstance().reloadConfig();
                    sender.sendMessage(ChatColor.GREEN + "Successfully reloaded SpleefGame's configuration.");
                }
            }
            return true;

    }
    }
     
    Event Class 1 - SpleefWinnerEvents:
    Code (Java):

    package events;

    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;

    import com.gmail.calorious.SpleefGame;

    import commands.SpleefBaseCommand;
    import net.md_5.bungee.api.ChatColor;

    public class SpleefWinnerEvents implements Listener {
        Player winner = null;
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerDeath(PlayerDeathEvent e) {
            if(SpleefBaseCommand.getPlayers().contains(e.getEntity().getUniqueId())) {
                SpleefBaseCommand.getPlayers().remove(e.getEntity().getUniqueId());
                for(UUID winneruuid : SpleefBaseCommand.getPlayers()) {
                    winner = Bukkit.getPlayer(winneruuid);
                }
                Bukkit.broadcastMessage(ChatColor.GOLD + winner.getName() + ChatColor.GREEN + " has won the Spleef challenge!");
                    Bukkit.broadcastMessage(ChatColor.GOLD + winner.getName() + ChatColor.GREEN + " has won the Spleef challenge!");
                Bukkit.getScheduler().scheduleSyncDelayedTask(SpleefGame.getInstance(), new Runnable() {
                    public void run() {
                        SpleefBaseCommand.getPlayers().clear();
                    }
                }, 20L);
            }
        }
    }
     
    Event Class 2 - During a challenge events:
    Code (Java):

    package events;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;

    import commands.SpleefBaseCommand;
    import net.md_5.bungee.api.ChatColor;

    public class ChallengingEvents implements Listener {
        @EventHandler
        public void onCommandPreprocess(PlayerCommandPreprocessEvent e) {
            if(e.getMessage().contains("spleef")) {
                if(SpleefBaseCommand.getPlayers().contains(e.getPlayer().getUniqueId())) {
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "You are unable to run this command while you are in a Spleef challenge.");
                }
            }
        }

    }
     
     
  2. This code is executed during /spleef reload
    Code (Text):
    if(args.length > 0 && !args[0].equalsIgnoreCase("accept") && !args[0].equalsIgnoreCase("deny")) {
                    Player target = Bukkit.getPlayer(args[0]);
                    if(target == null) {
                        sender.sendMessage(ChatColor.RED + "Unable to find " + args[0] + ChatColor.RED + ".");
                    } else {
                        sender.sendMessage(ChatColor.GREEN + "Successfully sent a challenge to " + ChatColor.GOLD + target.getName() + ChatColor.GREEN + ".");
                        target.sendMessage(ChatColor.GOLD + sender.getName() + " has sent you a Spleef challenge.");
                        TextComponent acceptmessage = new TextComponent("[Accept]");
                        TextComponent spacemessage = new TextComponent(" ");
                        TextComponent denymessage = new TextComponent("[Deny]");
                        acceptmessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/spleef accept"));
                        denymessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/spleef deny"));
                        acceptmessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Accept the challenge").bold(true).color(ChatColor.GREEN).create()));
                        denymessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Decline the challenge").bold(true).color(ChatColor.RED).create()));
                        target.spigot().sendMessage(acceptmessage, spacemessage, denymessage);
                        challengername = sender.getName();
                    }
                }
    You need to change your line line to :
    Code (Text):
    if(args.length > 0 && !args[0].equalsIgnoreCase("accept") && !args[0].equalsIgnoreCase("deny") && !args[0].equalsIgnoreCase("reload")) {
     
  3. Ok i'm blind and I need to analyse my code better, thanks!
     
    • Funny Funny x 1
Thread Status:
Not open for further replies.