1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

chore(item): 添加序列化方法

This commit is contained in:
Carm Jos 2022-06-18 00:16:02 +08:00
parent 7a06b39b31
commit b6bd4beda0
27 changed files with 1219 additions and 1160 deletions

View File

@ -14,6 +14,7 @@ assignees: ''
### **问题来源** ### **问题来源**
描述一下通过哪些操作才发现的问题,如: 描述一下通过哪些操作才发现的问题,如:
1. 使用了 ... 1. 使用了 ...
2. 输入了 ... 2. 输入了 ...
3. 出现了报错 ... 3. 出现了报错 ...
@ -32,7 +33,6 @@ assignees: ''
- Java版本: `JDK11` / `OPENJDK8` / `JRE8` / `...` - Java版本: `JDK11` / `OPENJDK8` / `JRE8` / `...`
- 服务端版本: 请在后台输入 `version` 并复制相关输出。 - 服务端版本: 请在后台输入 `version` 并复制相关输出。
### **其他补充** ### **其他补充**
如有其他补充,可以在这里描述。 如有其他补充,可以在这里描述。

View File

@ -8,13 +8,17 @@ assignees: ''
--- ---
### **功能简述** ### **功能简述**
简单的描述一下你想要的功能 简单的描述一下你想要的功能
### **需求来源** ### **需求来源**
简单的描述一下为什么需要这个功能。 简单的描述一下为什么需要这个功能。
### **功能参考**(可选) ### **功能参考**(可选)
如果有相关功能的参考,如文本、截图,请提供给我们。 如果有相关功能的参考,如文本、截图,请提供给我们。
### **附加内容** ### **附加内容**
如果有什么小细节需要重点注意,请在这里告诉我们。 如果有什么小细节需要重点注意,请在这里告诉我们。

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -1,4 +1,3 @@
package cc.carm.lib.easyplugin.command; package cc.carm.lib.easyplugin.command;
import org.bukkit.command.Command; import org.bukkit.command.Command;

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -156,14 +156,16 @@ public class GUI {
} }
public void openGUI(Player player) { public void openGUI(Player player) {
if (this.type == GUIType.CANCEL) { throw new IllegalStateException("被取消或不存在的GUI"); } if (this.type == GUIType.CANCEL) {
throw new IllegalStateException("被取消或不存在的GUI");
}
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name); Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR))); IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay())); getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
GUI previous = getOpenedGUI(player); GUI previous = getOpenedGUI(player);
if(previous != null){ if (previous != null) {
previous.listener.close(player); previous.listener.close(player);
} }

View File

@ -77,7 +77,7 @@ public class GUIListener implements Listener {
} }
protected void close(Player p){ protected void close(Player p) {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
getCurrentGUI().listener = null; getCurrentGUI().listener = null;
GUI.removeOpenedGUI(p); GUI.removeOpenedGUI(p);

View File

@ -3,18 +3,39 @@ package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.gui.GUIItem; import cc.carm.lib.easyplugin.gui.GUIItem;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class GUIActionConfiguration { public class GUIActionConfiguration {
public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
@Nullable ClickType clickType,
@Nullable String actionContent) {
return new GUIActionConfiguration(actionType, clickType, actionContent);
}
@Nullable ClickType clickType; public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
final @NotNull GUIActionType actionType; @Nullable String actionContent) {
final @Nullable String actionContent; return of(actionType, null, actionContent);
}
public GUIActionConfiguration(@Nullable ClickType clickType, public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
@NotNull GUIActionType actionType, @Nullable ClickType clickType) {
return of(actionType, clickType, null);
}
public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType) {
return of(actionType, null, null);
}
protected final @NotNull GUIActionType actionType;
protected final @Nullable ClickType clickType;
protected final @Nullable String actionContent;
public GUIActionConfiguration(@NotNull GUIActionType actionType,
@Nullable ClickType clickType,
@Nullable String actionContent) { @Nullable String actionContent) {
this.clickType = clickType; this.clickType = clickType;
this.actionType = actionType; this.actionType = actionType;
@ -50,4 +71,36 @@ public class GUIActionConfiguration {
}; };
} }
@Nullable
@Contract("null->null")
public static GUIActionConfiguration deserialize(@Nullable String actionString) {
if (actionString == null) return null;
int prefixStart = actionString.indexOf("[");
int prefixEnd = actionString.indexOf("]");
if (prefixStart < 0 || prefixEnd < 0) return null;
String prefix = actionString.substring(prefixStart + 1, prefixEnd);
ClickType clickType = null;
GUIActionType actionType;
if (prefix.contains(":")) {
String[] args = prefix.split(":");
clickType = GUIConfiguration.readClickType(args[0]);
actionType = GUIActionType.readActionType(args[1]);
} else {
actionType = GUIActionType.readActionType(prefix);
}
if (actionType == null) return null;
String content = actionString.substring(prefixEnd + 1).trim();
return of(actionType, clickType, content);
}
public @NotNull String serialize() {
String prefix = "[" + getActionType().name() + (getClickType() == null ? "" : ":" + getClickType().name()) + "]";
String content = getActionContent() == null ? "" : " " + getActionContent();
return prefix + content;
}
} }

View File

@ -7,30 +7,27 @@ import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.*;
import java.util.Collections; import java.util.stream.Collectors;
import java.util.List;
import java.util.Optional;
public class GUIItemConfiguration { public class GUIItemConfiguration {
Material material; @NotNull Material type;
int data; int data;
String name; @Nullable String name;
@NotNull List<String> lore; @NotNull List<String> lore;
@NotNull List<Integer> slots; @NotNull List<Integer> slots;
@NotNull List<GUIActionConfiguration> actions; @NotNull List<GUIActionConfiguration> actions;
public GUIItemConfiguration(Material material, int data, public GUIItemConfiguration(@NotNull Material type, int data,
String name, @NotNull List<String> lore, @Nullable String name, @NotNull List<String> lore,
@NotNull List<GUIActionConfiguration> actions, @NotNull List<GUIActionConfiguration> actions,
@NotNull List<Integer> slots) { @NotNull List<Integer> slots) {
this.material = material; this.type = type;
this.data = data; this.data = data;
this.name = name; this.name = name;
this.lore = lore; this.lore = lore;
@ -39,7 +36,7 @@ public class GUIItemConfiguration {
} }
public void setupItems(Player player, GUI gui) { public void setupItems(Player player, GUI gui) {
ItemStackFactory icon = new ItemStackFactory(this.material); ItemStackFactory icon = new ItemStackFactory(this.type);
icon.setDurability(this.data); icon.setDurability(this.data);
if (this.name != null) icon.setDisplayName(this.name); if (this.name != null) icon.setDisplayName(this.name);
icon.setLore(MessageUtils.setPlaceholders(player, this.lore)); icon.setLore(MessageUtils.setPlaceholders(player, this.lore));
@ -49,10 +46,29 @@ public class GUIItemConfiguration {
this.slots.forEach(slot -> gui.setItem(slot, item)); this.slots.forEach(slot -> gui.setItem(slot, item));
} }
public Map<String, Object> serialize() {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("type", this.type.name());
if (this.name != null) map.put("name", this.name);
if (this.data != 0) map.put("data", this.data);
if (!this.lore.isEmpty()) map.put("lore", this.lore);
if (this.slots.size() > 1) {
map.put("slots", this.slots);
} else if (slots.size() == 1) {
map.put("slots", this.slots.get(0));
}
if (!this.actions.isEmpty()) {
map.put("actions", this.actions.stream().map(GUIActionConfiguration::serialize).collect(Collectors.toList()));
}
return map;
}
@Nullable @Nullable
public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) { public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) {
if (itemSection == null) return null; if (itemSection == null) return null;
Material material = Optional.ofNullable(Material.matchMaterial(itemSection.getString("material", "STONE"))).orElse(Material.STONE); String material = Optional.ofNullable(itemSection.getString("type")).orElse("STONE");
Material type = Optional.ofNullable(Material.matchMaterial(material)).orElse(Material.STONE);
int data = itemSection.getInt("data", 0); int data = itemSection.getInt("data", 0);
String name = itemSection.getString("name"); String name = itemSection.getString("name");
List<String> lore = itemSection.getStringList("lore"); List<String> lore = itemSection.getStringList("lore");
@ -63,27 +79,13 @@ public class GUIItemConfiguration {
List<String> actionsString = itemSection.getStringList("actions"); List<String> actionsString = itemSection.getStringList("actions");
List<GUIActionConfiguration> actions = new ArrayList<>(); List<GUIActionConfiguration> actions = new ArrayList<>();
for (String actionString : actionsString) { for (String actionString : actionsString) {
int prefixStart = actionString.indexOf("["); GUIActionConfiguration action = GUIActionConfiguration.deserialize(actionString);
int prefixEnd = actionString.indexOf("]"); if (action == null) continue;
if (prefixStart < 0 || prefixEnd < 0) continue; actions.add(action);
String prefix = actionString.substring(prefixStart + 1, prefixEnd);
ClickType clickType = null;
GUIActionType actionType;
if (prefix.contains(":")) {
String[] args = prefix.split(":");
clickType = GUIConfiguration.readClickType(args[0]);
actionType = GUIActionType.readActionType(args[1]);
} else {
actionType = GUIActionType.readActionType(prefix);
}
if (actionType == null) continue;
actions.add(new GUIActionConfiguration(clickType, actionType, actionString.substring(prefixEnd + 1).trim()));
} }
return new GUIItemConfiguration( return new GUIItemConfiguration(
material, data, name, lore, actions, type, data, name, lore, actions,
slots.size() > 0 ? slots : Collections.singletonList(slot) slots.size() > 0 ? slots : Collections.singletonList(slot)
); );
} }

View File

@ -1,4 +1,3 @@
import cc.carm.lib.easyplugin.gui.configuration.GUIActionType; import cc.carm.lib.easyplugin.gui.configuration.GUIActionType;
import cc.carm.lib.easyplugin.gui.configuration.GUIConfiguration; import cc.carm.lib.easyplugin.gui.configuration.GUIConfiguration;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.6</version> <version>1.4.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>1.4.6</version> <version>1.4.7</version>
<modules> <modules>
<module>base/main</module> <module>base/main</module>