Skip to content

Commit

Permalink
Call getLocale() directly from pointer, don't cache it in Audience
Browse files Browse the repository at this point in the history
  • Loading branch information
CubBossa committed Mar 29, 2024
1 parent d3015c3 commit c58d830
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 65 deletions.
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 @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
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

0 comments on commit c58d830

Please sign in to comment.