我在《我的世界》中制作了一个计时器,但突然它根本不起作用,而且我没有改变任何东西。命令
/timer
发送我添加的“使用”消息,但是当我使用更多参数时,它将不起作用,并且显示“尝试执行此命令时发生内部错误”
主班
package tv.bebaeb.mcplugion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import tv.bebaeb.mcplugion.timer.Timer;
import tv.bebaeb.mcplugion.commands.HealCommand;
import tv.bebaeb.mcplugion.commands.TimerCommand;
import tv.bebaeb.mcplugion.listeners.JoinListener;
import tv.bebaeb.mcplugion.listeners.QuitListener;
import tv.bebaeb.mcplugion.tablist.TablistManager;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
public final class Mcplugion extends JavaPlugin {
private static Mcplugion instance;
private TablistManager tablistManager;
private Timer timer;
@Override
public void onLoad() {
instance = this;
}
@Override
public void onEnable() {
Bukkit.getConsoleSender().sendMessage(ChatColor.BLUE + "\n\n\n\n\nMcPlugion\n\nM\nC\nP\nL\nU\nG\nI\nO\nN\n\nMcPlugion\n\n\n\n\n");
PluginManager manager = Bukkit.getPluginManager();
manager.registerEvents(new JoinListener(), this);
manager.registerEvents(new QuitListener(), this);
getCommand("timer").setExecutor(new TimerCommand());
getCommand("heal").setExecutor(new HealCommand());
timer = new Timer(false, 0, 0, 0, 0);
tablistManager = new TablistManager();
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
public static Mcplugion getInstance() {
return instance;
}
public Timer getTimer() {
return timer;
}
public TablistManager getTablistManager() {
return tablistManager;
}
}
TimerCommand
班级
package tv.bebaeb.mcplugion.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 tv.bebaeb.mcplugion.Mcplugion;
import tv.bebaeb.mcplugion.timer.Timer;
import java.util.MissingFormatArgumentException;
public class TimerCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command.");
return true;
}
Player player = (Player) sender;
Mcplugion plugin = Mcplugion.getInstance();
Timer timer = Mcplugion.getInstance().getTimer();
if (args.length == 0){
sender.sendMessage(ChatColor.RED+"a"+ChatColor.GRAY + "Usage" + ChatColor.GRAY + ": " + ChatColor.BLUE + "/timer resume, /timer reverse, /timer pause, /timer set <Days> <Hours> <Minutes> <Seconds>, /timer reset, /timer time");
}
if (args[0].equalsIgnoreCase("resume")) {
if (timer.isRunning()) {
sender.sendMessage(ChatColor.RED + "The timer is already running.");
return false;
} else if (!timer.isRunning()) {
timer.setRunback(false);
timer.setRunning(true);
sender.sendMessage(ChatColor.GREEN + "The timer has started.");
}
}
if (args[0].equalsIgnoreCase("reverse")) {
if (timer.isRunning()) {
sender.sendMessage(ChatColor.RED + "The timer is already running.");
return false;
}
if (!timer.isRunning()) {
timer.setRunback(true);
timer.setRunning(true);
sender.sendMessage(ChatColor.GREEN + "The timer has started reversing.");
}
}
if (args[0].equalsIgnoreCase("pause")) {
if (!timer.isRunning()) {
sender.sendMessage(ChatColor.RED + "The timer is not running.");
return false;
}
timer.setRunning(true);
sender.sendMessage(ChatColor.GRAY + "The timer has stopped.");
return false;
}
if (args[0].equalsIgnoreCase("set")) {
try {
timer.setRunning(false);
timer.setTimeD(Integer.parseInt(args[1]));
timer.setTimeH(Integer.parseInt(args[2]));
timer.setTimeM(Integer.parseInt(args[3]));
timer.setTimeS(Integer.parseInt(args[4]));
if (timer.getTimeH() < 24) {
if (timer.getTimeM() < 60) {
if (timer.getTimeS() < 60) {
sender.sendMessage(ChatColor.GRAY + "The timer has been set to " + args[1] + " days, " + args[2] + " hours, " + args[3] + " minutes, " + args[4] + " seconds.");
}
}
}
} catch (MissingFormatArgumentException e) {
sender.sendMessage(ChatColor.RED + "You need to use the right format: Days, Hours, Minutes, Seconds");
}
return false;
}
if (args[0].equalsIgnoreCase("reset")) {
timer.setRunning(false);
timer.setRunback(false);
timer.setTimeS(0);
timer.setTimeM(0);
timer.setTimeH(0);
timer.setTimeD(0);
sender.sendMessage(ChatColor.GRAY + "The timer has been reset.");
return false;
}
if (args[0].equalsIgnoreCase("time")) {
sender.sendMessage(timer.getTimeD() + "d, " + timer.getTimeH() + "h, " + timer.getTimeM() + "m, " + timer.getTimeS() + "s");
}
if (args[0].equalsIgnoreCase("toggle")) {
if (timer.isRunning()) {
timer.setRunning(false);
sender.sendMessage(ChatColor.RED + "The timer is not running.");
} else {
timer.setRunning(true);
sender.sendMessage(ChatColor.GREEN + "The timer has started.");
}
}
return true;
}
}
Timer
班级
package tv.bebaeb.mcplugion.timer;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import tv.bebaeb.mcplugion.Mcplugion;
public class Timer {
private boolean running; // true or false
private boolean runback;
private int timeS;
private int timeM;
private int timeH;
private int timeD;
public Timer(boolean running, int timeS, int timeM, int timeH, int timeD) {
this.running = running;
this.timeS = timeS;
this.timeM = timeM;
this.timeH = timeH;
this.timeD = timeD;
run();
}
public boolean isRunning() {
return running;
}
public boolean isRunback() {
return runback;
}
public void setRunning(boolean running) {
this.running = running;
}
public void setRunback(boolean runback) {
this.runback = runback;
}
public int getTimeS() {
return timeS;
}
public int getTimeM() {
return timeM;
}
public int getTimeH() {
return timeH;
}
public int getTimeD() {
return timeD;
}
public void setTimeS(int time) {
this.timeS = time;
}
public void setTimeM(int time) {
this.timeM = time;
}
public void setTimeH(int time) {
this.timeH = time;
}
public void setTimeD(int time) {
this.timeD = time;
}
public void sendActionBar() {
for (Player player : Bukkit.getOnlinePlayers()) {
if (!isRunning()) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatColor.RED +
"Timer paused"));
continue;
}
if (isRunning()) {
if (getTimeM() == 0) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatColor.WHITE.toString() +
ChatColor.BOLD + String.format("%ds", getTimeS())));
} else if (getTimeH() == 0) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatColor.WHITE.toString() +
ChatColor.BOLD + String.format("%dm %ds", getTimeM(), getTimeS())));
} else if (getTimeD() == 0) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatColor.WHITE.toString() +
ChatColor.BOLD + String.format("%dh %dm %ds", getTimeH(), getTimeM(), getTimeS())));
} else if (getTimeD() > 0) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatColor.WHITE.toString() +
ChatColor.BOLD + String.format("%dd %dh %dm %ds", getTimeD(), getTimeH(), getTimeM(), getTimeS())));
}
}
}
}
private void run() {
new BukkitRunnable() {
@Override
public void run() {
sendActionBar();
if (!isRunning()) {
return;
}
if (isRunback()) {
if (getTimeS() > 0) {
setTimeS(getTimeS() - 1);
} else {
if (getTimeM() > 0) {
setTimeS(59);
setTimeM(getTimeM() - 1);
} else {
if (getTimeH() > 0) {
setTimeM(59);
setTimeS(59);
setTimeH(getTimeH() - 1);
} else {
if (getTimeD() > 0) {
setTimeH(23);
setTimeM(59);
setTimeS(59);
setTimeD(getTimeD() - 1);
} else {
sendActionBar();
setRunning(false);
}
}
}
}
} else {
if (getTimeS() < 60) {
setTimeS(getTimeS() + 1);
}
if (getTimeS() > 59) {
setTimeS(0);
setTimeM(getTimeM() + 1);
}
if (getTimeM() > 59) {
setTimeM(0);
setTimeH(getTimeH() + 1);
}
if (getTimeH() > 23) {
setTimeH(0);
setTimeD(getTimeD() + 1);
}
}
}
}.runTaskTimer(Mcplugion.getInstance(), 20, 20);
}
}
此方法的返回表明是否应该显示命令用法。所以
return true
意味着它会显示出来。
你总是返回 true。
否则,你就有问题了。您正在检查
args
是否为空,然后您正在检查 args
的第一个内容是否等于某个值。这是行不通的。你应该这样做:
if(args.length == 0) {
// here send help/usage message
} else if(args[0].equalsIgnoreCase("something")) {
// here it's fine
}
您还可以将 return 与 like 一起使用:
if(args.length == 0) {
// here send help/usage message
return false; // here you're stoping the method
}
if(args[0].equalsIgnoreCase("something")) {
// here it's fine
}