Skip to content

Commit 773ecbd

Browse files
authored
Merge pull request #33 from PaperMC/dev/3.0.0
[pull] main from PaperMC:dev/3.0.0
2 parents bde0c16 + 678c7aa commit 773ecbd

File tree

3 files changed

+29
-47
lines changed

3 files changed

+29
-47
lines changed

proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import java.nio.file.Files;
8282
import java.nio.file.Path;
8383
import java.security.KeyPair;
84+
import java.text.MessageFormat;
8485
import java.util.ArrayList;
8586
import java.util.Collection;
8687
import java.util.Collections;
@@ -104,7 +105,7 @@
104105
import net.kyori.adventure.key.Key;
105106
import net.kyori.adventure.text.Component;
106107
import net.kyori.adventure.translation.GlobalTranslator;
107-
import net.kyori.adventure.translation.TranslationRegistry;
108+
import net.kyori.adventure.translation.TranslationStore;
108109
import org.apache.logging.log4j.LogManager;
109110
import org.apache.logging.log4j.Logger;
110111
import org.bstats.MetricsBase;
@@ -337,8 +338,8 @@ void start() {
337338
}
338339

339340
private void registerTranslations() {
340-
final TranslationRegistry translationRegistry = TranslationRegistry
341-
.create(Key.key("velocity", "translations"));
341+
final TranslationStore.StringBased<MessageFormat> translationRegistry =
342+
TranslationStore.messageFormat(Key.key("velocity", "translations"));
342343
translationRegistry.defaultLocale(Locale.US);
343344
try {
344345
ResourceUtils.visitResources(VelocityServer.class, path -> {

proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
import net.kyori.adventure.platform.facet.FacetPointers;
124124
import net.kyori.adventure.platform.facet.FacetPointers.Type;
125125
import net.kyori.adventure.pointer.Pointers;
126+
import net.kyori.adventure.pointer.PointersSupplier;
126127
import net.kyori.adventure.resource.ResourcePackInfoLike;
127128
import net.kyori.adventure.resource.ResourcePackRequest;
128129
import net.kyori.adventure.resource.ResourcePackRequestLike;
@@ -152,7 +153,16 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
152153

153154
private static final ComponentLogger logger = ComponentLogger.logger(ConnectedPlayer.class);
154155

155-
private final Identity identity = new IdentityImpl();
156+
private static final @NotNull PointersSupplier<ConnectedPlayer> POINTERS_SUPPLIER =
157+
PointersSupplier.<ConnectedPlayer>builder()
158+
.resolving(Identity.UUID, Player::getUniqueId)
159+
.resolving(Identity.NAME, Player::getUsername)
160+
.resolving(Identity.DISPLAY_NAME, player -> Component.text(player.getUsername()))
161+
.resolving(Identity.LOCALE, Player::getEffectiveLocale)
162+
.resolving(PermissionChecker.POINTER, Player::getPermissionChecker)
163+
.resolving(FacetPointers.TYPE, player -> Type.PLAYER)
164+
.build();
165+
156166
/**
157167
* The actual Minecraft connection. This is actually a wrapper object around the Netty channel.
158168
*/
@@ -181,14 +191,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
181191
private final ResourcePackHandler resourcePackHandler;
182192
private final BundleDelimiterHandler bundleHandler = new BundleDelimiterHandler(this);
183193

184-
private final @NotNull Pointers pointers =
185-
Player.super.pointers().toBuilder()
186-
.withDynamic(Identity.UUID, this::getUniqueId)
187-
.withDynamic(Identity.NAME, this::getUsername)
188-
.withDynamic(Identity.DISPLAY_NAME, () -> Component.text(this.getUsername()))
189-
.withDynamic(Identity.LOCALE, this::getEffectiveLocale)
190-
.withStatic(PermissionChecker.POINTER, getPermissionChecker())
191-
.withStatic(FacetPointers.TYPE, Type.PLAYER).build();
192194
private @Nullable String clientBrand;
193195
private @Nullable Locale effectiveLocale;
194196
private final @Nullable IdentifiedKey playerKey;
@@ -257,7 +259,7 @@ public BundleDelimiterHandler getBundleHandler() {
257259

258260
@Override
259261
public @NonNull Identity identity() {
260-
return this.identity;
262+
return Identity.identity(this.getUniqueId());
261263
}
262264

263265
@Override
@@ -363,7 +365,7 @@ public void setModInfo(ModInfo modInfo) {
363365

364366
@Override
365367
public @NotNull Pointers pointers() {
366-
return this.pointers;
368+
return POINTERS_SUPPLIER.view(this);
367369
}
368370

369371
@Override
@@ -396,14 +398,20 @@ public ProtocolVersion getProtocolVersion() {
396398
}
397399

398400
/**
399-
* Translates the message in the user's locale.
401+
* Translates the message in the user's locale, falling back to the default locale if not set.
400402
*
401403
* @param message the message to translate
402404
* @return the translated message
403405
*/
404406
public Component translateMessage(Component message) {
405-
Locale locale = ClosestLocaleMatcher.INSTANCE
406-
.lookupClosest(getEffectiveLocale() == null ? Locale.getDefault() : getEffectiveLocale());
407+
Locale locale = this.getEffectiveLocale();
408+
if (locale == null && settings != null) {
409+
locale = settings.getLocale();
410+
}
411+
if (locale == null) {
412+
locale = Locale.getDefault();
413+
}
414+
locale = ClosestLocaleMatcher.INSTANCE.lookupClosest(locale);
407415
return GlobalTranslator.render(message, locale);
408416
}
409417

@@ -1361,14 +1369,6 @@ public Collection<ChannelIdentifier> getClientsideChannels() {
13611369
return playerKey;
13621370
}
13631371
1364-
private class IdentityImpl implements Identity {
1365-
1366-
@Override
1367-
public @NonNull UUID uuid() {
1368-
return ConnectedPlayer.this.getUniqueId();
1369-
}
1370-
}
1371-
13721372
@Override
13731373
public ProtocolState getProtocolState() {
13741374
return connection.getState().toProtocolState();

proxy/src/main/java/com/velocitypowered/proxy/util/TranslatableMapper.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import net.kyori.adventure.text.TranslatableComponent;
2525
import net.kyori.adventure.text.flattener.ComponentFlattener;
2626
import net.kyori.adventure.translation.GlobalTranslator;
27-
import net.kyori.adventure.translation.TranslationRegistry;
28-
import net.kyori.adventure.translation.Translator;
29-
import org.jetbrains.annotations.Nullable;
3027

3128
/**
3229
* Velocity Translation Mapper.
@@ -43,25 +40,9 @@ public void accept(
4340
final TranslatableComponent translatableComponent,
4441
final Consumer<Component> componentConsumer
4542
) {
46-
for (final Translator source : GlobalTranslator.translator().sources()) {
47-
if (source instanceof TranslationRegistry registry
48-
&& registry.contains(translatableComponent.key())) {
49-
componentConsumer.accept(GlobalTranslator.render(translatableComponent,
50-
ClosestLocaleMatcher.INSTANCE.lookupClosest(Locale.getDefault())));
51-
return;
52-
}
53-
}
54-
final @Nullable String fallback = translatableComponent.fallback();
55-
if (fallback == null) {
56-
return;
57-
}
58-
for (final Translator source : GlobalTranslator.translator().sources()) {
59-
if (source instanceof TranslationRegistry registry && registry.contains(fallback)) {
60-
componentConsumer.accept(
61-
GlobalTranslator.render(Component.translatable(fallback),
62-
ClosestLocaleMatcher.INSTANCE.lookupClosest(Locale.getDefault())));
63-
return;
64-
}
43+
final Locale locale = ClosestLocaleMatcher.INSTANCE.lookupClosest(Locale.getDefault());
44+
if (GlobalTranslator.translator().canTranslate(translatableComponent.key(), locale)) {
45+
componentConsumer.accept(GlobalTranslator.render(translatableComponent, locale));
6546
}
6647
}
6748
}

0 commit comments

Comments
 (0)