Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set player locale when creating Audience #160

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,17 @@

import com.viaversion.viaversion.api.connection.UserConnection;
import java.util.Collection;
import java.util.Locale;
import java.util.function.Function;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.platform.facet.Facet;
import net.kyori.adventure.platform.facet.FacetAudience;
import net.kyori.adventure.platform.facet.FacetAudienceProvider;
import net.kyori.adventure.platform.viaversion.ViaFacet;
import net.kyori.adventure.pointer.Pointers;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings("Convert2MethodRef")
final class BukkitAudience extends FacetAudience<CommandSender> {
Expand Down Expand Up @@ -95,30 +91,12 @@ final class BukkitAudience extends FacetAudience<CommandSender> {
);

private final @NotNull Plugin plugin;
// Bukkit only provides this as a String
private @Nullable Locale locale;

BukkitAudience(final @NotNull Plugin plugin, final FacetAudienceProvider<?, ?> provider, final @NotNull Collection<CommandSender> viewers) {
super(provider, viewers, CHAT, ACTION_BAR, TITLE, SOUND, ENTITY_SOUND, BOOK, BOSS_BAR, TAB_LIST, POINTERS);
this.plugin = plugin;
}

void locale(final @Nullable Locale locale) {
final boolean changed = this.locale != (this.locale = locale);
if (changed) {
this.refresh();
}
}

@Nullable Locale locale() {
return this.locale;
}

@Override
protected void contributePointers(final Pointers.Builder builder) {
builder.withDynamic(Identity.LOCALE, BukkitAudience.this::locale);
}

@Override
public void showBossBar(final @NotNull BossBar bar) {
// Some boss bar listeners need access to a Plugin to register events.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ static BukkitAudiences instanceFor(final @NotNull Plugin plugin) {
this.addViewer(event.getPlayer()));
this.registerEvent(PlayerQuitEvent.class, EventPriority.MONITOR, event ->
this.removeViewer(event.getPlayer()));
this.registerLocaleEvent(EventPriority.MONITOR, (viewer, locale) -> {
final @Nullable BukkitAudience audience = this.viewers.get(viewer);
if (audience != null) audience.locale(locale);
});
}

@Override
Expand All @@ -128,7 +124,7 @@ static BukkitAudiences instanceFor(final @NotNull Plugin plugin) {

@Override
public @NotNull Audience player(final @NotNull Player player) {
return this.player(player.getUniqueId());
return super.player(player.getUniqueId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import java.lang.invoke.MethodHandle;
import java.util.Collection;
import java.util.Locale;
import java.util.Set;
import java.util.function.Function;
import net.kyori.adventure.identity.Identity;
Expand All @@ -37,6 +38,7 @@
import net.kyori.adventure.platform.facet.FacetPointers;
import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.Translator;
import net.kyori.adventure.util.TriState;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -418,6 +420,8 @@ public void contributePointers(final ConsoleCommandSender viewer, final net.kyor

static final class PlayerPointers extends BukkitFacet<Player> implements Facet.Pointers<Player> {

private static final MethodHandle LOCALE_SUPPORTED = findMethod(Player.class, "getLocale", String.class);

PlayerPointers() {
super(Player.class);
}
Expand All @@ -426,6 +430,16 @@ static final class PlayerPointers extends BukkitFacet<Player> implements Facet.P
public void contributePointers(final Player viewer, final net.kyori.adventure.pointer.Pointers.Builder builder) {
builder.withDynamic(Identity.UUID, viewer::getUniqueId);
builder.withDynamic(Identity.DISPLAY_NAME, () -> BukkitComponentSerializer.legacy().deserializeOrNull(viewer.getDisplayName()));
builder.withDynamic(Identity.LOCALE, () -> {
if (LOCALE_SUPPORTED != null) {
try {
return Translator.parseLocale((String) LOCALE_SUPPORTED.invoke(viewer));
} catch (final Throwable error) {
logError(error, "Failed to call getLocale() for %s", viewer);
}
}
return Locale.getDefault();
});
builder.withStatic(FacetPointers.TYPE, FacetPointers.Type.PLAYER);
builder.withDynamic(FacetPointers.WORLD, () -> Key.key(viewer.getWorld().getName())); // :(
}
Expand Down