This commit is contained in:
YuTian 2024-08-03 15:02:51 +08:00
parent d1cffafcfd
commit 9101db3075
21 changed files with 694 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/.idea/ /.idea/
/target/ /target/
/ElementOriginLib.iml /ElementOriginLib.iml
/build/

View File

@ -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<ItemStat, StatData> 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 extends StatData> S getStatData(@NotNull ItemStat<S> 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<ItemStat> 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;
}
}

View File

@ -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<String> 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;
}
}

View File

@ -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<S extends StatData> {
@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<String> 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();
}
}

View File

@ -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<String, ItemStat<?>> itemStats = new HashMap<>();
public static final StringStat ID = register(new IDStat());
public static <I extends ItemStat> 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);
}
}

View File

@ -0,0 +1,12 @@
package com.io.yutian.elementoriginlib.item.stat;
import org.jetbrains.annotations.NotNull;
public interface Mergeable<S extends StatData> extends StatData {
void merge(S var1);
@NotNull
S cloneData();
}

View File

@ -0,0 +1,9 @@
package com.io.yutian.elementoriginlib.item.stat;
public interface StatData {
default boolean isEmpty() {
return true;
}
}

View File

@ -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);
}
}

View File

@ -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<DoubleData> {
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;
}
}

View File

@ -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<IntData> {
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;
}
}

View File

@ -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<StringData> {
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);
}
}

View File

@ -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<StringListData> {
private List<String> list;
public StringListData(List<String> list) {
this.list = list;
}
public List<String> 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();
}
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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<BooleanData> {
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);
}
}

View File

@ -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<DoubleData> {
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());
}
}

View File

@ -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<IntData> {
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());
}
}

View File

@ -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<StringListData> {
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<NBTString> 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<NBTString> nbtList = (NBTList<NBTString>) nbtItem.get(getNBTPath());
List<String> list = new ArrayList<>();
for (int i = 0; i < nbtList.size(); i++) {
list.add(nbtList.get(i).getString());
}
return new StringListData(list);
}
}

View File

@ -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<StringData> {
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());
}
}

View File

@ -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<UUIDData> {
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()));
}
}

View File

@ -1,5 +1,5 @@
name: ElementOriginLib name: ElementOriginLib
version: '1.0' version: '1.1'
main: com.io.yutian.elementoriginlib.ElementOriginLib main: com.io.yutian.elementoriginlib.ElementOriginLib
api-version: '1.20' api-version: '1.20'
authors: [ SuperYuTian ] authors: [ SuperYuTian ]