所以我有一个名为JustExplode的Minecraft插件,我试图让它如果有人拥有justexplode.timer的权限,他们必须等待10分钟才能再次使用命令/ explode。它不起作用,我尝试了多种方式,而且我是编程的新手......有人可以帮助我吗?这是我的主要类JustExplode.java:
package me.sam1370.justexplode;
import java.util.Date;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class JustExplode extends JavaPlugin{
@Override
public void onEnable() {
getLogger().info("JustExplode has been enabled!");
getConfig().options().copyDefaults(true);
saveConfig();
}
@Override
public void onDisable() {
getLogger().info("JustExplode has been disabled!");
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("explode") && sender.hasPermission("justexplode.explode")){
// Make sure that the player specified exactly one argument (the name of the player to exPLODE!).
if (args.length != 1) {
// When onCommand() returns false, the help message associated with that command is displayed.
return false;
}
// Make sure the sender is a player.
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can explode other players.");
return true;
}
// Get the player who should be exploded. Remember that indices start with 0, not 1.
Player target = Bukkit.getServer().getPlayer(args[0]);
// Make sure the player is online.
if (target == null) {
sender.sendMessage(args[0] + " is not currently online.");
return true;
}
Location loc = target.getLocation();
target.getWorld().spawnEntity(loc, EntityType.PRIMED_TNT);
String actuality = target.getName();
if(sender instanceof Player){
Player player = (Player) sender;
String localMessage = getConfig().getString("message");
if(localMessage.contains("%player%")) {
localMessage = localMessage.replace("%player%", actuality);
}
if(localMessage.contains("&4")) {
localMessage = localMessage.replace("&4", ChatColor.DARK_RED + "");
}
if(localMessage.contains("&c")) {
localMessage = localMessage.replace("&c", ChatColor.RED + "");
}
if(localMessage.contains("&6")) {
localMessage = localMessage.replace("&6", ChatColor.GOLD + "");
}
if(localMessage.contains("&e")) {
localMessage = localMessage.replace("&e", ChatColor.YELLOW + "");
}
if(localMessage.contains("&2")) {
localMessage = localMessage.replace("&2", ChatColor.DARK_GREEN + "");
}
if(localMessage.contains("&a")) {
localMessage = localMessage.replace("&a", ChatColor.GREEN + "");
}
if(localMessage.contains("&b")) {
localMessage = localMessage.replace("&b", ChatColor.AQUA + "");
}
if(localMessage.contains("&3")) {
localMessage = localMessage.replace("&3", ChatColor.DARK_AQUA + "");
}
if(localMessage.contains("&1")) {
localMessage = localMessage.replace("&1", ChatColor.DARK_BLUE + "");
}
if(localMessage.contains("&9")) {
localMessage = localMessage.replace("&9", ChatColor.BLUE + "");
}
if(localMessage.contains("&d")) {
localMessage = localMessage.replace("&d", ChatColor.LIGHT_PURPLE + "");
}
if(localMessage.contains("&5")) {
localMessage = localMessage.replace("&5", ChatColor.DARK_PURPLE + "");
}
if(localMessage.contains("&f")) {
localMessage = localMessage.replace("&f", ChatColor.WHITE + "");
}
if(localMessage.contains("&7")) {
localMessage = localMessage.replace("&7", ChatColor.GRAY + "");
}
if(localMessage.contains("&l")) {
localMessage = localMessage.replace("&l", ChatColor.BOLD + "");
}
if(localMessage.contains("&n")) {
localMessage = localMessage.replace("&n", ChatColor.UNDERLINE + "");
}
if(localMessage.contains("&o")) {
localMessage = localMessage.replace("&o", ChatColor.ITALIC + "");
}
if(localMessage.contains("&k")) {
localMessage = localMessage.replace("&k", ChatColor.MAGIC + "");
}
if(localMessage.contains("&m")) {
localMessage = localMessage.replace("&m", ChatColor.STRIKETHROUGH + "");
}
if(localMessage.contains("&r")) {
localMessage = localMessage.replace("&r", ChatColor.RESET + "");
}
player.sendMessage(localMessage);
return true;
}else{
if(sender instanceof Player){
Player player = (Player) sender;
player.sendMessage(ChatColor.RED + "You don't have permission to explode anyone!");
return true;
}
}
return true;
}
return true;
}
public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("explode") && sender.hasPermission("justexplode.timer")){
long timerMinutes = getConfig().getLong("timerMinutes");
long startTime = System.currentTimeMillis();
long elapsedTime = 0L;
while (elapsedTime < timerMinutes) {
long timeLeft = timerMinutes - elapsedTime;
String localMessage2 = getConfig().getString("timer");
String timeLeftString = Long.toString(timeLeft);
if(localMessage2.contains("%timeleft%")) {
localMessage2 = localMessage2.replace("%timeleft%", timeLeftString);
}
if(localMessage2.contains("&4")) {
localMessage2 = localMessage2.replace("&4", ChatColor.DARK_RED + "");
}
if(localMessage2.contains("&c")) {
localMessage2 = localMessage2.replace("&c", ChatColor.RED + "");
}
if(localMessage2.contains("&6")) {
localMessage2 = localMessage2.replace("&6", ChatColor.GOLD + "");
}
if(localMessage2.contains("&e")) {
localMessage2 = localMessage2.replace("&e", ChatColor.YELLOW + "");
}
if(localMessage2.contains("&2")) {
localMessage2 = localMessage2.replace("&2", ChatColor.DARK_GREEN + "");
}
if(localMessage2.contains("&a")) {
localMessage2 = localMessage2.replace("&a", ChatColor.GREEN + "");
}
if(localMessage2.contains("&b")) {
localMessage2 = localMessage2.replace("&b", ChatColor.AQUA + "");
}
if(localMessage2.contains("&3")) {
localMessage2 = localMessage2.replace("&3", ChatColor.DARK_AQUA + "");
}
if(localMessage2.contains("&1")) {
localMessage2 = localMessage2.replace("&1", ChatColor.DARK_BLUE + "");
}
if(localMessage2.contains("&9")) {
localMessage2 = localMessage2.replace("&9", ChatColor.BLUE + "");
}
if(localMessage2.contains("&d")) {
localMessage2 = localMessage2.replace("&d", ChatColor.LIGHT_PURPLE + "");
}
if(localMessage2.contains("&5")) {
localMessage2 = localMessage2.replace("&5", ChatColor.DARK_PURPLE + "");
}
if(localMessage2.contains("&f")) {
localMessage2 = localMessage2.replace("&f", ChatColor.WHITE + "");
}
if(localMessage2.contains("&7")) {
localMessage2 = localMessage2.replace("&7", ChatColor.GRAY + "");
}
if(localMessage2.contains("&l")) {
localMessage2 = localMessage2.replace("&l", ChatColor.BOLD + "");
}
if(localMessage2.contains("&n")) {
localMessage2 = localMessage2.replace("&n", ChatColor.UNDERLINE + "");
}
if(localMessage2.contains("&o")) {
localMessage2 = localMessage2.replace("&o", ChatColor.ITALIC + "");
}
if(localMessage2.contains("&k")) {
localMessage2 = localMessage2.replace("&k", ChatColor.MAGIC + "");
}
if(localMessage2.contains("&m")) {
localMessage2 = localMessage2.replace("&m", ChatColor.STRIKETHROUGH + "");
}
if(localMessage2.contains("&r")) {
localMessage2 = localMessage2.replace("&r", ChatColor.RESET + "");
}
if(sender instanceof Player){
Player player = (Player) sender;
player.sendMessage(localMessage2);
elapsedTime = (new Date()).getTime() - startTime;
}
}
return true;
}
return true;
}
}
这是我设置的config.yml:
# Default config file for JustExplode, by Sam1370
# There isn't a reload config command for right now, just reload or restart the server
# Change this to the message you want to see when you are exploding a player
# Put %player% in it to get the player that you are exploding
# Color codes are now supported, as opposed to version 1.0.8.2. YES!!!
message: "&aExploded &c%player%!"
# Change this to the message you want to see when a player tries to do /explode with the permission justexplode.timer
# Put %timeleft% in it to get the time left before they can explode again
# Color codes are supported
timer: "You can't explode anyone for %timeleft%!"
# Specify how many minutes, in milliseconds, how long you want someone to wait before they can do /explode again
# The default is 600000 milliseconds, which is 10 minutes
timerMinutes: 600000
任何人都知道如何做到这一点?提前谢谢。
答案 0 :(得分:0)
当有人执行命令/爆炸时,将地图中的玩家姓名和当前时间戳存储起来。当用户再次尝试执行该命令时,检查播放器是否在地图中,以及时间差是否等于大于config.yml中设置的时间。
小示例代码:
Map<Player, Long> times = new HashMap<>();
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player))
return false;
Player player = (Player) sender;
if (times.contains(player) && times.get(player) >= delay) {
//execute command
times.put(player, System.currentTimeMillis());
} else if (!times.contains(player)) {
//execute command
times.put(player, System.currentTimeMillis());
}
}
答案 1 :(得分:0)
对于这些类型的延迟,您需要使用System.currentTimeMillis()
和简单的HashMap
这实际上非常简单,让我向您展示几周前我写的一个例子。
private static HashMap<CommandSender, Long> lastExplodeUses = new HashMap<>();
public static HashMap<CommandSender, Long> getLastExplodeUses() {
return lastExplodeUses;
}
public static void setLastExplodeUse(CommandSender player, long till) {
lastHelpopSended.put(player, till);
}
public static boolean canExplode(CommandSender player) {
long timeExploded = lastExplodeUses.get(player) == null ? 0 : lastExplodeUses.get(player);
if (timeExploded == 0) return true; // if it's 0 - you can execute explode command.
long timeCurrent = System.currentTimeMillis(); // current time in millis
if (timeExploded < timeCurrent) { // if the timeExploded is lower than timeCurrent you can execude explode command.
return true;
}
return false; // if not, you cant.
}
下一步是什么?您需要在命令中添加一些函数。
if (sender.hasPermission("justexplode.timer")) { // if player has permission, if not - bypass the timer.
if (!canExplode(sender)) {
// you can't explode yet :/
return;
}
setLastExplodeUse(sender, System.currentTimeMillis() + 10 * 1000); // set the till time to explode use. Current time + 10000, 10000 = 10 seconds.
}
// there you can do your explosion etc.
请记住! 如果您在延迟大约kits
长时创建24 hours
,则需要保存HashMap
1}}到MySQL
或其他地方并加载启动!