Skip to content

Commit

Permalink
streamline code / api
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNavaStar committed Apr 22, 2021
1 parent 7bf4afb commit dbfd6ed
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 123 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/mrnavastar/invsync/Invsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
public class Invsync implements ModInitializer {

public static final String MODID = "invsync";
//public static final String databaseName = ConfigManager.Database_Name;

@Override
public void onInitialize() {
log(Level.INFO, "Initializing");

ConfigManager.loadConfig();
if (ConfigManager.started && !ConfigManager.Database_Directory.equals("/Where/To/Create/Database")) {
SQLHandler.start();
SQLHandler.connect();
}

if (SQLHandler.connection != null) {
log(Level.INFO,"Successfully connected to database!");
SQLHandler.disconnect();

PlayerDataSetup.start();

Expand All @@ -38,8 +40,6 @@ public void onInitialize() {
SQLHandler.dropTable(ConfigManager.Player_Roles_Table_Name);
}
}

SQLHandler.disconnect();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Mixin(PlayerManager.class)
public class PlayerJoinMixin {
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;onSpawn()V"), method = "onPlayerConnect", cancellable = true)
private void onPlayerJoin(ClientConnection connection, ServerPlayerEntity player, CallbackInfo info) {
private void onPlayerJoin(ClientConnection connection, ServerPlayerEntity player, CallbackInfo info) {
ActionResult result = PlayerJoinCallback.EVENT.invoker().joinServer(player, player.getServer());

if (result == ActionResult.FAIL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,47 @@ public static void nbtToSql(PlayerEntity player) {

if (ConfigManager.Sync_Inv) {
for (int i = 0; i < 36; i++) {
playerDataTable.saveString(uuid, "inv" + i, ConversionHelpers.itemStackToString(player.inventory.main.get(i)));
playerDataTable.set(uuid, "inv" + i, ConversionHelpers.itemStackToString(player.inventory.main.get(i)));
}
playerDataTable.saveString(uuid, "offHand", ConversionHelpers.itemStackToString(player.inventory.offHand.get(0)));
playerDataTable.saveInt(uuid, "selectedSlot", player.inventory.selectedSlot);
playerDataTable.set(uuid, "offHand", ConversionHelpers.itemStackToString(player.inventory.offHand.get(0)));
playerDataTable.set(uuid, "selectedSlot", player.inventory.selectedSlot);
}

if (ConfigManager.Sync_Armour) {
for (int i = 0; i < 4; i++) {
playerDataTable.saveString(uuid, "armour" + i, ConversionHelpers.itemStackToString(player.inventory.armor.get(i)));
playerDataTable.set(uuid, "armour" + i, ConversionHelpers.itemStackToString(player.inventory.armor.get(i)));
}
}

if (ConfigManager.Sync_eChest) {
for (int i = 0; i < 27; i++) {
playerDataTable.saveString(uuid, "eChest" + i, ConversionHelpers.itemStackToString(player.getEnderChestInventory().getStack(i)));
playerDataTable.set(uuid, "eChest" + i, ConversionHelpers.itemStackToString(player.getEnderChestInventory().getStack(i)));
}
}

if (ConfigManager.Sync_Xp) {
playerDataTable.saveInt(uuid, "xp", player.experienceLevel);
playerDataTable.saveFloat(uuid, "xpProgress", player.experienceProgress);
playerDataTable.set(uuid, "xp", player.experienceLevel);
playerDataTable.set(uuid, "xpProgress", player.experienceProgress);
}

if (ConfigManager.Sync_Score) {
playerDataTable.saveInt(uuid, "score", player.getScore());
playerDataTable.set(uuid, "score", player.getScore());
}

if (ConfigManager.Sync_Health) {
playerDataTable.saveFloat(uuid, "health", player.getHealth());
playerDataTable.set(uuid, "health", player.getHealth());
}

if (ConfigManager.Sync_Food_Level) {
playerDataTable.saveString(uuid,"foodLevel", ConversionHelpers.foodLevelToString(player.getHungerManager()));
playerDataTable.set(uuid,"foodLevel", ConversionHelpers.foodLevelToString(player.getHungerManager()));
}

if (ConfigManager.Sync_Status_Effects) {
ArrayList<String> effects = new ArrayList<>();
for (StatusEffectInstance s : player.getStatusEffects()) {
effects.add(ConversionHelpers.effectsToString(s));
}
playerDataTable.saveString(uuid, "statusEffects", effects.toString());
playerDataTable.set(uuid, "statusEffects", effects.toString());
}

playerDataTable.endTransaction();
Expand All @@ -71,49 +71,49 @@ public static void sqlToNbt(PlayerEntity player) {
if (ConfigManager.Sync_Inv) {
for (int i = 0; i < 36; i++) {
player.inventory.main.set(i, ItemStack.fromTag(ConversionHelpers.stringToTag(playerDataTable
.loadString(uuid, "inv" + i, ConversionHelpers.itemStackToString(player.inventory.main.get(i))))));
.get(uuid, "inv" + i, ConversionHelpers.itemStackToString(player.inventory.main.get(i))))));
}
player.inventory.offHand.set(0, ItemStack.fromTag(ConversionHelpers.stringToTag(playerDataTable
.loadString(uuid, "offHand", ConversionHelpers.itemStackToString(player.inventory.offHand.get(0))))));
.get(uuid, "offHand", ConversionHelpers.itemStackToString(player.inventory.offHand.get(0))))));

player.inventory.selectedSlot = playerDataTable.loadInt(uuid, "selectedSlot", player.inventory.selectedSlot);
player.inventory.selectedSlot = playerDataTable.get(uuid, "selectedSlot", player.inventory.selectedSlot);
}

if (ConfigManager.Sync_Armour) {
for (int i = 0; i < 4; i++) {
player.inventory.armor.set(i, ItemStack.fromTag(ConversionHelpers.stringToTag(playerDataTable
.loadString(uuid, "armour" + i, ConversionHelpers.itemStackToString(player.inventory.armor.get(i))))));
.get(uuid, "armour" + i, ConversionHelpers.itemStackToString(player.inventory.armor.get(i))))));
}
}

if (ConfigManager.Sync_eChest) {
for (int i = 0; i < 27; i++) {
player.getEnderChestInventory().setStack(i, ItemStack.fromTag(ConversionHelpers.stringToTag(playerDataTable
.loadString(uuid, "eChest" + i, ConversionHelpers.itemStackToString(player.getEnderChestInventory().getStack(i))))));
.get(uuid, "eChest" + i, ConversionHelpers.itemStackToString(player.getEnderChestInventory().getStack(i))))));
}
}

if (ConfigManager.Sync_Xp) {
player.experienceLevel = playerDataTable.loadInt(uuid, "xp", player.experienceLevel);
player.experienceProgress = playerDataTable.loadFloat(uuid, "xpProgress", player.experienceProgress);
player.experienceLevel = playerDataTable.get(uuid, "xp", player.experienceLevel);
player.experienceProgress = playerDataTable.get(uuid, "xpProgress", player.experienceProgress);
}

if (ConfigManager.Sync_Score) {
player.setScore(playerDataTable.loadInt(uuid, "score", player.getScore()));
player.setScore(playerDataTable.get(uuid, "score", player.getScore()));
}

if (ConfigManager.Sync_Health) {
player.setHealth(playerDataTable.loadFloat(uuid, "health", player.getHealth()));
player.setHealth(playerDataTable.get(uuid, "health", player.getHealth()));
}

if (ConfigManager.Sync_Food_Level) {
player.getHungerManager().fromTag(ConversionHelpers.stringToTag(playerDataTable
.loadString(uuid, "foodLevel", ConversionHelpers.foodLevelToString(player.getHungerManager()))));
.get(uuid, "foodLevel", ConversionHelpers.foodLevelToString(player.getHungerManager()))));
}

if (ConfigManager.Sync_Status_Effects) {
player.clearStatusEffects();
String str = playerDataTable.loadString(uuid, "statusEffects", "[]");
String str = playerDataTable.get(uuid, "statusEffects", "[]");
if (!str.equals("[]")) {
String[] strArr = str.replace("[", "").replace("]", "").split(", ");
for (String s : strArr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void rolesToSql(PlayerEntity player) {
ArrayList<String> roles = new ArrayList<>();

currentRoles.iterator().forEachRemaining(role -> roles.add(Objects.requireNonNull(PlayerRolesConfig.get().get(role.asString().replace("\"", ""))).getName()));
playerRolesTable.saveString(uuid, "roles", roles.toString());
playerRolesTable.set(uuid, "roles", roles.toString());

playerRolesTable.endTransaction();
}
Expand All @@ -38,7 +38,7 @@ public static void sqlToRoles(PlayerEntity player) {
ArrayList<Role> currentRolesArray = new ArrayList<>();
ArrayList<Role> storedRoles = new ArrayList<>();

String[] storedRoleNames = playerRolesTable.loadString(uuid, "roles", "[]")
String[] storedRoleNames = playerRolesTable.get(uuid, "roles", "[]")
.replace("[", "").replace("]", "").split(", ");

currentRoles.iterator().forEachRemaining(role -> currentRolesArray.add(PlayerRolesConfig.get().get(role.asString().replace("\"", ""))));
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/com/mrnavastar/invsync/setup/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,21 @@

public class ConfigManager {

public static File configFile;
public static File configFile = new File(FabricLoader.getInstance().getConfigDir().toString(), Invsync.MODID + "Config.json");

public static String Database_Name, Database_Directory, Player_Data_Table_Name, Player_Roles_Table_Name;
public static boolean Enable_WAL_Mode, Sync_Inv, Sync_Armour, Sync_eChest, Sync_Xp, Sync_Score, Sync_Health, Sync_Food_Level, Sync_Status_Effects, Sync_Player_Roles;
public static boolean Sync_Inv, Sync_Armour, Sync_eChest, Sync_Xp, Sync_Score, Sync_Health, Sync_Food_Level, Sync_Status_Effects, Sync_Player_Roles;
public static boolean started = false;

public static void prepareConfigFile() {
if (configFile != null) {
return;
}
configFile = new File(FabricLoader.getInstance().getConfigDir().toString(), Invsync.MODID + "Config.json");
}

//Generate content for config file
public static void createConfig() {
JsonObject SQLConfigProperties = new JsonObject();
SQLConfigProperties.addProperty("comment1", "Settings for your database. THESE MUST BE THE SAME BETWEEN SERVERS. (More info at https://github.com/MrNavaStar/invSync)");
SQLConfigProperties.addProperty("Database_Name", "InvSync.db");
SQLConfigProperties.addProperty("Player_Data_Table_Name", "PlayerData");
SQLConfigProperties.addProperty("Database_Directory", "/Where/To/Create/Database");
SQLConfigProperties.addProperty("Enable_WAL_Mode", false);

SQLConfigProperties.addProperty("comment2", "Settings for what to sync between servers. THESE MUST BE THE SAME BETWEEN SERVERS.");
SQLConfigProperties.addProperty("comment2", "Settings for what to sync between servers. These can be different between servers if you wish. Just be careful. ;)");
SQLConfigProperties.addProperty("Sync_Inv", true);
SQLConfigProperties.addProperty("Sync_Armour", true);
SQLConfigProperties.addProperty("Sync_eChest", true);
Expand All @@ -44,7 +36,7 @@ public static void createConfig() {
SQLConfigProperties.addProperty("Sync_Food_Level", true);
SQLConfigProperties.addProperty("Sync_Status_Effects", true);

SQLConfigProperties.addProperty("comment3", "If you have the following mods installed, you can sync them too! THESE MUST BE THE SAME BETWEEN SERVERS.");
SQLConfigProperties.addProperty("comment3", "If you have the following mods installed, you can sync them too! These can be different between servers if you wish. Just be careful. ;)");
SQLConfigProperties.addProperty("Player_Roles_Table_Name", "PlayerRoles");
SQLConfigProperties.addProperty("Sync_Player_Roles", true);

Expand All @@ -70,7 +62,6 @@ public static void jsonReader(File input) {
Database_Name = jsonObject.get("Database_Name").getAsString();
Player_Data_Table_Name = jsonObject.get("Player_Data_Table_Name").getAsString();
Database_Directory = jsonObject.get("Database_Directory").getAsString();
Enable_WAL_Mode = jsonObject.get("Enable_WAL_Mode").getAsBoolean();

Sync_Inv = jsonObject.get("Sync_Inv").getAsBoolean();
Sync_Armour = jsonObject.get("Sync_Armour").getAsBoolean();
Expand All @@ -88,16 +79,15 @@ public static void jsonReader(File input) {

} catch (FileNotFoundException ignore) {
} catch (NullPointerException ignore) {
log(Level.ERROR, "Whoops!, it looks like there is something wrong with your config");
log(Level.ERROR, "Whoops!, it looks like something went wrong with your config!");
log(Level.INFO, "If you just updated the mod, the config format likely changed");
log(Level.INFO, "Just delete the old config and let it regenerate");
log(Level.INFO, "If this is not the case, you may have messed up the config format");
log(Level.INFO, "If this is not the case, you may have messed up the config itself");
log(Level.INFO, "Generate a new config or fix the format error");
}
}

public static void loadConfig() {
prepareConfigFile();
if (!configFile.exists()) createConfig();
jsonReader(configFile);
}
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/com/mrnavastar/invsync/setup/PlayerDataSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@

public class PlayerDataSetup {

public static Table playerDataTable;
public static String tableName;
public static String tableName = ConfigManager.Player_Data_Table_Name;
public static Table playerDataTable = new Table(tableName, PlayerDataColumns.getColumns());

public static void getConfigData() {
tableName = ConfigManager.Player_Data_Table_Name;
}

public static void subToEvents() {
public static void start() {
PlayerJoinCallback.EVENT.register((player, server) -> {
PlayerDataConversion.sqlToNbt(player);
log(Level.INFO, "Getting Player Data From database");
Expand All @@ -32,10 +28,4 @@ public static void subToEvents() {
return ActionResult.PASS;
});
}

public static void start() {
getConfigData();
playerDataTable = new Table(tableName, PlayerDataColumns.getColumns());
subToEvents();
}
}
33 changes: 11 additions & 22 deletions src/main/java/com/mrnavastar/invsync/setup/PlayerRolesSetup.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mrnavastar.invsync.setup;

import com.mrnavastar.invsync.api.event.PlayerJoinCallback;

import com.mrnavastar.invsync.api.event.PlayerLeaveCallback;
import com.mrnavastar.invsync.conversion.PlayerRoleConversion;
import com.mrnavastar.invsync.sql.Table;
Expand All @@ -10,30 +9,20 @@

public class PlayerRolesSetup {

public static Table playerRolesTable;
public static String tableName;

public static void getConfigData() {
tableName = ConfigManager.Player_Roles_Table_Name;
}

public static void subToEvents() {
PlayerJoinCallback.EVENT.register((player, server) -> {
PlayerRoleConversion.sqlToRoles(player);
return ActionResult.PASS;
});

PlayerLeaveCallback.EVENT.register((player, server) -> {
PlayerRoleConversion.rolesToSql(player);
return ActionResult.PASS;
});
}
public static String tableName = ConfigManager.Player_Roles_Table_Name;
public static Table playerRolesTable = new Table(tableName, PlayerRolesColumns.getColumns());

public static void start() {
if (ConfigManager.Sync_Player_Roles) {
getConfigData();
playerRolesTable = new Table(tableName, PlayerRolesColumns.getColumns());
subToEvents();
PlayerJoinCallback.EVENT.register((player, server) -> {
PlayerRoleConversion.sqlToRoles(player);
return ActionResult.PASS;
});

PlayerLeaveCallback.EVENT.register((player, server) -> {
PlayerRoleConversion.rolesToSql(player);
return ActionResult.PASS;
});
}
}
}
Loading

0 comments on commit dbfd6ed

Please sign in to comment.