mirror of
https://github.com/CarmJos/ScriptItems
synced 2024-09-19 13:25:50 +00:00
添加冷却时间配置
This commit is contained in:
parent
d21c133654
commit
6a7234a85d
@ -1,7 +1,6 @@
|
||||
package cc.carm.plugin.commanditem.configuration;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
|
||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValueMap;
|
||||
|
||||
public class PluginConfig {
|
||||
|
||||
@ -29,5 +28,14 @@ public class PluginConfig {
|
||||
|
||||
}
|
||||
|
||||
public static class CoolDown {
|
||||
|
||||
public static ConfigValue<Boolean> ENABLE = new ConfigValue<>("cooldown.enable", Boolean.class, false);
|
||||
|
||||
public static ConfigValue<Long> TIME = new ConfigValue<>("cooldown.time", Long.class, 3000L);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cc.carm.plugin.commanditem.item;
|
||||
|
||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@ -42,7 +43,7 @@ public class ItemStackConfig {
|
||||
if (original != null) return original.clone();
|
||||
if (material == null) return null;
|
||||
ItemStackFactory factory = new ItemStackFactory(material, amount);
|
||||
if (displayName != null) factory.setDisplayName(displayName);
|
||||
if (displayName != null) factory.setDisplayName(ColorParser.parse(displayName));
|
||||
if (lore != null && !lore.isEmpty()) factory.setLore(lore);
|
||||
return factory.toItemStack();
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cc.carm.plugin.commanditem.listener;
|
||||
|
||||
import cc.carm.plugin.commanditem.CommandItemAPI;
|
||||
import cc.carm.plugin.commanditem.configuration.PluginConfig;
|
||||
import cc.carm.plugin.commanditem.item.CommandItem;
|
||||
import cc.carm.plugin.commanditem.item.ItemActionGroup;
|
||||
import cc.carm.plugin.commanditem.item.ItemRestrictions;
|
||||
@ -14,12 +15,16 @@ import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityPickupItemEvent;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ItemListener implements Listener {
|
||||
|
||||
private final HashMap<UUID, Long> clickTime = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 监听玩家点击,并执行物品对应的操作。
|
||||
@ -33,8 +38,13 @@ public class ItemListener implements Listener {
|
||||
ItemStack item = event.getPlayer().getInventory().getItemInMainHand();
|
||||
CommandItem commandItem = CommandItemAPI.getItemsManager().parseCommandItem(item);
|
||||
if (commandItem == null) return;
|
||||
event.setCancelled(true); // 阻止事件执行
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (!isClickable(player.getUniqueId())) {
|
||||
// TODO 给玩家发消息告诉他还在冷却
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandItem.getConfiguration().checkRestrictions() != ItemRestrictions.CheckResult.AVAILABLE) {
|
||||
// TODO 给玩家发消息告诉他还不能用
|
||||
@ -42,11 +52,9 @@ public class ItemListener implements Listener {
|
||||
}
|
||||
|
||||
ItemActionGroup actions = commandItem.getConfiguration().getPlayerActions(player);
|
||||
|
||||
if (actions == null) return;
|
||||
|
||||
actions.execute(player);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,5 +100,20 @@ public class ItemListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onLeave(PlayerQuitEvent event) {
|
||||
this.clickTime.remove(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
public void updateTime(UUID uuid) {
|
||||
this.clickTime.put(uuid, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public boolean isClickable(UUID uuid) {
|
||||
return !PluginConfig.CoolDown.ENABLE.get()
|
||||
|| !this.clickTime.containsKey(uuid)
|
||||
|| System.currentTimeMillis() - this.clickTime.get(uuid) > PluginConfig.CoolDown.TIME.get();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -15,6 +15,14 @@ metrics: true
|
||||
# 检查更新为异步操作,绝不会影响性能与使用体验。
|
||||
check-update: true
|
||||
|
||||
# 物品使用冷却,避免短时间重复使用物品,也避免网络延迟而导致物品被错误使用而对玩家造成的损失。
|
||||
# 强烈建议开启,且建议设置为 2000毫秒 以上。(1s = 1000ms)
|
||||
cooldown:
|
||||
#是否启用冷却功能
|
||||
enable: true
|
||||
# 冷却的事件,单位为毫秒 1秒 = 1000毫秒 = 20ticks
|
||||
time: 3000
|
||||
|
||||
log-storage:
|
||||
# 是否启用日志记录存储
|
||||
# 可用于追踪物品的发放、领取情况与执行记录。
|
||||
|
Loading…
Reference in New Issue
Block a user