Skip to content

Commit

Permalink
implement a way to get player's current server name
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmakila committed Nov 10, 2024
1 parent 2ad06be commit 57683f2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import fi.fabianadrian.proxychat.common.platform.PlatformPlayer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

import static net.kyori.adventure.text.Component.text;

public record BungeecordPlatformPlayer(ProxiedPlayer player, Audience audience) implements PlatformPlayer {
@Override
public UUID uuid() {
Expand All @@ -23,6 +26,11 @@ public boolean hasPermission(String permission) {
return this.player.hasPermission(permission);
}

@Override
public Component currentServerName() {
return text(this.player.getServer().getInfo().getName());
}

@Override
public @NotNull Audience audience() {
return this.audience;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fi.fabianadrian.proxychat.common.ProxyChat;
import fi.fabianadrian.proxychat.common.command.ProxyChatCommand;
import fi.fabianadrian.proxychat.common.locale.Messages;
import fi.fabianadrian.proxychat.common.service.NameService;
import fi.fabianadrian.proxychat.common.user.User;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -42,7 +43,7 @@ private void executeBlockList(CommandContext<User> ctx) {
if (nameOptional.isPresent()) {
resolvedNameComponents.add(Component.text(nameOptional.get()));
} else {
resolvedNameComponents.add(Component.translatable("proxychat.general.unknown").hoverEvent(Component.text(uuid.toString())));
resolvedNameComponents.add(Messages.GENERAL_UNKNOWN.hoverEvent(Component.text(uuid.toString())));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ public interface Messages {
);

Component GENERAL_ME = translatable("proxychat.general.me");

Component GENERAL_UNKNOWN = translatable("proxychat.general.unknown");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fi.fabianadrian.proxychat.common.platform;

import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;

import java.util.UUID;

Expand All @@ -10,4 +11,6 @@ public interface PlatformPlayer extends ForwardingAudience.Single {
String name();

boolean hasPermission(String permission);

Component currentServerName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fi.fabianadrian.proxychat.common.command.Commander;
import fi.fabianadrian.proxychat.common.platform.PlatformPlayer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
Expand Down Expand Up @@ -52,6 +53,10 @@ public boolean hasPermission(String permission) {
return this.player.hasPermission(permission);
}

public Component currentServerName() {
return this.player.currentServerName();
}

public Set<String> mutedChannels() {
return Collections.unmodifiableSet(this.mutedChannels);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package fi.fabianadrian.proxychat.velocity;

import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import fi.fabianadrian.proxychat.common.locale.Messages;
import fi.fabianadrian.proxychat.common.platform.PlatformPlayer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

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

import static net.kyori.adventure.text.Component.text;

public record VelocityPlatformPlayer(Player player) implements PlatformPlayer {
@Override
public UUID uuid() {
Expand All @@ -23,6 +29,15 @@ public boolean hasPermission(String permission) {
return this.player.hasPermission(permission);
}

@Override
public Component currentServerName() {
Optional<ServerConnection> serverOptional = this.player.getCurrentServer();
if (serverOptional.isEmpty()) {
return Messages.GENERAL_UNKNOWN;
}
return text(serverOptional.get().getServerInfo().getName());
}

@Override
public @NotNull Audience audience() {
return this.player;
Expand Down

0 comments on commit 57683f2

Please sign in to comment.