9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-27 02:29:10 +00:00

User data pinning, version validation checks, fixes

This commit is contained in:
William
2022-07-10 18:12:01 +01:00
parent d1e9f858fe
commit 2e7ed6d9f5
27 changed files with 573 additions and 240 deletions

View File

@@ -77,6 +77,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
public void onEnable() {
// Process initialization stages
CompletableFuture.supplyAsync(() -> {
// Set the logging adapter and resource reader
this.logger = new BukkitLogger(this.getLogger());
this.resourceReader = new BukkitResourceReader(this);
@@ -117,12 +118,19 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
}).thenApply(succeeded -> {
// Prepare migrators
if (succeeded) {
logger.debug("m0");
availableMigrators = new ArrayList<>();
logger.debug("m1");
availableMigrators.add(new LegacyMigrator(this));
logger.debug("m2");
final Plugin mySqlPlayerDataBridge = Bukkit.getPluginManager().getPlugin("MySqlPlayerDataBridge");
logger.debug("m3");
if (mySqlPlayerDataBridge != null) {
logger.debug("m4");
availableMigrators.add(new MpdbMigrator(this, mySqlPlayerDataBridge));
logger.debug("m5");
}
logger.debug("m6 - Successfully prepared migrators");
}
return succeeded;
}).thenApply(succeeded -> {
@@ -198,17 +206,22 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
// Check for updates
if (succeeded && settings.getBooleanValue(Settings.ConfigOption.CHECK_FOR_UPDATES)) {
getLoggingAdapter().log(Level.INFO, "Checking for updates...");
new UpdateChecker(getVersion(), getLoggingAdapter()).logToConsole();
new UpdateChecker(getPluginVersion(), getLoggingAdapter()).logToConsole();
}
return succeeded;
}).thenAccept(succeeded -> {
// Handle failed initialization
if (!succeeded) {
getLoggingAdapter().log(Level.SEVERE, "Failed to initialize HuskSync. " + "The plugin will now be disabled");
getLoggingAdapter().log(Level.SEVERE, "Failed to initialize HuskSync. The plugin will now be disabled");
getServer().getPluginManager().disablePlugin(this);
} else {
getLoggingAdapter().log(Level.INFO, "Successfully enabled HuskSync v" + getVersion());
getLoggingAdapter().log(Level.INFO, "Successfully enabled HuskSync v" + getPluginVersion());
}
}).exceptionally(exception -> {
getLoggingAdapter().log(Level.SEVERE, "An exception occurred initializing HuskSync. (" + exception.getMessage() + ") The plugin will now be disabled.");
exception.printStackTrace();
getServer().getPluginManager().disablePlugin(this);
return null;
});
}
@@ -217,7 +230,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
if (this.eventListener != null) {
this.eventListener.handlePluginDisable();
}
getLoggingAdapter().log(Level.INFO, "Successfully disabled HuskSync v" + getVersion());
getLoggingAdapter().log(Level.INFO, "Successfully disabled HuskSync v" + getPluginVersion());
}
@Override
@@ -281,8 +294,13 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
}
@Override
public @NotNull String getVersion() {
return getDescription().getVersion();
public @NotNull Version getPluginVersion() {
return Version.pluginVersion(getDescription().getVersion());
}
@Override
public @NotNull Version getMinecraftVersion() {
return Version.minecraftVersion(Bukkit.getBukkitVersion());
}
@Override

View File

@@ -53,12 +53,12 @@ public class BukkitEventListener extends EventListener implements Listener {
public void onInventoryClose(@NotNull InventoryCloseEvent event) {
if (event.getPlayer() instanceof Player player) {
final OnlineUser user = BukkitPlayer.adapt(player);
if (huskSync.getDataEditor().isEditingInventoryData(user)) {
if (plugin.getDataEditor().isEditingInventoryData(user)) {
try {
BukkitSerializer.serializeItemStackArray(event.getInventory().getContents()).thenAccept(
serializedInventory -> super.handleMenuClose(user, new ItemData(serializedInventory)));
} catch (DataSerializationException e) {
huskSync.getLoggingAdapter().log(Level.SEVERE,
plugin.getLoggingAdapter().log(Level.SEVERE,
"Failed to serialize inventory data during menu close", e);
}
}

View File

@@ -32,6 +32,8 @@ public class LegacyMigrator extends Migrator {
private String sourcePlayersTable;
private String sourceDataTable;
private final String minecraftVersion;
public LegacyMigrator(@NotNull HuskSync plugin) {
super(plugin);
this.hslConverter = HSLConverter.getInstance();
@@ -42,6 +44,7 @@ public class LegacyMigrator extends Migrator {
this.sourceDatabase = plugin.getSettings().getStringValue(Settings.ConfigOption.DATABASE_NAME);
this.sourcePlayersTable = "husksync_players";
this.sourceDataTable = "husksync_data";
this.minecraftVersion = plugin.getMinecraftVersion().getWithoutMeta();
}
@Override
@@ -110,7 +113,7 @@ public class LegacyMigrator extends Migrator {
}
plugin.getLoggingAdapter().log(Level.INFO, "Completed download of " + dataToMigrate.size() + " entries from the legacy database!");
plugin.getLoggingAdapter().log(Level.INFO, "Converting HuskSync 1.x data to the latest HuskSync user data format...");
dataToMigrate.forEach(data -> data.toUserData(hslConverter).thenAccept(convertedData ->
dataToMigrate.forEach(data -> data.toUserData(hslConverter, minecraftVersion).thenAccept(convertedData ->
plugin.getDatabase().ensureUser(data.user()).thenRun(() ->
plugin.getDatabase().setUserData(data.user(), convertedData, DataSaveCause.LEGACY_MIGRATION)
.exceptionally(exception -> {
@@ -246,7 +249,8 @@ public class LegacyMigrator extends Migrator {
@NotNull String serializedAdvancements, @NotNull String serializedLocation) {
@NotNull
public CompletableFuture<UserData> toUserData(@NotNull HSLConverter converter) {
public CompletableFuture<UserData> toUserData(@NotNull HSLConverter converter,
@NotNull String minecraftVersion) {
return CompletableFuture.supplyAsync(() -> {
try {
final DataSerializer.StatisticData legacyStatisticData = converter
@@ -278,7 +282,8 @@ public class LegacyMigrator extends Migrator {
new ItemData(serializedInventory), new ItemData(serializedEnderChest),
new PotionEffectData(serializedPotionEffects), convertedAdvancements,
convertedStatisticData, convertedLocationData,
new PersistentDataContainerData(new HashMap<>()));
new PersistentDataContainerData(new HashMap<>()),
minecraftVersion);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@@ -35,6 +35,7 @@ public class MpdbMigrator extends Migrator {
private String sourceInventoryTable;
private String sourceEnderChestTable;
private String sourceExperienceTable;
private final String minecraftVersion;
public MpdbMigrator(@NotNull BukkitHuskSync plugin, @NotNull Plugin mySqlPlayerDataBridge) {
super(plugin);
@@ -47,6 +48,8 @@ public class MpdbMigrator extends Migrator {
this.sourceInventoryTable = "mpdb_inventory";
this.sourceEnderChestTable = "mpdb_enderchest";
this.sourceExperienceTable = "mpdb_experience";
this.minecraftVersion = plugin.getMinecraftVersion().getWithoutMeta();
}
@Override
@@ -106,7 +109,7 @@ public class MpdbMigrator extends Migrator {
}
plugin.getLoggingAdapter().log(Level.INFO, "Completed download of " + dataToMigrate.size() + " entries from the MySQLPlayerDataBridge database!");
plugin.getLoggingAdapter().log(Level.INFO, "Converting raw MySQLPlayerDataBridge data to HuskSync user data...");
dataToMigrate.forEach(data -> data.toUserData(mpdbConverter).thenAccept(convertedData ->
dataToMigrate.forEach(data -> data.toUserData(mpdbConverter, minecraftVersion).thenAccept(convertedData ->
plugin.getDatabase().ensureUser(data.user()).thenRun(() ->
plugin.getDatabase().setUserData(data.user(), convertedData, DataSaveCause.MPDB_MIGRATION))
.exceptionally(exception -> {
@@ -257,7 +260,8 @@ public class MpdbMigrator extends Migrator {
* @return A {@link CompletableFuture} that will resolve to the converted {@link UserData} object
*/
@NotNull
public CompletableFuture<UserData> toUserData(@NotNull MPDBConverter converter) {
public CompletableFuture<UserData> toUserData(@NotNull MPDBConverter converter,
@NotNull String minecraftVersion) {
return CompletableFuture.supplyAsync(() -> {
// Combine inventory and armour
final Inventory inventory = Bukkit.createInventory(null, InventoryType.PLAYER);
@@ -278,7 +282,8 @@ public class MpdbMigrator extends Migrator {
new StatisticsData(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>()),
new LocationData("world", UUID.randomUUID(), "NORMAL", 0, 0, 0,
0f, 0f),
new PersistentDataContainerData(new HashMap<>()));
new PersistentDataContainerData(new HashMap<>()),
minecraftVersion);
});
}
}

View File

@@ -6,6 +6,7 @@ import net.md_5.bungee.api.chat.BaseComponent;
import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.data.*;
import net.william278.husksync.editor.ItemEditorMenu;
import net.william278.husksync.util.Version;
import org.apache.commons.lang.ArrayUtils;
import org.bukkit.*;
import org.bukkit.advancement.Advancement;
@@ -434,6 +435,12 @@ public class BukkitPlayer extends OnlineUser {
}
}
@NotNull
@Override
public Version getMinecraftVersion() {
return Version.minecraftVersion(Bukkit.getBukkitVersion());
}
@Override
public boolean hasPermission(@NotNull String node) {
return player.hasPermission(node);