私があなたの状況で行うことは、Timer class
を作成することです。私として
package yourPackage;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class EnvoyCommand implements CommandExecutor {
// This maps contains all players and their timers
// This is to prevent players from running multiple timers at once.
private final Map<Player, Timer> timers = new HashMap<>();
private final Main main;
public EnvoyCommand(Main main) {
this.main = main;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(!(sender instanceof Player)){
sender.sendMessage("You must be a Player to preform this command");
return true;
// Make sure to return true here, because return false is only used
// for an error in the usage of the command.
}
Player player = (Player) sender;
if(timers.containsKey(player)){
player.sendMessage("The timer is already running!");
return true;
}
Timer timer = new Timer(player, 5, 4, 3, 2, 1);
timers.put(player, timer);
timer.runTaskTimer(main, 0, 20 * 60); // 20*60 = 60 * 20 ticks or 60 seconds or 1 minute.
return true;
}
}
:あなたは、単にいくつかの変更とあなたのようなエグゼキュータのクラスを作ることができ、あなたのコマンドでこれをフックしたい場合は今
package yourPackage;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class Timer extends BukkitRunnable {
// This is the player that executed the command
private final Player player;
// These are the numbers that will be shown to the player when the timer
// reaches this number.
private final int[] mentionNumbers;
// The amount of minutes +1 because when the timer starts, it instantly
// removes 1 minute (due to the 'count--;' at the begin of the 'run' method.)
private int count = 6;
public Timer(Player player, int... mentionNumbers) {
this.player = player;
this.mentionNumbers = mentionNumbers;
}
@Override
public void run() {
count--;
if(count == 0){
player.sendMessage("The timer has finished!");
//Do whatever you want to do when the timer finishes.
} else {
for(int i : mentionNumbers){
if(count == i){
player.sendMessage(ChatColor.GREEN + "Envoys Will drop in " + i + "seconds");
break;
}
}
}
}
}
:これはあなたのTimerクラスがどのように見えるかです
package test;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
getCommand("envoy").setExecutor(new EnvoyCommand(this));
}
}
そして最後ににコマンドを登録する:あなたはすでにあなたの
onEnable
方法であなたのエグゼキュータのクラスを登録することを確認し、行っていると思います:
name: yourPluginName
version: 1.0
main: yourPackage.yourMainClass
author: AgroCupcake
commands:
envoy:
description: Command description here
usage: /envoy