Skip to content

Commit 4cc82db

Browse files
committed
feat: 添加次数性物品系统并重构配置管理架构
- 实现多文件夹配置架构,支持自动热重载和线程安全访问 - 新增次数性物品系统,支持四种消耗模式(固定数量、概率、区间随机、概率区间随机) - 重构配置管理系统,分离主配置与物品配置 - 添加自动迁移工具,兼容旧版本配置 - 完善调试信息和多语言支持
1 parent d03c457 commit 4cc82db

10 files changed

Lines changed: 297 additions & 10 deletions

File tree

docs/commands.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
- bellcommand.list - 允许查看物品列表
1313

1414
## 命令物品配置
15-
在 config.yml 中配置命令物品:
15+
从版本 1.4.0 开始,命令物品定义已移至独立的文件夹(默认为 `Default_config/`)中的 `commands.yml` 文件。
1616

17+
### 基础结构
1718
```yaml
1819
items:
1920
example_item:
@@ -23,17 +24,40 @@ items:
2324
- "&7右键点击使用"
2425
permission: "example.use"
2526
cooldown: 10
27+
# 自动给予配置
28+
auto-give:
29+
join: true
30+
respawn: true
31+
# 自动清理配置
32+
auto-cleanup:
33+
enabled: true
34+
delay: 30
2635
commands:
2736
right-click:
2837
1:
2938
command: "say 你好,%player%!"
3039
as-console: false
31-
shift-right-click:
32-
1:
33-
command: "gamemode creative"
34-
as-console: true
3540
```
3641
42+
### 次数性物品 (Consumable)
43+
从版本 1.4.0-alpha-3 开始,支持配置物品的使用次数和消耗逻辑。
44+
45+
```yaml
46+
consumable:
47+
enabled: true
48+
mode: "COUNT" # 消耗模式: COUNT, PROBABILITY, RANGE, PROBABILITY_RANGE
49+
amount: 1 # 消耗数量 (适用于 COUNT 和 PROBABILITY 模式)
50+
probability: 0.5 # 消耗概率 (适用于 PROBABILITY 和 PROBABILITY_RANGE 模式)
51+
min-amount: 1 # 最小消耗数量 (适用于 RANGE 和 PROBABILITY_RANGE 模式)
52+
max-amount: 3 # 最大消耗数量 (适用于 RANGE 和 PROBABILITY_RANGE 模式)
53+
```
54+
55+
#### 模式详解:
56+
- **COUNT**: 每次执行命令固定消耗 `amount` 个物品。
57+
- **PROBABILITY**: 每次执行命令有 `probability` 的概率消耗 `amount` 个物品。
58+
- **RANGE**: 每次执行命令随机消耗 `min-amount` 到 `max-amount` 之间的物品数量。
59+
- **PROBABILITY_RANGE**: 每次执行命令有 `probability` 的概率,随机消耗 `min-amount` 到 `max-amount` 之间的物品数量。
60+
3761
### 命令类型
3862
- right-click - 右键点击
3963
- left-click - 左键点击

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ BellCommand 是一个功能强大的 Minecraft 命令物品插件,支持自定
1010
- 🔒 完善的权限控制
1111
- ⚡ 性能优化
1212
- 📱 基岩版玩家支持
13+
- 📦 **v1.4.0-alpha-3**: 多文件夹配置架构与次数性物品系统
1314

1415
## 快速开始
1516

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# BellCommand v1.4.0-alpha-3 开发与功能文档
2+
3+
本文档详细说明了 BellCommand 在 v1.4.0-alpha-3 版本中的重大重构、新功能及其配置方式。
4+
5+
---
6+
7+
## 1. 配置管理系统重构 (Configuration System Refactoring)
8+
9+
为了提高插件的可维护性和扩展性,我们彻底重构了配置管理系统。
10+
11+
### 1.1 多文件夹架构设计
12+
- **主配置文件 (`config.yml`)**: 仅保留全局系统参数(如调试模式、检查更新、语言设置等)。
13+
- **模块化物品配置**:
14+
- 默认采用 `Default_config/` 文件夹存储物品定义。
15+
- 支持自定义文件夹,文件夹内必须包含 `commands.yml`
16+
- 插件会自动扫描并合并所有符合规则的配置文件。
17+
18+
### 1.2 线程安全与并发控制
19+
- 引入了 `java.util.concurrent.locks.ReentrantReadWriteLock`
20+
- **读锁**: 在玩家触发命令、读取配置时使用,支持多线程并发读取。
21+
- **写锁**: 在执行 `/bc reload` 或文件热重载时使用,确保重载期间配置的一致性,防止脏读。
22+
23+
### 1.3 实时热重载机制
24+
- 利用 `java.nio.file.WatchService` 监控配置文件。
25+
- 玩家直接修改磁盘上的 `.yml` 文件并保存后,插件会立即感知并自动执行热重载逻辑,无需手动输入命令。
26+
27+
### 1.4 平滑迁移策略
28+
- **检测旧配置**: 自动识别 1.3.x 及以前版本的 `config.yml` 结构。
29+
- **自动备份**: 迁移前自动创建 `config.backup.yml`
30+
- **配置分发**: 将原全局 `auto-give``auto-cleanup` 设置自动分发到每个迁移后的物品项中,确保用户原有功能不受影响。
31+
- **存储位置**: 迁移后的旧物品配置将存储在 `Legacy_config/commands.yml` 中。
32+
33+
---
34+
35+
## 2. 次数性物品系统 (Consumable Item System)
36+
37+
新增“次数性物品”功能,允许开发者精细控制物品的使用寿命。
38+
39+
### 2.1 消耗模式 (Modes)
40+
41+
| 模式 | 描述 | 关键配置项 |
42+
| :--- | :--- | :--- |
43+
| `COUNT` | 固定数量消耗 | `amount` |
44+
| `PROBABILITY` | 概率消耗固定数量 | `probability`, `amount` |
45+
| `RANGE` | 区间随机数量消耗 | `min-amount`, `max-amount` |
46+
| `PROBABILITY_RANGE` | 概率触发后的区间随机消耗 | `probability`, `min-amount`, `max-amount` |
47+
48+
### 2.2 配置示例
49+
50+
```yaml
51+
items:
52+
super_food:
53+
item-id: COOKED_BEEF
54+
name: "&6超级牛肉"
55+
consumable:
56+
enabled: true
57+
mode: "PROBABILITY_RANGE"
58+
probability: 0.5 # 50% 概率触发消耗
59+
min-amount: 1 # 最少消耗 1 个
60+
max-amount: 3 # 最多消耗 3 个
61+
commands:
62+
right-click:
63+
1:
64+
command: "heal %player%"
65+
as-console: true
66+
```
67+
68+
---
69+
70+
## 3. 技术规范与环境 (Technical Specifications)
71+
72+
- **开发环境**: Windows Server 2022 / PowerShell。
73+
- **构建工具**: Maven 3.9.12。
74+
- **Java 版本**: JDK 21。
75+
- **编码规范**: 全面支持 UTF-8 编码,解决多语言环境下的乱码问题。
76+
- **国际化**: 所有系统日志(迁移成功、热重载提示等)均已集成到 `LanguageManager`。
77+
78+
---
79+
80+
## 4. 知识库总结 (Knowledge Base)
81+
82+
1. **资源释放**: 使用 `saveResource("Default_config/commands.yml", false)` 确保插件首次运行时自动创建示例模板。
83+
2. **消耗逻辑**: 消耗判定在命令执行后立即执行。若 `ItemStack.getAmount()` 减去消耗量小于等于 0,则该物品堆栈将被销毁。
84+
3. **性能监控**: 集成了 `PerformanceMonitor`,对配置加载和热重载过程进行毫秒级耗时监控。

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cn.ningmo</groupId>
88
<artifactId>bellcommand</artifactId>
9-
<version>1.4.0-alpha-2</version>
9+
<version>1.4.0-alpha-3</version>
1010
<packaging>jar</packaging>
1111

1212
<name>BellCommand</name>

src/main/java/cn/ningmo/bellcommand/item/CommandItem.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@ public class CommandItem {
2020
private final Map<String, List<CommandEntry>> commands;
2121
private final AutoGiveConfig autoGive;
2222
private final AutoCleanupConfig autoCleanup;
23+
private final ConsumableConfig consumable;
24+
25+
public static class ConsumableConfig {
26+
private final boolean enabled;
27+
private final String mode; // COUNT, PROBABILITY, RANGE, PROBABILITY_RANGE
28+
private final int amount;
29+
private final double probability;
30+
private final int minAmount;
31+
private final int maxAmount;
32+
33+
public ConsumableConfig(ConfigurationSection config) {
34+
if (config == null) {
35+
this.enabled = false;
36+
this.mode = "COUNT";
37+
this.amount = 1;
38+
this.probability = 1.0;
39+
this.minAmount = 1;
40+
this.maxAmount = 1;
41+
} else {
42+
this.enabled = config.getBoolean("enabled", false);
43+
this.mode = config.getString("mode", "COUNT").toUpperCase();
44+
this.amount = config.getInt("amount", 1);
45+
this.probability = config.getDouble("probability", 1.0);
46+
this.minAmount = config.getInt("min-amount", 1);
47+
this.maxAmount = config.getInt("max-amount", 1);
48+
}
49+
}
50+
51+
public boolean isEnabled() { return enabled; }
52+
public String getMode() { return mode; }
53+
public int getAmount() { return amount; }
54+
public double getProbability() { return probability; }
55+
public int getMinAmount() { return minAmount; }
56+
public int getMaxAmount() { return maxAmount; }
57+
}
2358

2459
public static class AutoGiveConfig {
2560
private final boolean join;
@@ -103,6 +138,7 @@ public CommandItem(String id, ConfigurationSection config) {
103138
this.commands = new HashMap<>();
104139
this.autoGive = new AutoGiveConfig(config.getConfigurationSection("auto-give"));
105140
this.autoCleanup = new AutoCleanupConfig(config.getConfigurationSection("auto-cleanup"));
141+
this.consumable = new ConsumableConfig(config.getConfigurationSection("consumable"));
106142

107143
// 加载命令
108144
ConfigurationSection commandsSection = config.getConfigurationSection("commands");
@@ -160,6 +196,10 @@ public AutoCleanupConfig getAutoCleanup() {
160196
return autoCleanup;
161197
}
162198

199+
public ConsumableConfig getConsumable() {
200+
return consumable;
201+
}
202+
163203
public ItemStack createItemStack() {
164204
ItemStack item = new ItemStack(material);
165205
ItemMeta meta = item.getItemMeta();

src/main/java/cn/ningmo/bellcommand/listener/ItemClickListener.java

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,66 @@ public void onPlayerInteract(PlayerInteractEvent event) {
166166
}
167167

168168
// 执行命令
169-
itemManager.executeCommands(player, commandItem, commandType);
170-
event.setCancelled(true);
169+
if (!commandItem.getCommands(commandType).isEmpty()) {
170+
itemManager.executeCommands(player, commandItem, commandType);
171+
handleConsumable(player, item, commandItem);
172+
event.setCancelled(true);
173+
}
174+
}
175+
176+
private void handleConsumable(Player player, ItemStack item, CommandItem commandItem) {
177+
CommandItem.ConsumableConfig config = commandItem.getConsumable();
178+
if (!config.isEnabled()) return;
179+
180+
int toRemove = 0;
181+
String mode = config.getMode();
182+
java.util.Random random = new java.util.Random();
183+
184+
switch (mode) {
185+
case "COUNT":
186+
toRemove = config.getAmount();
187+
break;
188+
case "PROBABILITY":
189+
if (random.nextDouble() <= config.getProbability()) {
190+
toRemove = config.getAmount();
191+
}
192+
break;
193+
case "RANGE":
194+
int min = config.getMinAmount();
195+
int max = config.getMaxAmount();
196+
if (max >= min) {
197+
toRemove = random.nextInt(max - min + 1) + min;
198+
}
199+
break;
200+
case "PROBABILITY_RANGE":
201+
if (random.nextDouble() <= config.getProbability()) {
202+
int pMin = config.getMinAmount();
203+
int pMax = config.getMaxAmount();
204+
if (pMax >= pMin) {
205+
toRemove = random.nextInt(pMax - pMin + 1) + pMin;
206+
}
207+
}
208+
break;
209+
}
210+
211+
if (toRemove > 0) {
212+
int currentAmount = item.getAmount();
213+
if (currentAmount > toRemove) {
214+
item.setAmount(currentAmount - toRemove);
215+
} else {
216+
item.setAmount(0);
217+
}
218+
219+
if (plugin.isDebugEnabled()) {
220+
Map<String, String> placeholders = new HashMap<>();
221+
placeholders.put("player", player.getName());
222+
placeholders.put("item", commandItem.getId());
223+
placeholders.put("amount", String.valueOf(toRemove));
224+
plugin.getLogger().info(ColorUtils.translateConsoleColors(
225+
plugin.getLanguageManager().getMessage("messages.debug.consumable.consumed", placeholders)
226+
));
227+
}
228+
}
171229
}
172230

173231
private String determineCommandType(PlayerInteractEvent event, Player player) {

src/main/resources/Default_config/commands.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,79 @@ items:
100100
1:
101101
command: "backpack"
102102
as-console: false
103+
104+
# 示例物品 - 次数性物品 (消耗模式: 固定数量)
105+
consumable_count:
106+
item-id: BREAD
107+
name: "&a消耗性干粮 (&aConsumable Food)"
108+
lore:
109+
- "&7右键点击: &f获得饱和效果"
110+
- "&c使用后消耗 1 个物品"
111+
cooldown: 5
112+
consumable:
113+
enabled: true
114+
mode: "COUNT"
115+
amount: 1
116+
commands:
117+
right-click:
118+
1:
119+
command: "effect give %player% saturation 1 1"
120+
as-console: true
121+
122+
# 示例物品 - 次数性物品 (消耗模式: 概率触发)
123+
consumable_prob:
124+
item-id: FIREWORK_ROCKET
125+
name: "&d概率型烟花 (&dProbability Rocket)"
126+
lore:
127+
- "&7右键点击: &f发射烟花"
128+
- "&e50% 概率消耗 1 个物品"
129+
cooldown: 2
130+
consumable:
131+
enabled: true
132+
mode: "PROBABILITY"
133+
probability: 0.5
134+
amount: 1
135+
commands:
136+
right-click:
137+
1:
138+
command: "summon firework_rocket ~ ~1 ~"
139+
as-console: true
140+
141+
# 示例物品 - 次数性物品 (消耗模式: 区间随机)
142+
consumable_range:
143+
item-id: GOLD_NUGGET
144+
name: "&e随机金块 (&eRandom Gold)"
145+
lore:
146+
- "&7右键点击: &f获得金币"
147+
- "&c使用后消耗 1-3 个物品"
148+
cooldown: 1
149+
consumable:
150+
enabled: true
151+
mode: "RANGE"
152+
min-amount: 1
153+
max-amount: 3
154+
commands:
155+
right-click:
156+
1:
157+
command: "eco give %player% 10"
158+
as-console: true
159+
160+
# 示例物品 - 次数性物品 (消耗模式: 概率区间随机)
161+
consumable_prob_range:
162+
item-id: DIAMOND
163+
name: "&b幸运钻石 (&bLucky Diamond)"
164+
lore:
165+
- "&7右键点击: &f尝试获得双倍奖励"
166+
- "&e30% 概率消耗 1-2 个物品"
167+
cooldown: 3
168+
consumable:
169+
enabled: true
170+
mode: "PROBABILITY_RANGE"
171+
probability: 0.3
172+
min-amount: 1
173+
max-amount: 2
174+
commands:
175+
right-click:
176+
1:
177+
command: "eco give %player% 50"
178+
as-console: true

src/main/resources/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# BellCommand 配置文件 (BellCommand Configuration File)
2-
# 版本: 1.4.0-alpha-2 (Version: 1.4.0-alpha-2)
2+
# 版本: 1.4.0-alpha-3 (Version: 1.4.0-alpha-3)
33
# 作者: 柠枺 (Author: Ningmo)
44

55
# 配置文件说明 (Configuration File Instructions):

src/main/resources/lang/messages.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ messages:
139139
action-cooldown-active: "§e[调试] 玩家 %player% 的物品 %item% 的动作 %type% 正在冷却中 (剩余: %.1f秒)"
140140
player-offline: "§e[调试] 玩家已离线,取消执行延迟命令"
141141
delayed-command-result: "§e[调试] 延迟命令执行结果: §6%result% (延迟: %delay%秒)"
142+
143+
# 消耗品调试
144+
consumable:
145+
consumed: "§e[调试] 玩家 §6%player% §e使用的物品 §6%item% §e已消耗 §6%amount% §e个"
142146

143147
# 更新源调试信息
144148
update:

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: BellCommand
2-
version: '1.4.0-alpha-2'
2+
version: '1.4.0-alpha-3'
33
main: cn.ningmo.bellcommand.BellCommand
44
api: '1.13'
55
api-version: '1.13'

0 commit comments

Comments
 (0)