2025-01-29 13:20:04 -05:00
|
|
|
package kamkeel.npcs.controllers;
|
2025-01-23 16:10:01 -05:00
|
|
|
|
2025-01-29 13:20:04 -05:00
|
|
|
import kamkeel.npcs.controllers.data.IProfileData;
|
|
|
|
|
import kamkeel.npcs.controllers.data.Profile;
|
|
|
|
|
import kamkeel.npcs.controllers.data.Slot;
|
2025-01-23 16:10:01 -05:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import noppes.npcs.CustomNpcs;
|
|
|
|
|
import noppes.npcs.LogWriter;
|
|
|
|
|
import noppes.npcs.util.CustomNPCsThreader;
|
|
|
|
|
import noppes.npcs.util.NBTJsonUtil;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
public class ProfileController {
|
|
|
|
|
|
|
|
|
|
public static HashMap<String, IProfileData> profileTypes;
|
|
|
|
|
public static HashMap<UUID, Profile> activeProfiles;
|
|
|
|
|
|
|
|
|
|
public static String profile_directory = "profiles";
|
|
|
|
|
|
|
|
|
|
public ProfileController(){
|
|
|
|
|
profileTypes = new HashMap<>();
|
|
|
|
|
activeProfiles = new HashMap<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register during ServerStartEvent()
|
|
|
|
|
*
|
|
|
|
|
* @param type - IProfile Type to always Save/Load
|
|
|
|
|
* @return true/false if profile was successfully added
|
|
|
|
|
*/
|
|
|
|
|
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{
|
|
|
|
|
File file = new File(CustomNpcs.getWorldSaveDirectory(), profile_directory);
|
|
|
|
|
if(!file.exists())
|
|
|
|
|
file.mkdir();
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
activeProfiles.put(player.getUniqueID(), profile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized NBTTagCompound load(EntityPlayer player) {
|
|
|
|
|
File saveDir = getProfileDir();
|
|
|
|
|
String filename = player.getUniqueID().toString();
|
|
|
|
|
filename += ".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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized void save(EntityPlayer player, Profile profile) {
|
|
|
|
|
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.renameTo(file1);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LogWriter.except(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:45:13 -05:00
|
|
|
public static void changeSlot(EntityPlayer player, int slotNumber){
|
2025-01-23 16:10:01 -05:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:45:13 -05:00
|
|
|
public static void saveSlotData(EntityPlayer player){
|
2025-01-23 16:10:01 -05:00
|
|
|
if(!activeProfiles.containsKey(player.getUniqueID()))
|
|
|
|
|
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();
|
|
|
|
|
} else {
|
2025-01-23 17:45:13 -05:00
|
|
|
slot = new Slot(profile.currentID, "Slot " + profile.currentID);
|
2025-01-23 16:10:01 -05:00
|
|
|
dataCompound = new NBTTagCompound();
|
2025-01-23 17:45:13 -05:00
|
|
|
slot.setCompound(dataCompound);
|
2025-01-23 16:10:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(IProfileData profileData : profileTypes.values()){
|
|
|
|
|
NBTTagCompound cloned = (NBTTagCompound) profileData.getCurrentNBT(player).copy();
|
|
|
|
|
dataCompound.setTag(profileData.getTagName(), cloned);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slot.setCompound(dataCompound);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:45:13 -05:00
|
|
|
public static void changePlayerSlot(EntityPlayer player, int id){
|
2025-01-23 16:10:01 -05:00
|
|
|
if(!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();
|
2025-01-23 17:45:13 -05:00
|
|
|
slot.setCompound(slotCompound);
|
2025-01-23 16:10:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slot.setLastLoaded(System.currentTimeMillis());
|
|
|
|
|
profile.slots.put(id, slot);
|
|
|
|
|
|
|
|
|
|
for(IProfileData profileData : profileTypes.values()){
|
2025-01-23 17:45:13 -05:00
|
|
|
profileData.setNBT(player, slotCompound.getCompoundTag(profileData.getTagName()));
|
2025-01-23 16:10:01 -05:00
|
|
|
profileData.save(player);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
profile.currentID = id;
|
|
|
|
|
}
|
|
|
|
|
}
|