Reputation: 45
I am making a plugin with essential commands for a server, and I have put in a couple of events which are working fine. But when I try to add commands, it doesn't work. (Before you ask, I've replaced everything in the onCommand section with a simple sendMessage, and it doesn't do anything). When I execute the command, it just tells me the usage I have put in the plugin.yml.
Here is my code: sorry it's really long
package com.pepsi.core;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
@Override
public void onEnable() {
Bukkit.getLogger().info("PepsiCore >> Enabled!");
Bukkit.getServer().getPluginManager().registerEvents(this, this);
getConfig().options().copyDefaults(true);
saveConfig();
}
@Override
public void onDisable() {
Bukkit.getLogger().info("PepsiCore >> Disabled!");
}
public boolean onCommand(Command cmd, CommandSender sender, String cmdLbl, String[] args) {
if (args.length == 0) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eNot enough arguments! Use &6/pcore help &e to see a list of commands!"));
return false;
}
if (args.length == 1) {
if (args[0].equalsIgnoreCase("help")) {
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
sender.sendMessage(Util.transMsg("&6/pcore help &8>> &eDisplays the help page"));
sender.sendMessage(Util.transMsg("&6/pcore info &8>> &eDisplays the plugin info page"));
sender.sendMessage(Util.transMsg("&6/pcore ranks &8>> &eDisplays all the ranks"));
sender.sendMessage(Util.transMsg("&6/pcore setrank <player> <rank> &8>> &eDisplays all the ranks"));
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
return true;
}
if (args[0].equalsIgnoreCase("info")) {
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
sender.sendMessage(Util.transMsg("&ePepsiCore version &61.0.0-BETA"));
sender.sendMessage(Util.transMsg("&eDeveloped by &6P3pi &efor &6Project: Infernal"));
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
return true;
}
if (args[0].equalsIgnoreCase("ranks")) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eRanks: &6guest&e, &6gold&e, &6diamond&e, &6emerald&e, &6yt&e, &6staff&e, &6developer&e, &6owner"));
return false;
}
if (args[0].equalsIgnoreCase("setrank")) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eNot enough arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eInvalid arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}
if (args.length == 2) {
if (args[0].equalsIgnoreCase("setrank")) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eNot enough arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eInvalid arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}
if (args.length == 3) {
if (args[0].equalsIgnoreCase("setrank")) {
Player t = Bukkit.getServer().getPlayer(args[1]);
if (t == null) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eThe player &6" + args[1] + " &eis not online at the moment!"));
return false;
}
String uuid = t.getUniqueId().toString();
String rank = args[2];
if (rank == ("guest")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("gold")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("diamond")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("emerald")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("yt")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("staff")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("developer")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("owner")) { getConfig().set("ranks." + uuid, rank); }
else { sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eThe rank &6" + rank + " &e does not exist! Use &6/pcore ranks &eto see a list of ranks!")); }
}
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eInvalid arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}
return true;
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
Player p = e.getPlayer();
String uuid = p.getUniqueId().toString();
String name = p.getName();
String msg = e.getMessage();
String rank = (String) getConfig().get("ranks." + uuid);
if (rank == "guest") { e.setFormat(Util.transMsg("&fGuest &7&l" + name + " &8>> &f" + msg)); }
else if (rank == "gold") { e.setFormat(Util.transMsg("&eGold &6&l" + name + " &8>> &e" + msg)); }
else if (rank == "diamond") { e.setFormat(Util.transMsg("&bDiamond &3&l" + name + " &8>> &b" + msg)); }
else if (rank == "emerald") { e.setFormat(Util.transMsg("&aEmerald &2&l" + name + " &8>> &a" + msg)); }
else if (rank == "yt") { e.setFormat(Util.transMsg("&dYouTuber &5&l" + name + " &8>> &d" + msg)); }
else if (rank == "staff") { e.setFormat(Util.transMsg("&9Staff &1&l" + name + " &8>> &9" + msg)); }
else if (rank == "developer") { e.setFormat(Util.transMsg("&cDeveloper &4&l" + name + " &8>> &c" + msg)); }
else if (rank == "owner") { e.setFormat(Util.transMsg("&cOwner &4&l" + name + " &8>> &c" + msg)); }
else { e.setCancelled(true); p.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eYour rank seems to be broken! Please let a member of staff know!")); }
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
Player p = e.getPlayer();
String uuid = p.getUniqueId().toString();
if (p.hasPlayedBefore() == false) {
getConfig().set("ranks." + uuid, "guest");
saveConfig();
return;
}
}
}
and here is my plugin.yml:
name: PepsiCore
author: P3pi
version: 1.0.3
main: com.pepsi.core.Main
commands:
pcore:
description: Core command
usage: /pcore
pepsicore:
description: Core command
usage: /pepsicore
Thanks for the help
Upvotes: 0
Views: 92
Reputation: 46
The way that Bukkit handles commands is it runs the onCommand
method of your JavaPlugin class. Specifically the onCommand
method that takes the arguments CommandSender sender, Command command, String label, String[] args
Your method has the arguments in the wrong order, as Vlexing mentioned, so it's not running your method when the command is run, so instead running the onCommand
method from JavaPlugin
which only contains return false;
. Bukkit still recognizes your command because of your plugin.yml, but when it tried to run your method it got false
so it printed out your usage message.
Basically, just fix the order of your arguments and follow Vlexing's answer and you should be set.
Upvotes: 1
Reputation: 46
When writing code in the onCommand()
method, the code block should always return true
, in examples such as:
if (args[0].equalsIgnoreCase("ranks")) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eRanks: &6guest&e, &6gold&e, &6diamond&e, &6emerald&e, &6yt&e, &6staff&e, &6developer&e, &6owner"));
return false;
}
You use return false;
which will give you the plugin.yml
usage.
Please see this spigot thread talking about the same issue.
Please see this bukkit thread talking about the same issue.
Upvotes: 0