Message and setting loading

This commit is contained in:
William committed 2022-05-20 19:48:32 +01:00
1 parent 704e4b12f8
commit 10e3345b38
16 files changed
+821 -25

No files matched your search

@@ -1,6 +1,9 @@
package net.william278.huskhomes;
import net.william278.huskhomes.config.Messages;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.Player;
import net.william278.huskhomes.position.Server;
import net.william278.huskhomes.util.Logger;
import java.util.Set;
@@ -15,7 +18,7 @@ public interface HuskHomes {
*
* @return the {@link Logger} implementation to use
*/
Logger getLogger();
Logger getLoggingAdapter();
/**
* Returns a {@link Set} of online {@link Player}s on this server
@@ -24,4 +27,25 @@ public interface HuskHomes {
*/
Set<Player> getOnlinePlayers();
/**
* Return the plugin {@link Settings} loaded from file
*
* @return the plugin {@link Settings}
*/
Settings getSettings();
/**
* Return the plugin messages loaded from file
*
* @return The plugin {@link Messages}
*/
Messages getMessages();
/**
* Return the {@link Server} the plugin is running on
*
* @return the {@link Server} object representing the implementing server
*/
Server getServerData();
}
@@ -55,4 +55,4 @@ public abstract class CommandBase {
*/
public abstract List<String> onTabComplete(Player player, String[] args);
}
}
@@ -0,0 +1,95 @@
package net.william278.huskhomes.config;
import de.themoep.minedown.MineDown;
import dev.dejvokep.boostedyaml.YamlDocument;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Optional;
/**
* Loaded locales used by the plugin to display various messages
*/
public class Messages {
@NotNull
private final HashMap<String, String> rawMessages;
private Messages(YamlDocument messagesConfig) {
this.rawMessages = new HashMap<>();
for (String messageId : messagesConfig.getRoutesAsStrings(false)) {
rawMessages.put(messageId, messagesConfig.getString(messageId));
}
}
/**
* Returns an unformatted message loaded from the messages file
*
* @param messageId String identifier of the message, corresponding to a key in the file
* @return An {@link Optional} containing the message corresponding to the id, if it exists
*/
public Optional<String> getRawMessage(String messageId) {
if (rawMessages.containsKey(messageId)) {
return Optional.of(rawMessages.get(messageId));
}
return Optional.empty();
}
/**
* Returns an unformatted message loaded from the messages file, with replacements applied
*
* @param messageId String identifier of the message, corresponding to a key in the file
* @return An {@link Optional} containing the replacement-applied message corresponding to the id, if it exists
*/
public Optional<String> getRawMessage(String messageId, String... replacements) {
return getRawMessage(messageId).map(message -> applyReplacements(message, replacements));
}
/**
* Returns a MineDown-formatted message from the messages file
*
* @param messageId String identifier of the message, corresponding to a key in the file
* @return An {@link Optional} containing the formatted message corresponding to the id, if it exists
*/
public Optional<MineDown> getMessage(String messageId) {
return getRawMessage(messageId).map(MineDown::new);
}
/**
* Returns a MineDown-formatted message from the messages file, with replacements applied
*
* @param messageId String identifier of the message, corresponding to a key in the file
* @return An {@link Optional} containing the replacement-applied, formatted message corresponding to the id, if it exists
*/
public Optional<MineDown> getMessage(String messageId, String... replacements) {
return getRawMessage(messageId, replacements).map(MineDown::new);
}
/**
* Apply placeholder replacements to a raw message
*
* @param rawMessage The raw, unparsed message
* @param replacements Ordered list of placeholder replacement Strings
* @return the raw message, with inserted placeholders
*/
private String applyReplacements(String rawMessage, String... replacements) {
int replacementIndexer = 1;
for (String replacement : replacements) {
String replacementString = "%" + replacementIndexer + "%";
rawMessage = rawMessage.replace(replacementString, replacement);
replacementIndexer = replacementIndexer + 1;
}
return rawMessage;
}
/**
* Load the messages from a BoostedYaml {@link YamlDocument} messages file
*
* @param messagesConfig The loaded {@link YamlDocument} messages.yml file
* @return the loaded {@link Messages}
*/
public Messages load(YamlDocument messagesConfig) {
return new Messages(messagesConfig);
}
}
@@ -0,0 +1,279 @@
package net.william278.huskhomes.config;
import dev.dejvokep.boostedyaml.YamlDocument;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* Settings used for the plugin, as read from the config file
*/
public class Settings {
/**
* Map of {@link ConfigOption}s read from the config file
*/
private final HashMap<ConfigOption, Object> configOptions;
// Load the settings from the document
private Settings(YamlDocument config) {
this.configOptions = new HashMap<>();
Arrays.stream(ConfigOption.values()).forEach(configOption -> configOptions
.put(configOption, switch (configOption.optionType) {
case BOOLEAN -> configOption.getBooleanValue(config);
case STRING -> configOption.getStringValue(config);
case DOUBLE -> configOption.getDoubleValue(config);
case FLOAT -> configOption.getFloatValue(config);
case INTEGER -> configOption.getIntValue(config);
case STRING_LIST -> configOption.getStringListValue(config);
}));
}
/**
* Get the value of the specified {@link ConfigOption}
*
* @param option the {@link ConfigOption} to check
* @return the value of the {@link ConfigOption} as a boolean
* @throws ClassCastException if the option is not a boolean
*/
public boolean getBooleanValue(ConfigOption option) throws ClassCastException {
return (Boolean) configOptions.get(option);
}
/**
* Get the value of the specified {@link ConfigOption}
*
* @param option the {@link ConfigOption} to check
* @return the value of the {@link ConfigOption} as a string
* @throws ClassCastException if the option is not a string
*/
public String getStringValue(ConfigOption option) throws ClassCastException {
return (String) configOptions.get(option);
}
/**
* Get the value of the specified {@link ConfigOption}
*
* @param option the {@link ConfigOption} to check
* @return the value of the {@link ConfigOption} as a double
* @throws ClassCastException if the option is not a double
*/
public double getDoubleValue(ConfigOption option) throws ClassCastException {
return (Double) configOptions.get(option);
}
/**
* Get the value of the specified {@link ConfigOption}
*
* @param option the {@link ConfigOption} to check
* @return the value of the {@link ConfigOption} as a float
* @throws ClassCastException if the option is not a float
*/
public double getFloatValue(ConfigOption option) throws ClassCastException {
return (Float) configOptions.get(option);
}
/**
* Get the value of the specified {@link ConfigOption}
*
* @param option the {@link ConfigOption} to check
* @return the value of the {@link ConfigOption} as an integer
* @throws ClassCastException if the option is not an integer
*/
public int getIntegerValue(ConfigOption option) throws ClassCastException {
return (Integer) configOptions.get(option);
}
/**
* Get the value of the specified {@link ConfigOption}
*
* @param option the {@link ConfigOption} to check
* @return the value of the {@link ConfigOption} as a string {@link List}
* @throws ClassCastException if the option is not a string list
*/
@SuppressWarnings("unchecked")
public List<String> getStringListValue(ConfigOption option) throws ClassCastException {
return (List<String>) configOptions.get(option);
}
/**
* Load the settings from a BoostedYaml {@link YamlDocument} config file
*
* @param config The loaded {@link YamlDocument} config.yml file
* @return the loaded {@link Settings}
*/
public Settings load(YamlDocument config) {
return new Settings(config);
}
/**
* Represents an option stored by a path in config.yml
*/
public enum ConfigOption {
LANGUAGE("language", OptionType.STRING, "en-gb"),
CHECK_FOR_UPDATES("check_for_updates", OptionType.BOOLEAN, true),
DATA_STORAGE_TYPE("data_storage_options.storage_type", OptionType.STRING, "SQLite"),
DATABASE_HOST("data_storage_options.mysql_credentials.host", OptionType.STRING, "localhost"),
DATABASE_PORT("data_storage_options.mysql_credentials.port", OptionType.INTEGER, 3306),
DATABASE_NAME("data_storage_options.mysql_credentials.database", OptionType.STRING, "HuskHomes"),
DATABASE_USERNAME("data_storage_options.mysql_credentials.username", OptionType.STRING, "root"),
DATABASE_PASSWORD("data_storage_options.mysql_credentials.password", OptionType.STRING, "pa55w0rd"),
DATABASE_CONNECTION_PARAMS("data_storage_options.mysql_credentials.params", OptionType.STRING, "?autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull"),
DATABASE_CONNECTION_POOL_MAX_SIZE("data_storage_options.connection_pool_options.maximum_pool_size", OptionType.INTEGER, 10),
DATABASE_CONNECTION_POOL_MIN_IDLE("data_storage_options.connection_pool_options.minimum_idle", OptionType.INTEGER, 10),
DATABASE_CONNECTION_POOL_MAX_LIFETIME("data_storage_options.connection_pool_options.maximum_lifetime", OptionType.INTEGER, 1800000),
DATABASE_CONNECTION_POOL_KEEPALIVE("data_storage_options.connection_pool_options.keepalive_time", OptionType.INTEGER, 0),
DATABASE_CONNECTION_POOL_TIMEOUT("data_storage_options.connection_pool_options.connection_timeout", OptionType.INTEGER, 5000),
DATABASE_PLAYER_TABLE_NAME("data_storage_options.table_names.player_data", OptionType.STRING, "huskhomes_player_data"),
DATABASE_LOCATION_TABLE_NAME("data_storage_options.table_names.locations_data", OptionType.STRING, "huskhomes_location_data"),
DATABASE_HOMES_TABLE_NAME("data_storage_options.table_names.homes_data", OptionType.STRING, "huskhomes_home_data"),
DATABASE_WARPS_TABLE_NAME("data_storage_options.table_names.warps_data", OptionType.STRING, "huskhomes_warp_data"),
MAX_SETHOMES("general.max_sethomes", OptionType.INTEGER, 10),
AUTO_COMPLETE_PLAYER_NAMES("general.auto_complete_player_names", OptionType.BOOLEAN, true),
HOME_LIMIT_PERMISSION_STACKING("general.home_limit_permission_stacking", OptionType.BOOLEAN, false),
TELEPORT_WARMUP_TIME("general.teleport_warmup_time", OptionType.INTEGER, 5),
TELEPORT_WARMUP_DISPLAY_SLOT("general.teleport_warmup_display", OptionType.STRING, "ACTION_BAR"),
TELEPORT_REQUEST_EXPIRY_TIME("general.teleport_request_expiry_time", OptionType.INTEGER, 60),
ALLOW_UNICODE_DESCRIPTIONS("general.allow_unicode_descriptions", OptionType.BOOLEAN, true),
USE_PAPERLIB_IF_AVAILABLE("general.use_paperlib_if_available", OptionType.BOOLEAN, true),
TELEPORT_COMPLETE_SOUND("general.sounds.teleportation_complete", OptionType.STRING, "ENTITY_ENDERMAN_TELEPORT"),
TELEPORT_WARMUP_SOUND("general.sounds.teleportation_warmup", OptionType.STRING, "BLOCK_NOTE_BLOCK_BANJO"),
TELEPORT_CANCELLED_SOUND("general.sounds.teleportation_cancelled", OptionType.STRING, "ENTITY_ITEM_BREAK"),
PRIVATE_HOME_LIST_ITEMS_PER_PAGE("general.lists.private_homes_per_page", OptionType.INTEGER, 10),
PUBLIC_HOME_LIST_ITEMS_PER_PAGE("general.lists.public_homes_per_page", OptionType.INTEGER, 10),
WARP_LIST_ITEMS_PER_PAGE("general.lists.warps_per_page", OptionType.INTEGER, 10),
HELP_MENU_HIDE_COMMANDS_WITHOUT_PERMISSION("general.help_menu.hide_commands_without_permission", OptionType.BOOLEAN, true),
HELP_MENU_HIDE_HUSKHOMES_COMMAND("general.help_menu.hide_huskhomes_command", OptionType.BOOLEAN, false),
ENABLE_PROXY_MODE("proxy_options.enable_proxy_mode", OptionType.BOOLEAN, false),
CLUSTER_ID("proxy_options.cluster_id", OptionType.STRING, ""),
MESSENGER_TYPE("proxy_options.messenger_type", OptionType.STRING, "PLUGIN_MESSAGE"),
REDIS_HOST("proxy_options.redis_credentials.host", OptionType.STRING, "localhost"),
REDIS_PORT("proxy_options.redis_credentials.port", OptionType.INTEGER, 6379),
REDIS_PASSWORD("proxy_options.redis_credentials.password", OptionType.STRING, ""),
REDIS_USE_SSL("proxy_options.redis_credentials.use_ssl", OptionType.BOOLEAN, false);
/**
* The path in the config.yml file to the value
*/
@NotNull
public final String configPath;
/**
* The {@link OptionType} of this option
*/
@NotNull
public final OptionType optionType;
/**
* The default value of this option if not set in config
*/
@Nullable
private final Object defaultValue;
ConfigOption(@NotNull String configPath, @NotNull OptionType optionType, @Nullable Object defaultValue) {
this.configPath = configPath;
this.optionType = optionType;
this.defaultValue = defaultValue;
}
ConfigOption(@NotNull String configPath, @NotNull OptionType optionType) {
this.configPath = configPath;
this.optionType = optionType;
this.defaultValue = null;
}
/**
* Get the value at the path specified (or return default if set), as a string
*
* @param config The {@link YamlDocument} config file
* @return the value defined in the config, as a string
*/
public String getStringValue(@NotNull YamlDocument config) {
return defaultValue != null
? config.getString(configPath, (String) defaultValue)
: config.getString(configPath);
}
/**
* Get the value at the path specified (or return default if set), as a boolean
*
* @param config The {@link YamlDocument} config file
* @return the value defined in the config, as a boolean
*/
public boolean getBooleanValue(@NotNull YamlDocument config) {
return defaultValue != null
? config.getBoolean(configPath, (Boolean) defaultValue)
: config.getBoolean(configPath);
}
/**
* Get the value at the path specified (or return default if set), as a double
*
* @param config The {@link YamlDocument} config file
* @return the value defined in the config, as a double
*/
public double getDoubleValue(@NotNull YamlDocument config) {
return defaultValue != null
? config.getDouble(configPath, (Double) defaultValue)
: config.getDouble(configPath);
}
/**
* Get the value at the path specified (or return default if set), as a float
*
* @param config The {@link YamlDocument} config file
* @return the value defined in the config, as a float
*/
public float getFloatValue(@NotNull YamlDocument config) {
return defaultValue != null
? config.getFloat(configPath, (Float) defaultValue)
: config.getFloat(configPath);
}
/**
* Get the value at the path specified (or return default if set), as an int
*
* @param config The {@link YamlDocument} config file
* @return the value defined in the config, as an int
*/
public int getIntValue(@NotNull YamlDocument config) {
return defaultValue != null
? config.getInt(configPath, (Integer) defaultValue)
: config.getInt(configPath);
}
/**
* Get the value at the path specified (or return default if set), as a string {@link List}
*
* @param config The {@link YamlDocument} config file
* @return the value defined in the config, as a string {@link List}
*/
public List<String> getStringListValue(@NotNull YamlDocument config) {
return config.getStringList(configPath, new ArrayList<>());
}
/**
* Represents the type of the object
*/
public enum OptionType {
BOOLEAN,
STRING,
DOUBLE,
FLOAT,
INTEGER,
STRING_LIST
}
}
}
@@ -1,7 +1,8 @@
package net.william278.huskhomes.player;
import de.themoep.minedown.MineDown;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportResult;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@@ -38,9 +39,14 @@ public interface Player {
* @param node The permission node string
* @return {@code true} if the player has the node; {@code false} otherwise
*/
boolean hasPermission(String node);
boolean hasPermission(@NotNull String node);
void sendMessage();
/**
* Dispatch a MineDown-formatted message to this player
*
* @param mineDown the parsed {@link MineDown} to send
*/
void sendMessage(@NotNull MineDown mineDown);
/**
* Returns the maximum number of homes this player can set
@@ -56,12 +62,4 @@ public interface Player {
*/
CompletableFuture<Integer> getFreeHomes();
/**
* Teleport the player to a specified {@link Position}
*
* @param position the target {@link Position} to teleport to
* @return A {@link CompletableFuture} callback returning the {@link TeleportResult} of completing the teleport
*/
CompletableFuture<TeleportResult> teleport(Position position);
}
@@ -2,7 +2,14 @@ package net.william278.huskhomes.position;
import org.jetbrains.annotations.NotNull;
/**
* Represents a persistently-saved {@link Position}
*/
public class SavedPosition extends Position {
/**
* Metadata about this position (name, description)
*/
@NotNull
public final PositionMeta positionMeta;
@@ -1,7 +1,5 @@
package net.william278.huskhomes.position;
import java.util.UUID;
/**
* Represents a server on a proxied network
*/
@@ -12,9 +10,16 @@ public class Server {
*/
public String id;
/**
* Plugin-assigned identifier of this server
*/
public UUID uuid;
public Server(String id) {
this.id = id;
}
@Override
public boolean equals(Object other) {
if (other instanceof Server server) {
// If the id of this server matches another, the servers are the same.
return server.id.equalsIgnoreCase(this.id);
}
return super.equals(other);
}
}
@@ -0,0 +1,28 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.player.Player;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
/**
* Represents a teleport in the process of being executed
*/
public class Teleport {
/**
* The player involved in the teleport
*/
@NotNull
public Player player;
/**
* The target position for the player
*/
@NotNull
public Position target;
public Teleport(@NotNull Player player, @NotNull Position target) {
this.player = player;
this.target = target;
}
}
@@ -0,0 +1,54 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.Player;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
/**
* Cross-platform teleportation manager
*/
public abstract class TeleportManager {
/**
* Instance of the implementing plugin
*/
@NotNull
private final HuskHomes plugin;
/**
* {@link Set} of currently processing teleports
*/
@NotNull
private final Set<CompletableFuture<Teleport>> processingTeleports;
public TeleportManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.processingTeleports = new HashSet<>();
}
/**
* Teleport the player to a specified {@link Position}
*
* @param position the target {@link Position} to teleport to
* @return A {@link CompletableFuture} callback returning the {@link TeleportResult} of completing the teleport
*/
public CompletableFuture<TeleportResult> teleport(Player player, Position position) {
if (position.server.equals(plugin.getServerData())) {
return teleportLocally(new Teleport(player, position));
} else {
return teleportCrossServer(new Teleport(player, position));
}
}
public abstract CompletableFuture<TeleportResult> teleportLocally(Teleport teleport);
private CompletableFuture<TeleportResult> teleportCrossServer(Teleport teleport) {
return null; //todo dispatch cross-server message here!
}
}
@@ -4,31 +4,31 @@ import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
/**
* Represents the result of a completed teleport
* Represents the result of a completed {@link Teleport}
*/
public enum TeleportResult {
/**
* Returns if the teleport completed successfully
* Returns if the {@link Teleport} completed successfully
*/
SUCCESS(true),
/**
* Returns if the teleport failed because the {@link World} of the
* Returns if the {@link Teleport} failed because the {@link World} of the
* target {@link Position} could not be found
* <p>
*/
FAILED_INVALID_WORLD(false),
/**
* Returns if the teleport failed because the server of the target
* Returns if the {@link Teleport} failed because the server of the target
* {@link Position} was invalid or not online
* <p>
*/
FAILED_INVALID_SERVER(false),
/**
* Returns if the teleport failed because the coordinates of the
* Returns if the {@link Teleport} failed because the coordinates of the
* target {@link Position} were outside the world border limits
* <p>
*/
@@ -2,6 +2,9 @@ package net.william278.huskhomes.util;
import java.util.logging.Level;
/**
* An abstract, cross-platform representation of a logger
*/
public interface Logger {
void log(Level level, String message, Exception e);