mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-21 04:32:39 +00:00
Magic Packet Fixes
This commit is contained in:
1 parent
c2fa74c541
commit
4e6d6b7082
13 files changed
+177
-54
No files matched your search
@@ -142,9 +142,8 @@ public class MagicController {
|
||||
public void loadMagic(DataInputStream stream) throws IOException {
|
||||
NBTTagCompound compound = CompressedStreamTools.read(stream);
|
||||
lastUsedMagicID = compound.getInteger("lastID");
|
||||
lastUsedCycleID = compound.getInteger("lastCatID");
|
||||
lastUsedCycleID = compound.getInteger("lastCycleID");
|
||||
|
||||
// Load magics
|
||||
magics.clear();
|
||||
NBTTagList magicList = compound.getTagList("Magics", 10);
|
||||
for (int i = 0; i < magicList.tagCount(); i++) {
|
||||
@@ -154,14 +153,13 @@ public class MagicController {
|
||||
magics.put(mag.id, mag);
|
||||
}
|
||||
|
||||
// Load categories
|
||||
cycles.clear();
|
||||
NBTTagList catList = compound.getTagList("Cycles", 10);
|
||||
for (int i = 0; i < catList.tagCount(); i++) {
|
||||
NBTTagCompound catCompound = catList.getCompoundTagAt(i);
|
||||
MagicCycle cat = new MagicCycle();
|
||||
cat.readNBT(catCompound);
|
||||
cycles.put(cat.id, cat);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,9 +191,12 @@ public class MagicController {
|
||||
File fileOld = new File(saveDir, "magic.dat_old");
|
||||
File fileCurrent = new File(saveDir, "magic.dat");
|
||||
CompressedStreamTools.writeCompressed(getNBT(), new FileOutputStream(fileNew));
|
||||
if (fileOld.exists()) fileOld.delete();
|
||||
if (fileOld.exists())
|
||||
fileOld.delete();
|
||||
fileCurrent.renameTo(fileOld);
|
||||
if (fileCurrent.exists()) fileCurrent.delete();
|
||||
|
||||
if (fileCurrent.exists())
|
||||
fileCurrent.delete();
|
||||
fileNew.renameTo(fileCurrent);
|
||||
if (fileNew.exists()) fileNew.delete();
|
||||
} catch (Exception e) {
|
||||
@@ -203,6 +204,7 @@ public class MagicController {
|
||||
}
|
||||
}
|
||||
|
||||
// Enforce unique names and IDs for Magics
|
||||
public void saveMagic(Magic mag) {
|
||||
if (mag.id < 0) {
|
||||
mag.id = getUnusedId();
|
||||
@@ -218,6 +220,13 @@ public class MagicController {
|
||||
saveMagicData();
|
||||
}
|
||||
|
||||
public void removeMagic(int magicID) {
|
||||
if (magics.containsKey(magicID)) {
|
||||
magics.remove(magicID);
|
||||
saveMagicData();
|
||||
}
|
||||
}
|
||||
|
||||
public int getUnusedId() {
|
||||
if (lastUsedMagicID == 0) {
|
||||
for (int id : magics.keySet())
|
||||
@@ -231,35 +240,56 @@ public class MagicController {
|
||||
public boolean hasName(String newName) {
|
||||
if (newName.trim().isEmpty()) return true;
|
||||
for (Magic mag : magics.values())
|
||||
if (mag.name.equals(newName))
|
||||
if (mag.name.equalsIgnoreCase(newName))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// === Category management methods ===
|
||||
// === Cycle Management Methods ===
|
||||
|
||||
public void addCycle(MagicCycle category) {
|
||||
if (category.id < 0) {
|
||||
lastUsedCycleID++;
|
||||
category.id = lastUsedCycleID;
|
||||
} else {
|
||||
while (containsCategoryName(category.title))
|
||||
category.title += "_";
|
||||
// Returns a new unique cycle ID
|
||||
public int getUnusedCycleId() {
|
||||
if (lastUsedCycleID == 0) {
|
||||
for (int id : cycles.keySet())
|
||||
if (id > lastUsedCycleID)
|
||||
lastUsedCycleID = id;
|
||||
}
|
||||
cycles.put(category.id, category);
|
||||
saveMagicData();
|
||||
lastUsedCycleID++;
|
||||
return lastUsedCycleID;
|
||||
}
|
||||
|
||||
// Checks if a cycle title already exists (case-insensitive)
|
||||
public boolean containsCategoryName(String title) {
|
||||
title = title.toLowerCase();
|
||||
for (MagicCycle cat : cycles.values()) {
|
||||
if (cat.title.toLowerCase().equals(title))
|
||||
if (cat.name.toLowerCase().equals(title))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removeCategory(int categoryId) {
|
||||
// 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) {
|
||||
if (cycles.containsKey(categoryId)) {
|
||||
cycles.remove(categoryId);
|
||||
saveMagicData();
|
||||
@@ -269,9 +299,15 @@ public class MagicController {
|
||||
/**
|
||||
* Associates a magic with a category along with its per-category ordering data.
|
||||
*/
|
||||
public void addMagicToCycle(int magicId, int categoryId, int index, int priority) {
|
||||
MagicCycle cat = cycles.get(categoryId);
|
||||
if (cat == null) return;
|
||||
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;
|
||||
|
||||
MagicAssociation assoc = new MagicAssociation();
|
||||
assoc.magicId = magicId;
|
||||
assoc.index = index;
|
||||
@@ -280,8 +316,8 @@ public class MagicController {
|
||||
saveMagicData();
|
||||
}
|
||||
|
||||
public void removeMagicFromCycle(int magicId, int categoryId) {
|
||||
MagicCycle cat = cycles.get(categoryId);
|
||||
public void removeMagicFromCycle(int magicId, int cycleId) {
|
||||
MagicCycle cat = cycles.get(cycleId);
|
||||
if (cat == null) return;
|
||||
cat.associations.remove(magicId);
|
||||
saveMagicData();
|
||||
|
||||
@@ -2,6 +2,8 @@ package noppes.npcs.controllers.data;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import noppes.npcs.constants.EnumDiagramLayout;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
@@ -10,14 +12,17 @@ import java.util.HashMap;
|
||||
*/
|
||||
public class MagicCycle {
|
||||
public int id = -1;
|
||||
public String title = "";
|
||||
public String name = "";
|
||||
public String displayName = "";
|
||||
public EnumDiagramLayout layout = EnumDiagramLayout.CIRCULAR;
|
||||
public HashMap<Integer, MagicAssociation> associations = new HashMap<>();
|
||||
|
||||
public MagicCycle() {}
|
||||
|
||||
public void readNBT(NBTTagCompound compound) {
|
||||
id = compound.getInteger("CategoryID");
|
||||
title = compound.getString("Title");
|
||||
name = compound.getString("Name");
|
||||
displayName = compound.getString("DisplayName");
|
||||
layout = EnumDiagramLayout.values()[compound.getInteger("Layout")];
|
||||
associations.clear();
|
||||
NBTTagList list = compound.getTagList("Associations", 10); // compound tags
|
||||
for (int i = 0; i < list.tagCount(); i++) {
|
||||
@@ -32,7 +37,9 @@ public class MagicCycle {
|
||||
|
||||
public void writeNBT(NBTTagCompound compound) {
|
||||
compound.setInteger("CategoryID", id);
|
||||
compound.setString("Title", title);
|
||||
compound.setString("Title", name);
|
||||
compound.setString("DisplayName", displayName);
|
||||
compound.setInteger("Layout", layout.ordinal());
|
||||
NBTTagList list = new NBTTagList();
|
||||
for (MagicAssociation assoc : associations.values()) {
|
||||
NBTTagCompound assocTag = new NBTTagCompound();
|
||||
@@ -43,4 +50,40 @@ public class MagicCycle {
|
||||
}
|
||||
compound.setTag("Associations", list);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public void setDisplayName(String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public EnumDiagramLayout getLayout() {
|
||||
return layout;
|
||||
}
|
||||
|
||||
public void setLayout(EnumDiagramLayout layout) {
|
||||
this.layout = layout;
|
||||
}
|
||||
|
||||
public HashMap<Integer, MagicAssociation> getAssociations() {
|
||||
return associations;
|
||||
}
|
||||
|
||||
public void setAssociations(HashMap<Integer, MagicAssociation> associations) {
|
||||
this.associations = associations;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user