3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-10-03 16:31:14 +02:00

Add option#(uuid, type) method to api, add logger.debug(message, objects...) method to easily debug-log messages without debug checks preventing formatting overhead

Dieser Commit ist enthalten in:
onebeastchris 2024-08-22 20:26:37 +02:00
Ursprung 18776a8000
Commit 19954a201c
13 geänderte Dateien mit 130 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -54,11 +54,11 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent {
/** /**
* Registers a {@link ResourcePack} to be sent to the client. * 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, * @return true if the resource pack was added successfully,
* or false if already present * 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 * Registers a {@link ResourcePack} to be sent to the client, alongside
@ -89,6 +89,15 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent {
*/ */
public abstract Collection<ResourcePackOption<?>> options(@NonNull UUID uuid); public abstract Collection<ResourcePackOption<?>> 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. * Unregisters a resource pack from being sent to the client.
* *

Datei anzeigen

@ -51,12 +51,12 @@ public abstract class GeyserDefineResourcePacksEvent implements Event {
* Registers a {@link ResourcePack} to be sent to the client, optionally alongside * Registers a {@link ResourcePack} to be sent to the client, optionally alongside
* specific options. * 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 * @param options {@link ResourcePackOption}'s that specify how clients load the pack
* @return true if the resource pack was added successfully, * @return true if the resource pack was added successfully,
* or false if already present * 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 * Sets {@link ResourcePackOption}'s for a resource pack
@ -76,6 +76,15 @@ public abstract class GeyserDefineResourcePacksEvent implements Event {
*/ */
public abstract Collection<ResourcePackOption<?>> options(@NonNull UUID uuid); public abstract Collection<ResourcePackOption<?>> 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. * Unregisters a {@link ResourcePack} from being sent to clients.
* *

Datei anzeigen

@ -75,4 +75,11 @@ public class GeyserBungeeLogger implements GeyserLogger {
info(message); info(message);
} }
} }
@Override
public void debug(String message, Object... arguments) {
if (debug) {
info(String.format(message, arguments));
}
}
} }

Datei anzeigen

@ -84,6 +84,13 @@ public class GeyserModLogger implements GeyserLogger {
} }
} }
@Override
public void debug(String message, Object... arguments) {
if (debug) {
logger.info(message, arguments);
}
}
@Override @Override
public void setDebug(boolean debug) { public void setDebug(boolean debug) {
this.debug = debug; this.debug = debug;

Datei anzeigen

@ -75,4 +75,11 @@ public class GeyserSpigotLogger implements GeyserLogger {
info(message); info(message);
} }
} }
@Override
public void debug(String message, Object... arguments) {
if (debug) {
info(String.format(message, arguments));
}
}
} }

Datei anzeigen

@ -89,6 +89,11 @@ public class GeyserStandaloneLogger extends SimpleTerminalConsole implements Gey
log.debug(ChatColor.GRAY + message); log.debug(ChatColor.GRAY + message);
} }
@Override
public void debug(String message, Object... arguments) {
log.debug(ChatColor.GRAY + message, arguments);
}
@Override @Override
public void setDebug(boolean debug) { public void setDebug(boolean debug) {
Configurator.setLevel(log.getName(), debug ? Level.DEBUG : Level.INFO); Configurator.setLevel(log.getName(), debug ? Level.DEBUG : Level.INFO);

Datei anzeigen

@ -73,4 +73,11 @@ public class GeyserVelocityLogger implements GeyserLogger {
info(message); info(message);
} }
} }
}
@Override
public void debug(String message, Object... arguments) {
if (debug) {
logger.info(message, arguments);
}
}
}

Datei anzeigen

@ -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 @Override
public void setDebug(boolean debug) { public void setDebug(boolean debug) {
this.debug = debug; this.debug = debug;

Datei anzeigen

@ -103,6 +103,15 @@ public interface GeyserLogger extends GeyserCommandSource {
debug(String.valueOf(object)); 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 * Sets if the logger should print debug messages
* *

Datei anzeigen

@ -94,6 +94,19 @@ public class GeyserDefineResourcePacksEventImpl extends GeyserDefineResourcePack
return packHolder.optionHolder().immutableValues(); 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 @Override
public boolean unregister(@NonNull UUID uuid) { public boolean unregister(@NonNull UUID uuid) {
return packs.remove(uuid) != null; return packs.remove(uuid) != null;

Datei anzeigen

@ -97,7 +97,7 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
Objects.requireNonNull(options); Objects.requireNonNull(options);
ResourcePackHolder holder = packs.get(uuid); ResourcePackHolder holder = packs.get(uuid);
if (holder == null) { 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); registerOption(holder.pack(), options);
@ -115,6 +115,23 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
return optionHolder.immutableValues(packHolder.optionHolder()); 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 @Override
public boolean unregister(@NonNull UUID uuid) { public boolean unregister(@NonNull UUID uuid) {
options.remove(uuid); options.remove(uuid);

Datei anzeigen

@ -302,8 +302,8 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
ResourcePackHolder holder = this.resourcePackLoadEvent.getPacks().get(packet.getPackId()); ResourcePackHolder holder = this.resourcePackLoadEvent.getPacks().get(packet.getPackId());
if (holder == null) { if (holder == null) {
GeyserImpl.getInstance().getLogger().debug("Client %s tried to request pack id (%s) not sent to it!" GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request pack id {1} not sent to it!",
.formatted(session.bedrockUsername(), packet.getPackId())); session.bedrockUsername(), packet.getPackId());
session.disconnect("disconnectionScreen.resourcePack"); session.disconnect("disconnectionScreen.resourcePack");
return PacketSignal.HANDLED; return PacketSignal.HANDLED;
} }
@ -356,8 +356,8 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
String[] packID = id.split("_"); String[] packID = id.split("_");
if (packID.length < 2) { if (packID.length < 2) {
GeyserImpl.getInstance().getLogger().debug("Client %s tried to request invalid pack id %s!" GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request invalid pack id {1}!",
.formatted(session.bedrockUsername(), packID)); session.bedrockUsername(), packID);
session.disconnect("disconnectionScreen.resourcePack"); session.disconnect("disconnectionScreen.resourcePack");
return; return;
} }
@ -366,16 +366,16 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
try { try {
packId = UUID.fromString(packID[0]); packId = UUID.fromString(packID[0]);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
GeyserImpl.getInstance().getLogger().debug("Client %s tried to request pack with an invalid id (%s)" GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request pack with an invalid id {1})",
.formatted(session.bedrockUsername(), id)); session.bedrockUsername(), id);
session.disconnect("disconnectionScreen.resourcePack"); session.disconnect("disconnectionScreen.resourcePack");
return; return;
} }
ResourcePackHolder holder = this.resourcePackLoadEvent.getPacks().get(packId); ResourcePackHolder holder = this.resourcePackLoadEvent.getPacks().get(packId);
if (holder == null) { if (holder == null) {
GeyserImpl.getInstance().getLogger().debug("Client %s tried to request pack id (%s) not sent to it!" GeyserImpl.getInstance().getLogger().debug("Client {0} tried to request pack id {1} not sent to it!",
.formatted(session.bedrockUsername(), id)); session.bedrockUsername(), id);
session.disconnect("disconnectionScreen.resourcePack"); session.disconnect("disconnectionScreen.resourcePack");
return; return;
} }

Datei anzeigen

@ -78,6 +78,25 @@ public class OptionHolder extends HashMap<ResourcePackOption.Type, ResourcePackO
return defaultValue; return defaultValue;
} }
public static ResourcePackOption<?> 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) { public void remove(ResourcePackOption<?> option) {
super.remove(option.type()); super.remove(option.type());
} }