From 19954a201c2fdbefbeb85b0b021a57e63e15df5a Mon Sep 17 00:00:00 2001 From: onebeastchris Date: Thu, 22 Aug 2024 20:26:37 +0200 Subject: [PATCH] Add option#(uuid, type) method to api, add logger.debug(message, objects...) method to easily debug-log messages without debug checks preventing formatting overhead --- .../SessionLoadResourcePacksEvent.java | 13 +++++++++++-- .../GeyserDefineResourcePacksEvent.java | 13 +++++++++++-- .../bungeecord/GeyserBungeeLogger.java | 7 +++++++ .../geyser/platform/mod/GeyserModLogger.java | 7 +++++++ .../platform/spigot/GeyserSpigotLogger.java | 7 +++++++ .../standalone/GeyserStandaloneLogger.java | 5 +++++ .../velocity/GeyserVelocityLogger.java | 9 ++++++++- .../viaproxy/GeyserViaProxyLogger.java | 7 +++++++ .../org/geysermc/geyser/GeyserLogger.java | 9 +++++++++ .../GeyserDefineResourcePacksEventImpl.java | 13 +++++++++++++ .../SessionLoadResourcePacksEventImpl.java | 19 ++++++++++++++++++- .../geyser/network/UpstreamPacketHandler.java | 16 ++++++++-------- .../geyser/pack/option/OptionHolder.java | 19 +++++++++++++++++++ 13 files changed, 130 insertions(+), 14 deletions(-) diff --git a/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionLoadResourcePacksEvent.java b/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionLoadResourcePacksEvent.java index 43757f916..efa6b4ea8 100644 --- a/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionLoadResourcePacksEvent.java +++ b/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionLoadResourcePacksEvent.java @@ -54,11 +54,11 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent { /** * Registers a {@link ResourcePack} to be sent to the client. * - * @param resourcePack a resource pack that will be sent to the client. + * @param pack a resource pack that will be sent to the client. * @return true if the resource pack was added successfully, * or false if already present */ - public abstract boolean register(@NonNull ResourcePack resourcePack); + public abstract boolean register(@NonNull ResourcePack pack); /** * Registers a {@link ResourcePack} to be sent to the client, alongside @@ -89,6 +89,15 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent { */ public abstract Collection> options(@NonNull UUID uuid); + /** + * Returns the current option, or null, for a given ResourcePackOption type. + * + * @param uuid the resource pack for which the option type is set + * @param type the {@link ResourcePackOption.Type} of the option to query + * @throws IllegalArgumentException if the pack is not registered. + */ + public abstract @Nullable ResourcePackOption option(@NonNull UUID uuid, ResourcePackOption.@NonNull Type type); + /** * Unregisters a resource pack from being sent to the client. * diff --git a/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineResourcePacksEvent.java b/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineResourcePacksEvent.java index 68ccc1e5b..fbd26d05a 100644 --- a/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineResourcePacksEvent.java +++ b/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineResourcePacksEvent.java @@ -51,12 +51,12 @@ public abstract class GeyserDefineResourcePacksEvent implements Event { * Registers a {@link ResourcePack} to be sent to the client, optionally alongside * specific options. * - * @param resourcePack a resource pack that will be sent to the client. + * @param pack a resource pack that will be sent to the client. * @param options {@link ResourcePackOption}'s that specify how clients load the pack * @return true if the resource pack was added successfully, * or false if already present */ - public abstract boolean register(@NonNull ResourcePack resourcePack, @Nullable ResourcePackOption... options); + public abstract boolean register(@NonNull ResourcePack pack, @Nullable ResourcePackOption... options); /** * Sets {@link ResourcePackOption}'s for a resource pack @@ -76,6 +76,15 @@ public abstract class GeyserDefineResourcePacksEvent implements Event { */ public abstract Collection> options(@NonNull UUID uuid); + /** + * Returns the current option, or null, for a given ResourcePackOption type. + * + * @param uuid the resource pack for which the option type is set + * @param type the {@link ResourcePackOption.Type} of the option to query + * @throws IllegalArgumentException if the pack is not registered. + */ + public abstract @Nullable ResourcePackOption option(@NonNull UUID uuid, ResourcePackOption.@NonNull Type type); + /** * Unregisters a {@link ResourcePack} from being sent to clients. * diff --git a/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeeLogger.java b/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeeLogger.java index e8cf7ee39..e9b18acca 100644 --- a/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeeLogger.java +++ b/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeeLogger.java @@ -75,4 +75,11 @@ public class GeyserBungeeLogger implements GeyserLogger { info(message); } } + + @Override + public void debug(String message, Object... arguments) { + if (debug) { + info(String.format(message, arguments)); + } + } } diff --git a/bootstrap/mod/src/main/java/org/geysermc/geyser/platform/mod/GeyserModLogger.java b/bootstrap/mod/src/main/java/org/geysermc/geyser/platform/mod/GeyserModLogger.java index 9260288d7..9903d0d2e 100644 --- a/bootstrap/mod/src/main/java/org/geysermc/geyser/platform/mod/GeyserModLogger.java +++ b/bootstrap/mod/src/main/java/org/geysermc/geyser/platform/mod/GeyserModLogger.java @@ -84,6 +84,13 @@ public class GeyserModLogger implements GeyserLogger { } } + @Override + public void debug(String message, Object... arguments) { + if (debug) { + logger.info(message, arguments); + } + } + @Override public void setDebug(boolean debug) { this.debug = debug; diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotLogger.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotLogger.java index 5c6101eae..231255fec 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotLogger.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotLogger.java @@ -75,4 +75,11 @@ public class GeyserSpigotLogger implements GeyserLogger { info(message); } } + + @Override + public void debug(String message, Object... arguments) { + if (debug) { + info(String.format(message, arguments)); + } + } } diff --git a/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneLogger.java b/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneLogger.java index 21e6a5e82..bdf699e28 100644 --- a/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneLogger.java +++ b/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneLogger.java @@ -89,6 +89,11 @@ public class GeyserStandaloneLogger extends SimpleTerminalConsole implements Gey log.debug(ChatColor.GRAY + message); } + @Override + public void debug(String message, Object... arguments) { + log.debug(ChatColor.GRAY + message, arguments); + } + @Override public void setDebug(boolean debug) { Configurator.setLevel(log.getName(), debug ? Level.DEBUG : Level.INFO); diff --git a/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityLogger.java b/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityLogger.java index 4d10e4daf..5155d8958 100644 --- a/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityLogger.java +++ b/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityLogger.java @@ -73,4 +73,11 @@ public class GeyserVelocityLogger implements GeyserLogger { info(message); } } -} \ No newline at end of file + + @Override + public void debug(String message, Object... arguments) { + if (debug) { + logger.info(message, arguments); + } + } +} diff --git a/bootstrap/viaproxy/src/main/java/org/geysermc/geyser/platform/viaproxy/GeyserViaProxyLogger.java b/bootstrap/viaproxy/src/main/java/org/geysermc/geyser/platform/viaproxy/GeyserViaProxyLogger.java index 10f414b51..fdcfe2279 100644 --- a/bootstrap/viaproxy/src/main/java/org/geysermc/geyser/platform/viaproxy/GeyserViaProxyLogger.java +++ b/bootstrap/viaproxy/src/main/java/org/geysermc/geyser/platform/viaproxy/GeyserViaProxyLogger.java @@ -75,6 +75,13 @@ public class GeyserViaProxyLogger implements GeyserLogger, GeyserCommandSource { } } + @Override + public void debug(String message, Object... arguments) { + if (this.debug) { + this.debug(String.format(message, arguments)); + } + } + @Override public void setDebug(boolean debug) { this.debug = debug; diff --git a/core/src/main/java/org/geysermc/geyser/GeyserLogger.java b/core/src/main/java/org/geysermc/geyser/GeyserLogger.java index f408de29c..34a2d6e93 100644 --- a/core/src/main/java/org/geysermc/geyser/GeyserLogger.java +++ b/core/src/main/java/org/geysermc/geyser/GeyserLogger.java @@ -103,6 +103,15 @@ public interface GeyserLogger extends GeyserCommandSource { debug(String.valueOf(object)); } + /** + * Logs an object to console if debug mode is enabled, + * and formats it with the provided arguments + * + * @param message the message to log + * @param arguments the arguments to replace in the message + */ + void debug(String message, Object... arguments); + /** * Sets if the logger should print debug messages * diff --git a/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineResourcePacksEventImpl.java b/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineResourcePacksEventImpl.java index 4906dc928..5429767ea 100644 --- a/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineResourcePacksEventImpl.java +++ b/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineResourcePacksEventImpl.java @@ -94,6 +94,19 @@ public class GeyserDefineResourcePacksEventImpl extends GeyserDefineResourcePack return packHolder.optionHolder().immutableValues(); } + @Override + public @Nullable ResourcePackOption option(@NonNull UUID uuid, ResourcePackOption.@NonNull Type type) { + Objects.requireNonNull(uuid); + Objects.requireNonNull(type); + + ResourcePackHolder packHolder = packs.get(uuid); + if (packHolder == null) { + throw new IllegalArgumentException("ResourcePack with " + uuid + " not found, unable to provide options"); + } + + return packHolder.optionHolder().get(type); + } + @Override public boolean unregister(@NonNull UUID uuid) { return packs.remove(uuid) != null; diff --git a/core/src/main/java/org/geysermc/geyser/event/type/SessionLoadResourcePacksEventImpl.java b/core/src/main/java/org/geysermc/geyser/event/type/SessionLoadResourcePacksEventImpl.java index ba8acdd20..c13bbde92 100644 --- a/core/src/main/java/org/geysermc/geyser/event/type/SessionLoadResourcePacksEventImpl.java +++ b/core/src/main/java/org/geysermc/geyser/event/type/SessionLoadResourcePacksEventImpl.java @@ -97,7 +97,7 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE Objects.requireNonNull(options); ResourcePackHolder holder = packs.get(uuid); if (holder == null) { - throw new IllegalArgumentException("Pack with uuid %s not registered yet!".formatted(uuid)); + throw new IllegalArgumentException("ResourcePack with " + uuid + " not found, unable to provide options"); } registerOption(holder.pack(), options); @@ -115,6 +115,23 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE return optionHolder.immutableValues(packHolder.optionHolder()); } + @Override + public @Nullable ResourcePackOption option(@NonNull UUID uuid, ResourcePackOption.@NonNull Type type) { + Objects.requireNonNull(uuid); + Objects.requireNonNull(type); + + ResourcePackHolder packHolder = packs.get(uuid); + if (packHolder == null) { + throw new IllegalArgumentException("ResourcePack with " + uuid + " not found, unable to provide options"); + } + + OptionHolder holder = options.get(uuid); + OptionHolder defaultHolder = packHolder.optionHolder(); + Objects.requireNonNull(defaultHolder); // should never be null + + return OptionHolder.getOptionByType(type, holder, defaultHolder); + } + @Override public boolean unregister(@NonNull UUID uuid) { options.remove(uuid); diff --git a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java index 1516e8b1e..e40bbbc7d 100644 --- a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java +++ b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java @@ -302,8 +302,8 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { ResourcePackHolder holder = this.resourcePackLoadEvent.getPacks().get(packet.getPackId()); if (holder == null) { - GeyserImpl.getInstance().getLogger().debug("Client %s tried to request pack id (%s) not sent to it!" - .formatted(session.bedrockUsername(), packet.getPackId())); + GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request pack id {1} not sent to it!", + session.bedrockUsername(), packet.getPackId()); session.disconnect("disconnectionScreen.resourcePack"); return PacketSignal.HANDLED; } @@ -356,8 +356,8 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { String[] packID = id.split("_"); if (packID.length < 2) { - GeyserImpl.getInstance().getLogger().debug("Client %s tried to request invalid pack id %s!" - .formatted(session.bedrockUsername(), packID)); + GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request invalid pack id {1}!", + session.bedrockUsername(), packID); session.disconnect("disconnectionScreen.resourcePack"); return; } @@ -366,16 +366,16 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { try { packId = UUID.fromString(packID[0]); } catch (IllegalArgumentException e) { - GeyserImpl.getInstance().getLogger().debug("Client %s tried to request pack with an invalid id (%s)" - .formatted(session.bedrockUsername(), id)); + GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request pack with an invalid id {1})", + session.bedrockUsername(), id); session.disconnect("disconnectionScreen.resourcePack"); return; } ResourcePackHolder holder = this.resourcePackLoadEvent.getPacks().get(packId); if (holder == null) { - GeyserImpl.getInstance().getLogger().debug("Client %s tried to request pack id (%s) not sent to it!" - .formatted(session.bedrockUsername(), id)); + GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request pack id {1} not sent to it!", + session.bedrockUsername(), id); session.disconnect("disconnectionScreen.resourcePack"); return; } diff --git a/core/src/main/java/org/geysermc/geyser/pack/option/OptionHolder.java b/core/src/main/java/org/geysermc/geyser/pack/option/OptionHolder.java index d4645c333..958ab9413 100644 --- a/core/src/main/java/org/geysermc/geyser/pack/option/OptionHolder.java +++ b/core/src/main/java/org/geysermc/geyser/pack/option/OptionHolder.java @@ -78,6 +78,25 @@ public class OptionHolder extends HashMap getOptionByType(ResourcePackOption.@NonNull Type type, + @Nullable OptionHolder holder, + @NonNull OptionHolder defaultHolder) { + ResourcePackOption option; + + // First: the optionHolder's option, if it exists + if (holder != null) { + option = holder.get(type); + if (option != null) { + return option; + } + } + + // Second: check the default optionHolder for the option, if it exists; + // Or return null if the option isn't set. + option = defaultHolder.get(type); + return option; + } + public void remove(ResourcePackOption option) { super.remove(option.type()); }