Cut down jar size, adjust shadings, fix message formatting, adjust home pattern matching

This commit is contained in:
William committed 2022-08-10 22:42:37 +01:00
1 parent ff2465ac85
commit 979d57e7b2
22 files changed
+160 -107

No files matched your search

@@ -17,7 +17,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable, Conso
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
if (homes.size() == 1) {
@@ -29,25 +29,21 @@ public class DelHomeCommand extends CommandBase implements TabCompletable, Conso
});
case 1 -> {
final String homeName = args[0];
if (RegexUtil.OWNER_NAME_PATTERN.matcher(homeName).matches()) {
final String ownerName = homeName.split("\\.")[0];
final String ownersHomeName = homeName.split("\\.")[1];
plugin.getDatabase().getUserDataByName(ownerName).thenAccept(optionalUserData -> optionalUserData
.ifPresentOrElse(userData -> {
if (!userData.getUserUuid().equals(onlineUser.uuid)) {
if (!onlineUser.hasPermission(Permission.COMMAND_DELETE_HOME_OTHER.node)) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(onlineUser::sendMessage);
return;
}
}
plugin.getSavedPositionManager().deleteHome(userData.user(), ownersHomeName);
},
() -> plugin.getLocales().getLocale("error_home_invalid_other", ownerName, homeName)
.ifPresent(onlineUser::sendMessage)));
} else {
plugin.getSavedPositionManager().deleteHome(onlineUser, homeName);
}
RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse(homeIdentifier ->
plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName()).thenAccept(
optionalUserData -> optionalUserData.ifPresentOrElse(userData -> {
if (!userData.getUserUuid().equals(onlineUser.uuid)) {
if (!onlineUser.hasPermission(Permission.COMMAND_DELETE_HOME_OTHER.node)) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(onlineUser::sendMessage);
return;
}
}
plugin.getSavedPositionManager().deleteHome(userData.user(), homeIdentifier.homeName());
},
() -> plugin.getLocales().getLocale("error_home_invalid_other",
homeIdentifier.ownerName(), homeIdentifier.homeName()).ifPresent(onlineUser::sendMessage))),
() -> plugin.getSavedPositionManager().deleteHome(onlineUser, homeName));
}
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/delhome <name>")
.ifPresent(onlineUser::sendMessage);
@@ -18,7 +18,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
switch (homes.size()) {
@@ -34,16 +34,13 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
});
case 1 -> {
final String homeName = args[0];
if (RegexUtil.OWNER_NAME_PATTERN.matcher(homeName).matches()) {
final String ownerName = homeName.split("\\.")[0];
final String ownersHomeName = homeName.split("\\.")[1];
plugin.getDatabase().getUserDataByName(ownerName).thenAccept(optionalUserData -> optionalUserData
.ifPresentOrElse(userData -> plugin.getTeleportManager().teleportToHome(onlineUser, userData.user(), ownersHomeName),
() -> plugin.getLocales().getLocale("error_home_invalid_other", ownerName, homeName)
.ifPresent(onlineUser::sendMessage)));
} else {
plugin.getTeleportManager().teleportToHome(onlineUser, onlineUser, homeName);
}
RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse(
homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName())
.thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse(
userData -> plugin.getTeleportManager().teleportToHome(onlineUser, userData.user(), homeIdentifier.ownerName()),
() -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName())
.ifPresent(onlineUser::sendMessage))),
() -> plugin.getTeleportManager().teleportToHome(onlineUser, onlineUser, homeName));
}
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/home [name]")
.ifPresent(onlineUser::sendMessage);
@@ -52,26 +52,20 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
return;
}
}
plugin.getLoggingAdapter().info("Getting home list for " + homeOwner);
plugin.getDatabase().getUserDataByName(homeOwner).thenAccept(optionalUser -> optionalUser.ifPresentOrElse(userData -> {
plugin.getLoggingAdapter().info("Got user!");
if (!userData.getUserUuid().equals(onlineUser.uuid)) {
if (!onlineUser.hasPermission(Permission.COMMAND_HOME_OTHER.node)) {
plugin.getLocales().getLocale("error_no_permission").ifPresent(onlineUser::sendMessage);
return;
}
}
plugin.getLoggingAdapter().info("User has perms! Getting list...");
plugin.getDatabase().getHomes(userData.user()).thenAccept(homes -> {
plugin.getLoggingAdapter().info("Got home list!");
if (homes.isEmpty()) {
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
return;
}
plugin.getLoggingAdapter().info("Making list...");
final PrivateHomeList homeList = new PrivateHomeList(homes, userData.user(), plugin);
plugin.getCache().positionLists.put(userData.user().uuid, homeList);
plugin.getLoggingAdapter().info("Showing list...");
homeList.getDisplay(pageNumber).forEach(onlineUser::sendMessage);
});
}, () -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(onlineUser::sendMessage)));
@@ -16,7 +16,6 @@ public class SetHomeCommand extends CommandBase {
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
if (homes.isEmpty()) {
@@ -2,6 +2,7 @@ package net.william278.huskhomes.config;
import de.themoep.minedown.MineDown;
import dev.dejvokep.boostedyaml.YamlDocument;
import org.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
@@ -15,10 +16,10 @@ public class Locales {
@NotNull
private final HashMap<String, String> rawLocales;
private Locales(YamlDocument localesConfig) {
private Locales(@NotNull YamlDocument localesConfig) {
this.rawLocales = new HashMap<>();
for (String localeId : localesConfig.getRoutesAsStrings(false)) {
rawLocales.put(localeId, localesConfig.getString(localeId));
rawLocales.put(localeId, StringEscapeUtils.unescapeJava(localesConfig.getString(localeId)));
}
}
@@ -289,13 +289,9 @@ public class MySqlDatabase extends Database {
resultSet.getBoolean("ignoring_requests"),
resultSet.getTimestamp("rtp_cooldown").toInstant().getEpochSecond()));
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to fetch a player by name from the database", e);
} catch (Exception e) {
e.printStackTrace();
}
return Optional.empty();
});
@@ -15,21 +15,37 @@ public class EventListener {
@NotNull
protected final HuskHomes plugin;
// Indicates if the first user has joined yet
private boolean firstUser = true;
protected EventListener(@NotNull HuskHomes implementor) {
this.plugin = implementor;
}
/**
* Handle when the first {@link OnlineUser} joins the server
*
* @param onlineUser {@link OnlineUser} joining the server
*/
private void handleFirstPlayerJoin(@NotNull OnlineUser onlineUser) {
// Cache the server name
plugin.getServer(onlineUser);
}
/**
* Handle when a {@link OnlineUser} joins the server
*
* @param onlineUser the joining {@link OnlineUser}
*/
protected final void handlePlayerJoin(@NotNull OnlineUser onlineUser) {
plugin.getLoggingAdapter().info("Player " + onlineUser.username + " joined");
// Handle the first player joining the server
if (firstUser) {
handleFirstPlayerJoin(onlineUser);
firstUser = false;
}
// Ensure the player is present on the database first
plugin.getDatabase().ensureUser(onlineUser).thenRun(() -> {
plugin.getLoggingAdapter().info("Player " + onlineUser.username + " ensured");
// If the server is in proxy mode, check if the player is teleporting cross-server and handle
if (plugin.getSettings().getBooleanValue(Settings.ConfigOption.ENABLE_PROXY_MODE)) {
plugin.getDatabase().getCurrentTeleport(onlineUser)
@@ -43,7 +59,7 @@ public class EventListener {
plugin.getLocales().getLocale("teleporting_complete")
.ifPresent(onlineUser::sendMessage);
}
})));
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null))));
// Update the player list
assert plugin.getNetworkMessenger() != null;
plugin.getCache().updatePlayerList(plugin, onlineUser);
@@ -46,7 +46,8 @@ public class Message {
* ID of the cluster this message is for
*/
@SerializedName("cluster_id")
public int clusterId;
@NotNull
public String clusterId;
/**
* Data to send in the message
@@ -55,7 +56,7 @@ public class Message {
public String payload;
public Message(@NotNull MessageType type, @NotNull String sender, @NotNull String targetPlayer,
@NotNull MessageSerializable payload, @NotNull MessageKind messageKind, int clusterId) {
@NotNull MessageSerializable payload, @NotNull MessageKind messageKind, @NotNull String clusterId) {
this.type = type;
this.sender = sender;
this.targetPlayer = targetPlayer;
@@ -41,7 +41,7 @@ public abstract class NetworkMessenger {
/**
* ID of the HuskHomes network cluster this server is on
*/
protected int clusterId;
protected String clusterId;
/**
* Initialize the network messenger
@@ -50,7 +50,7 @@ public abstract class NetworkMessenger {
*/
public void initialize(@NotNull HuskHomes implementor) {
this.processingMessages = new HashMap<>();
this.clusterId = implementor.getSettings().getIntegerValue(Settings.ConfigOption.CLUSTER_ID);
this.clusterId = implementor.getSettings().getStringValue(Settings.ConfigOption.CLUSTER_ID);
}
/**
@@ -15,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
/**
* Cross-platform teleportation manager
@@ -34,9 +35,9 @@ public abstract class TeleportManager {
/**
* Teleport a {@link OnlineUser} to another {@link User}'s {@link Home}
*
* @param onlineUser the {@link OnlineUser} to teleport
* @param homeOwner the {@link User} who owns the home
* @param homeName the name of the home
* @param onlineUser the {@link OnlineUser} to teleport
* @param homeOwner the {@link User} who owns the home
* @param homeName the name of the home
*/
public void teleportToHome(@NotNull OnlineUser onlineUser, @NotNull User homeOwner, @NotNull String homeName) {
plugin.getDatabase().getHome(homeOwner, homeName).thenAccept(optionalHome ->
@@ -61,8 +62,8 @@ public abstract class TeleportManager {
/**
* Teleport a {@link OnlineUser} to a server {@link Warp}
*
* @param onlineUser the {@link OnlineUser} to teleport
* @param warpName the name of the warp
* @param onlineUser the {@link OnlineUser} to teleport
* @param warpName the name of the warp
*/
public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull String warpName) {
plugin.getDatabase().getWarp(warpName).thenAccept(optionalWarp ->
@@ -75,7 +76,7 @@ public abstract class TeleportManager {
/**
* Teleport a {@link OnlineUser} to another player by given input name
*
* @param onlineUser the {@link OnlineUser} to teleport
* @param onlineUser the {@link OnlineUser} to teleport
* @param targetPlayer the name of the target player
*/
public void teleportToPlayer(@NotNull OnlineUser onlineUser, @NotNull String targetPlayer) {
@@ -111,7 +112,7 @@ public abstract class TeleportManager {
playerName,
new EmptyPayload(),
Message.MessageKind.MESSAGE,
plugin.getSettings().getIntegerValue(Settings.ConfigOption.CLUSTER_ID)))
plugin.getSettings().getStringValue(Settings.ConfigOption.CLUSTER_ID)))
.thenApply(reply -> Optional.of(Position.fromJson(reply.payload))).join();
}
return Optional.empty();
@@ -121,8 +122,8 @@ public abstract class TeleportManager {
/**
* Teleport a {@link OnlineUser} to a specified {@link Position}. Respects timed teleport.
*
* @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to
* @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to
*/
public CompletableFuture<TeleportResult> teleport(@NotNull OnlineUser onlineUser, @NotNull Position position) {
return CompletableFuture.supplyAsync(() -> {
@@ -131,7 +132,7 @@ public abstract class TeleportManager {
return processTimedTeleport(new TimedTeleport(onlineUser, position, teleportWarmupTime))
.thenApply(teleport -> {
if (!teleport.cancelled) {
return teleport.getPlayer().teleport(teleport.getTargetPosition()).join();
return teleportNow(onlineUser, position).join();
} else {
return TeleportResult.CANCELLED;
}
@@ -145,7 +146,7 @@ public abstract class TeleportManager {
/**
* Handles a completed {@link OnlineUser}'s {@link TeleportResult} with the appropriate message
*
* @param onlineUser the {@link OnlineUser} who just completed a teleport
* @param onlineUser the {@link OnlineUser} who just completed a teleport
* @param teleportResult the {@link TeleportResult} to handle
*/
public void finishTeleport(@NotNull OnlineUser onlineUser, @NotNull TeleportResult teleportResult) {
@@ -167,8 +168,8 @@ public abstract class TeleportManager {
/**
* Executes a teleport now, teleporting a {@link OnlineUser} to a specified {@link Position}
*
* @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to
* @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to
*/
private CompletableFuture<TeleportResult> teleportNow(@NotNull OnlineUser onlineUser, @NotNull Position position) {
final Teleport teleport = new Teleport(onlineUser, position);
@@ -208,6 +209,8 @@ public abstract class TeleportManager {
if (teleport.hasTakenDamage()) {
plugin.getLocales().getLocale("teleporting_cancelled_damage").ifPresent(locale ->
teleport.getPlayer().sendMessage(locale));
plugin.getLocales().getLocale("teleporting_action_bar_cancelled").ifPresent(locale ->
teleport.getPlayer().sendActionBar(locale));
teleport.cancelled = true;
return Optional.of(teleport);
}
@@ -216,6 +219,8 @@ public abstract class TeleportManager {
if (teleport.hasMoved()) {
plugin.getLocales().getLocale("teleporting_cancelled_movement").ifPresent(locale ->
teleport.getPlayer().sendMessage(locale));
plugin.getLocales().getLocale("teleporting_action_bar_cancelled").ifPresent(locale ->
teleport.getPlayer().sendActionBar(locale));
teleport.cancelled = true;
return Optional.of(teleport);
}
@@ -17,7 +17,7 @@ public class TimedTeleport {
private final double startHealth;
private final int duration;
private int timeLeft;
protected int timeLeft;
public boolean cancelled = false;
@@ -1,8 +1,10 @@
package net.william278.huskhomes.util;
import java.util.Optional;
import java.util.regex.Pattern;
import net.william278.huskhomes.position.PositionMeta;
import org.jetbrains.annotations.NotNull;
/**
* Provides a number of utility regular expressions for checking home name patterns
@@ -20,9 +22,44 @@ public final class RegexUtil {
public static final Pattern NAME_PATTERN = Pattern.compile("[A-Za-z\\d_\\-]+");
/**
* Pattern for checking public home input fields
* Pattern for checking home input fields disambiguated by the owner's name
* <p>
* e.g. {@code ownerName.homeName}
*/
public static final Pattern OWNER_NAME_PATTERN = Pattern.compile("\\w+\\.[A-Za-z\\d_\\-]+");
private static final Pattern OWNER_DISAMBIGUATED_HOME_IDENTIFIER_PATTERN = Pattern.compile("\\w+\\.[A-Za-z\\d_\\-]+");
/**
* Match pattern for checking home input fields disambiguated by the owner's name
*
* @param input input string to match
* @return An optional containing the {@link DisambiguatedHomeIdentifier} if matched, otherwise empty if not
*/
public static Optional<DisambiguatedHomeIdentifier> matchDisambiguatedHomeIdentifier(@NotNull String input) {
if (OWNER_DISAMBIGUATED_HOME_IDENTIFIER_PATTERN.matcher(input).matches()) {
final String[] separatedInput = input.split(Pattern.quote("."));
return Optional.of(new DisambiguatedHomeIdentifier(separatedInput[0], separatedInput[1]));
} else {
return Optional.empty();
}
}
/**
* Represents an identifier for a home or warp {@link PositionMeta} that is disambiguated by the owner's name
*
* @param ownerName the username of a home's owner
* @param homeName the name of a home
*/
public record DisambiguatedHomeIdentifier(String ownerName, String homeName) {
/**
* Get the period-separated formatted disambiguated home identifier
*
* @return the formatted disambiguated home identifier (e.g. {@code ownerName.homeName})
*/
public String toString() {
return ownerName + "." + homeName;
}
}
}