forked from Fascinated/Bat
big ass refactor to handle loading guilds and users without spring to make it more futureproof
This commit is contained in:
@ -1,25 +0,0 @@
|
||||
package cc.fascinated.bat.common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public abstract class Profile {
|
||||
/**
|
||||
* The key of the profile.
|
||||
*/
|
||||
private String profileKey;
|
||||
|
||||
/**
|
||||
* Resets the profile
|
||||
*/
|
||||
public abstract void reset();
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package cc.fascinated.bat.common;
|
||||
|
||||
import cc.fascinated.bat.BatApplication;
|
||||
import lombok.Getter;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -9,11 +12,11 @@ import java.util.Map;
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Getter
|
||||
public class ProfileHolder {
|
||||
public abstract class ProfileHolder {
|
||||
/**
|
||||
* The profiles for the holder
|
||||
*/
|
||||
private Map<String, Profile> profiles;
|
||||
private final Map<String, Serializable> profiles = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Gets a profile for the holder
|
||||
@ -22,19 +25,25 @@ public class ProfileHolder {
|
||||
* @param <T> The type of the profile
|
||||
* @return The profile
|
||||
*/
|
||||
public <T extends Profile> T getProfile(Class<T> clazz) {
|
||||
if (profiles == null) {
|
||||
profiles = new HashMap<>();
|
||||
}
|
||||
public abstract <T extends Serializable> T getProfile(Class<T> clazz);
|
||||
|
||||
Profile profile = profiles.values().stream().filter(clazz::isInstance).findFirst().orElse(null);
|
||||
/**
|
||||
* Gets the profiles for the holder
|
||||
* using the provided document
|
||||
*
|
||||
* @return the profiles
|
||||
*/
|
||||
@SneakyThrows
|
||||
protected <T extends Serializable> T getProfileFromDocument(Class<T> clazz, Document document) {
|
||||
Serializable profile = getProfiles().get(clazz.getSimpleName());
|
||||
if (profile == null) {
|
||||
try {
|
||||
profile = clazz.newInstance();
|
||||
profiles.put(profile.getProfileKey(), profile);
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
T newProfile = clazz.cast(clazz.getDeclaredConstructors()[0].newInstance());
|
||||
org.bson.Document profiles = document.get("profiles", new org.bson.Document());
|
||||
org.bson.Document profileDocument = (org.bson.Document) profiles.getOrDefault(clazz.getSimpleName(), new org.bson.Document());
|
||||
|
||||
newProfile.load(profileDocument, BatApplication.GSON);
|
||||
getProfiles().put(clazz.getSimpleName(), newProfile);
|
||||
return newProfile;
|
||||
}
|
||||
return clazz.cast(profile);
|
||||
}
|
||||
|
36
src/main/java/cc/fascinated/bat/common/Serializable.java
Normal file
36
src/main/java/cc/fascinated/bat/common/Serializable.java
Normal file
@ -0,0 +1,36 @@
|
||||
package cc.fascinated.bat.common;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
public abstract class Serializable {
|
||||
/**
|
||||
* Load data from the provided document into this profile
|
||||
*
|
||||
* @param document the document to load data from
|
||||
* @param gson the GSON instance to use
|
||||
*/
|
||||
public abstract void load(Document document, Gson gson);
|
||||
|
||||
/**
|
||||
* Serialize this profile into a Bson document
|
||||
*
|
||||
* @param gson the GSON instance to use
|
||||
* @return the serialized document
|
||||
*/
|
||||
public abstract Document serialize(Gson gson);
|
||||
|
||||
/**
|
||||
* Resets the profile to its default state
|
||||
*/
|
||||
public abstract void reset();
|
||||
}
|
Reference in New Issue
Block a user