From 9101db3075e06852e1a77c447cd2422ee50cb8d4 Mon Sep 17 00:00:00 2001 From: YuTian <2953516620@qq.com> Date: Sat, 3 Aug 2024 15:02:51 +0800 Subject: [PATCH] 1.1 --- .gitignore | 1 + .../elementoriginlib/item/OriginItem.java | 80 +++++++++++++++++++ .../item/OriginItemStackBuilder.java | 68 ++++++++++++++++ .../elementoriginlib/item/stat/ItemStat.java | 64 +++++++++++++++ .../elementoriginlib/item/stat/ItemStats.java | 31 +++++++ .../elementoriginlib/item/stat/Mergeable.java | 12 +++ .../elementoriginlib/item/stat/StatData.java | 9 +++ .../item/stat/data/BooleanData.java | 27 +++++++ .../item/stat/data/DoubleData.java | 56 +++++++++++++ .../item/stat/data/IntData.java | 38 +++++++++ .../item/stat/data/StringData.java | 40 ++++++++++ .../item/stat/data/StringListData.java | 41 ++++++++++ .../item/stat/data/UUIDData.java | 24 ++++++ .../item/stat/list/IDStat.java | 11 +++ .../item/stat/type/BooleanStat.java | 29 +++++++ .../item/stat/type/DoubleStat.java | 29 +++++++ .../item/stat/type/IntStat.java | 29 +++++++ .../item/stat/type/StringListStat.java | 43 ++++++++++ .../item/stat/type/StringStat.java | 30 +++++++ .../item/stat/type/UUIDStat.java | 31 +++++++ src/main/resources/plugin.yml | 2 +- 21 files changed, 694 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/OriginItem.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/OriginItemStackBuilder.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStat.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStats.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/Mergeable.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/StatData.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/data/BooleanData.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/data/DoubleData.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/data/IntData.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringData.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringListData.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/data/UUIDData.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/list/IDStat.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/type/BooleanStat.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/type/DoubleStat.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/type/IntStat.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringListStat.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringStat.java create mode 100644 src/main/java/com/io/yutian/elementoriginlib/item/stat/type/UUIDStat.java diff --git a/.gitignore b/.gitignore index 3a27527..9646c41 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/ /target/ /ElementOriginLib.iml +/build/ diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/OriginItem.java b/src/main/java/com/io/yutian/elementoriginlib/item/OriginItem.java new file mode 100644 index 0000000..1aa6e0e --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/OriginItem.java @@ -0,0 +1,80 @@ +package com.io.yutian.elementoriginlib.item; + +import com.io.yutian.elementoriginlib.item.stat.ItemStat; +import com.io.yutian.elementoriginlib.item.stat.StatData; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class OriginItem { + + private ItemStack itemStack; + private NBTItem nbtItem; + private Map stats = new HashMap<>(); + + public OriginItem(ItemStack itemStack) { + this.itemStack = itemStack; + this.nbtItem = new NBTItem(itemStack); + } + + public void setStatData(@NotNull ItemStat stat, @NotNull StatData data) { + this.stats.put(stat, data); + } + + public void removeStatData(@NotNull ItemStat stat) { + this.stats.remove(stat); + } + + public S getStatData(@NotNull ItemStat stat) { + if (!hasStatData(stat)) { + stat.load(this); + } + return (S) this.stats.get(stat); + } + + public boolean hasStatData(@NotNull ItemStat stat) { + if (!this.stats.containsKey(stat)) { + try { + stat.load(this); + } catch (Exception e) { + e.printStackTrace(); + } + } + return this.stats.containsKey(stat); + } + + @NotNull + public OriginItemStackBuilder builder() { + return new OriginItemStackBuilder(this); + } + + @NotNull + public Set getStats() { + return this.stats.keySet(); + } + + public ItemStack getItemStack() { + return itemStack; + } + + public NBTItem getNBTItem() { + return nbtItem; + } + + @Override + public OriginItem clone() { + OriginItem originItem = new OriginItem(itemStack.clone()); + Iterator iterator = this.stats.keySet().iterator(); + while(iterator.hasNext()) { + ItemStat itemStat = (ItemStat) iterator.next(); + originItem.stats.put(itemStat, this.stats.get(itemStat)); + } + return originItem; + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/OriginItemStackBuilder.java b/src/main/java/com/io/yutian/elementoriginlib/item/OriginItemStackBuilder.java new file mode 100644 index 0000000..ae74a78 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/OriginItemStackBuilder.java @@ -0,0 +1,68 @@ +package com.io.yutian.elementoriginlib.item; + +import com.io.yutian.elementoriginlib.item.stat.StatData; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class OriginItemStackBuilder { + + private final OriginItem originItem; + + protected ItemStack itemStack; + protected ItemMeta itemMeta; + + protected NBTItem nbtItem; + + public OriginItemStackBuilder(OriginItem originItem) { + this.originItem = originItem; + this.itemStack = originItem.getItemStack(); + this.itemMeta = itemStack.getItemMeta(); + this.nbtItem = originItem.getNBTItem(); + } + + @NotNull + public ItemStack build() { + buildNBT(); + return itemStack; + } + + @NotNull + public ItemMeta getItemMeta() { + return itemMeta; + } + + private void buildNBT() { + originItem.getStats().forEach(stat -> { + StatData statData = originItem.getStatData(stat); + stat.whenApplied(this, statData); + }); + itemStack = nbtItem.getItemStack(); + itemMeta = itemStack.getItemMeta(); + originItem.getStats().forEach(stat -> { + StatData statData = originItem.getStatData(stat); + stat.applyMeta(this, statData); + }); + itemStack.setItemMeta(itemMeta); + List lores = itemMeta.hasLore() ? itemMeta.getLore() : new ArrayList<>(); + originItem.getStats().forEach(stat -> { + StatData statData = originItem.getStatData(stat); + stat.whenApplyLore(this, statData, lores); + }); + itemStack.setLore(lores); + } + + @NotNull + public NBTItem getNBTItem() { + return nbtItem; + } + + public OriginItem getOriginItem() { + return originItem; + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStat.java new file mode 100644 index 0000000..7f96115 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStat.java @@ -0,0 +1,64 @@ +package com.io.yutian.elementoriginlib.item.stat; + +import com.io.yutian.elementoriginlib.item.OriginItem; +import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public abstract class ItemStat { + + @NotNull + private final String id; + @NotNull + private final String nbtPath; + + public ItemStat(@NotNull String id, @NotNull String path) { + this.id = id; + this.nbtPath = path; + } + + public abstract void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull S statData); + + public void applyMeta(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull S statData) { + } + + public void whenApplyLore(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull S statData, @NotNull List lores) { + } + + @Nullable + public abstract S getLoadedNBT(@NotNull NBTItem nbtItem); + + public void load(@NotNull OriginItem originItem) { + S loadedNBT = getLoadedNBT(originItem.getNBTItem()); + if (loadedNBT != null) { + originItem.setStatData(this, loadedNBT); + } + } + + @NotNull + public String getId() { + return id; + } + + @NotNull + public String getNBTPath() { + return nbtPath; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ItemStat itemStat = (ItemStat) o; + return id.equals(itemStat.id); + } + + @Override + public int hashCode() { + return id.hashCode(); + } +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStats.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStats.java new file mode 100644 index 0000000..9c0c658 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/ItemStats.java @@ -0,0 +1,31 @@ +package com.io.yutian.elementoriginlib.item.stat; + +import com.io.yutian.elementoriginlib.item.stat.list.IDStat; +import com.io.yutian.elementoriginlib.item.stat.type.StringStat; + +import java.util.HashMap; +import java.util.Map; + +public class ItemStats { + + private static Map> itemStats = new HashMap<>(); + + public static final StringStat ID = register(new IDStat()); + + public static I register(I itemStat) { + if (itemStats.containsKey(itemStat.getId())) { + return itemStat; + } + itemStats.put(itemStat.getId(), itemStat); + return itemStat; + } + + public static void unregister(ItemStat itemStat) { + itemStats.remove(itemStat.getId()); + } + + public static ItemStat getItemStat(String id) { + return itemStats.get(id); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/Mergeable.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/Mergeable.java new file mode 100644 index 0000000..a2cc647 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/Mergeable.java @@ -0,0 +1,12 @@ +package com.io.yutian.elementoriginlib.item.stat; + +import org.jetbrains.annotations.NotNull; + +public interface Mergeable extends StatData { + + void merge(S var1); + + @NotNull + S cloneData(); + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/StatData.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/StatData.java new file mode 100644 index 0000000..a5b64fa --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/StatData.java @@ -0,0 +1,9 @@ +package com.io.yutian.elementoriginlib.item.stat; + +public interface StatData { + + default boolean isEmpty() { + return true; + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/BooleanData.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/BooleanData.java new file mode 100644 index 0000000..c32a9e2 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/BooleanData.java @@ -0,0 +1,27 @@ +package com.io.yutian.elementoriginlib.item.stat.data; + +import com.io.yutian.elementoriginlib.item.stat.StatData; + +public class BooleanData implements StatData { + + private boolean value; + + public BooleanData(boolean value) { + this.value = value; + } + + public boolean getValue() { + return value; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public String toString() { + return String.valueOf(value); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/DoubleData.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/DoubleData.java new file mode 100644 index 0000000..d8a0b31 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/DoubleData.java @@ -0,0 +1,56 @@ +package com.io.yutian.elementoriginlib.item.stat.data; + +import com.io.yutian.elementoriginlib.item.stat.Mergeable; +import com.io.yutian.elementoriginlib.item.stat.StatData; +import org.jetbrains.annotations.NotNull; + +public class DoubleData implements StatData, Mergeable { + + private double value; + + public DoubleData(double value) { + this.value = value; + } + + public double getValue() { + return value; + } + + public void setValue(double value) { + this.value = value; + } + + public void add(double d) { + this.value += d; + } + + @Override + public void merge(DoubleData var1) { + this.value += var1.getValue(); + } + + @NotNull + @Override + public DoubleData cloneData() { + return new DoubleData(getValue()); + } + + @Override + public boolean isEmpty() { + return value == 0.0d; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DoubleData that = (DoubleData) o; + return Double.compare(that.value, value) == 0; + } + + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/IntData.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/IntData.java new file mode 100644 index 0000000..56dfe89 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/IntData.java @@ -0,0 +1,38 @@ +package com.io.yutian.elementoriginlib.item.stat.data; + +import com.io.yutian.elementoriginlib.item.stat.Mergeable; +import com.io.yutian.elementoriginlib.item.stat.StatData; +import org.jetbrains.annotations.NotNull; + +public class IntData implements StatData, Mergeable { + + private int value; + + public IntData(int value) { + this.value = value; + } + + public int getInt() { + return value; + } + + public void setInt(int value) { + this.value = value; + } + + @Override + public void merge(IntData var1) { + this.value += var1.value; + } + + @NotNull + @Override + public IntData cloneData() { + return new IntData(value); + } + + @Override + public boolean isEmpty() { + return false; + } +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringData.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringData.java new file mode 100644 index 0000000..80d3ea0 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringData.java @@ -0,0 +1,40 @@ +package com.io.yutian.elementoriginlib.item.stat.data; + +import com.io.yutian.elementoriginlib.item.stat.Mergeable; +import com.io.yutian.elementoriginlib.item.stat.StatData; +import org.jetbrains.annotations.NotNull; + +public class StringData implements StatData, Mergeable { + + private String value; + + public StringData(String value) { + this.value = value; + } + + public String getString() { + return value; + } + + public void setString(String string) { + this.value = string; + } + + @Override + public boolean isEmpty() { + return value == null || value.trim().isEmpty(); + } + + + @Override + public void merge(StringData var1) { + this.value = var1.value; + } + + @NotNull + @Override + public StringData cloneData() { + return new StringData(value); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringListData.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringListData.java new file mode 100644 index 0000000..e8c6c54 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/StringListData.java @@ -0,0 +1,41 @@ +package com.io.yutian.elementoriginlib.item.stat.data; + +import com.io.yutian.elementoriginlib.item.stat.Mergeable; +import com.io.yutian.elementoriginlib.item.stat.StatData; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class StringListData implements StatData, Mergeable { + + private List list; + + public StringListData(List list) { + this.list = list; + } + + public List getList() { + return list; + } + + public void add(String str) { + this.list.add(str); + } + + @Override + public void merge(StringListData var1) { + this.list.addAll(var1.getList()); + } + + @NotNull + @Override + public StringListData cloneData() { + return new StringListData(list); + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/UUIDData.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/UUIDData.java new file mode 100644 index 0000000..b898a90 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/data/UUIDData.java @@ -0,0 +1,24 @@ +package com.io.yutian.elementoriginlib.item.stat.data; + +import com.io.yutian.elementoriginlib.item.stat.StatData; + +import java.util.UUID; + +public class UUIDData implements StatData { + + private UUID value; + + public UUIDData(UUID value) { + this.value = value; + } + + public UUID getUUID() { + return value; + } + + @Override + public boolean isEmpty() { + return false; + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/list/IDStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/list/IDStat.java new file mode 100644 index 0000000..153ae63 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/list/IDStat.java @@ -0,0 +1,11 @@ +package com.io.yutian.elementoriginlib.item.stat.list; + +import com.io.yutian.elementoriginlib.item.stat.type.StringStat; + +public class IDStat extends StringStat { + + public IDStat() { + super("id", "Id"); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/BooleanStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/BooleanStat.java new file mode 100644 index 0000000..c2080a4 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/BooleanStat.java @@ -0,0 +1,29 @@ +package com.io.yutian.elementoriginlib.item.stat.type; + +import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder; +import com.io.yutian.elementoriginlib.item.stat.ItemStat; +import com.io.yutian.elementoriginlib.item.stat.data.BooleanData; +import com.io.yutian.elementoriginlib.nbt.NBTByte; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public abstract class BooleanStat extends ItemStat { + + public BooleanStat(@NotNull String id, @NotNull String path) { + super(id, path); + } + + @Override + public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull BooleanData statData) { + NBTItem nbtItem = itemStackBuilder.getNBTItem(); + nbtItem.editTag(nbtCompound -> nbtCompound.putByte(getNBTPath(), (byte) (statData.getValue() ? 1 : 0))); + } + + @Nullable + @Override + public BooleanData getLoadedNBT(@NotNull NBTItem nbtItem) { + return new BooleanData(((NBTByte) nbtItem.get(getNBTPath())).getByte() == 1); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/DoubleStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/DoubleStat.java new file mode 100644 index 0000000..aa9a47d --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/DoubleStat.java @@ -0,0 +1,29 @@ +package com.io.yutian.elementoriginlib.item.stat.type; + +import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder; +import com.io.yutian.elementoriginlib.item.stat.ItemStat; +import com.io.yutian.elementoriginlib.item.stat.data.DoubleData; +import com.io.yutian.elementoriginlib.nbt.NBTDouble; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public abstract class DoubleStat extends ItemStat { + + public DoubleStat(@NotNull String id, @NotNull String path) { + super(id, path); + } + + @Override + public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull DoubleData statData) { + NBTItem nbtItem = itemStackBuilder.getNBTItem(); + nbtItem.editTag(nbtCompound -> nbtCompound.putDouble(getNBTPath(), statData.getValue())); + } + + @Nullable + @Override + public DoubleData getLoadedNBT(@NotNull NBTItem nbtItem) { + return new DoubleData(((NBTDouble) nbtItem.get(getNBTPath())).getDouble()); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/IntStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/IntStat.java new file mode 100644 index 0000000..c9bbc1e --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/IntStat.java @@ -0,0 +1,29 @@ +package com.io.yutian.elementoriginlib.item.stat.type; + +import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder; +import com.io.yutian.elementoriginlib.item.stat.ItemStat; +import com.io.yutian.elementoriginlib.item.stat.data.IntData; +import com.io.yutian.elementoriginlib.nbt.NBTInt; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public abstract class IntStat extends ItemStat { + + public IntStat(@NotNull String id, @NotNull String path) { + super(id, path); + } + + @Override + public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull IntData statData) { + NBTItem nbtItem = itemStackBuilder.getNBTItem(); + nbtItem.editTag(nbtCompound -> nbtCompound.putInt(getNBTPath(), statData.getInt())); + } + + @Nullable + @Override + public IntData getLoadedNBT(@NotNull NBTItem nbtItem) { + return new IntData(((NBTInt)nbtItem.get(getNBTPath())).getInt()); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringListStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringListStat.java new file mode 100644 index 0000000..e5c55b1 --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringListStat.java @@ -0,0 +1,43 @@ +package com.io.yutian.elementoriginlib.item.stat.type; + +import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder; +import com.io.yutian.elementoriginlib.item.stat.ItemStat; +import com.io.yutian.elementoriginlib.item.stat.data.StringListData; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import com.io.yutian.elementoriginlib.nbt.NBTList; +import com.io.yutian.elementoriginlib.nbt.NBTString; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +public abstract class StringListStat extends ItemStat { + + public StringListStat(@NotNull String id, @NotNull String path) { + super(id, path); + } + + @Override + public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull StringListData statData) { + NBTItem nbtItem = itemStackBuilder.getNBTItem(); + NBTList nbtList = new NBTList<>(); + statData.getList().forEach(s -> nbtList.add(new NBTString(s))); + nbtItem.editTag(nbtCompound -> nbtCompound.put(getNBTPath(), nbtList)); + } + + @Nullable + @Override + public StringListData getLoadedNBT(@NotNull NBTItem nbtItem) { + if (!nbtItem.has(getNBTPath(), NBTList.TYPE_ID)) { + return new StringListData(new ArrayList<>()); + } + NBTList nbtList = (NBTList) nbtItem.get(getNBTPath()); + List list = new ArrayList<>(); + for (int i = 0; i < nbtList.size(); i++) { + list.add(nbtList.get(i).getString()); + } + return new StringListData(list); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringStat.java new file mode 100644 index 0000000..71246ab --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/StringStat.java @@ -0,0 +1,30 @@ +package com.io.yutian.elementoriginlib.item.stat.type; + +import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder; +import com.io.yutian.elementoriginlib.item.stat.ItemStat; +import com.io.yutian.elementoriginlib.item.stat.data.StringData; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import com.io.yutian.elementoriginlib.nbt.NBTString; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public abstract class StringStat extends ItemStat { + + public StringStat(@NotNull String id, @NotNull String path) { + super(id, path); + } + + @Override + public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull StringData statData) { + NBTItem nbtItem = itemStackBuilder.getNBTItem(); + nbtItem.editTag(nbtCompound -> nbtCompound.putString(getNBTPath(), statData.getString())); + } + + + @Nullable + @Override + public StringData getLoadedNBT(@NotNull NBTItem nbtItem) { + return new StringData(((NBTString) nbtItem.get(getNBTPath())).getString()); + } + +} diff --git a/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/UUIDStat.java b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/UUIDStat.java new file mode 100644 index 0000000..133c49b --- /dev/null +++ b/src/main/java/com/io/yutian/elementoriginlib/item/stat/type/UUIDStat.java @@ -0,0 +1,31 @@ +package com.io.yutian.elementoriginlib.item.stat.type; + +import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder; +import com.io.yutian.elementoriginlib.item.stat.ItemStat; +import com.io.yutian.elementoriginlib.item.stat.data.UUIDData; +import com.io.yutian.elementoriginlib.nbt.NBTItem; +import com.io.yutian.elementoriginlib.nbt.NBTString; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.UUID; + +public abstract class UUIDStat extends ItemStat { + + public UUIDStat(@NotNull String id, @NotNull String path) { + super(id, path); + } + + @Override + public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull UUIDData statData) { + NBTItem nbtItem = itemStackBuilder.getNBTItem(); + nbtItem.editTag(nbtCompound -> nbtCompound.putString(getNBTPath(), statData.getUUID().toString())); + } + + @Nullable + @Override + public UUIDData getLoadedNBT(@NotNull NBTItem nbtItem) { + return new UUIDData(UUID.fromString(((NBTString) nbtItem.get(getNBTPath())).getString())); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 95a7444..e7c1ade 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: ElementOriginLib -version: '1.0' +version: '1.1' main: com.io.yutian.elementoriginlib.ElementOriginLib api-version: '1.20' authors: [ SuperYuTian ]