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

325 lines
11 KiB
Java
Raw Normal View History

2024-08-23 14:12:59 -04:00
package noppes.npcs.controllers;
2025-02-27 00:48:10 -05:00
import net.minecraft.item.ItemStack;
2024-08-23 14:12:59 -04:00
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
2025-02-27 00:48:10 -05:00
import noppes.npcs.CustomItems;
2024-08-23 14:12:59 -04:00
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.controllers.data.Magic;
2025-03-03 05:13:07 -05:00
import noppes.npcs.controllers.data.MagicCycle;
2025-02-24 02:57:43 -05:00
import noppes.npcs.controllers.data.MagicAssociation;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
2024-08-23 14:12:59 -04:00
import java.util.zip.GZIPInputStream;
public class MagicController {
2025-02-24 02:57:43 -05:00
public HashMap<Integer, Magic> magics = new HashMap<>();
public HashMap<Integer, Magic> magicSync = new HashMap<>();
2024-08-23 14:12:59 -04:00
2025-03-03 05:13:07 -05:00
public HashMap<Integer, MagicCycle> cycles = new HashMap<>();
public HashMap<Integer, MagicCycle> cyclesSync = new HashMap<>();
2024-08-23 14:12:59 -04:00
2025-03-03 05:13:07 -05:00
public int lastUsedCycleID = 0;
2025-03-03 20:05:19 -05:00
private int lastUsedMagicID = 0;
2025-02-24 15:37:39 -05:00
2025-02-24 02:57:43 -05:00
private static MagicController instance;
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public MagicController() {
instance = this;
}
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public static MagicController getInstance() {
return instance;
}
2024-08-23 14:12:59 -04:00
2025-02-24 02:57:43 -05:00
public Magic getMagic(int magicId) {
return magics.get(magicId);
}
2025-03-03 05:13:07 -05:00
public MagicCycle getCycle(int cycleID) {
return cycles.get(cycleID);
2025-02-03 16:59:06 -05:00
}
2024-08-23 14:12:59 -04:00
2025-02-03 16:59:06 -05:00
public void load() {
2025-03-03 20:05:19 -05:00
magics.clear();
cycles.clear();
lastUsedCycleID = 0;
lastUsedMagicID = 0;
2025-02-03 16:59:06 -05:00
File saveDir = CustomNpcs.getWorldSaveDirectory();
2025-02-24 02:57:43 -05:00
if (saveDir == null) return;
2025-02-03 16:59:06 -05:00
try {
File file = new File(saveDir, "magic.dat");
if (file.exists()) {
loadMagicFile(file);
}
} catch (Exception e) {
try {
File file = new File(saveDir, "magic.dat_old");
if (file.exists()) {
loadMagicFile(file);
}
} catch (Exception ee) { }
}
2025-02-27 00:48:10 -05:00
if (magics.isEmpty()) {
// Create default magics
Magic earth = new Magic(0, "Earth", 0x00DD00);
earth.setIconItem(new ItemStack(CustomItems.earthElement));
Magic water = new Magic(1, "Water", 0xF2DD00);
water.setIconItem(new ItemStack(CustomItems.waterElement));
Magic fire = new Magic(2, "Fire", 0xDD0000);
fire.setIconItem(new ItemStack(CustomItems.spellFire));
Magic air = new Magic(3, "Air", 0xDD0000);
air.setIconItem(new ItemStack(CustomItems.airElement));
Magic dark = new Magic(4, "Dark", 0xDD0000);
dark.setIconItem(new ItemStack(CustomItems.spellDark));
Magic holy = new Magic(5, "Holy", 0xDD0000);
holy.setIconItem(new ItemStack(CustomItems.spellHoly));
Magic nature = new Magic(6, "Nature", 0xDD0000);
nature.setIconItem(new ItemStack(CustomItems.spellNature));
Magic arcane = new Magic(7, "Arcane", 0xDD0000);
arcane.setIconItem(new ItemStack(CustomItems.spellArcane));
// Insiders
earth.interactions.put(air.id, 0.60f);
water.interactions.put(earth.id, 0.60f);
fire.interactions.put(water.id, 0.60f);
air.interactions.put(fire.id, 0.60f);
// Outsiders
dark.interactions.put(nature.id, 0.60f);
nature.interactions.put(holy.id, 0.60f);
holy.interactions.put(arcane.id, 0.60f);
arcane.interactions.put(dark.id, 0.60f);
// Cross Interactions
earth.interactions.put(nature.id, 0.30f);
water.interactions.put(holy.id, 0.30f);
fire.interactions.put(arcane.id, 0.30f);
air.interactions.put(dark.id, 0.30f);
dark.interactions.put(fire.id, 0.30f);
nature.interactions.put(air.id, 0.30f);
holy.interactions.put(earth.id, 0.30f);
arcane.interactions.put(water.id, 0.30f);
// Add them to the registry
magics.put(earth.id, earth);
magics.put(water.id, water);
magics.put(fire.id, fire);
magics.put(air.id, air);
magics.put(dark.id, dark);
magics.put(holy.id, holy);
magics.put(nature.id, nature);
magics.put(arcane.id, arcane);
}
2025-02-03 16:59:06 -05:00
}
private void loadMagicFile(File file) throws IOException {
DataInputStream stream = new DataInputStream(
new BufferedInputStream(new GZIPInputStream(new FileInputStream(file)))
);
loadMagic(stream);
stream.close();
}
public void loadMagic(DataInputStream stream) throws IOException {
2025-02-24 02:57:43 -05:00
NBTTagCompound compound = CompressedStreamTools.read(stream);
2025-03-03 20:05:19 -05:00
lastUsedMagicID = compound.getInteger("lastID");
2025-03-03 23:03:12 -05:00
lastUsedCycleID = compound.getInteger("lastCycleID");
2025-02-03 16:59:06 -05:00
2025-02-24 02:57:43 -05:00
magics.clear();
NBTTagList magicList = compound.getTagList("Magics", 10);
for (int i = 0; i < magicList.tagCount(); i++) {
NBTTagCompound magCompound = magicList.getCompoundTagAt(i);
Magic mag = new Magic();
mag.readNBT(magCompound);
magics.put(mag.id, mag);
}
2025-03-03 05:13:07 -05:00
cycles.clear();
2025-03-03 23:03:12 -05:00
NBTTagList cycleList = compound.getTagList("Cycles", 10);
for (int i = 0; i < cycleList.tagCount(); i++) {
NBTTagCompound catCompound = cycleList.getCompoundTagAt(i);
MagicCycle cycle = new MagicCycle();
cycle.readNBT(catCompound);
cycles.put(cycle.id, cycle);
2025-02-03 16:59:06 -05:00
}
}
public NBTTagCompound getNBT() {
2025-02-24 02:57:43 -05:00
NBTTagList magicList = new NBTTagList();
for (Magic mag : magics.values()) {
NBTTagCompound magCompound = new NBTTagCompound();
mag.writeNBT(magCompound);
magicList.appendTag(magCompound);
}
NBTTagList catList = new NBTTagList();
2025-03-03 05:13:07 -05:00
for (MagicCycle cat : cycles.values()) {
2025-02-24 02:57:43 -05:00
NBTTagCompound catCompound = new NBTTagCompound();
cat.writeNBT(catCompound);
catList.appendTag(catCompound);
2025-02-03 16:59:06 -05:00
}
2025-02-24 02:57:43 -05:00
NBTTagCompound compound = new NBTTagCompound();
2025-03-03 20:05:19 -05:00
compound.setInteger("lastID", lastUsedMagicID);
2025-03-03 05:13:07 -05:00
compound.setInteger("lastCycleID", lastUsedCycleID);
2025-02-24 02:57:43 -05:00
compound.setTag("Magics", magicList);
2025-03-03 05:13:07 -05:00
compound.setTag("Cycles", catList);
2025-02-24 02:57:43 -05:00
return compound;
2025-02-03 16:59:06 -05:00
}
2025-02-24 02:57:43 -05:00
public void saveMagicData() {
2025-02-03 16:59:06 -05:00
try {
File saveDir = CustomNpcs.getWorldSaveDirectory();
2025-02-24 02:57:43 -05:00
File fileNew = new File(saveDir, "magic.dat_new");
File fileOld = new File(saveDir, "magic.dat_old");
File fileCurrent = new File(saveDir, "magic.dat");
CompressedStreamTools.writeCompressed(getNBT(), new FileOutputStream(fileNew));
2025-03-03 23:03:12 -05:00
if (fileOld.exists())
fileOld.delete();
2025-02-24 02:57:43 -05:00
fileCurrent.renameTo(fileOld);
2025-03-03 23:03:12 -05:00
if (fileCurrent.exists())
fileCurrent.delete();
2025-02-24 02:57:43 -05:00
fileNew.renameTo(fileCurrent);
if (fileNew.exists()) fileNew.delete();
2025-02-03 16:59:06 -05:00
} catch (Exception e) {
LogWriter.except(e);
}
}
2025-03-03 23:03:12 -05:00
// Enforce unique names and IDs for Magics
2025-02-03 16:59:06 -05:00
public void saveMagic(Magic mag) {
if (mag.id < 0) {
mag.id = getUnusedId();
while (hasName(mag.name))
mag.name += "_";
} else {
Magic existing = magics.get(mag.id);
if (existing != null && !existing.name.equals(mag.name))
while (hasName(mag.name))
mag.name += "_";
}
magics.put(mag.id, mag);
2025-02-24 02:57:43 -05:00
saveMagicData();
2025-02-03 16:59:06 -05:00
}
2025-03-03 23:03:12 -05:00
public void removeMagic(int magicID) {
if (magics.containsKey(magicID)) {
magics.remove(magicID);
saveMagicData();
}
}
2025-02-03 16:59:06 -05:00
public int getUnusedId() {
2025-03-03 20:05:19 -05:00
if (lastUsedMagicID == 0) {
2025-02-24 02:57:43 -05:00
for (int id : magics.keySet())
2025-03-03 20:05:19 -05:00
if (id > lastUsedMagicID)
lastUsedMagicID = id;
2025-02-03 16:59:06 -05:00
}
2025-03-03 20:05:19 -05:00
lastUsedMagicID++;
return lastUsedMagicID;
2025-02-03 16:59:06 -05:00
}
public boolean hasName(String newName) {
2025-02-24 02:57:43 -05:00
if (newName.trim().isEmpty()) return true;
2025-02-03 16:59:06 -05:00
for (Magic mag : magics.values())
2025-03-03 23:03:12 -05:00
if (mag.name.equalsIgnoreCase(newName))
2025-02-03 16:59:06 -05:00
return true;
return false;
}
2025-03-03 23:03:12 -05:00
// === Cycle Management Methods ===
2025-02-03 16:59:06 -05:00
2025-03-03 23:03:12 -05:00
// Returns a new unique cycle ID
public int getUnusedCycleId() {
if (lastUsedCycleID == 0) {
for (int id : cycles.keySet())
if (id > lastUsedCycleID)
lastUsedCycleID = id;
2025-02-03 16:59:06 -05:00
}
2025-03-03 23:03:12 -05:00
lastUsedCycleID++;
return lastUsedCycleID;
2025-02-03 16:59:06 -05:00
}
2025-03-03 23:03:12 -05:00
// Checks if a cycle title already exists (case-insensitive)
2025-02-24 02:57:43 -05:00
public boolean containsCategoryName(String title) {
title = title.toLowerCase();
2025-03-03 05:13:07 -05:00
for (MagicCycle cat : cycles.values()) {
2025-03-03 23:03:12 -05:00
if (cat.name.toLowerCase().equals(title))
2025-02-24 02:57:43 -05:00
return true;
2025-02-03 16:59:06 -05:00
}
2025-02-24 02:57:43 -05:00
return false;
2025-02-03 16:59:06 -05:00
}
2025-03-03 23:03:12 -05:00
// Saves a cycle ensuring a unique title and ID
public void saveCycle(MagicCycle cycle) {
if (cycle.id < 0) {
cycle.id = getUnusedCycleId();
while (containsCategoryName(cycle.name))
cycle.name += "_";
} else {
MagicCycle existing = cycles.get(cycle.id);
if (existing != null && !existing.name.equals(cycle.name))
while (containsCategoryName(cycle.name))
cycle.name += "_";
}
cycles.put(cycle.id, cycle);
saveMagicData();
}
// Alternatively, if you prefer an addCycle method:
public void addCycle(MagicCycle cycle) {
saveCycle(cycle);
}
public void removeCycle(int categoryId) {
2025-03-03 05:13:07 -05:00
if (cycles.containsKey(categoryId)) {
cycles.remove(categoryId);
2025-02-24 02:57:43 -05:00
saveMagicData();
2025-02-03 16:59:06 -05:00
}
}
/**
2025-02-24 02:57:43 -05:00
* Associates a magic with a category along with its per-category ordering data.
2025-02-03 16:59:06 -05:00
*/
2025-03-03 23:03:12 -05:00
public void addMagicToCycle(int magicId, int cycleId, int index, int priority) {
MagicCycle cat = cycles.get(cycleId);
if (cat == null)
return;
Magic magic = magics.get(magicId);
if(magic == null)
return;
2025-02-24 02:57:43 -05:00
MagicAssociation assoc = new MagicAssociation();
assoc.magicId = magicId;
assoc.index = index;
assoc.priority = priority;
cat.associations.put(magicId, assoc);
saveMagicData();
}
2025-03-03 23:03:12 -05:00
public void removeMagicFromCycle(int magicId, int cycleId) {
MagicCycle cat = cycles.get(cycleId);
2025-02-24 02:57:43 -05:00
if (cat == null) return;
cat.associations.remove(magicId);
saveMagicData();
2025-02-03 16:59:06 -05:00
}
2024-08-23 14:12:59 -04:00
}