Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly extract kick messages from downstream services #1399

Open
wants to merge 3 commits into
base: nightly
Choose a base branch
from
Open
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 @@ -47,6 +47,7 @@ public final class BridgeConfiguration {
.put("proxy-join-cancel-because-permission", "§7You do not have the required permissions to join this proxy.")
.put("proxy-join-cancel-because-maintenance", "§7This proxy is currently in maintenance mode.")
.put("proxy-join-disconnect-because-no-hub", "§cThere is currently no hub server you can connect to.")
.put("server-kick-no-other-hub", "§cThere is currently no hub server you can connect to.")
.put("command-cloud-sub-command-no-permission", "§7You are not allowed to use §b%command%.")
.put("already-connected", "§cYou are already connected to this network!")
.put("error-connecting-to-server", "§cUnable to connect to %server%: %reason%")
Expand All @@ -66,6 +67,7 @@ public BridgeConfiguration() {
this.hubCommandNames = Arrays.asList("hub", "lobby", "leave", "l");
this.fallbackConfigurations = new ArrayList<>(List.of(ProxyFallbackConfiguration.builder()
.targetGroup("Proxy")
.showDownstreamKickMessage(false)
.defaultFallbackTask("Lobby")
.build()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

public record ProxyFallbackConfiguration(
@NonNull String targetGroup,
boolean showDownstreamKickMessage,
@Nullable String defaultFallbackTask,
@NonNull List<ProxyFallback> fallbacks
) {
Expand All @@ -35,13 +36,15 @@ public record ProxyFallbackConfiguration(
public static @NonNull Builder builder(@NonNull ProxyFallbackConfiguration configuration) {
return builder()
.targetGroup(configuration.targetGroup())
.showDownstreamKickMessage(configuration.showDownstreamKickMessage())
.defaultFallbackTask(configuration.defaultFallbackTask())
.fallbacks(configuration.fallbacks());
}

public static class Builder {

private String targetGroup;
private boolean showDownstreamKickMessage;
private String defaultFallbackTask;
private List<ProxyFallback> fallbacks = new ArrayList<>();

Expand All @@ -50,6 +53,11 @@ public static class Builder {
return this;
}

public @NonNull Builder showDownstreamKickMessage(boolean showDownstreamKickMessage) {
this.showDownstreamKickMessage = showDownstreamKickMessage;
return this;
}

public @NonNull Builder defaultFallbackTask(@Nullable String defaultFallbackTask) {
this.defaultFallbackTask = defaultFallbackTask;
return this;
Expand All @@ -63,7 +71,11 @@ public static class Builder {
public @NonNull ProxyFallbackConfiguration build() {
Preconditions.checkNotNull(this.targetGroup, "Missing targetGroup");

return new ProxyFallbackConfiguration(this.targetGroup, this.defaultFallbackTask, this.fallbacks);
return new ProxyFallbackConfiguration(
this.targetGroup,
this.showDownstreamKickMessage,
this.defaultFallbackTask,
this.fallbacks);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ public void initModule(
httpServer.annotationParser().parseAndRegister(V2HttpHandlerBridge.class);
}

// todo: remove in a release after 4.0.0-RC11
@ModuleTask(lifecycle = ModuleLifeCycle.STARTED)
public void updateBridgeConfiguration() {
var config = DocumentFactory.json().parse(this.configPath());
var localizedMessages = config.readMutableDocument("localizedMessages");
var defaultMessages = localizedMessages.readMutableDocument("default");

// if there is no entry in the default messages with the key -> add a new entry
// it is important to check on json level here, as we want to distinguish between the key being present
// with a null value and the key not being present at all
if (!defaultMessages.contains("server-kick-no-other-hub")) {
defaultMessages.append(
"server-kick-no-other-hub",
BridgeConfiguration.DEFAULT_MESSAGES.get("default").get("server-kick-no-other-hub"));

// we need to update from the inside out
localizedMessages.append("default", defaultMessages);
config.append("localizedMessages", localizedMessages);

// write the changes to the configuration
this.writeConfig(config);
}
}

@ModuleTask(lifecycle = ModuleLifeCycle.STARTED)
public void registerCommand(@NonNull CommandProvider commandProvider) {
// register the bridge command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void createBridgeEntry(
// create a new configuration for the given target group
var fallbackConfiguration = new ProxyFallbackConfiguration(
group.name(),
false,
"Lobby",
Collections.emptyList());
var configuration = this.bridgeManagement.configuration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ public void postInit() {
this.selfTask = this.taskProvider.serviceTask(this.wrapperConfig.serviceConfiguration().serviceId().taskName());
}

public @Nullable ProxyFallbackConfiguration currentFallbackConfiguration() {
return this.currentFallbackConfiguration;
}

public @NonNull NetworkServiceInfo ownNetworkServiceInfo() {
return this.ownNetworkServiceInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ public void handle(@NonNull ServerConnectEvent event) {
} else {
// no lobby server - cancel the event; another plugin might be able to override that decision
event.setCancelled(true);

var kickMessage = this.management.configuration().findMessage(
event.getPlayer().getLocale(),
"proxy-join-disconnect-because-no-hub",
this.bungeeHelper::translateToComponent,
null,
true);

// if there is a kick message specified, we will kick the player. No other plugin can override in that case
// otherwise we just do nothing.
if (kickMessage != null) {
player.disconnect(kickMessage);
}
}
}
}
Expand Down Expand Up @@ -181,12 +194,15 @@ public void handle(@NonNull ServerKickEvent event) {
event.setCancelled(false);
event.setCancelServer(null);

// set the cancel reason
this.management.configuration().handleMessage(
event.getPlayer().getLocale(),
"proxy-join-disconnect-because-no-hub",
this.bungeeHelper::translateToComponent,
event::setKickReasonComponent);
var fallbackConfig = this.management.currentFallbackConfiguration();
if (event.getReason() == null || (fallbackConfig != null && !fallbackConfig.showDownstreamKickMessage())) {
// set the cancel reason if there is no reason from the downstream service
this.management.configuration().handleMessage(
event.getPlayer().getLocale(),
"server-kick-no-other-hub",
this.bungeeHelper::translateToComponent,
event::setKickReasonComponent);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,21 @@ public void handleLogin(@NonNull LoginEvent event) {
@Subscribe(order = PostOrder.FIRST)
public void handleInitialServerChoose(@NonNull PlayerChooseInitialServerEvent event) {
// filter the next fallback
event.setInitialServer(this.management.fallback(event.getPlayer())
this.management.fallback(event.getPlayer())
.flatMap(service -> this.proxyServer.getServer(service.name()))
.orElse(null));
.ifPresentOrElse(event::setInitialServer, () -> {
var kickMessage = this.management.configuration().findMessage(
event.getPlayer().getEffectiveLocale(),
"proxy-join-disconnect-because-no-hub",
ComponentFormats.BUNGEE_TO_ADVENTURE::convert,
null,
true);
// kick the player if a kick message is set. By setting a kick message there is no possibility for other plugins
// to overwrite this decision
if (kickMessage != null) {
event.getPlayer().disconnect(kickMessage);
}
});
}

@Subscribe(order = PostOrder.FIRST)
Expand All @@ -124,12 +136,22 @@ public void handleServerKick(@NonNull KickedFromServerEvent event) {
return KickedFromServerEvent.RedirectPlayer.create(server, this.extractReasonComponent(event));
}
})
.orElse(KickedFromServerEvent.DisconnectPlayer.create(this.management.configuration().findMessage(
event.getPlayer().getEffectiveLocale(),
"proxy-join-disconnect-because-no-hub",
ComponentFormats.BUNGEE_TO_ADVENTURE::convert,
Component.empty(),
true))));
.orElseGet(() -> {
var serverKickReason = event.getServerKickReason();
var fallbackConfig = this.management.currentFallbackConfiguration();
// use the server kick reason if present & enabled in the configuration
if (serverKickReason.isPresent() && fallbackConfig != null && fallbackConfig.showDownstreamKickMessage()) {
return KickedFromServerEvent.DisconnectPlayer.create(serverKickReason.get());
}

// just fallback to the configuration message
return KickedFromServerEvent.DisconnectPlayer.create(this.management.configuration().findMessage(
event.getPlayer().getEffectiveLocale(),
"server-kick-no-other-hub",
ComponentFormats.BUNGEE_TO_ADVENTURE::convert,
Component.empty(),
true));
}));
}
}

Expand Down
Loading