Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

/**
* Applies Survival Discord gate via RPCharacters (optional, reflection).
* Players with {@link #BYPASS_PERMISSION} are not gated.
*/
public final class DiscordGateService {

/** Survival play without a Discord link. Registered in plugin.yml. */
public static final String BYPASS_PERMISSION = "tfmcweb.discord.bypass";

private final JavaPlugin plugin;
private final LinkCache linkCache;
private Plugin resolvedRpc;
Expand Down Expand Up @@ -90,10 +94,18 @@ public void applyGate(Player player, boolean eligible) {
if (player == null || !isRpcAvailable()) {
return;
}
boolean required = player.getGameMode() == GameMode.SURVIVAL && !eligible;
boolean required = requiresGate(
player.getGameMode(),
eligible,
player.hasPermission(BYPASS_PERMISSION)
);
invokeGate(player.getUniqueId(), player, required);
}

static boolean requiresGate(GameMode mode, boolean eligible, boolean bypass) {
return mode == GameMode.SURVIVAL && !eligible && !bypass;
}

/** Offline-safe: set UUID gate flag; RPC reevaluates if online. */
public void applyGate(UUID uuid, boolean eligible) {
if (uuid == null || !isRpcAvailable()) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ permissions:
tfmcweb.linkdiscord:
description: Use /linkdiscord and /unlinkdiscord
default: true
tfmcweb.discord.bypass:
description: Play Survival without linking Discord
default: false
tfmcweb.admin:
description: Admin /web commands
default: op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ void freezesUnlinkedSurvivalPlayerAfterRpcEnablesLater() {
assertTrue(gate.isRpcAvailable());
}

@Test
void staffBypassSkipsSurvivalGateWhenUnlinked() {
when(rpc.isEnabled()).thenReturn(true);
when(player.hasPermission(DiscordGateService.BYPASS_PERMISSION)).thenReturn(true);
cache.putUnlinked(uuid);

gate.applyGate(player);

assertEquals(Boolean.FALSE, RpcPlugin.lastRequired);
assertFalse(DiscordGateService.requiresGate(GameMode.SURVIVAL, false, true));
assertTrue(DiscordGateService.requiresGate(GameMode.SURVIVAL, false, false));
}

@Test
void releasesEligiblePlayersAndDoesNotFreezeOtherGameModes() {
when(rpc.isEnabled()).thenReturn(true);
Expand Down