Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/data/Magic.java
T

126 lines
3.7 KiB
Java
Raw Normal View History

2024-08-23 14:12:59 -04:00
package noppes.npcs.controllers.data;
2025-02-03 16:59:06 -05:00
import net.minecraft.item.ItemStack;
2024-08-23 14:12:59 -04:00
import net.minecraft.nbt.NBTTagCompound;
2025-02-03 16:59:06 -05:00
import net.minecraft.nbt.NBTTagList;
2024-08-23 14:12:59 -04:00
import noppes.npcs.api.handler.data.IMagic;
import noppes.npcs.controllers.MagicController;
2025-02-03 16:59:06 -05:00
import java.util.HashMap;
import java.util.Map;
2024-08-23 14:12:59 -04:00
public class Magic implements IMagic {
2025-02-03 16:59:06 -05:00
public String name = "";
public int color = Integer.parseInt("FF00", 16);
public int id = -1;
public ItemStack iconItem = null;
public String iconTexture = "";
2025-02-24 02:57:43 -05:00
public Map<Integer, Float> weaknesses = new HashMap<>();
2025-02-03 16:59:06 -05:00
public Magic() {}
public Magic(int id, String name, int color) {
this.name = name;
this.color = color;
this.id = id;
}
public static String formatName(String name) {
name = name.toLowerCase().trim();
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
public void readNBT(NBTTagCompound compound) {
2024-08-23 14:12:59 -04:00
name = compound.getString("Name");
color = compound.getInteger("Color");
id = compound.getInteger("Slot");
2025-02-24 02:57:43 -05:00
// No longer reading index/priority here.
2025-02-03 16:59:06 -05:00
if(compound.hasKey("IconItem")){
NBTTagCompound itemTag = compound.getCompoundTag("IconItem");
iconItem = ItemStack.loadItemStackFromNBT(itemTag);
}
iconTexture = compound.getString("IconTexture");
weaknesses.clear();
if(compound.hasKey("Weaknesses")) {
2025-02-24 02:57:43 -05:00
NBTTagList weaknessList = compound.getTagList("Weaknesses", 10);
2025-02-03 16:59:06 -05:00
for(int i = 0; i < weaknessList.tagCount(); i++){
NBTTagCompound weaknessTag = weaknessList.getCompoundTagAt(i);
int weakMagicId = weaknessTag.getInteger("MagicID");
float percentage = weaknessTag.getFloat("Percentage");
weaknesses.put(weakMagicId, percentage);
}
}
}
public void writeNBT(NBTTagCompound compound) {
compound.setInteger("Slot", id);
compound.setString("Name", name);
compound.setInteger("Color", color);
2025-02-24 02:57:43 -05:00
// No longer writing index/priority here.
2025-02-03 16:59:06 -05:00
if(iconItem != null) {
NBTTagCompound itemTag = new NBTTagCompound();
iconItem.writeToNBT(itemTag);
compound.setTag("IconItem", itemTag);
}
if(iconTexture != null && !iconTexture.isEmpty()){
compound.setString("IconTexture", iconTexture);
}
NBTTagList weaknessList = new NBTTagList();
for(Map.Entry<Integer, Float> entry : weaknesses.entrySet()){
NBTTagCompound weaknessTag = new NBTTagCompound();
weaknessTag.setInteger("MagicID", entry.getKey());
weaknessTag.setFloat("Percentage", entry.getValue());
weaknessList.appendTag(weaknessTag);
}
compound.setTag("Weaknesses", weaknessList);
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
2025-02-24 02:57:43 -05:00
public void setColor(int c) {
this.color = c;
2025-02-03 16:59:06 -05:00
}
2025-02-24 02:57:43 -05:00
@Override
2025-02-03 16:59:06 -05:00
public int getColor() {
return this.color;
}
public void save() {
MagicController.getInstance().saveMagic(this);
}
public void setIconItem(ItemStack item) {
this.iconItem = item;
}
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public ItemStack getIconItem() {
return this.iconItem;
}
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public void setIconTexture(String texture) {
this.iconTexture = texture;
}
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public String getIconTexture() {
return this.iconTexture;
}
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public void setWeaknesses(Map<Integer, Float> weaknesses) {
this.weaknesses = weaknesses;
}
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public Map<Integer, Float> getWeaknesses() {
return this.weaknesses;
}
2024-08-23 14:12:59 -04:00
}