Skip to content

Commit 368a701

Browse files
[1.13.10.0]新增DataHolder接口,Menu和Icon默认实现此接口
1 parent 131c11f commit 368a701

File tree

4 files changed

+106
-4
lines changed

4 files changed

+106
-4
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
java.sourceCompatibility = JavaVersion.VERSION_1_8
22
java.targetCompatibility = JavaVersion.VERSION_1_8
33
rootProject.group = "com.crypticlib"
4-
rootProject.version = "1.13.9.0"
4+
rootProject.version = "1.13.10.0"
55
//全项目重构时更新大版本号
66
//添加模块或有较大更改时更新次版本号
77
//有API变动(新增/删除/更改声明)时更新修订号

module/bukkit/ui/src/main/java/crypticlib/ui/display/Icon.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package crypticlib.ui.display;
22

3+
import crypticlib.DataHolder;
34
import crypticlib.chat.BukkitTextProcessor;
45
import crypticlib.util.ItemHelper;
56
import org.bukkit.Bukkit;
@@ -11,18 +12,21 @@
1112
import org.jetbrains.annotations.Nullable;
1213

1314
import java.util.List;
15+
import java.util.Map;
1416
import java.util.Optional;
1517
import java.util.UUID;
18+
import java.util.concurrent.ConcurrentHashMap;
1619
import java.util.function.Consumer;
1720

18-
public class Icon {
21+
public class Icon implements DataHolder {
1922

2023
protected ItemStack display;
2124
protected Consumer<InventoryClickEvent> clickAction;
2225
/**
2326
* 用于某些情况下图标需要解析玩家变量时使用,一般为图标所属页面的玩家,默认在{@link crypticlib.ui.menu.Menu#preprocessIconWhenDraw}前赋值
2427
*/
2528
private @Nullable UUID parsePlayerId;
29+
protected final Map<String, Object> dataMap = new ConcurrentHashMap<>();
2630

2731
public Icon(@NotNull IconDisplay iconDisplay) {
2832
this.display = iconDisplay.display();
@@ -143,4 +147,33 @@ public String parseIconText(String originText) {
143147
return BukkitTextProcessor.color(BukkitTextProcessor.placeholder(iconParsePlayer, originText));
144148
}
145149

150+
@Override
151+
public Map<String, Object> allData() {
152+
return dataMap;
153+
}
154+
155+
@Override
156+
public void setAllData(Map<String, Object> data) {
157+
this.dataMap.clear();
158+
this.dataMap.putAll(data);
159+
}
160+
161+
@Override
162+
public Optional<Object> getData(String key) {
163+
if (dataMap.containsKey(key)) {
164+
return Optional.ofNullable(dataMap.get(key));
165+
}
166+
return Optional.empty();
167+
}
168+
169+
@Override
170+
public Object putData(String key, Object value) {
171+
return dataMap.put(key, value);
172+
}
173+
174+
@Override
175+
public void clearData() {
176+
dataMap.clear();
177+
}
178+
146179
}

module/bukkit/ui/src/main/java/crypticlib/ui/menu/Menu.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package crypticlib.ui.menu;
22

33
import crypticlib.CrypticLibBukkit;
4+
import crypticlib.DataHolder;
45
import crypticlib.chat.BukkitTextProcessor;
56
import crypticlib.scheduler.task.TaskWrapper;
67
import crypticlib.ui.display.Icon;
@@ -22,15 +23,17 @@
2223
import org.jetbrains.annotations.Nullable;
2324

2425
import java.util.*;
26+
import java.util.concurrent.ConcurrentHashMap;
2527
import java.util.function.Supplier;
2628

27-
public class Menu implements InventoryHolder {
29+
public class Menu implements InventoryHolder, DataHolder {
2830

2931
protected final Map<Integer, Icon> slotMap;
3032
protected final UUID playerId;
3133
protected MenuDisplay display;
3234
protected final Map<Character, List<Integer>> layoutSlotMap;
33-
protected Inventory inventoryCache;
35+
protected @Nullable Inventory inventoryCache;
36+
protected final Map<String, Object> dataMap = new ConcurrentHashMap<>();
3437

3538
public Menu(@NotNull Player player) {
3639
this(player, new MenuDisplay());
@@ -390,4 +393,33 @@ public Inventory inventoryCache() {
390393
return inventoryCache;
391394
}
392395

396+
@Override
397+
public Map<String, Object> allData() {
398+
return dataMap;
399+
}
400+
401+
@Override
402+
public void setAllData(Map<String, Object> data) {
403+
this.dataMap.clear();
404+
this.dataMap.putAll(data);
405+
}
406+
407+
@Override
408+
public Optional<Object> getData(String key) {
409+
if (dataMap.containsKey(key)) {
410+
return Optional.ofNullable(dataMap.get(key));
411+
}
412+
return Optional.empty();
413+
}
414+
415+
@Override
416+
public Object putData(String key, Object value) {
417+
return dataMap.put(key, value);
418+
}
419+
420+
@Override
421+
public void clearData() {
422+
dataMap.clear();
423+
}
424+
393425
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package crypticlib;
2+
3+
import java.util.Map;
4+
import java.util.Optional;
5+
6+
public interface DataHolder {
7+
8+
/**
9+
* 获取当前存储的所有数据
10+
* @return
11+
*/
12+
Map<String, Object> allData();
13+
14+
/**
15+
* 设置当前存储的所有数据
16+
* @param data
17+
*/
18+
void setAllData(Map<String, Object> data);
19+
20+
/**
21+
* 获取某个键存储的数据
22+
* @param key
23+
* @return
24+
*/
25+
Optional<Object> getData(String key);
26+
27+
/**
28+
* 记录一个数据
29+
* @param key
30+
* @param value
31+
* @return 如果原本存在数据,返回被覆盖的数据
32+
*/
33+
Object putData(String key, Object value);
34+
35+
void clearData();
36+
37+
}

0 commit comments

Comments
 (0)