Skip to content

Commit

Permalink
feat(velocity): support velocity 3.3.0-SNAPSHOT
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed Dec 17, 2023
1 parent 03d9de1 commit 43dba5c
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 39 deletions.
4 changes: 2 additions & 2 deletions triton-velocity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ dependencies {
compileOnly project(path: ":api")
compileOnly project(path: ":core")

annotationProcessor 'com.velocitypowered:velocity-api:3.1.0'
annotationProcessor 'com.velocitypowered:velocity-api:3.3.0-SNAPSHOT'

compileOnly 'com.velocitypowered:velocity-proxy:3.2.0-SNAPSHOT'
compileOnly 'com.velocitypowered:velocity-proxy:3.3.0-SNAPSHOT'

compileOnly 'io.netty:netty-codec:4.1.80.Final'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.rexcantor64.triton.Triton;
import com.rexcantor64.triton.api.config.FeatureSyntax;
import com.rexcantor64.triton.api.language.MessageParser;
import com.rexcantor64.triton.velocity.utils.ComponentUtils;
import com.rexcantor64.triton.velocity.player.VelocityLanguagePlayer;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.BossBar;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import lombok.val;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -44,16 +44,16 @@ private FeatureSyntax getBossBarSyntax() {

val text = bossBarPacket.getName();
if (text != null && (action == BossBar.ADD || action == BossBar.UPDATE_NAME)) {
player.setBossbar(uuid, bossBarPacket.getName());
player.setBossbar(uuid, bossBarPacket.getName().getComponent());

parser()
.translateComponent(
ComponentUtils.deserializeFromJson(bossBarPacket.getName(), player.getProtocolVersion()),
bossBarPacket.getName().getComponent(),
player,
getBossBarSyntax()
)
.getResultOrToRemove(Component::empty)
.map(result -> ComponentUtils.serializeToJson(result, player.getProtocolVersion()))
.map(result -> new ComponentHolder(player.getProtocolVersion(), result))
.ifPresent(bossBarPacket::setName);
}
return Optional.of(bossBarPacket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.rexcantor64.triton.velocity.utils.ComponentUtils;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.chat.ChatType;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import com.velocitypowered.proxy.protocol.packet.chat.SystemChat;
import com.velocitypowered.proxy.protocol.packet.chat.legacy.LegacyChat;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -46,10 +47,11 @@ private FeatureSyntax getActionBarSyntax() {

return Objects.requireNonNull(
parser().translateComponent(
systemChatPacket.getComponent(),
systemChatPacket.getComponent().getComponent(),
player,
actionBar ? getActionBarSyntax() : getChatSyntax()
)
.map(result -> new ComponentHolder(player.getProtocolVersion(), result))
.mapToObj(
result -> Optional.of(cloneSystemChatWithComponent(systemChatPacket, result)),
() -> Optional.of(systemChatPacket),
Expand Down Expand Up @@ -80,7 +82,7 @@ private FeatureSyntax getActionBarSyntax() {
);
}

private @NotNull SystemChat cloneSystemChatWithComponent(@NotNull SystemChat systemChatPacket, Component newComponent) {
private @NotNull SystemChat cloneSystemChatWithComponent(@NotNull SystemChat systemChatPacket, ComponentHolder newComponent) {
return new SystemChat(newComponent, systemChatPacket.getType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.rexcantor64.triton.velocity.utils.ComponentUtils;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.Disconnect;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand All @@ -33,13 +34,16 @@ private FeatureSyntax getKickSyntax() {

return Objects.requireNonNull(
parser().translateComponent(
ComponentUtils.deserializeFromJson(disconnectPacket.getReason(), player.getProtocolVersion()),
disconnectPacket.getReason().getComponent(),
player,
getKickSyntax()
)
.map(result -> ComponentUtils.serializeToJson(result, player.getProtocolVersion()))
.map(result -> new ComponentHolder(player.getProtocolVersion(), result))
.mapToObj(
result -> Optional.of(new Disconnect(result)),
result -> {
disconnectPacket.setReason(result);
return Optional.of(disconnectPacket);
},
() -> Optional.of(disconnectPacket),
Optional::empty
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.rexcantor64.triton.velocity.player.VelocityLanguagePlayer;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.ResourcePackRequest;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -33,11 +34,12 @@ private FeatureSyntax getResourcePackSyntax() {
}

parser().translateComponent(
resourcePackRequest.getPrompt(),
resourcePackRequest.getPrompt().getComponent(),
player,
getResourcePackSyntax()
)
.getResultOrToRemove(Component::empty)
.map(result -> new ComponentHolder(player.getProtocolVersion(), result))
.ifPresent(resourcePackRequest::setPrompt);
return Optional.of(resourcePackRequest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
import com.rexcantor64.triton.api.config.FeatureSyntax;
import com.rexcantor64.triton.api.language.MessageParser;
import com.rexcantor64.triton.velocity.player.VelocityLanguagePlayer;
import com.rexcantor64.triton.velocity.utils.ComponentUtils;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.HeaderAndFooter;
import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItem;
import com.velocitypowered.proxy.protocol.packet.RemovePlayerInfo;
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfo;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import lombok.val;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

public class TabHandler {

private static final String EMPTY_COMPONENT = "{\"translate\":\"\"}";

private MessageParser parser() {
return Triton.get().getMessageParser();
}
Expand All @@ -37,23 +36,23 @@ private FeatureSyntax getTabSyntax() {
return Optional.of(headerFooterPacket);
}

player.setLastTabHeader(headerFooterPacket.getHeader());
player.setLastTabFooter(headerFooterPacket.getFooter());
player.setLastTabHeader(headerFooterPacket.getHeader().getComponent());
player.setLastTabFooter(headerFooterPacket.getFooter().getComponent());

Optional<String> newHeader = parser().translateComponent(
ComponentUtils.deserializeFromJson(headerFooterPacket.getHeader(), player.getProtocolVersion()),
Optional<ComponentHolder> newHeader = parser().translateComponent(
headerFooterPacket.getHeader().getComponent(),
player,
getTabSyntax()
)
.map(result -> ComponentUtils.serializeToJson(result, player.getProtocolVersion()))
.getResultOrToRemove(() -> EMPTY_COMPONENT);
Optional<String> newFooter = parser().translateComponent(
ComponentUtils.deserializeFromJson(headerFooterPacket.getFooter(), player.getProtocolVersion()),
.getResultOrToRemove(Component::empty)
.map(result -> new ComponentHolder(player.getProtocolVersion(), result));
Optional<ComponentHolder> newFooter = parser().translateComponent(
headerFooterPacket.getFooter().getComponent(),
player,
getTabSyntax()
)
.map(result -> ComponentUtils.serializeToJson(result, player.getProtocolVersion()))
.getResultOrToRemove(() -> EMPTY_COMPONENT);
.getResultOrToRemove(Component::empty)
.map(result -> new ComponentHolder(player.getProtocolVersion(), result));

if (newFooter.isPresent() || newHeader.isPresent()) {
return Optional.of(
Expand All @@ -67,6 +66,10 @@ private FeatureSyntax getTabSyntax() {
}

public @NotNull Optional<MinecraftPacket> handlePlayerListItem(@NotNull LegacyPlayerListItem playerListItemPacket, @NotNull VelocityLanguagePlayer player) {
if (shouldNotTranslateTab()) {
return Optional.of(playerListItemPacket);
}

val items = playerListItemPacket.getItems();
val action = playerListItemPacket.getAction();

Expand Down Expand Up @@ -102,6 +105,9 @@ private FeatureSyntax getTabSyntax() {
}

public @NotNull Optional<MinecraftPacket> handleUpsertPlayerInfo(@NotNull UpsertPlayerInfo upsertPlayerInfoPacket, @NotNull VelocityLanguagePlayer player) {
if (shouldNotTranslateTab()) {
return Optional.of(upsertPlayerInfoPacket);
}
if (!upsertPlayerInfoPacket.getActions().contains(UpsertPlayerInfo.Action.UPDATE_DISPLAY_NAME)) {
return Optional.of(upsertPlayerInfoPacket);
}
Expand All @@ -113,13 +119,13 @@ private FeatureSyntax getTabSyntax() {
}
parser()
.translateComponent(
item.getDisplayName(),
item.getDisplayName().getComponent(),
player,
getTabSyntax()
)
.ifChanged(result -> {
player.cachePlayerListItem(uuid, item.getDisplayName());
item.setDisplayName(result);
player.cachePlayerListItem(uuid, item.getDisplayName().getComponent());
item.setDisplayName(new ComponentHolder(player.getProtocolVersion(), result));
})
.ifUnchanged(() -> player.deleteCachedPlayerListItem(uuid))
.ifToRemove(() -> player.deleteCachedPlayerListItem(uuid));
Expand All @@ -130,6 +136,9 @@ private FeatureSyntax getTabSyntax() {
}

public @NotNull Optional<MinecraftPacket> handleRemovePlayerInfo(@NotNull RemovePlayerInfo removePlayerInfoPacket, @NotNull VelocityLanguagePlayer player) {
if (shouldNotTranslateTab()) {
return Optional.of(removePlayerInfoPacket);
}
for (UUID uuid : removePlayerInfoPacket.getProfilesToRemove()) {
player.deleteCachedPlayerListItem(uuid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.rexcantor64.triton.velocity.player.VelocityLanguagePlayer;
import com.rexcantor64.triton.velocity.utils.ComponentUtils;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import com.velocitypowered.proxy.protocol.packet.title.GenericTitlePacket;
import com.velocitypowered.proxy.protocol.packet.title.LegacyTitlePacket;
import com.velocitypowered.proxy.protocol.packet.title.TitleActionbarPacket;
Expand Down Expand Up @@ -52,11 +53,11 @@ private boolean isActionBarPacket(GenericTitlePacket titlePacket) {

return Objects.requireNonNull(
parser().translateComponent(
ComponentUtils.deserializeFromJson(titlePacket.getComponent(), player.getProtocolVersion()),
titlePacket.getComponent().getComponent(),
player,
isActionBarPacket ? getActionBarSyntax() : getTitleSyntax()
)
.map(result -> ComponentUtils.serializeToJson(result, player.getProtocolVersion()))
.map(result -> new ComponentHolder(player.getProtocolVersion(), result))
.mapToObj(
result -> {
titlePacket.setComponent(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.velocitypowered.proxy.protocol.packet.HeaderAndFooter;
import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItem;
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfo;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import lombok.RequiredArgsConstructor;
import lombok.val;
import net.kyori.adventure.text.Component;

import java.util.EnumSet;
import java.util.UUID;
Expand All @@ -29,11 +31,11 @@ private void refreshBossBars() {
player.getCachedBossBars().forEach(this::refreshBossBar);
}

private void refreshBossBar(UUID uuid, String json) {
private void refreshBossBar(UUID uuid, Component component) {
val bossBarPacket = new BossBar();
bossBarPacket.setUuid(uuid);
bossBarPacket.setAction(BossBar.UPDATE_NAME);
bossBarPacket.setName(json);
bossBarPacket.setName(new ComponentHolder(player.getProtocolVersion(), component));

sendPacket(bossBarPacket);
}
Expand All @@ -44,7 +46,10 @@ private void refreshPlayerListHeaderFooter() {
if (header == null || footer == null) {
return;
}
val headerFooterPacket = new HeaderAndFooter(header, footer);
val headerFooterPacket = new HeaderAndFooter(
new ComponentHolder(player.getProtocolVersion(), header),
new ComponentHolder(player.getProtocolVersion(), footer)
);

sendPacket(headerFooterPacket);
}
Expand All @@ -56,7 +61,7 @@ private void refreshPlayerListItems() {
.stream()
.map(entry -> {
val playerEntry = new UpsertPlayerInfo.Entry(entry.getKey());
playerEntry.setDisplayName(entry.getValue());
playerEntry.setDisplayName(new ComponentHolder(player.getProtocolVersion(), entry.getValue()));
return playerEntry;
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class VelocityLanguagePlayer implements LanguagePlayer {

@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.PUBLIC)
private String lastTabHeader;
private Component lastTabHeader;
@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.PUBLIC)
private String lastTabFooter;
private final Map<UUID, String> bossBars = new HashMap<>();
private Component lastTabFooter;
private final Map<UUID, Component> bossBars = new HashMap<>();
private final Map<UUID, Component> playerListItemCache = new ConcurrentHashMap<>();
private boolean waitingForClientLocale = false;
private String clientLocale;
Expand All @@ -56,15 +56,15 @@ public static VelocityLanguagePlayer fromUUID(UUID uuid) {
return player.map(VelocityLanguagePlayer::new).orElse(null);
}

public void setBossbar(UUID uuid, String lastBossBar) {
public void setBossbar(UUID uuid, Component lastBossBar) {
bossBars.put(uuid, lastBossBar);
}

public void removeBossbar(UUID uuid) {
bossBars.remove(uuid);
}

Map<UUID, String> getCachedBossBars() {
Map<UUID, Component> getCachedBossBars() {
return Collections.unmodifiableMap(bossBars);
}

Expand Down

0 comments on commit 43dba5c

Please sign in to comment.