2024-08-23 14:12:59 -04:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
2025-03-04 18:19:53 -05:00
|
|
|
import kamkeel.npcs.controllers.SyncController;
|
|
|
|
|
import kamkeel.npcs.network.enums.EnumSyncType;
|
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;
|
2025-03-13 17:20:23 -04:00
|
|
|
import noppes.npcs.api.handler.IMagicHandler;
|
2025-03-14 20:57:43 -04:00
|
|
|
import noppes.npcs.constants.EnumDiagramLayout;
|
2024-08-23 14:12:59 -04:00
|
|
|
import noppes.npcs.controllers.data.Magic;
|
2025-02-24 02:57:43 -05:00
|
|
|
import noppes.npcs.controllers.data.MagicAssociation;
|
2025-03-13 17:14:45 -04:00
|
|
|
import noppes.npcs.controllers.data.MagicCycle;
|
2025-02-24 02:57:43 -05:00
|
|
|
|
2025-07-23 14:22:52 -04:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
2025-02-24 02:57:43 -05:00
|
|
|
import java.util.HashMap;
|
2024-08-23 14:12:59 -04:00
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
|
2025-03-13 17:20:23 -04:00
|
|
|
public class MagicController implements IMagicHandler {
|
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-03-13 17:20:23 -04:00
|
|
|
@Override
|
2025-02-24 02:57:43 -05:00
|
|
|
public Magic getMagic(int magicId) {
|
|
|
|
|
return magics.get(magicId);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:20:23 -04:00
|
|
|
@Override
|
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);
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} catch (Exception ee) {
|
|
|
|
|
}
|
2025-02-03 16:59:06 -05:00
|
|
|
}
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-05 14:27:32 -05:00
|
|
|
if (magics.isEmpty() && cycles.isEmpty()) {
|
2025-02-27 00:48:10 -05:00
|
|
|
// Create default magics
|
2025-03-13 17:14:45 -04:00
|
|
|
Magic earth = new Magic(getUnusedId(), "Earth", 0x00DD00);
|
2025-03-04 16:34:18 -05:00
|
|
|
earth.setItem(new ItemStack(CustomItems.earthElement));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
Magic water = new Magic(getUnusedId(), "Water", 0xF2DD00);
|
2025-03-04 16:34:18 -05:00
|
|
|
water.setItem(new ItemStack(CustomItems.waterElement));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
Magic fire = new Magic(getUnusedId(), "Fire", 0xDD0000);
|
2025-03-04 16:34:18 -05:00
|
|
|
fire.setItem(new ItemStack(CustomItems.spellFire));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
Magic air = new Magic(getUnusedId(), "Air", 0xDD0000);
|
2025-03-04 16:34:18 -05:00
|
|
|
air.setItem(new ItemStack(CustomItems.airElement));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
Magic dark = new Magic(getUnusedId(), "Dark", 0xDD0000);
|
2025-03-04 16:34:18 -05:00
|
|
|
dark.setItem(new ItemStack(CustomItems.spellDark));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
Magic holy = new Magic(getUnusedId(), "Holy", 0xDD0000);
|
2025-03-04 16:34:18 -05:00
|
|
|
holy.setItem(new ItemStack(CustomItems.spellHoly));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-05 14:27:32 -05:00
|
|
|
Magic nature = new Magic(getUnusedId(), "Nature", 0xDD0000);
|
2025-03-04 16:34:18 -05:00
|
|
|
nature.setItem(new ItemStack(CustomItems.spellNature));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
2025-03-05 14:27:32 -05:00
|
|
|
Magic arcane = new Magic(getUnusedId(), "Arcane", 0xDD0000);
|
2025-03-04 16:34:18 -05:00
|
|
|
arcane.setItem(new ItemStack(CustomItems.spellArcane));
|
2025-02-27 00:48:10 -05:00
|
|
|
|
|
|
|
|
// Insiders
|
2025-03-14 20:57:43 -04:00
|
|
|
earth.interactions.put(air.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
air.interactions.put(earth.id, 0.50f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
water.interactions.put(earth.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
earth.interactions.put(water.id, 0.50f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
fire.interactions.put(water.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
water.interactions.put(fire.id, 0.50f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
air.interactions.put(fire.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
fire.interactions.put(air.id, 0.50f);
|
2025-02-27 00:48:10 -05:00
|
|
|
|
|
|
|
|
// Outsiders
|
2025-03-14 20:57:43 -04:00
|
|
|
dark.interactions.put(nature.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
nature.interactions.put(dark.id, 0.50f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
nature.interactions.put(holy.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
holy.interactions.put(nature.id, 0.50f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
holy.interactions.put(arcane.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
arcane.interactions.put(holy.id, 0.50f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
arcane.interactions.put(dark.id, -0.50f);
|
2025-05-20 13:53:26 -04:00
|
|
|
dark.interactions.put(arcane.id, 0.50f);
|
2025-02-27 00:48:10 -05:00
|
|
|
|
|
|
|
|
// Cross Interactions
|
2025-03-14 20:57:43 -04:00
|
|
|
earth.interactions.put(nature.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
nature.interactions.put(earth.id, 0.25f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
water.interactions.put(holy.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
holy.interactions.put(water.id, 0.25f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
fire.interactions.put(arcane.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
arcane.interactions.put(fire.id, 0.25f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
air.interactions.put(dark.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
dark.interactions.put(air.id, 0.25f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
dark.interactions.put(fire.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
fire.interactions.put(dark.id, 0.25f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
nature.interactions.put(air.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
air.interactions.put(nature.id, 0.25f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
holy.interactions.put(earth.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
earth.interactions.put(holy.id, 0.25f);
|
2025-03-14 20:57:43 -04:00
|
|
|
|
|
|
|
|
arcane.interactions.put(water.id, -0.25f);
|
2025-05-20 13:53:26 -04:00
|
|
|
water.interactions.put(arcane.id, 0.25f);
|
2025-02-27 00:48:10 -05:00
|
|
|
|
|
|
|
|
// 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-03-05 14:27:32 -05:00
|
|
|
|
|
|
|
|
MagicCycle defaultCycle = new MagicCycle();
|
|
|
|
|
defaultCycle.id = getUnusedCycleId();
|
|
|
|
|
defaultCycle.name = "Universal";
|
2025-03-14 20:57:43 -04:00
|
|
|
defaultCycle.layout = EnumDiagramLayout.CIRCULAR_MANUAL;
|
2025-03-15 17:13:19 -04:00
|
|
|
defaultCycle.displayName = "&6Elementa Cycle";
|
2025-03-05 14:27:32 -05:00
|
|
|
cycles.put(defaultCycle.id, defaultCycle);
|
|
|
|
|
|
|
|
|
|
// Add magic associations using your old index and priority values:
|
|
|
|
|
addMagicToCycle(earth.id, defaultCycle.id, 1, 1); // Earth: index 1, priority 1
|
|
|
|
|
addMagicToCycle(water.id, defaultCycle.id, 1, 2); // Water: index 1, priority 2
|
|
|
|
|
addMagicToCycle(fire.id, defaultCycle.id, 1, 3); // Fire: index 1, priority 3
|
|
|
|
|
addMagicToCycle(air.id, defaultCycle.id, 1, 0); // Air: index 1, priority 0
|
|
|
|
|
addMagicToCycle(dark.id, defaultCycle.id, 0, 3); // Dark: index 0, priority 3
|
|
|
|
|
addMagicToCycle(holy.id, defaultCycle.id, 0, 1); // Holy: index 0, priority 1
|
|
|
|
|
addMagicToCycle(nature.id, defaultCycle.id, 0, 0); // Nature: index 0, priority 0
|
|
|
|
|
addMagicToCycle(arcane.id, defaultCycle.id, 0, 2); // Arcane: index 0, priority 2
|
|
|
|
|
|
|
|
|
|
saveMagicData();
|
2025-02-27 00:48:10 -05:00
|
|
|
}
|
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-03-04 18:19:53 -05:00
|
|
|
|
|
|
|
|
NBTTagCompound magicCompound = new NBTTagCompound();
|
|
|
|
|
mag.writeNBT(magicCompound);
|
|
|
|
|
SyncController.syncUpdate(EnumSyncType.MAGIC, -1, magicCompound);
|
|
|
|
|
|
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);
|
2025-03-04 18:19:53 -05:00
|
|
|
SyncController.syncRemove(EnumSyncType.MAGIC, magicID);
|
2025-03-03 23:03:12 -05:00
|
|
|
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);
|
|
|
|
|
|
2025-03-04 18:19:53 -05:00
|
|
|
NBTTagCompound cycleCompound = new NBTTagCompound();
|
|
|
|
|
cycle.writeNBT(cycleCompound);
|
|
|
|
|
SyncController.syncUpdate(EnumSyncType.MAGIC_CYCLE, -1, cycleCompound);
|
|
|
|
|
|
|
|
|
|
saveMagicData();
|
2025-03-03 23:03:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeCycle(int categoryId) {
|
2025-03-03 05:13:07 -05:00
|
|
|
if (cycles.containsKey(categoryId)) {
|
|
|
|
|
cycles.remove(categoryId);
|
2025-03-04 18:19:53 -05:00
|
|
|
SyncController.syncRemove(EnumSyncType.MAGIC_CYCLE, 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-13 17:20:23 -04:00
|
|
|
@Override
|
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);
|
2025-03-13 17:14:45 -04:00
|
|
|
if (magic == null)
|
2025-03-03 23:03:12 -05:00
|
|
|
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);
|
2025-03-04 18:19:53 -05:00
|
|
|
|
|
|
|
|
saveCycle(cat);
|
2025-02-24 02:57:43 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:20:23 -04:00
|
|
|
@Override
|
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);
|
2025-03-04 18:19:53 -05:00
|
|
|
|
|
|
|
|
saveCycle(cat);
|
2025-02-03 16:59:06 -05:00
|
|
|
}
|
2024-08-23 14:12:59 -04:00
|
|
|
}
|