Skip to content

Commit

Permalink
feat: add config option PRINT_TELEPORT_COORDINATES (default value f…
Browse files Browse the repository at this point in the history
…or the profile setting) ...

also, make all profile options explicitly Optional<T> (behavioral change for `print_teleport_coordinates`, which could not previously be null
  • Loading branch information
John-Paul-R committed Jun 21, 2024
1 parent 98cbdda commit 7c5ed3f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static LiteralCommandNode<ServerCommandSource> buildNode() {
var player = context.getSource().getPlayerOrThrow();
var profile = ((ServerPlayerEntityAccess) player).ec$getProfile();
context.getSource().sendFeedback(() ->
Text.literal(option.profileGetter().getValue(profile).toString()),
Text.literal(option.profileGetter().getValue(profile).map(Object::toString).orElse("<not set>")),
CONFIG.BROADCAST_TO_OPS);
return 0;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import com.fibermc.essentialcommands.ManagerLocator;
import com.fibermc.essentialcommands.playerdata.PlayerDataManager;
import com.fibermc.essentialcommands.types.RespawnCondition;

import net.minecraft.registry.DynamicRegistryManager;

import org.jetbrains.annotations.NotNull;

import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.RegistryKey;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -111,6 +109,7 @@ public final class EssentialCommandsConfig extends Config<EssentialCommandsConfi
@ConfigOption public final Option<Integer> FLY_MAX_SPEED = new Option<>("fly_max_speed", 5, ConfigUtil::parseInt);
@ConfigOption public final Option<Integer> NEAR_COMMAND_DEFAULT_RADIUS = new Option<>("near_command_default_radius", 200, ConfigUtil::parseInt);
@ConfigOption public final Option<Integer> NEAR_COMMAND_MAX_RADIUS = new Option<>("near_command_max_radius", 200, ConfigUtil::parseInt);
@ConfigOption public final Option<Boolean> PRINT_TELEPORT_COORDINATES = new Option<>("print_teleport_coordinates", true, Boolean::parseBoolean);

public EssentialCommandsConfig(Path savePath, String displayName, String documentationLink) {
super(savePath, displayName, documentationLink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public final class EssentialCommandsConfigSnapshot {
public final int FLY_MAX_SPEED;
public final int NEAR_COMMAND_DEFAULT_RADIUS;
public final int NEAR_COMMAND_MAX_RADIUS;
public final boolean PRINT_TELEPORT_COORDINATES;

private EssentialCommandsConfigSnapshot(EssentialCommandsConfig config) {
this.FORMATTING_DEFAULT = config.FORMATTING_DEFAULT.getValue();
Expand Down Expand Up @@ -158,6 +159,7 @@ private EssentialCommandsConfigSnapshot(EssentialCommandsConfig config) {
this.FLY_MAX_SPEED = config.FLY_MAX_SPEED.getValue();
this.NEAR_COMMAND_DEFAULT_RADIUS = config.NEAR_COMMAND_DEFAULT_RADIUS.getValue();
this.NEAR_COMMAND_MAX_RADIUS = config.NEAR_COMMAND_MAX_RADIUS.getValue();
this.PRINT_TELEPORT_COORDINATES = config.PRINT_TELEPORT_COORDINATES.getValue();
}

public static EssentialCommandsConfigSnapshot create(EssentialCommandsConfig config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public PlayerProfile(@NotNull ServerPlayerEntity player, File saveFile) {
this.profileOptions = new ProfileOptions();
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private static final class ProfileOptions {
private Style formattingDefault;
private Style formattingAccent;
private Style formattingError;
private boolean printTeleportCoordinates = true;
private Optional<Style> formattingDefault = Optional.empty();
private Optional<Style> formattingAccent = Optional.empty();
private Optional<Style> formattingError = Optional.empty();
private Optional<Boolean> printTeleportCoordinates = Optional.empty();
}

public static final Map<String, ProfileOption<?>> OPTIONS = Map.ofEntries(
Expand All @@ -51,22 +52,22 @@ private static final class ProfileOptions {
new ProfileOption<>(
BoolArgumentType.bool(),
false,
(context, name, profile) -> profile.profileOptions.printTeleportCoordinates = BoolArgumentType.getBool(context, name),
(context, name, profile) -> profile.profileOptions.printTeleportCoordinates = Optional.of(BoolArgumentType.getBool(context, name)),
(profile) -> profile.profileOptions.printTeleportCoordinates)),
new SimpleEntry<>(
StorageKey.FORMATTING_DEAULT,
new ProfileOption<>(
StringArgumentType.greedyString(),
null,
(context, name, profile) -> profile.profileOptions.formattingDefault = ConfigUtil.parseStyle(StringArgumentType.getString(context, name)),
(profile) -> ConfigUtil.serializeStyle(profile.profileOptions.formattingDefault))),
(context, name, profile) -> profile.profileOptions.formattingDefault = Optional.ofNullable(ConfigUtil.parseStyle(StringArgumentType.getString(context, name))),
(profile) -> profile.profileOptions.formattingDefault.map(ConfigUtil::serializeStyle))),
new SimpleEntry<>(
StorageKey.FORMATTING_ACCENT,
new ProfileOption<>(
StringArgumentType.greedyString(),
null,
(context, name, profile) -> profile.profileOptions.formattingAccent = ConfigUtil.parseStyle(StringArgumentType.getString(context, name)),
(profile) -> ConfigUtil.serializeStyle(profile.profileOptions.formattingAccent)))
(context, name, profile) -> profile.profileOptions.formattingAccent = Optional.ofNullable(ConfigUtil.parseStyle(StringArgumentType.getString(context, name))),
(profile) -> profile.profileOptions.formattingAccent.map(ConfigUtil::serializeStyle)))
// new SimpleEntry<>(
// StorageKey.FORMATTING_ERROR,
// new ProfileOption<>(
Expand All @@ -76,20 +77,20 @@ private static final class ProfileOptions {
// (profile) -> ConfigUtil.serializeStyle(profile.profileOptions.formattingError)))
);

public boolean shouldPrintTeleportCoordinates() {
public Optional<Boolean> shouldPrintTeleportCoordinates() {
return profileOptions.printTeleportCoordinates;
}

public @Nullable Style getFormattingDefault() {
return profileOptions.formattingDefault;
return profileOptions.formattingDefault.orElse(null);
}

public @Nullable Style getFormattingAccent() {
return profileOptions.formattingAccent;
return profileOptions.formattingAccent.orElse(null);
}

public @Nullable Style getFormattingError() {
return profileOptions.formattingError;
return profileOptions.formattingError.orElse(null);
}

private static final class StorageKey {
Expand All @@ -102,33 +103,38 @@ private static final class StorageKey {
public void fromNbt(NbtCompound tag) {
NbtCompound dataTag = tag.getCompound("data");
this.profileOptions = new ProfileOptions();

this.profileOptions.formattingDefault = Optional.ofNullable(dataTag.get(StorageKey.FORMATTING_DEAULT))
.map(NbtElement::asString)
.map(ConfigUtil::parseStyle)
.orElse(null);
.map(ConfigUtil::parseStyle);

this.profileOptions.formattingAccent = Optional.ofNullable(dataTag.get(StorageKey.FORMATTING_ACCENT))
.map(NbtElement::asString)
.map(ConfigUtil::parseStyle)
.orElse(null);
.map(ConfigUtil::parseStyle);

this.profileOptions.formattingError = Optional.ofNullable(dataTag.get(StorageKey.FORMATTING_ERROR))
.map(NbtElement::asString)
.map(ConfigUtil::parseStyle)
.orElse(null);
this.profileOptions.printTeleportCoordinates = dataTag.getBoolean(StorageKey.PRINT_TELEPORT_COORDINATES);
.map(ConfigUtil::parseStyle);

this.profileOptions.printTeleportCoordinates = dataTag.contains(StorageKey.PRINT_TELEPORT_COORDINATES)
? Optional.of(dataTag.getBoolean(StorageKey.PRINT_TELEPORT_COORDINATES))
: Optional.empty();
}

@Override
public NbtCompound writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup wrapperLookup) {
if (this.profileOptions.formattingDefault != null) {
tag.putString(StorageKey.FORMATTING_DEAULT, ConfigUtil.serializeStyle(this.profileOptions.formattingDefault));
}
if (this.profileOptions.formattingAccent != null) {
tag.putString(StorageKey.FORMATTING_ACCENT, ConfigUtil.serializeStyle(this.profileOptions.formattingAccent));
}
if (this.profileOptions.formattingError != null) {
tag.putString(StorageKey.FORMATTING_ERROR, ConfigUtil.serializeStyle(this.profileOptions.formattingError));
}
tag.putBoolean(StorageKey.PRINT_TELEPORT_COORDINATES, this.profileOptions.printTeleportCoordinates);
this.profileOptions.formattingDefault
.ifPresent(style -> tag.putString(StorageKey.FORMATTING_DEAULT, ConfigUtil.serializeStyle(style)));

this.profileOptions.formattingAccent
.ifPresent(style -> tag.putString(StorageKey.FORMATTING_ACCENT, ConfigUtil.serializeStyle(style)));

this.profileOptions.formattingError
.ifPresent(style -> tag.putString(StorageKey.FORMATTING_ERROR, ConfigUtil.serializeStyle(style)));

this.profileOptions.printTeleportCoordinates.ifPresent(
printTeleportCoordinates -> tag.putBoolean(StorageKey.PRINT_TELEPORT_COORDINATES, printTeleportCoordinates));

return tag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void execTeleport(ServerPlayerEntity playerEntity, MinecraftLocat
var playerProfile = playerAccess.ec$getProfile();
playerAccess.ec$getPlayerData().sendMessage(
"teleport.done",
playerProfile.shouldPrintTeleportCoordinates()
playerProfile.shouldPrintTeleportCoordinates().orElse(CONFIG.PRINT_TELEPORT_COORDINATES)
? TextUtil.join(
new Text[]{
destName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.fibermc.essentialcommands.types;

import java.util.Optional;

import com.mojang.brigadier.arguments.ArgumentType;

public record ProfileOption<T>(
ArgumentType<T> argumentType,
T defaultValue,
ProfileOptionFromContextSetter<T> profileSetter,
ProfileOptionGetter<T> profileGetter) {}
ProfileOptionGetter<Optional<T>> profileGetter) {}

0 comments on commit 7c5ed3f

Please sign in to comment.