Skip to content

Commit

Permalink
Add (dynamic) afk-prefix placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
anhcraft committed Feb 26, 2023
1 parent 762f4f7 commit f52bb5b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fibermc.essentialcommands;

import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess;
import eu.pb4.placeholders.api.PlaceholderResult;
import eu.pb4.placeholders.api.Placeholders;

import net.minecraft.util.Identifier;

import static com.fibermc.essentialcommands.EssentialCommands.CONFIG;

final class ECPlaceholderRegistry {
private ECPlaceholderRegistry() {}

public static void register() {
var namespace = EssentialCommands.MOD_ID;
Placeholders.register(
new Identifier(namespace, "afk-prefix"),
(ctx, arg) -> {
if (ctx.hasPlayer()) {
var playerData = ((ServerPlayerEntityAccess) ctx.player()).ec$getPlayerData();
if (playerData != null && playerData.isAfk()) {
return PlaceholderResult.value(CONFIG.AFK_PREFIX);
}
}
return PlaceholderResult.value("");
}
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void onInitialize() {
BACKING_CONFIG.registerLoadHandler((backingConfig) -> CONFIG = EssentialCommandsConfigSnapshot.create(backingConfig));
BACKING_CONFIG.loadOrCreateProperties();

ECPlaceholderRegistry.register();
ECAbilitySources.init();

ManagerLocator managers = ManagerLocator.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public final class EssentialCommandsConfig extends Config<EssentialCommandsConfi
@ConfigOption public final Option<Boolean> GRANT_LOWEST_NUMERIC_BY_DEFAULT = new Option<>("grant_lowest_numeric_by_default", true, Boolean::parseBoolean);
@ConfigOption public final Option<String> LANGUAGE = new Option<>("language", "en_us", String::toString);
@ConfigOption public final Option<String> MOTD = new Option<>("motd", "<yellow>Welcome to our server <blue>%player:displayname%</blue>!\nPlease read the rules.</yellow>", String::toString);
@ConfigOption public final Option<Text> AFK_PREFIX = new Option<>("afk_prefix", Text.literal("[AFK] ").formatted(Formatting.GRAY), TextUtil::parseText, Text.Serializer::toJson);
@ConfigOption public final Option<Boolean> INVULN_WHILE_AFK = new Option<>("invuln_while_afk", false, Boolean::parseBoolean);
@ConfigOption public final Option<Boolean> AUTO_AFK_ENABLED = new Option<>("auto_afk_enabled", true, Boolean::parseBoolean);
@ConfigOption public final Option<Integer> AUTO_AFK_TICKS = new Option<>("auto_afk_time", durationToTicks(Duration.ofMinutes(15)), ConfigUtil::parseDurationToTicks, ConfigUtil::serializeTicksAsDuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public final class EssentialCommandsConfigSnapshot {
public final boolean GRANT_LOWEST_NUMERIC_BY_DEFAULT;
public final String LANGUAGE;
public final String MOTD;
public final Text AFK_PREFIX;
public final boolean INVULN_WHILE_AFK;
public final boolean AUTO_AFK_ENABLED;
public final int AUTO_AFK_TICKS;
Expand Down Expand Up @@ -111,6 +112,7 @@ private EssentialCommandsConfigSnapshot(EssentialCommandsConfig config) {
this.GRANT_LOWEST_NUMERIC_BY_DEFAULT = config.GRANT_LOWEST_NUMERIC_BY_DEFAULT.getValue();
this.LANGUAGE = config.LANGUAGE.getValue();
this.MOTD = config.MOTD.getValue();
this.AFK_PREFIX = config.AFK_PREFIX.getValue();
this.INVULN_WHILE_AFK = config.INVULN_WHILE_AFK.getValue();
this.AUTO_AFK_ENABLED = config.AUTO_AFK_ENABLED.getValue();
this.AUTO_AFK_TICKS = config.AUTO_AFK_TICKS.getValue();
Expand Down

0 comments on commit f52bb5b

Please sign in to comment.