Skip to content

Commit

Permalink
feat: add Gson and UserTypeAdapter for User serialization (main)
Browse files Browse the repository at this point in the history
- Add Gson and UserTypeAdapter classes to handle User serialization and deserialization
- Replace multiple columns in database with a single data column that stores User as JSON
- Remove unused getInstance() method from VoteReward class
- Add getter for VoteReward instance and Gson instance
- Improve exception logging in database setup and command locales loading

BREAKING CHANGE: Database schema has been changed
  • Loading branch information
GeorgeV220 committed Feb 16, 2024
1 parent 7a499d0 commit 184f485
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/main/java/com/georgev22/voterewards/VoteReward.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
import com.georgev22.voterewards.utilities.configmanager.FileManager;
import com.georgev22.voterewards.utilities.interfaces.Holograms;
import com.georgev22.voterewards.utilities.player.PlayerDataManager;
import com.georgev22.voterewards.utilities.player.User;
import com.georgev22.voterewards.utilities.player.UserTypeAdapter;
import com.georgev22.voterewards.utilities.player.VoteUtils;
import com.georgev22.voterewards.votereward.VoteRewardImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.Getter;
import lombok.Setter;
import org.bstats.bukkit.Metrics;
Expand Down Expand Up @@ -76,6 +80,7 @@ public class VoteReward {
@Getter
private NoPlayerCharacterAPI noPlayerCharacterAPI = null;

@Getter
private static VoteReward instance = null;

@Getter
Expand All @@ -97,10 +102,8 @@ public class VoteReward {
private LibraryLoader libraryLoader;

private int tick = 0;

public static VoteReward getInstance() {
return instance;
}
@Getter
private Gson gson;

public VoteReward(@NotNull VoteRewardImpl voteReward) {
this.voteReward = voteReward;
Expand All @@ -118,6 +121,10 @@ public void onLoad() throws UnknownDependencyException, InvalidDependencyExcepti
}

public void onEnable() throws Exception {
this.gson = new GsonBuilder()
.registerTypeAdapter(User.class, new UserTypeAdapter())
//.setPrettyPrinting()
.create();
fileManager = FileManager.getInstance();

fileManager.loadFiles(this.logger, this.getClass());
Expand Down Expand Up @@ -228,7 +235,7 @@ public void onEnable() throws Exception {
try {
setupDatabase();
} catch (Exception throwable) {
throwable.printStackTrace();
this.getLogger().log(Level.SEVERE, "Error while setting up the database:", throwable);
}
}, 1L);

Expand Down Expand Up @@ -298,14 +305,7 @@ private void setupDatabase() throws Exception {
BukkitMinecraftUtils.debug(getName(), getVersion(), "setupDatabase() Thread ID: " + Thread.currentThread().getId());
ObjectMap<String, ObjectMap.Pair<String, String>> map = new HashObjectMap<String, ObjectMap.Pair<String, String>>()
.append("entity_id", ObjectMap.Pair.create("VARCHAR(38)", "NULL"))
.append("name", ObjectMap.Pair.create("VARCHAR(18)", "NULL"))
.append("votes", ObjectMap.Pair.create("INT(10)", "0"))
.append("daily", ObjectMap.Pair.create("INT(10)", "0"))
.append("last", ObjectMap.Pair.create("BIGINT(30)", "0"))
.append("voteparty", ObjectMap.Pair.create("INT(10)", "0"))
.append("totalvotes", ObjectMap.Pair.create("INT(10)", "0"))
.append("services", ObjectMap.Pair.create("BLOB", "NULL"))
.append("servicesLastVote", ObjectMap.Pair.create("BLOB", "NULL"));
.append("data", ObjectMap.Pair.create("LONGTEXT", "NULL"));
switch (OptionsUtil.DATABASE_TYPE.getStringValue()) {
case "MySQL" -> {
if (databaseWrapper == null || !databaseWrapper.isConnected()) {
Expand Down Expand Up @@ -492,8 +492,7 @@ private void loadCommandLocales(@NotNull PaperCommandManager commandManager) {
commandManager.getLocales().loadYamlLanguageFile(new File(getDataFolder(), "lang_en.yaml"), Locale.ENGLISH);
commandManager.usePerIssuerLocale(true);
} catch (IOException | InvalidConfigurationException e) {
BukkitMinecraftUtils.debug(getName(), getVersion(), "Failed to load language config 'lang_en.yaml': " + e.getMessage());
e.printStackTrace();
this.getLogger().log(Level.SEVERE, "Error while trying to load command locales", e);
}
}

Expand Down

0 comments on commit 184f485

Please sign in to comment.