1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-04 16:48:16 +08:00

feat(gui): GUI原生物品配置读取

This commit is contained in:
2022-09-11 23:25:17 +08:00
parent 7fc0663e89
commit 6863c02611
@@ -2,11 +2,14 @@ package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.gui.GUI; import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.gui.GUIItem; import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.ItemStackFactory; import cc.carm.lib.easyplugin.utils.ItemStackFactory;
import cc.carm.lib.easyplugin.utils.MessageUtils; 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.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -15,6 +18,8 @@ import java.util.stream.Collectors;
public class GUIItemConfiguration { public class GUIItemConfiguration {
@Nullable ItemStack original;
@NotNull Material type; @NotNull Material type;
int amount; int amount;
int data; int data;
@@ -28,6 +33,15 @@ public class GUIItemConfiguration {
@Nullable 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(null, type, amount, data, name, lore, actions, slots);
}
public GUIItemConfiguration(@Nullable ItemStack original,
@NotNull Material type, int amount, int data,
@Nullable String name, @NotNull List<String> lore,
@NotNull List<GUIActionConfiguration> actions,
@NotNull List<Integer> slots) {
this.original = original;
this.type = type; this.type = type;
this.amount = amount; this.amount = amount;
this.data = data; this.data = data;
@@ -38,22 +52,58 @@ public class GUIItemConfiguration {
} }
public void setupItems(Player player, GUI gui) { public void setupItems(Player player, GUI gui) {
ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data); ItemStack itemStack;
if (this.name != null) icon.setDisplayName(this.name); if (original != null) {
icon.setLore(MessageUtils.setPlaceholders(player, this.lore)); ItemStack tmp = original.clone();
ItemMeta originalMeta = original.getItemMeta();
if (originalMeta != null) {
if (originalMeta.hasDisplayName()) {
originalMeta.setDisplayName(parseText(player, originalMeta.getDisplayName()));
}
if (originalMeta.getLore() != null) {
originalMeta.setLore(parseTexts(player, originalMeta.getLore()));
}
GUIItem item = new GUIItem(icon.toItemStack()); }
tmp.setItemMeta(originalMeta);
itemStack = tmp;
} else {
ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data);
if (this.name != null) {
icon.setDisplayName(parseText(player, this.name));
}
if (!this.lore.isEmpty()) {
icon.setLore(parseTexts(player, this.lore));
}
itemStack = icon.toItemStack();
}
GUIItem item = new GUIItem(itemStack);
this.actions.stream().map(GUIActionConfiguration::toClickAction).forEach(item::addClickAction); this.actions.stream().map(GUIActionConfiguration::toClickAction).forEach(item::addClickAction);
this.slots.forEach(slot -> gui.setItem(slot, item)); this.slots.forEach(slot -> gui.setItem(slot, item));
} }
private List<String> parseTexts(Player player, List<String> lore) {
return ColorParser.parse(MessageUtils.setPlaceholders(player, lore));
}
@NotNull
private String parseText(Player player, @NotNull String name) {
return ColorParser.parse(MessageUtils.setPlaceholders(player, name));
}
public @NotNull Map<String, Object> serialize() { public @NotNull Map<String, Object> serialize() {
LinkedHashMap<String, Object> map = new LinkedHashMap<>(); LinkedHashMap<String, Object> map = new LinkedHashMap<>();
if (original != null) map.put("original", original);
else {
map.put("type", this.type.name());
if (this.data != 0) map.put("data", this.data);
}
map.put("type", this.type.name());
if (this.name != null) map.put("name", this.name); if (this.name != null) map.put("name", this.name);
if (this.amount != 1) map.put("amount", this.amount); if (this.amount != 1) map.put("amount", this.amount);
if (this.data != 0) map.put("data", this.data);
if (!this.lore.isEmpty()) map.put("lore", this.lore); if (!this.lore.isEmpty()) map.put("lore", this.lore);
if (this.slots.size() > 1) { if (this.slots.size() > 1) {
map.put("slots", this.slots); map.put("slots", this.slots);
@@ -69,6 +119,10 @@ public class GUIItemConfiguration {
@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;
ItemStack original = null;
if (itemSection.contains("original")) original = itemSection.getItemStack("original");
String material = Optional.ofNullable(itemSection.getString("type")).orElse("STONE"); String material = Optional.ofNullable(itemSection.getString("type")).orElse("STONE");
Material type = Optional.ofNullable(Material.matchMaterial(material)).orElse(Material.STONE); Material type = Optional.ofNullable(Material.matchMaterial(material)).orElse(Material.STONE);
int data = itemSection.getInt("data", 0); int data = itemSection.getInt("data", 0);
@@ -88,7 +142,7 @@ public class GUIItemConfiguration {
} }
return new GUIItemConfiguration( return new GUIItemConfiguration(
type, amount, data, name, lore, actions, original, type, amount, data, name, lore, actions,
slots.size() > 0 ? slots : Collections.singletonList(slot) slots.size() > 0 ? slots : Collections.singletonList(slot)
); );
} }