diff --git a/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudience.java b/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudience.java index fad9d6d7..60167578 100644 --- a/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudience.java +++ b/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudience.java @@ -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 { @@ -95,30 +91,12 @@ final class BukkitAudience extends FacetAudience { ); 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 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. diff --git a/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudiencesImpl.java b/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudiencesImpl.java index eb11efd7..bc66e36c 100644 --- a/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudiencesImpl.java +++ b/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitAudiencesImpl.java @@ -33,7 +33,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.UUID; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -107,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 @@ -127,46 +122,9 @@ static BukkitAudiences instanceFor(final @NotNull Plugin plugin) { return this.createAudience(Collections.singletonList(sender)); } - @Override - public @NotNull Audience player(final @NotNull UUID playerId) { - final Player player = Bukkit.getPlayer(playerId); - if (player != null) { - return this.player(playerId, this.playerLocale(player)); - } - return this.player(playerId, null); - } - @Override public @NotNull Audience player(final @NotNull Player player) { - return this.player(player.getUniqueId(), this.playerLocale(player)); - } - - private @NotNull Audience player(final @NotNull UUID playerId, final @Nullable String locale) { - final Audience audience = super.player(playerId); - if (locale != null) { - if (audience instanceof BukkitAudience) { - ((BukkitAudience) audience).locale(Translator.parseLocale(locale)); - } - } - return audience; - } - - private @Nullable String playerLocale(final Player player) { - final Class playerClass = Player.class; - - MethodHandle getMethod = findMethod(playerClass, "locale", String.class); - if (getMethod == null) { - getMethod = findMethod(playerClass, "getLocale", String.class); - } - if (getMethod == null) { - return null; - } - try { - return (String) getMethod.invoke(player); - } catch (final Throwable error) { - logError(error, "Failed to call %s for %s", getMethod, player); - } - return null; + return super.player(player.getUniqueId()); } @Override diff --git a/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitFacet.java b/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitFacet.java index 94fd181a..55a94a37 100644 --- a/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitFacet.java +++ b/platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/BukkitFacet.java @@ -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; @@ -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; @@ -418,6 +420,8 @@ public void contributePointers(final ConsoleCommandSender viewer, final net.kyor static final class PlayerPointers extends BukkitFacet implements Facet.Pointers { + private static final MethodHandle LOCALE_SUPPORTED = findMethod(Player.class, "getLocale", String.class); + PlayerPointers() { super(Player.class); } @@ -426,6 +430,16 @@ static final class PlayerPointers extends BukkitFacet 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())); // :( }