Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vidal committed 2026-02-17 23:02:39 -03:00
commit 90e146e7b0
12 files changed
+171 -185

No files matched your search

@@ -33,20 +33,19 @@ public class AbilityCommand extends CommandKamkeelBase {
desc = "List all custom abilities"
)
public void list(ICommandSender sender, String[] args) {
Set<String> uuids = AbilityController.Instance.getCustomAbilityIds();
if (uuids.isEmpty()) {
Set<String> names = AbilityController.Instance.getCustomAbilityNames();
if (names.isEmpty()) {
sendResult(sender, "No custom abilities found.");
return;
}
sendResult(sender, "Custom Abilities (" + uuids.size() + "):");
for (String uuid : uuids) {
Ability ability = AbilityController.Instance.getCustomAbility(uuid);
sendResult(sender, "Custom Abilities (" + names.size() + "):");
for (String name : names) {
Ability ability = AbilityController.Instance.getCustomAbility(name);
if (ability != null) {
String name = ability.getName() != null ? ability.getName() : uuid;
sendResult(sender, " - \u00A7b" + name + "\u00A77 (" + ability.getTypeId() + ") [" + uuid + "]");
sendResult(sender, " - \u00A7b" + name + "\u00A77 (" + ability.getTypeId() + ")");
} else {
sendResult(sender, " - \u00A7b" + uuid);
sendResult(sender, " - \u00A7b" + name);
}
}
}
@@ -82,23 +81,23 @@ public class AbilityCommand extends CommandKamkeelBase {
}
@SubCommand(
desc = "Delete a custom ability by UUID",
usage = "<uuid>"
desc = "Delete a custom ability by name",
usage = "<name>"
)
public void delete(ICommandSender sender, String[] args) {
if (args.length < 1) {
sendError(sender, "Usage: /kam ability delete <uuid>");
sendError(sender, "Usage: /kam ability delete <name>");
return;
}
String uuid = args[0];
if (!AbilityController.Instance.hasCustomAbility(uuid)) {
sendError(sender, "No custom ability with UUID: " + uuid);
String name = joinArgs(args, 0);
if (!AbilityController.Instance.hasCustomAbilityName(name)) {
sendError(sender, "No custom ability with name: " + name);
return;
}
AbilityController.Instance.deleteCustomAbility(uuid);
sendResult(sender, "Deleted ability: \u00A7b" + uuid);
AbilityController.Instance.deleteCustomAbility(name);
sendResult(sender, "Deleted ability: \u00A7b" + name);
}
@SubCommand(
@@ -189,7 +188,7 @@ public class AbilityCommand extends CommandKamkeelBase {
return;
}
// Use the ability's canonical ID for storage (registry key for built-in, UUID for custom)
// Use the ability's canonical ID for storage (registry key for built-in, name for custom)
String canonicalKey = ability.getId();
if (canonicalKey == null || canonicalKey.isEmpty()) {
canonicalKey = abilityKey; // Fallback to user-provided key
@@ -24,6 +24,7 @@ import java.io.File;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
public class AbilityController implements IAbilityHandler {
@@ -40,6 +41,11 @@ public class AbilityController implements IAbilityHandler {
private final List<IAbilityExtender> extenders = new ArrayList<>();
private final List<Predicate<EntityPlayer>> flightCheckers = new ArrayList<>();
// ── Legacy Migration ─────────────────────────────────────────────────────
private static final Pattern UUID_PATTERN =
Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
Pattern.CASE_INSENSITIVE);
// ── Derived State ────────────────────────────────────────────────────────
private final Set<String> builtInTypeIds = new HashSet<>();
@@ -156,6 +162,7 @@ public class AbilityController implements IAbilityHandler {
public void load() {
customAbilities.clear();
int migrated = 0;
File dir = getDir();
File[] files = dir.exists() ? dir.listFiles() : null;
@@ -164,14 +171,39 @@ public class AbilityController implements IAbilityHandler {
if (!file.isFile() || !file.getName().endsWith(".json")) continue;
try {
String filename = file.getName();
String uuid = filename.substring(0, filename.length() - 5);
String key = filename.substring(0, filename.length() - 5);
NBTTagCompound nbt = NBTJsonUtil.LoadFile(file);
Ability ability = fromNBT(nbt);
if (ability != null) {
ability.setId(uuid);
customAbilities.put(uuid, ability);
if (ability == null) continue;
// Legacy migration: UUID-named files get renamed to name-based
if (UUID_PATTERN.matcher(key).matches()) {
String name = ability.getName();
if (name == null || name.isEmpty()) {
name = key; // Fallback to UUID if no name stored
}
// Ensure unique name
while (hasCustomAbilityName(name) || nameFileExists(dir, name)) {
name = name + "_";
}
ability.setName(name);
// Rename file from UUID.json to name.json
File newFile = new File(dir, name + ".json");
if (file.renameTo(newFile)) {
LogWriter.info("Migrated ability file: " + key + ".json -> " + name + ".json");
} else {
LogWriter.error("Failed to rename ability file: " + key + ".json -> " + name + ".json");
}
key = name;
migrated++;
}
ability.setId(key);
customAbilities.put(key, ability);
} catch (Exception e) {
LogWriter.error("Error loading custom ability: " + file.getAbsolutePath(), e);
}
@@ -179,21 +211,57 @@ public class AbilityController implements IAbilityHandler {
}
customAbilityRevision++;
if (migrated > 0) {
LogWriter.info("Migrated " + migrated + " UUID-named abilities to name-based files");
}
LogWriter.info("Loaded " + customAbilities.size() + " custom abilities");
}
private boolean nameFileExists(File dir, String name) {
return new File(dir, name + ".json").exists();
}
public boolean saveCustomAbility(Ability ability) {
if (ability == null) return false;
String uuid = ability.getId();
if (uuid == null || uuid.isEmpty()) {
uuid = UUID.randomUUID().toString();
ability.setId(uuid);
String name = ability.getName();
if (name == null || name.isEmpty()) return false;
// Ensure unique name for new abilities (no existing id)
String oldName = ability.getId();
boolean isNew = oldName == null || oldName.isEmpty();
if (isNew) {
while (hasCustomAbilityName(name)) {
name = name + "_";
}
ability.setName(name);
}
// Handle rename: old id (previous name) differs from current name
if (!isNew && !oldName.equals(name)) {
// Check uniqueness of new name (excluding the entry being renamed)
String testName = name;
while (customAbilities.containsKey(testName) && !testName.equals(oldName)) {
testName = testName + "_";
}
if (!testName.equals(name)) {
name = testName;
ability.setName(name);
}
// Delete old file and remove old map entry
File oldFile = new File(getDir(), oldName + ".json");
if (oldFile.exists()) {
oldFile.delete();
}
customAbilities.remove(oldName);
}
ability.setId(name);
File dir = getDir();
File fileNew = new File(dir, uuid + ".json_new");
File fileCurrent = new File(dir, uuid + ".json");
File fileNew = new File(dir, name + ".json_new");
File fileCurrent = new File(dir, name + ".json");
try {
NBTTagCompound nbt = ability.writeNBT();
@@ -207,52 +275,43 @@ public class AbilityController implements IAbilityHandler {
fileNew.delete();
}
customAbilities.put(uuid, ability);
customAbilities.put(name, ability);
customAbilityRevision++;
LogWriter.info("Saved custom ability: " + ability.getName() + " [" + uuid + "]");
LogWriter.info("Saved custom ability: " + name);
SyncController.syncAllCustomAbilities();
return true;
} catch (Exception e) {
LogWriter.error("Error saving custom ability: " + uuid, e);
LogWriter.error("Error saving custom ability: " + name, e);
return false;
}
}
public boolean deleteCustomAbility(String uuid) {
if (uuid == null || uuid.isEmpty()) return false;
public boolean deleteCustomAbility(String name) {
if (name == null || name.isEmpty()) return false;
Ability removed = customAbilities.remove(uuid);
Ability removed = customAbilities.remove(name);
if (removed == null) return false;
File dir = getDir();
File file = new File(dir, uuid + ".json");
File file = new File(dir, name + ".json");
if (file.exists()) {
file.delete();
}
customAbilityRevision++;
LogWriter.info("Deleted custom ability: " + uuid);
LogWriter.info("Deleted custom ability: " + name);
SyncController.syncAllCustomAbilities();
return true;
}
public Ability getCustomAbility(String uuid) {
return customAbilities.get(uuid);
public Ability getCustomAbility(String name) {
return customAbilities.get(name);
}
public boolean hasCustomAbility(String uuid) {
return customAbilities.containsKey(uuid);
}
public Set<String> getCustomAbilityIds() {
public Set<String> getCustomAbilityNames() {
return new LinkedHashSet<>(customAbilities.keySet());
}
public String getCustomAbilityName(String uuid) {
Ability a = customAbilities.get(uuid);
return a != null ? a.getName() : null;
}
public Map<String, Ability> getCustomAbilities() {
return customAbilities;
}
@@ -300,7 +359,7 @@ public class AbilityController implements IAbilityHandler {
}
}
// Custom: exact UUID
// Custom: exact name
Ability custom = customAbilities.get(key);
if (custom != null) return custom.deepCopy();
@@ -322,7 +381,7 @@ public class AbilityController implements IAbilityHandler {
keys.add(id);
}
}
// Use ability names for custom abilities (instead of UUIDs) for readable tab completion
// Custom abilities keyed by name
for (Ability ability : customAbilities.values()) {
String name = ability.getName();
if (name != null && !name.isEmpty()) {
@@ -342,7 +401,7 @@ public class AbilityController implements IAbilityHandler {
}
}
}
// Use ability names for custom abilities (instead of UUIDs) for readable tab completion
// Custom abilities keyed by name
for (Ability ability : customAbilities.values()) {
if (ability.getAllowedBy().allowsPlayer()) {
String name = ability.getName();
@@ -378,7 +437,7 @@ public class AbilityController implements IAbilityHandler {
if (ability.getId() != null && ability.getId().equalsIgnoreCase(key)) return true;
}
// Custom: exact UUID
// Custom: exact name
if (customAbilities.containsKey(key)) return true;
// Custom: case-insensitive name
@@ -549,17 +608,17 @@ public class AbilityController implements IAbilityHandler {
}
@Override
public String[] getCustomAbilityIdArray() {
public String[] getCustomAbilityNameArray() {
return customAbilities.keySet().toArray(new String[0]);
}
@Override
public boolean hasCustomAbilityId(String uuid) {
return customAbilities.containsKey(uuid);
public boolean hasCustomAbilityName(String name) {
return customAbilities.containsKey(name);
}
@Override
public boolean deleteCustomAbilityById(String uuid) {
return deleteCustomAbility(uuid);
public boolean deleteCustomAbilityByName(String name) {
return deleteCustomAbility(name);
}
}
@@ -33,7 +33,7 @@ public class AbilitySlot {
}
/**
* Create a slot that references a built-in ability (by name) or custom ability (by UUID).
* Create a slot that references a built-in ability (by name) or custom ability (by name).
*/
public static AbilitySlot reference(String referenceId) {
AbilitySlot slot = new AbilitySlot();
@@ -20,18 +20,18 @@ import net.minecraft.nbt.NBTTagCompound;
import java.io.IOException;
/**
* Request packet to get a specific custom ability by UUID.
* Request packet to get a specific custom ability by name.
*/
public final class CustomAbilityGetPacket extends AbstractPacket {
public static String packetName = "Request|CustomAbilityGet";
private String uuid;
private String name;
public CustomAbilityGetPacket() {
}
public CustomAbilityGetPacket(String uuid) {
this.uuid = uuid;
public CustomAbilityGetPacket(String name) {
this.name = name;
}
@Override
@@ -47,7 +47,7 @@ public final class CustomAbilityGetPacket extends AbstractPacket {
@SideOnly(Side.CLIENT)
@Override
public void sendData(ByteBuf out) throws IOException {
ByteBufUtils.writeString(out, uuid);
ByteBufUtils.writeString(out, name);
}
@Override
@@ -58,12 +58,10 @@ public final class CustomAbilityGetPacket extends AbstractPacket {
if (!PacketUtil.verifyItemPacket(packetName, EnumItemPacketType.WAND, player))
return;
String id = ByteBufUtils.readString(in);
Ability ability = AbilityController.Instance.getCustomAbility(id);
String abilityName = ByteBufUtils.readString(in);
Ability ability = AbilityController.Instance.getCustomAbility(abilityName);
if (ability != null) {
NBTTagCompound compound = ability.writeNBT();
// Include the UUID so the client can track it
compound.setString("_uuid", id);
GuiDataPacket.sendGuiData((EntityPlayerMP) player, compound);
}
}
@@ -19,18 +19,18 @@ import noppes.npcs.NoppesUtilServer;
import java.io.IOException;
/**
* Request packet to remove a custom ability by UUID.
* Request packet to remove a custom ability by name.
*/
public final class CustomAbilityRemovePacket extends AbstractPacket {
public static String packetName = "Request|CustomAbilityRemove";
private String uuid;
private String name;
public CustomAbilityRemovePacket() {
}
public CustomAbilityRemovePacket(String uuid) {
this.uuid = uuid;
public CustomAbilityRemovePacket(String name) {
this.name = name;
}
@Override
@@ -51,7 +51,7 @@ public final class CustomAbilityRemovePacket extends AbstractPacket {
@SideOnly(Side.CLIENT)
@Override
public void sendData(ByteBuf out) throws IOException {
ByteBufUtils.writeString(out, uuid);
ByteBufUtils.writeString(out, name);
}
@Override
@@ -71,7 +71,6 @@ public final class CustomAbilitySavePacket extends AbstractPacket {
AbilityController.Instance.saveCustomAbility(ability);
NoppesUtilServer.sendCustomAbilitiesData((EntityPlayerMP) player);
NBTTagCompound responseNbt = ability.writeNBT();
responseNbt.setString("_uuid", ability.getId());
GuiDataPacket.sendGuiData((EntityPlayerMP) player, responseNbt);
}
}