mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 15:49:43 +00:00
Profile Implementations
This commit is contained in:
1 parent
8ed86f5ec7
commit
2ebe4ed9c5
11 files changed
+769
-114
No files matched your search
@@ -1,18 +1,27 @@
|
||||
package kamkeel.npcs.controllers;
|
||||
|
||||
import kamkeel.npcs.controllers.data.IProfileData;
|
||||
import kamkeel.npcs.controllers.data.ProfileOperation;
|
||||
import kamkeel.npcs.controllers.data.Profile;
|
||||
import kamkeel.npcs.controllers.data.Slot;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.LogWriter;
|
||||
import noppes.npcs.controllers.data.PlayerData;
|
||||
import noppes.npcs.util.CustomNPCsThreader;
|
||||
import noppes.npcs.util.NBTJsonUtil;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -20,7 +29,6 @@ public class ProfileController {
|
||||
|
||||
public static HashMap<String, IProfileData> profileTypes;
|
||||
public static HashMap<UUID, Profile> activeProfiles;
|
||||
|
||||
public static String profile_directory = "profiles";
|
||||
|
||||
public ProfileController(){
|
||||
@@ -28,52 +36,45 @@ public class ProfileController {
|
||||
activeProfiles = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register during ServerStartEvent()
|
||||
*
|
||||
* @param type - IProfile Type to always Save/Load
|
||||
* @return true/false if profile was successfully added
|
||||
*/
|
||||
// ---------- Registration & I/O ----------
|
||||
|
||||
public static boolean registerProfileType(IProfileData type){
|
||||
if(profileTypes.containsKey(type.getTagName()))
|
||||
return false;
|
||||
|
||||
profileTypes.put(type.getTagName(), type);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static File getProfileDir(){
|
||||
try{
|
||||
try {
|
||||
File file = new File(CustomNpcs.getWorldSaveDirectory(), profile_directory);
|
||||
if(!file.exists())
|
||||
file.mkdir();
|
||||
return file;
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Login for online players
|
||||
public static synchronized void login(EntityPlayer player){
|
||||
if(player == null)
|
||||
return;
|
||||
|
||||
Profile profile;
|
||||
if(activeProfiles.containsKey(player.getUniqueID())){
|
||||
profile = activeProfiles.get(player.getUniqueID());
|
||||
profile.player = player;
|
||||
} else {
|
||||
NBTTagCompound profileCompound = load(player);
|
||||
profile = new Profile(player, profileCompound);
|
||||
NBTTagCompound compound = load(player);
|
||||
profile = new Profile(player, compound);
|
||||
activeProfiles.put(player.getUniqueID(), profile);
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized NBTTagCompound load(EntityPlayer player) {
|
||||
File saveDir = getProfileDir();
|
||||
String filename = player.getUniqueID().toString();
|
||||
filename += ".dat";
|
||||
String filename = player.getUniqueID().toString() + ".dat";
|
||||
try {
|
||||
File file = new File(saveDir, filename);
|
||||
if(file.exists()){
|
||||
@@ -85,51 +86,354 @@ public class ProfileController {
|
||||
return new NBTTagCompound();
|
||||
}
|
||||
|
||||
public static synchronized NBTTagCompound load(UUID uuid) {
|
||||
File saveDir = getProfileDir();
|
||||
String filename = uuid.toString() + ".dat";
|
||||
try {
|
||||
File file = new File(saveDir, filename);
|
||||
if(file.exists()){
|
||||
return NBTJsonUtil.loadNBTData(file);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogWriter.error("Error loading profile file: " + filename, e);
|
||||
}
|
||||
return new NBTTagCompound();
|
||||
}
|
||||
|
||||
// Offline save (synchronous) when no EntityPlayer is available
|
||||
public static synchronized void saveOffline(Profile profile, UUID uuid) {
|
||||
final NBTTagCompound compound = profile.writeToNBT();
|
||||
final String filename = uuid.toString() + ".dat";
|
||||
try {
|
||||
File saveDir = getProfileDir();
|
||||
File fileNew = new File(saveDir, filename + "_new");
|
||||
File fileOld = new File(saveDir, filename);
|
||||
CompressedStreamTools.writeCompressed(compound, new FileOutputStream(fileNew));
|
||||
if(fileOld.exists()){
|
||||
fileOld.delete();
|
||||
}
|
||||
fileNew.renameTo(fileOld);
|
||||
} catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Save for online players (asynchronous)
|
||||
public static synchronized void save(EntityPlayer player, Profile profile) {
|
||||
profile.locked = true;
|
||||
CustomNPCsThreader.customNPCThread.execute(() -> {
|
||||
final NBTTagCompound compound = profile.writeToNBT();
|
||||
final String filename = player.getUniqueID() + ".dat";
|
||||
try {
|
||||
File saveDir = getProfileDir();
|
||||
File file = new File(saveDir, filename + "_new");
|
||||
File file1 = new File(saveDir, filename);
|
||||
CompressedStreamTools.writeCompressed(compound, new FileOutputStream(file));
|
||||
if(file1.exists()){
|
||||
file1.delete();
|
||||
File fileNew = new File(saveDir, filename + "_new");
|
||||
File fileOld = new File(saveDir, filename);
|
||||
CompressedStreamTools.writeCompressed(compound, new FileOutputStream(fileNew));
|
||||
if(fileOld.exists()){
|
||||
fileOld.delete();
|
||||
}
|
||||
file.renameTo(file1);
|
||||
fileNew.renameTo(fileOld);
|
||||
} catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
} finally {
|
||||
profile.locked = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void changeSlot(EntityPlayer player, int slotNumber){
|
||||
if(!activeProfiles.containsKey(player.getUniqueID()))
|
||||
return;
|
||||
|
||||
Profile profile = activeProfiles.get(player.getUniqueID());
|
||||
if(profile.currentID == slotNumber)
|
||||
return;
|
||||
|
||||
// Saves Current Slot Data
|
||||
saveSlotData(player);
|
||||
|
||||
// Sets Player to New Slot
|
||||
changePlayerSlot(player, slotNumber);
|
||||
|
||||
// Save Slot File
|
||||
save(player, profile);
|
||||
// Logout removes an active profile
|
||||
public static synchronized void logout(EntityPlayer player) {
|
||||
if(player != null) {
|
||||
activeProfiles.remove(player.getUniqueID());
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveSlotData(EntityPlayer player){
|
||||
if(!activeProfiles.containsKey(player.getUniqueID()))
|
||||
return;
|
||||
// ---------- Profile Retrieval Helpers ----------
|
||||
|
||||
// Get profile via EntityPlayer (online)
|
||||
public static Profile getProfile(EntityPlayer player) {
|
||||
if(player == null) return null;
|
||||
if(!activeProfiles.containsKey(player.getUniqueID()))
|
||||
login(player);
|
||||
return activeProfiles.get(player.getUniqueID());
|
||||
}
|
||||
|
||||
// Get profile via UUID (may be offline)
|
||||
public static Profile getProfile(UUID uuid) {
|
||||
if(activeProfiles.containsKey(uuid))
|
||||
return activeProfiles.get(uuid);
|
||||
NBTTagCompound compound = load(uuid);
|
||||
return new Profile(null, compound);
|
||||
}
|
||||
|
||||
// Get profile via username (tries online first, then offline via Mojang API)
|
||||
public static Profile getProfile(String username) {
|
||||
EntityPlayer player = getPlayer(username);
|
||||
if(player != null) {
|
||||
return getProfile(player);
|
||||
} else {
|
||||
UUID uuid = getUUIDFromUsername(username);
|
||||
if(uuid == null) return null;
|
||||
return getProfile(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Mojang API UUID Lookup ----------
|
||||
|
||||
public static UUID getUUIDFromUsername(String username) {
|
||||
try {
|
||||
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + username);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.setConnectTimeout(5000);
|
||||
con.setReadTimeout(5000);
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
StringBuilder temp = new StringBuilder();
|
||||
String line;
|
||||
while((line = in.readLine()) != null){
|
||||
temp.append(line);
|
||||
}
|
||||
in.close();
|
||||
con.disconnect();
|
||||
String response = temp.toString();
|
||||
if(response.contains("\"id\"")) {
|
||||
int idIndex = response.indexOf("\"id\"");
|
||||
int colonIndex = response.indexOf(":", idIndex);
|
||||
int quoteStart = response.indexOf("\"", colonIndex);
|
||||
int quoteEnd = response.indexOf("\"", quoteStart + 1);
|
||||
String idString = response.substring(quoteStart + 1, quoteEnd);
|
||||
idString = idString.replaceFirst(
|
||||
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)",
|
||||
"$1-$2-$3-$4-$5"
|
||||
);
|
||||
return UUID.fromString(idString);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LogWriter.error("Error retrieving UUID for " + username, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------- Internal Slot Operation Helpers ----------
|
||||
|
||||
// Returns the next available temporary (negative) slot id (starting at -1)
|
||||
private static int getNextAvailableTempSlot(Profile profile) {
|
||||
int id = -1;
|
||||
while(profile.slots.containsKey(id)) {
|
||||
id--;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
// Clone a slot within a Profile.
|
||||
// sourceSlotId is the slot to copy.
|
||||
// destinationSlotId is the target id for non-temporary clones.
|
||||
// If temporary==true, destinationSlotId is computed automatically.
|
||||
private static ProfileOperation cloneSlotInternal(Profile profile, int sourceSlotId, int destinationSlotId, boolean temporary) {
|
||||
if(profile.locked) {
|
||||
LogWriter.error("Profile is locked; cannot clone slot.");
|
||||
return ProfileOperation.LOCKED;
|
||||
}
|
||||
if(!profile.slots.containsKey(sourceSlotId)) {
|
||||
LogWriter.error("Source slot " + sourceSlotId + " does not exist.");
|
||||
return ProfileOperation.ERROR;
|
||||
}
|
||||
if(destinationSlotId == profile.currentID) {
|
||||
LogWriter.error("Cannot clone to the current active slot (" + profile.currentID + ").");
|
||||
return ProfileOperation.ERROR;
|
||||
}
|
||||
if(temporary) {
|
||||
destinationSlotId = getNextAvailableTempSlot(profile);
|
||||
} else {
|
||||
if(destinationSlotId <= 0) {
|
||||
LogWriter.error("Invalid destination slot id for a non-temporary clone.");
|
||||
return ProfileOperation.ERROR;
|
||||
}
|
||||
}
|
||||
Slot sourceSlot = profile.slots.get(sourceSlotId);
|
||||
NBTTagCompound clonedData = (NBTTagCompound) sourceSlot.getCompound().copy();
|
||||
Slot clonedSlot = new Slot(destinationSlotId, "Cloned Slot " + destinationSlotId, clonedData, System.currentTimeMillis(), temporary);
|
||||
profile.slots.put(destinationSlotId, clonedSlot);
|
||||
return ProfileOperation.SUCCESS;
|
||||
}
|
||||
|
||||
// Remove a slot from a Profile.
|
||||
private static ProfileOperation removeSlotInternal(Profile profile, int slotId) {
|
||||
if(profile.locked) {
|
||||
LogWriter.error("Profile is locked; cannot remove slot.");
|
||||
return ProfileOperation.LOCKED;
|
||||
}
|
||||
if(slotId == profile.currentID) {
|
||||
LogWriter.error("Cannot remove the currently active slot (" + profile.currentID + ").");
|
||||
return ProfileOperation.ERROR;
|
||||
}
|
||||
if(!profile.slots.containsKey(slotId)) {
|
||||
LogWriter.error("Slot " + slotId + " does not exist.");
|
||||
return ProfileOperation.ERROR;
|
||||
}
|
||||
profile.slots.remove(slotId);
|
||||
return ProfileOperation.SUCCESS;
|
||||
}
|
||||
|
||||
// Change the active slot in a Profile.
|
||||
// Saves current slot data before switching.
|
||||
private static ProfileOperation changeSlotInternal(Profile profile, int newSlotId) {
|
||||
if(profile.locked) {
|
||||
LogWriter.error("Profile is locked; cannot change slot.");
|
||||
return ProfileOperation.LOCKED;
|
||||
}
|
||||
if(profile.player != null)
|
||||
saveSlotData(profile.player);
|
||||
// If the new slot doesn't exist, create it.
|
||||
if(!profile.slots.containsKey(newSlotId)) {
|
||||
Slot newSlot = new Slot(newSlotId, "Slot " + newSlotId);
|
||||
newSlot.setCompound(new NBTTagCompound());
|
||||
profile.slots.put(newSlotId, newSlot);
|
||||
}
|
||||
|
||||
profile.currentID = newSlotId;
|
||||
if(profile.player != null)
|
||||
loadSlotData(profile.player);
|
||||
|
||||
// Update PlayerData so that PlayerData.get(player).profileSlot matches Profile.currentID
|
||||
if(profile.player != null) {
|
||||
PlayerData pdata = PlayerData.get(profile.player);
|
||||
pdata.profileSlot = newSlotId;
|
||||
pdata.save();
|
||||
}
|
||||
return ProfileOperation.SUCCESS;
|
||||
}
|
||||
|
||||
// ---------- Public Operations (Overloaded for EntityPlayer, UUID, or username) ----------
|
||||
|
||||
// ----- Clone Slot -----
|
||||
public static ProfileOperation cloneSlot(EntityPlayer player, int sourceSlotId, int destinationSlotId, boolean temporary) {
|
||||
Profile profile = getProfile(player);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = cloneSlotInternal(profile, sourceSlotId, destinationSlotId, temporary);
|
||||
if(result == ProfileOperation.SUCCESS && player != null)
|
||||
save(player, profile);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ProfileOperation cloneSlot(UUID uuid, int sourceSlotId, int destinationSlotId, boolean temporary) {
|
||||
Profile profile = getProfile(uuid);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = cloneSlotInternal(profile, sourceSlotId, destinationSlotId, temporary);
|
||||
if(result == ProfileOperation.SUCCESS) {
|
||||
if(profile.player != null)
|
||||
save(profile.player, profile);
|
||||
else
|
||||
saveOffline(profile, uuid);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ProfileOperation cloneSlot(String username, int sourceSlotId, int destinationSlotId, boolean temporary) {
|
||||
Profile profile = getProfile(username);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = cloneSlotInternal(profile, sourceSlotId, destinationSlotId, temporary);
|
||||
if(result == ProfileOperation.SUCCESS) {
|
||||
if(profile.player != null)
|
||||
save(profile.player, profile);
|
||||
else {
|
||||
UUID uuid = getUUIDFromUsername(username);
|
||||
if(uuid != null)
|
||||
saveOffline(profile, uuid);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ----- Remove Slot -----
|
||||
public static ProfileOperation removeSlot(EntityPlayer player, int slotId) {
|
||||
Profile profile = getProfile(player);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = removeSlotInternal(profile, slotId);
|
||||
if(result == ProfileOperation.SUCCESS && player != null)
|
||||
save(player, profile);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ProfileOperation removeSlot(UUID uuid, int slotId) {
|
||||
Profile profile = getProfile(uuid);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = removeSlotInternal(profile, slotId);
|
||||
if(result == ProfileOperation.SUCCESS) {
|
||||
if(profile.player != null)
|
||||
save(profile.player, profile);
|
||||
else
|
||||
saveOffline(profile, uuid);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ProfileOperation removeSlot(String username, int slotId) {
|
||||
Profile profile = getProfile(username);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = removeSlotInternal(profile, slotId);
|
||||
if(result == ProfileOperation.SUCCESS) {
|
||||
if(profile.player != null)
|
||||
save(profile.player, profile);
|
||||
else {
|
||||
UUID uuid = getUUIDFromUsername(username);
|
||||
if(uuid != null)
|
||||
saveOffline(profile, uuid);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ----- Change Slot -----
|
||||
public static ProfileOperation changeSlot(EntityPlayer player, int newSlotId) {
|
||||
Profile profile = getProfile(player);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = changeSlotInternal(profile, newSlotId);
|
||||
if(result == ProfileOperation.SUCCESS && player != null)
|
||||
save(player, profile);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ProfileOperation changeSlot(UUID uuid, int newSlotId) {
|
||||
Profile profile = getProfile(uuid);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = changeSlotInternal(profile, newSlotId);
|
||||
if(result == ProfileOperation.SUCCESS) {
|
||||
if(profile.player != null)
|
||||
save(profile.player, profile);
|
||||
else
|
||||
saveOffline(profile, uuid);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ProfileOperation changeSlot(String username, int newSlotId) {
|
||||
Profile profile = getProfile(username);
|
||||
if(profile == null) return ProfileOperation.PLAYER_NOT_FOUND;
|
||||
ProfileOperation result = changeSlotInternal(profile, newSlotId);
|
||||
if(result == ProfileOperation.SUCCESS) {
|
||||
if(profile.player != null)
|
||||
save(profile.player, profile);
|
||||
else {
|
||||
UUID uuid = getUUIDFromUsername(username);
|
||||
if(uuid != null)
|
||||
saveOffline(profile, uuid);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ---------- Saving current slot data (used before switching) ----------
|
||||
public static void saveSlotData(EntityPlayer player){
|
||||
if(player == null || !activeProfiles.containsKey(player.getUniqueID()))
|
||||
return;
|
||||
Profile profile = activeProfiles.get(player.getUniqueID());
|
||||
if(profile.locked) {
|
||||
LogWriter.error("Profile is locked; cannot save slot data.");
|
||||
return;
|
||||
}
|
||||
NBTTagCompound dataCompound;
|
||||
Slot slot;
|
||||
|
||||
Profile profile = activeProfiles.get(player.getUniqueID());
|
||||
if(!profile.slots.isEmpty() && profile.slots.containsKey(profile.currentID)){
|
||||
slot = profile.slots.get(profile.currentID);
|
||||
dataCompound = (NBTTagCompound) slot.getCompound().copy();
|
||||
@@ -138,39 +442,39 @@ public class ProfileController {
|
||||
dataCompound = new NBTTagCompound();
|
||||
slot.setCompound(dataCompound);
|
||||
}
|
||||
|
||||
for(IProfileData profileData : profileTypes.values()){
|
||||
NBTTagCompound cloned = (NBTTagCompound) profileData.getCurrentNBT(player).copy();
|
||||
dataCompound.setTag(profileData.getTagName(), cloned);
|
||||
}
|
||||
|
||||
slot.setCompound(dataCompound);
|
||||
}
|
||||
|
||||
public static void changePlayerSlot(EntityPlayer player, int id){
|
||||
if(!activeProfiles.containsKey(player.getUniqueID()))
|
||||
// TODO:
|
||||
public static void loadSlotData(EntityPlayer player) {
|
||||
if(player == null || !activeProfiles.containsKey(player.getUniqueID()))
|
||||
return;
|
||||
|
||||
NBTTagCompound slotCompound;
|
||||
Slot slot;
|
||||
Profile profile = activeProfiles.get(player.getUniqueID());
|
||||
if(!profile.slots.isEmpty() && profile.slots.containsKey(id)){
|
||||
slot = profile.slots.get(id);
|
||||
slotCompound = (NBTTagCompound) slot.getCompound().copy();
|
||||
} else {
|
||||
slot = new Slot(id, "Slot " + id);
|
||||
slotCompound = new NBTTagCompound();
|
||||
slot.setCompound(slotCompound);
|
||||
}
|
||||
if(profile.slots.isEmpty() || !profile.slots.containsKey(profile.currentID))
|
||||
return;
|
||||
|
||||
slot.setLastLoaded(System.currentTimeMillis());
|
||||
profile.slots.put(id, slot);
|
||||
// Get a copy of the active slot's NBT compound.
|
||||
NBTTagCompound slotCompound = (NBTTagCompound) profile.slots.get(profile.currentID).getCompound().copy();
|
||||
|
||||
// For each registered profile data type, load its saved compound onto the player.
|
||||
for(IProfileData profileData : profileTypes.values()){
|
||||
profileData.setNBT(player, slotCompound.getCompoundTag(profileData.getTagName()));
|
||||
NBTTagCompound data;
|
||||
if(slotCompound.hasKey(profileData.getTagName()))
|
||||
data = slotCompound.getCompoundTag(profileData.getTagName());
|
||||
else
|
||||
data = new NBTTagCompound();
|
||||
|
||||
profileData.setNBT(player, data);
|
||||
profileData.save(player);
|
||||
}
|
||||
}
|
||||
|
||||
profile.currentID = id;
|
||||
public static EntityPlayerMP getPlayer(String username){
|
||||
return MinecraftServer.getServer().getConfigurationManager().func_152612_a(username);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user