From 83c16ae419dea3eeb97f36c4973acef6a697a7c6 Mon Sep 17 00:00:00 2001 From: Kam Date: Thu, 20 Feb 2025 21:44:15 -0500 Subject: [PATCH] Fine Tuning Profile Controller Instance --- .../command/profile/CommandProfileAdmin.java | 20 +++---- .../command/profile/CommandProfileChange.java | 2 +- .../command/profile/CommandProfileCreate.java | 4 +- .../command/profile/CommandProfileList.java | 2 +- .../command/profile/CommandProfileRemove.java | 2 +- .../command/profile/CommandProfileRename.java | 8 +-- .../npcs/controllers/ProfileController.java | 60 +++++++++---------- .../npcs/controllers/data/PlayerData.java | 4 +- 8 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java index ed6001775..c66649112 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java @@ -51,7 +51,7 @@ public class CommandProfileAdmin extends CommandProfileBase { return; } boolean temporary = (args.length >= 5 && args[4].equalsIgnoreCase("temp")); - ProfileOperation result = ProfileController.cloneSlot(sourcePlayer, sourceSlot, destinationSlot, temporary); + ProfileOperation result = ProfileController.Instance.cloneSlot(sourcePlayer, sourceSlot, destinationSlot, temporary); if(result.getResult() == EnumProfileOperation.SUCCESS) { sendResult(sender, "Successfully cloned slot %d from %s to slot %d for %s.", sourceSlot, sourcePlayer, destinationSlot, destinationPlayer); } else if(result.getResult() == EnumProfileOperation.LOCKED) { @@ -81,7 +81,7 @@ public class CommandProfileAdmin extends CommandProfileBase { sendError(sender, "Slot ID must be a number."); return; } - ProfileOperation result = ProfileController.removeSlot(targetPlayer, slotId); + ProfileOperation result = ProfileController.Instance.removeSlot(targetPlayer, slotId); if(result.getResult() == EnumProfileOperation.SUCCESS) { sendResult(sender, "Successfully removed slot %d from %s.", slotId, targetPlayer); } else if(result.getResult() == EnumProfileOperation.LOCKED) { @@ -110,7 +110,7 @@ public class CommandProfileAdmin extends CommandProfileBase { sendError(sender, "Slot ID must be a number."); return; } - ProfileOperation result = ProfileController.changeSlot(targetPlayer, newSlotId); + ProfileOperation result = ProfileController.Instance.changeSlot(targetPlayer, newSlotId); if(result.getResult() == EnumProfileOperation.SUCCESS) { sendResult(sender, "Successfully changed %s's active slot to %d.", targetPlayer, newSlotId); } else if(result.getResult() == EnumProfileOperation.LOCKED) { @@ -132,12 +132,12 @@ public class CommandProfileAdmin extends CommandProfileBase { return; } String targetPlayer = args[0]; - Profile profile = ProfileController.getProfile(targetPlayer); + Profile profile = ProfileController.Instance.getProfile(targetPlayer); if(profile == null) { sendError(sender, "Profile not found for player: %s", targetPlayer); return; } - ProfileOperation result = ProfileController.createSlotInternal(profile); + ProfileOperation result = ProfileController.Instance.createSlotInternal(profile); if(result.getResult() == EnumProfileOperation.SUCCESS) { sendResult(sender, "New slot created successfully for %s.", targetPlayer); } else if(result.getResult() == EnumProfileOperation.LOCKED) { @@ -218,11 +218,11 @@ public class CommandProfileAdmin extends CommandProfileBase { } profile.getSlots().get(slotId).setName(newName); if(profile.getPlayer() != null) { - ProfileController.save(profile.player, profile); + ProfileController.Instance.save(profile.player, profile); } else { - UUID uuid = ProfileController.getUUIDFromUsername(targetPlayer); + UUID uuid = ProfileController.Instance.getUUIDFromUsername(targetPlayer); if(uuid != null) - ProfileController.saveOffline(profile, uuid); + ProfileController.Instance.saveOffline(profile, uuid); } sendResult(sender, "Successfully renamed slot %d for %s to '%s'.", slotId, targetPlayer, newName); } @@ -239,7 +239,7 @@ public class CommandProfileAdmin extends CommandProfileBase { String targetPlayer = args[0]; String backupFileName = args[1] + ".dat"; - Profile profile = ProfileController.getProfile(targetPlayer); + Profile profile = ProfileController.Instance.getProfile(targetPlayer); if(profile == null || profile.player == null) return; @@ -249,7 +249,7 @@ public class CommandProfileAdmin extends CommandProfileBase { sendError(sender, "Backup file %s not found for player %s.", backupFileName, targetPlayer); return; } - boolean success = ProfileController.rollbackProfile(targetPlayer, backupFile); + boolean success = ProfileController.Instance.rollbackProfile(targetPlayer, backupFile); if(success) { sendResult(sender, "Successfully rolled back %s's profile to backup %s.", targetPlayer, backupFileName); } else { diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileChange.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileChange.java index 30f5ca0f9..b7373aa34 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfileChange.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileChange.java @@ -50,7 +50,7 @@ public class CommandProfileChange extends CommandProfileBase { return; } EntityPlayer player = (EntityPlayer)sender; - ProfileOperation result = ProfileController.changeSlot(player, slotId); + ProfileOperation result = ProfileController.Instance.changeSlot(player, slotId); if(result.getResult() == EnumProfileOperation.SUCCESS) { sendResult(sender, "Successfully changed profile slot to %d.", slotId); } else if(result.getResult() == EnumProfileOperation.LOCKED) { diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileCreate.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileCreate.java index 70e6a179a..6b06f266a 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfileCreate.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileCreate.java @@ -39,11 +39,11 @@ public class CommandProfileCreate extends CommandProfileBase { return; } EntityPlayer player = (EntityPlayer)sender; - if(ProfileController.getProfile(player) == null) { + if(ProfileController.Instance.getProfile(player) == null) { sendError(sender, "Profile not found."); return; } - ProfileOperation result = ProfileController.createSlotInternal(ProfileController.getProfile(player)); + ProfileOperation result = ProfileController.Instance.createSlotInternal(ProfileController.Instance.getProfile(player)); if(result.getResult() == EnumProfileOperation.SUCCESS) { sendResult(sender, "New profile slot created successfully."); } else if(result.getResult() == EnumProfileOperation.LOCKED) { diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileList.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileList.java index 0e0af6dee..7c15bc548 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfileList.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileList.java @@ -36,7 +36,7 @@ public class CommandProfileList extends CommandProfileBase { return; } EntityPlayer player = (EntityPlayer)sender; - Profile profile = ProfileController.getProfile(player); + Profile profile = ProfileController.Instance.getProfile(player); if(profile == null) { sendError(sender, "Profile not found."); return; diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileRemove.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileRemove.java index 892c9e592..a80d76f8b 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfileRemove.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileRemove.java @@ -50,7 +50,7 @@ public class CommandProfileRemove extends CommandProfileBase { return; } EntityPlayer player = (EntityPlayer)sender; - ProfileOperation result = ProfileController.removeSlot(player, slotId); + ProfileOperation result = ProfileController.Instance.removeSlot(player, slotId); if(result.getResult() == EnumProfileOperation.SUCCESS) { sendResult(sender, "Successfully removed slot %d from your profile.", slotId); } else if(result.getResult() == EnumProfileOperation.LOCKED) { diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileRename.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileRename.java index f234c1110..50108d7b1 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfileRename.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileRename.java @@ -56,16 +56,16 @@ public class CommandProfileRename extends CommandProfileBase { return; } EntityPlayer player = (EntityPlayer)sender; - if(ProfileController.getProfile(player) == null) { + if(ProfileController.Instance.getProfile(player) == null) { sendError(sender, "Profile not found."); return; } - if(!ProfileController.getProfile(player).getSlots().containsKey(slotId)) { + if(!ProfileController.Instance.getProfile(player).getSlots().containsKey(slotId)) { sendError(sender, "Slot %d not found in your profile.", slotId); return; } - ProfileController.getProfile(player).getSlots().get(slotId).setName(newName); - ProfileController.save(player, ProfileController.getProfile(player)); + ProfileController.Instance.getProfile(player).getSlots().get(slotId).setName(newName); + ProfileController.Instance.save(player, ProfileController.Instance.getProfile(player)); sendResult(sender, "Successfully renamed slot %d to '%s'.", slotId, newName); } } diff --git a/src/main/java/kamkeel/npcs/controllers/ProfileController.java b/src/main/java/kamkeel/npcs/controllers/ProfileController.java index 9e3278c4d..09526352e 100644 --- a/src/main/java/kamkeel/npcs/controllers/ProfileController.java +++ b/src/main/java/kamkeel/npcs/controllers/ProfileController.java @@ -109,7 +109,7 @@ public class ProfileController implements IProfileController { } } - public static synchronized NBTTagCompound load(EntityPlayer player) { + public synchronized NBTTagCompound load(EntityPlayer player) { File saveDir = getProfileDir(); String filename = player.getUniqueID().toString() + ".dat"; try { @@ -123,7 +123,7 @@ public class ProfileController implements IProfileController { return new NBTTagCompound(); } - public static synchronized NBTTagCompound load(UUID uuid) { + public synchronized NBTTagCompound load(UUID uuid) { File saveDir = getProfileDir(); String filename = uuid.toString() + ".dat"; try { @@ -137,7 +137,7 @@ public class ProfileController implements IProfileController { return new NBTTagCompound(); } - public static synchronized void saveOffline(Profile profile, UUID uuid) { + public synchronized void saveOffline(Profile profile, UUID uuid) { profile.setLocked(true); CustomNPCsThreader.customNPCThread.execute(() -> { final NBTTagCompound compound = profile.writeToNBT(); @@ -158,7 +158,7 @@ public class ProfileController implements IProfileController { }); } - public static synchronized void save(EntityPlayer player, Profile profile) { + public synchronized void save(EntityPlayer player, Profile profile) { profile.setLocked(true); CustomNPCsThreader.customNPCThread.execute(() -> { final NBTTagCompound compound = profile.writeToNBT(); @@ -183,7 +183,7 @@ public class ProfileController implements IProfileController { }); } - private static void backupProfile(UUID uuid, NBTTagCompound compound) { + private void backupProfile(UUID uuid, NBTTagCompound compound) { try { File backupDir = new File(getBackupDir(), uuid.toString()); if (!backupDir.exists()) { @@ -204,7 +204,7 @@ public class ProfileController implements IProfileController { } } - public static boolean rollbackProfile(String username, File backupFile) { + public boolean rollbackProfile(String username, File backupFile) { UUID uuid = getUUIDFromUsername(username); if (uuid == null) return false; @@ -235,13 +235,13 @@ public class ProfileController implements IProfileController { } } - public static synchronized void logout(EntityPlayer player) { + public synchronized void logout(EntityPlayer player) { if (player != null) { activeProfiles.remove(player.getUniqueID()); } } - public static Profile getProfile(EntityPlayer player) { + public Profile getProfile(EntityPlayer player) { if (player == null) return null; if (!activeProfiles.containsKey(player.getUniqueID())) @@ -249,14 +249,14 @@ public class ProfileController implements IProfileController { return activeProfiles.get(player.getUniqueID()); } - public static Profile getProfile(UUID uuid) { + public Profile getProfile(UUID uuid) { if (activeProfiles.containsKey(uuid)) return activeProfiles.get(uuid); NBTTagCompound compound = load(uuid); return new Profile(null, compound); } - public static Profile getProfile(String username) { + public Profile getProfile(String username) { EntityPlayer player = getPlayer(username); if (player != null) { return getProfile(player); @@ -268,7 +268,7 @@ public class ProfileController implements IProfileController { } } - public static UUID getUUIDFromUsername(String username) { + public UUID getUUIDFromUsername(String username) { try { URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + username); HttpURLConnection con = (HttpURLConnection) url.openConnection(); @@ -302,7 +302,7 @@ public class ProfileController implements IProfileController { return null; } - private static int getNextAvailableTempSlot(Profile profile) { + private int getNextAvailableTempSlot(Profile profile) { int id = -1; while (profile.getSlots().containsKey(id)) { id--; @@ -310,7 +310,7 @@ public class ProfileController implements IProfileController { return id; } - private static ProfileOperation cloneSlotInternal(Profile profile, int sourceSlotId, int destinationSlotId, boolean temporary) { + private ProfileOperation cloneSlotInternal(Profile profile, int sourceSlotId, int destinationSlotId, boolean temporary) { if (profile.isLocked()) { return ProfileOperation.locked(MSG_PROFILE_LOCKED_CLONE); } @@ -339,14 +339,14 @@ public class ProfileController implements IProfileController { ISlot sourceSlot = profile.getSlots().get(sourceSlotId); Map newComponents = new HashMap<>(); for (String key : sourceSlot.getComponents().keySet()) { - newComponents.put(key, sourceSlot.getComponentData(key).copy()); + newComponents.put(key, (NBTTagCompound) sourceSlot.getComponentData(key).copy()); } Slot clonedSlot = new Slot(destinationSlotId, "Cloned Slot " + destinationSlotId, System.currentTimeMillis(), temporary, newComponents); profile.getSlots().put(destinationSlotId, clonedSlot); return ProfileOperation.success(MSG_CLONE_SUCCESS); } - private static ProfileOperation removeSlotInternal(Profile profile, int slotId) { + private ProfileOperation removeSlotInternal(Profile profile, int slotId) { if (profile.isLocked()) { return ProfileOperation.locked(MSG_PROFILE_LOCKED_REMOVE); } @@ -360,7 +360,7 @@ public class ProfileController implements IProfileController { return ProfileOperation.success(MSG_REMOVE_SUCCESS); } - public static ProfileOperation createSlotInternal(Profile profile) { + public ProfileOperation createSlotInternal(Profile profile) { if (profile.isLocked()) { return ProfileOperation.locked(MSG_PROFILE_LOCKED_CREATE); } @@ -386,7 +386,7 @@ public class ProfileController implements IProfileController { return ProfileOperation.success(MSG_NEW_SLOT_CREATED); } - private static ProfileOperation changeSlotInternal(Profile profile, int newSlotId) { + private ProfileOperation changeSlotInternal(Profile profile, int newSlotId) { if (profile.isLocked()) { return ProfileOperation.locked(MSG_PROFILE_LOCKED_CHANGE); } @@ -446,7 +446,7 @@ public class ProfileController implements IProfileController { return ProfileOperation.success(MSG_CHANGE_SUCCESS); } - public static ProfileOperation cloneSlot(EntityPlayer player, int sourceSlotId, int destinationSlotId, boolean temporary) { + public ProfileOperation cloneSlot(EntityPlayer player, int sourceSlotId, int destinationSlotId, boolean temporary) { Profile profile = getProfile(player); if (profile == null) return ProfileOperation.error(MSG_PLAYER_NOT_FOUND); @@ -457,7 +457,7 @@ public class ProfileController implements IProfileController { return result; } - public static ProfileOperation cloneSlot(UUID uuid, int sourceSlotId, int destinationSlotId, boolean temporary) { + public ProfileOperation cloneSlot(UUID uuid, int sourceSlotId, int destinationSlotId, boolean temporary) { Profile profile = getProfile(uuid); if (profile == null) return ProfileOperation.error(MSG_PLAYER_NOT_FOUND); @@ -471,7 +471,7 @@ public class ProfileController implements IProfileController { return result; } - public static ProfileOperation cloneSlot(String username, int sourceSlotId, int destinationSlotId, boolean temporary) { + public ProfileOperation cloneSlot(String username, int sourceSlotId, int destinationSlotId, boolean temporary) { Profile profile = getProfile(username); if (profile == null) return ProfileOperation.error(MSG_PLAYER_NOT_FOUND); @@ -488,7 +488,7 @@ public class ProfileController implements IProfileController { return result; } - public static ProfileOperation removeSlot(EntityPlayer player, int slotId) { + public ProfileOperation removeSlot(EntityPlayer player, int slotId) { Profile profile = getProfile(player); if (profile == null) return ProfileOperation.error(MSG_PLAYER_NOT_FOUND); @@ -499,7 +499,7 @@ public class ProfileController implements IProfileController { return result; } - public static ProfileOperation removeSlot(String username, int slotId) { + public ProfileOperation removeSlot(String username, int slotId) { Profile profile = getProfile(username); if (profile == null) return ProfileOperation.error(MSG_PLAYER_NOT_FOUND); @@ -516,7 +516,7 @@ public class ProfileController implements IProfileController { return result; } - public static ProfileOperation changeSlot(EntityPlayer player, int newSlotId) { + public ProfileOperation changeSlot(EntityPlayer player, int newSlotId) { Profile profile = getProfile(player); if (profile == null) return ProfileOperation.error(MSG_PLAYER_NOT_FOUND); @@ -526,7 +526,7 @@ public class ProfileController implements IProfileController { return result; } - public static ProfileOperation changeSlot(String username, int newSlotId) { + public ProfileOperation changeSlot(String username, int newSlotId) { Profile profile = getProfile(username); if (profile == null) return ProfileOperation.error(MSG_PLAYER_NOT_FOUND); @@ -556,13 +556,13 @@ public class ProfileController implements IProfileController { profile.getSlots().put(profile.getCurrentSlotId(), slot); } for (IProfileData profileData : profileTypes.values()) { - NBTTagCompound cloned = profileData.getCurrentNBT(player).copy(); + NBTTagCompound cloned = (NBTTagCompound) profileData.getCurrentNBT(player).copy(); slot.setComponentData(profileData.getTagName(), cloned); } slot.setLastLoaded(System.currentTimeMillis()); } - public static void loadSlotData(EntityPlayer player) { + public void loadSlotData(EntityPlayer player) { if (player == null || !activeProfiles.containsKey(player.getUniqueID())) return; Profile profile = activeProfiles.get(player.getUniqueID()); @@ -582,7 +582,7 @@ public class ProfileController implements IProfileController { } } - public static List getProfileInfo(EntityPlayer player, int slotId) { + public List getProfileInfo(EntityPlayer player, int slotId) { List infoList = new ArrayList<>(); Profile profile = getProfile(player); if (profile == null) @@ -608,7 +608,7 @@ public class ProfileController implements IProfileController { return infoList; } - public static boolean allowSlotPermission(EntityPlayer player) { + public boolean allowSlotPermission(EntityPlayer player) { Profile profile = getProfile(player); int currentSlots = profile.getSlots().size(); if (CustomNpcsPermissions.hasCustomPermission(player, "customnpcs.profile.max.*")) { @@ -627,7 +627,7 @@ public class ProfileController implements IProfileController { return currentSlots < highestAllowed; } - public static EntityPlayerMP getPlayer(String username) { + public EntityPlayerMP getPlayer(String username) { return MinecraftServer.getServer().getConfigurationManager().func_152612_a(username); } @@ -635,7 +635,7 @@ public class ProfileController implements IProfileController { public IProfile getProfile(IPlayer player) { if(player == null || player.getMCEntity() == null) return null; - return ProfileController.getProfile((EntityPlayer) player.getMCEntity()); + return getProfile((EntityPlayer) player.getMCEntity()); } @Override diff --git a/src/main/java/noppes/npcs/controllers/data/PlayerData.java b/src/main/java/noppes/npcs/controllers/data/PlayerData.java index 027d10a24..041b15c0c 100644 --- a/src/main/java/noppes/npcs/controllers/data/PlayerData.java +++ b/src/main/java/noppes/npcs/controllers/data/PlayerData.java @@ -376,9 +376,9 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData { public synchronized void save() { // Don't Save this is a Modification of a Profile's PlayerData - Profile profile = ProfileController.getProfile(UUID.fromString(uuid)); + Profile profile = ProfileController.Instance.getProfile(UUID.fromString(uuid)); if(profile != null && profile.currentSlotId != this.profileSlot){ - ProfileController.saveOffline(profile, UUID.fromString(uuid)); + ProfileController.Instance.saveOffline(profile, UUID.fromString(uuid)); return; }