9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-26 01:59:20 +00:00

Paper plugin support, save player itemsToKeep rather than drops if not empty (#179)

* Paper plugin support, save itemsToKeep if present, close #172

* Fixup wrong packages, suppress a warning

* Update docs, add settings for death saving, reorganise config slightly

* Improve default server name lookup

* docs: Add note on Unsupported Versions

* docs: Minor Sync Modes tweaks
This commit is contained in:
William
2023-10-10 00:10:06 +01:00
committed by GitHub
parent 6d9e68a65b
commit 7db3ed678f
19 changed files with 384 additions and 59 deletions

View File

@@ -294,10 +294,16 @@ public interface HuskSync extends Task.Supplier, EventDispatcher {
default void loadConfigs() {
try {
// Load settings
setSettings(Annotaml.create(new File(getDataFolder(), "config.yml"), Settings.class).get());
setSettings(Annotaml.create(
new File(getDataFolder(), "config.yml"),
Settings.class
).get());
// Load server name
setServer(Annotaml.create(new File(getDataFolder(), "server.yml"), Server.class).get());
setServer(Annotaml.create(
new File(getDataFolder(), "server.yml"),
Server.getDefault(this)
).get());
// Load locales from language preset default
final Locales languagePresets = Annotaml.create(

View File

@@ -19,11 +19,15 @@
package net.william278.husksync.config;
import net.william278.annotaml.Annotaml;
import net.william278.annotaml.YamlFile;
import net.william278.annotaml.YamlKey;
import net.william278.husksync.HuskSync;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
/**
* Represents a server on a proxied network.
@@ -37,26 +41,44 @@ import java.nio.file.Path;
┗╸ If you join it using /server alpha, then set it to 'alpha' (case-sensitive)""")
public class Server {
/**
* Default server identifier.
*/
@NotNull
public static String getDefaultServerName() {
try {
final Path serverDirectory = Path.of(System.getProperty("user.dir"));
return serverDirectory.getFileName().toString().trim();
} catch (Exception e) {
return "server";
}
}
@YamlKey("name")
private String serverName = getDefaultServerName();
private String serverName;
private Server(@NotNull String serverName) {
this.serverName = serverName;
}
@SuppressWarnings("unused")
private Server() {
}
@NotNull
public static Server getDefault(@NotNull HuskSync plugin) {
return new Server(getDefaultServerName(plugin));
}
/**
* Find a sensible default name for the server name property
*/
@NotNull
private static String getDefaultServerName(@NotNull HuskSync plugin) {
try {
// Fetch server default from supported plugins if present
for (String s : List.of("HuskHomes", "HuskTowns")) {
final File serverFile = Path.of(plugin.getDataFolder().getParent(), s, "server.yml").toFile();
if (serverFile.exists()) {
return Annotaml.create(serverFile, Server.class).get().getName();
}
}
// Fetch server default from user dir name
final Path serverDirectory = Path.of(System.getProperty("user.dir"));
return serverDirectory.getFileName().toString().trim();
} catch (Throwable e) {
return "server";
}
}
@Override
public boolean equals(@NotNull Object other) {
// If the name of this server matches another, the servers are the same.

View File

@@ -165,12 +165,21 @@ public class Settings {
private boolean saveOnWorldSave = true;
@YamlComment("Whether to create a snapshot for users when they die (containing their death drops)")
@YamlKey("synchronization.save_on_death")
@YamlKey("synchronization.save_on_death.enabled")
private boolean saveOnDeath = false;
@YamlComment("Whether to save empty death drops for users when they die")
@YamlKey("synchronization.save_empty_drops_on_death")
private boolean saveEmptyDropsOnDeath = true;
@YamlComment("What items to save in death snapshots? (DROPS or ITEMS_TO_KEEP). "
+ " Note that ITEMS_TO_KEEP (suggested for keepInventory servers) requires a Paper 1.19.4+ server.")
@YamlKey("synchronization.save_on_death.items_to_save")
private DeathItemsMode deathItemsMode = DeathItemsMode.DROPS;
@YamlComment("Should a death snapshot still be created even if the items to save on the player's death are empty?")
@YamlKey("synchronization.save_on_death.save_empty_items")
private boolean saveEmptyDeathItems = true;
@YamlComment("Whether dead players who log out and log in to a different server should have their items saved.")
@YamlKey("synchronization.save_on_death.sync_dead_players_changing_server")
private boolean synchronizeDeadPlayersChangingServer = true;
@YamlComment("Whether to use the snappy data compression algorithm. Keep on unless you know what you're doing")
@YamlKey("synchronization.compress_data")
@@ -188,11 +197,6 @@ public class Settings {
@YamlKey("synchronization.synchronize_max_health")
private boolean synchronizeMaxHealth = true;
@YamlComment("Whether dead players who log out and log in to a different server should have their items saved. "
+ "You may need to modify this if you're using the keepInventory gamerule.")
@YamlKey("synchronization.synchronize_dead_players_changing_server")
private boolean synchronizeDeadPlayersChangingServer = true;
@YamlComment("If using the DELAY sync method, how long should this server listen for Redis key data updates before "
+ "pulling data from the database instead (i.e., if the user did not change servers).")
@YamlKey("synchronization.network_latency_milliseconds")
@@ -341,8 +345,13 @@ public class Settings {
return saveOnDeath;
}
public boolean doSaveEmptyDropsOnDeath() {
return saveEmptyDropsOnDeath;
@NotNull
public DeathItemsMode getDeathItemsMode() {
return deathItemsMode;
}
public boolean doSaveEmptyDeathItems() {
return saveEmptyDeathItems;
}
public boolean doCompressData() {
@@ -397,6 +406,14 @@ public class Settings {
}
}
/**
* Represents the mode of saving items on death
*/
public enum DeathItemsMode {
DROPS,
ITEMS_TO_KEEP
}
/**
* Represents the names of tables in the database
*/

View File

@@ -88,16 +88,16 @@ public abstract class EventListener {
* Handles the saving of data when a player dies
*
* @param user The user who died
* @param drops The items that this user would have dropped
* @param items The items that should be saved for this user on their death
*/
protected void saveOnPlayerDeath(@NotNull OnlineUser user, @NotNull Data.Items drops) {
protected void saveOnPlayerDeath(@NotNull OnlineUser user, @NotNull Data.Items items) {
if (plugin.isDisabling() || !plugin.getSettings().doSaveOnDeath() || plugin.isLocked(user.getUuid())
|| user.isNpc() || (!plugin.getSettings().doSaveEmptyDropsOnDeath() && drops.isEmpty())) {
|| user.isNpc() || (!plugin.getSettings().doSaveEmptyDeathItems() && items.isEmpty())) {
return;
}
final DataSnapshot.Packed snapshot = user.createSnapshot(DataSnapshot.SaveCause.DEATH);
snapshot.edit(plugin, (data -> data.getInventory().ifPresent(inventory -> inventory.setContents(drops))));
snapshot.edit(plugin, (data -> data.getInventory().ifPresent(inventory -> inventory.setContents(items))));
plugin.getDatabase().addSnapshot(user, snapshot);
}