mirror of
https://github.com/Theta-Dev/ConstructionWand.git
synced 2025-08-05 02:15:27 +02:00
79 lines
2.2 KiB
Java
79 lines
2.2 KiB
Java
package thetadev.constructionwand.basics.option;
|
|
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
import net.minecraft.nbt.ListNBT;
|
|
import net.minecraft.nbt.StringNBT;
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraftforge.common.util.Constants;
|
|
import net.minecraftforge.registries.ForgeRegistries;
|
|
import thetadev.constructionwand.ConstructionWand;
|
|
import thetadev.constructionwand.api.IWandUpgrade;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class WandUpgrades<T extends IWandUpgrade>
|
|
{
|
|
protected final CompoundNBT tag;
|
|
protected final String key;
|
|
protected final ArrayList<T> upgrades;
|
|
protected final T dval;
|
|
|
|
public WandUpgrades(CompoundNBT tag, String key, T dval) {
|
|
this.tag = tag;
|
|
this.key = key;
|
|
this.dval = dval;
|
|
|
|
upgrades = new ArrayList<>();
|
|
if(dval != null) upgrades.add(0, dval);
|
|
|
|
deserialize();
|
|
}
|
|
|
|
protected void deserialize() {
|
|
ListNBT listnbt = tag.getList(key, Constants.NBT.TAG_STRING);
|
|
boolean require_fix = false;
|
|
|
|
for(int i = 0; i < listnbt.size(); i++) {
|
|
String str = listnbt.getString(i);
|
|
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(str));
|
|
|
|
T data;
|
|
try {
|
|
//noinspection unchecked
|
|
data = (T) item;
|
|
upgrades.add(data);
|
|
} catch(ClassCastException e) {
|
|
ConstructionWand.LOGGER.warn("Invalid wand upgrade: " + str);
|
|
require_fix = true;
|
|
}
|
|
}
|
|
if(require_fix) serialize();
|
|
}
|
|
|
|
protected void serialize() {
|
|
ListNBT listnbt = new ListNBT();
|
|
|
|
for(T item : upgrades) {
|
|
if(item == dval) continue;
|
|
listnbt.add(StringNBT.valueOf(item.getRegistryName().toString()));
|
|
}
|
|
tag.put(key, listnbt);
|
|
}
|
|
|
|
public boolean addUpgrade(T upgrade) {
|
|
if(hasUpgrade(upgrade)) return false;
|
|
|
|
upgrades.add(upgrade);
|
|
serialize();
|
|
return true;
|
|
}
|
|
|
|
public boolean hasUpgrade(T upgrade) {
|
|
return upgrades.contains(upgrade);
|
|
}
|
|
|
|
public ArrayList<T> getUpgrades() {
|
|
return upgrades;
|
|
}
|
|
}
|