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

165 lines
4.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;
2025-02-04 03:48:25 -05:00
public int index;
public int priority;
2025-02-03 16:59:06 -05:00
// Icon as an ItemStack or as a texture file
public ItemStack iconItem = null;
public String iconTexture = "";
// Weaknesses mapping: key = magic ID this magic is weak against,
// value = extra damage percentage (as a fraction, e.g. 0.2 for 20% extra damage)
public Map<Integer, Float> weaknesses = new HashMap<Integer, Float>();
public Magic() {}
public Magic(int id, String name, int color) {
this.name = name;
this.color = color;
this.id = id;
}
2025-02-04 03:48:25 -05:00
public Magic(int id, String name, int color, int index, int priority) {
this.name = name;
this.color = color;
this.id = id;
this.index = index;
this.priority = priority;
}
2025-02-03 16:59:06 -05:00
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-04 03:48:25 -05:00
index = compound.getInteger("Index");
priority = compound.getInteger("Priority");
2024-08-23 14:12:59 -04:00
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");
// Read the weaknesses mapping from NBT.
weaknesses.clear();
if(compound.hasKey("Weaknesses")) {
NBTTagList weaknessList = compound.getTagList("Weaknesses", 10); // compound tags
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-04 03:48:25 -05:00
compound.setInteger("Index", index);
compound.setInteger("Priority", priority);
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);
}
// Write the weaknesses mapping.
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-04 03:48:25 -05:00
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
2025-02-03 16:59:06 -05:00
public void setColor(int color) {
this.color = color;
}
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
}