我做了一个插件,有2个套件,首先与名称铁和其他与名称DProtectso我希望他们给项目和我做的一切,我做了一个冷却下来的问题是,当我输入套件铁或套件DProtect它不工作,我试着做了很多事情,不工作,这是代码,我可以有一些帮助?
package Rivals.iSryMan.Kits.commands;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import Rivals.iSryMan.Kits.Main;
public class Kit implements CommandExecutor{
private HashMap<UUID,Long> ironcooldown = new HashMap<UUID,Long>();
private int ironcooldowntime = 300;
private HashMap<UUID,Long> DProtectcooldown = new HashMap<UUID,Long>();
private int DProtectcooldowntime = 5259487;
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(sender instanceof Player) {
Player p = (Player)sender;
if(args.length == 1) {
//Kit Iron
if(args[0].equals("Iron")) {
if(ironcooldown.containsKey(p.getUniqueId())) {
long ironsecondsleft = ( (ironcooldown.get(p.getUniqueId())/ 1000) + ironcooldowntime) - (System.currentTimeMillis() / 1000);
if(ironsecondsleft > 0) {
p.sendMessage(Main.Color(Main.prefix + "You must wait " + ironsecondsleft + " before you take that kit again!"));
}else if(ironsecondsleft == 0) {
final ItemStack ironhelmet = new ItemStack(Material.IRON_HELMET);
final ItemStack ironchestplate = new ItemStack(Material.IRON_CHESTPLATE);
final ItemStack ironleggings = new ItemStack(Material.IRON_LEGGINGS);
final ItemStack ironboots = new ItemStack(Material.IRON_BOOTS);
p.getInventory().addItem(ironhelmet);
p.getInventory().addItem(ironchestplate);
p.getInventory().addItem(ironleggings);
p.getInventory().addItem(ironboots);
p.sendMessage(Main.Color(Main.prefix + " You've got your kit, Enjoy!"));
}
}
//Kit Iron
//Kit DProtect
} else if (args[0].equals("DProtect")) {
if(DProtectcooldown.containsKey(p.getUniqueId())) {
long DProtectsecondsleft = ( (DProtectcooldown.get(p.getUniqueId()) / 1000) + DProtectcooldowntime) - (System.currentTimeMillis() / 1000);
if(DProtectsecondsleft > 0) {
p.sendMessage(Main.Color(Main.prefix + "You must wait " + DProtectsecondsleft + " before you take that kit again!"));
}else if(DProtectcooldowntime == 0) {
final ItemStack dphelmet = new ItemStack(Material.IRON_HELMET);
final ItemMeta dphelmetmeta = dphelmet.getItemMeta();
final ItemStack dpchestplate = new ItemStack(Material.IRON_CHESTPLATE);
final ItemMeta dpchestplatemetaa = dphelmet.getItemMeta();
final ItemStack dpleggings = new ItemStack(Material.IRON_LEGGINGS);
final ItemMeta dpleggingsmeta = dphelmet.getItemMeta();
final ItemStack dpboots = new ItemStack(Material.IRON_BOOTS);
final ItemMeta dpbootsmeta = dphelmet.getItemMeta();
ArrayList<String> dplore = new ArrayList<String>();
dplore.add(Main.Color("&bAuthentic Protection 4 Armor"));
//Set Meta
dphelmet.setItemMeta(dphelmetmeta);
dpchestplate.setItemMeta(dpchestplatemetaa);
dpleggings.setItemMeta(dpleggingsmeta);
dpboots.setItemMeta(dpbootsmeta);
//Set Meta
//Set Lore
dphelmetmeta.setLore(dplore);
dpchestplatemetaa.setLore(dplore);
dpleggingsmeta.setLore(dplore);
dpbootsmeta.setLore(dplore);
//Set Lore
//Adding Kit
p.getInventory().addItem(dphelmet);
p.getInventory().addItem(dpchestplate);
p.getInventory().addItem(dpleggings);
p.getInventory().addItem(dpboots);
//Adding Kit
p.sendMessage(Main.Color(Main.prefix + " You've got your kit, Enjoy!"));
}
}
//Kit DProtect
}
return true;
}
}
return false;
}
}
你有没有确定。
onEnable()
和 plugin.yml
?equalsIgnoreCase()
ironcooldown.containsKey(p.getUniqueId())
. 我建议检查它们是否在其中。如果它们在,检查时间是否为0,否则在代码执行后添加它们。让我知道这是否有效.另外,你应该修正你的代码的缩进--它一开始让我感到困惑,将来可能会让你感到困惑。不过,这不是什么大事。
虽然 Slinthn 回答了你的问题,我想我应该再补充一点解释。
你声明了一个 私人 HashMap的两个套件冷却时间的。ironcooldown
和 DProtectcooldown
.
你检查你的内部 onCommand
如果执行该命令的玩家在你的条件中的HashMap中 if(ironcooldown.containsKey(p.getUniqueId()))
然而,这个条件永远不会是真的,因为玩家的UUID永远不会被添加到HashMap中。
在我看来,你想做的是像下面这样的事情(一半是伪代码)。
if(!(sender instanceof Player))
return true;
if(args.length == 0)
return true;
Player p = (Player) sender;
if(args[0].equalsIgnoreCase("iron")){
if(ironcooldown.containsKey(p.getUniqueId())){
//Do check if they are still on cooldown
//... if so, return here
}
ironcooldown.put(p.getUniqueId(), System.currentTimeMillis());
givePlayerKit(Kits.IRON, p); //made up function that takes Enum & Player
}
//etc
似乎你还没有将播放器添加到HashMap中,所以它将返回false,所以你可能想让你的HashMaps公开和静态,并在你的 PlayerJoinEvent
添加
Kit.ironcooldown.put(p.getUniqueId(), 0);
或者,如果你没有PlayerJoinEvent,或者你不想这样做,你可以在args前加上
if (!ironcooldown.containsKey(p.getUniqueId()) {
ironcooldown.put(p.getUniqueId(), 0);
}
对Dprotectcooldown做同样的处理。
希望这能帮到你