Skip to content

Commit

Permalink
Add option to kick player via plugin messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
A248 committed Jul 29, 2023
1 parent d8b4564 commit f7d228d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ interface Commands {

interface Platforms {

@SubSection
Bukkit bukkit();

interface Bukkit {

@ConfKey("kick-via-plugin-messaging")
@ConfComments({
"This option is relevant for backend servers running within a network (BungeeCord or Velocity).",
"It instructs the proxy to kick the player from the network via plugin messaging.",
"",
"If enabled, the player will NOT be kicked by the backend server, so you MUST use a proxy",
"otherwise players will not be kicked at all."
})
@DefaultBoolean(false)
boolean kickViaPluginMessaging();

}

@SubSection
Sponge sponge();

Expand Down
23 changes: 23 additions & 0 deletions bans-env/spigot/pom.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
<!--
~ LibertyBans
~ Copyright © 2023 Anand Beh
~
~ LibertyBans is free software: you can redistribute it and/or modify
~ it under the terms of the GNU Affero General Public License as
~ published by the Free Software Foundation, either version 3 of the
~ License, or (at your option) any later version.
~
~ LibertyBans is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU Affero General Public License for more details.
~
~ You should have received a copy of the GNU Affero General Public License
~ along with LibertyBans. If not, see <https://www.gnu.org/licenses/>
~ and navigate to version 3 of the GNU Affero General Public License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand Down Expand Up @@ -58,6 +77,10 @@
<groupId>space.arim.morepaperlib</groupId>
<artifactId>morepaperlib</artifactId>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
1 change: 1 addition & 0 deletions bans-env/spigot/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
module space.arim.libertybans.env.spigot {
requires jakarta.inject;
requires net.kyori.adventure.text.serializer.legacy;
requires org.slf4j;
requires space.arim.api.env;
requires space.arim.api.env.bukkit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* LibertyBans
* Copyright © 2023 Anand Beh
*
* LibertyBans is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* LibertyBans is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with LibertyBans. If not, see <https://www.gnu.org/licenses/>
* and navigate to version 3 of the GNU Affero General Public License.
*/

package space.arim.libertybans.env.spigot;

import jakarta.inject.Inject;
import org.bukkit.plugin.Plugin;
import space.arim.libertybans.core.env.PlatformListener;

public final class ChannelRegistration implements PlatformListener {

private final Plugin plugin;

static final String BUNGEE_CHANNEL = "BungeeCord";

@Inject
public ChannelRegistration(Plugin plugin) {
this.plugin = plugin;
}

@Override
public void register() {
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, BUNGEE_CHANNEL);
}

@Override
public void unregister() {
plugin.getServer().getMessenger().unregisterOutgoingPluginChannel(plugin, BUNGEE_CHANNEL);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* LibertyBans
* Copyright © 2022 Anand Beh
* Copyright © 2023 Anand Beh
*
* LibertyBans is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand All @@ -22,32 +22,40 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import space.arim.api.env.AudienceRepresenter;
import space.arim.libertybans.core.config.Configs;
import space.arim.libertybans.core.config.InternalFormatter;
import space.arim.libertybans.core.env.AbstractEnvEnforcer;
import space.arim.libertybans.core.env.Interlocutor;
import space.arim.morepaperlib.adventure.MorePaperLibAdventure;
import space.arim.omnibus.util.concurrent.CentralisedFuture;
import space.arim.omnibus.util.concurrent.FactoryOfTheFuture;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.util.UUID;
import java.util.function.Consumer;

@Singleton
public class SpigotEnforcer extends AbstractEnvEnforcer<Player> {

private final Configs configs;
private final Server server;
private final MorePaperLibAdventure morePaperLibAdventure;

@Inject
public SpigotEnforcer(FactoryOfTheFuture futuresFactory, InternalFormatter formatter,
Interlocutor interlocutor, AudienceRepresenter<CommandSender> audienceRepresenter,
Server server, MorePaperLibAdventure morePaperLibAdventure) {
Configs configs, Server server, MorePaperLibAdventure morePaperLibAdventure) {
super(futuresFactory, formatter, interlocutor, audienceRepresenter);
this.configs = configs;
this.server = server;
this.morePaperLibAdventure = morePaperLibAdventure;
}
Expand All @@ -69,7 +77,26 @@ protected CentralisedFuture<Void> doForAllPlayers(Consumer<Player> callback) {

@Override
public void kickPlayer(Player player, Component message) {
morePaperLibAdventure.kickPlayer(player, message);
if (configs.getMainConfig().platforms().bukkit().kickViaPluginMessaging()) {
byte[] kickData;
try (ByteArrayOutputStream kickDataStream = new ByteArrayOutputStream();
DataOutputStream write = new DataOutputStream(kickDataStream)) {

write.writeUTF("KickPlayer");
write.writeUTF(player.getName());
write.writeUTF(LegacyComponentSerializer.legacySection().serialize(message));
kickData = kickDataStream.toByteArray();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
player.sendPluginMessage(
morePaperLibAdventure.getMorePaperLib().getPlugin(),
ChannelRegistration.BUNGEE_CHANNEL,
kickData
);
} else {
morePaperLibAdventure.kickPlayer(player, message);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* LibertyBans
* Copyright © 2022 Anand Beh
* Copyright © 2023 Anand Beh
*
* LibertyBans is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -32,22 +32,24 @@ public final class SpigotEnv implements Environment {
private final Provider<ConnectionListener> connectionListener;
private final Provider<ChatListener> chatListener;
private final CommandHandler.CommandHelper commandHelper;
private final ChannelRegistration channelRegistration;

@Inject
public SpigotEnv(Provider<ConnectionListener> connectionListener,
Provider<ChatListener> chatListener,
CommandHandler.CommandHelper commandHelper) {
public SpigotEnv(Provider<ConnectionListener> connectionListener, Provider<ChatListener> chatListener,
CommandHandler.CommandHelper commandHelper, ChannelRegistration channelRegistration) {
this.connectionListener = connectionListener;
this.chatListener = chatListener;
this.commandHelper = commandHelper;
this.channelRegistration = channelRegistration;
}

@Override
public Set<PlatformListener> createListeners() {
return Set.of(
connectionListener.get(),
chatListener.get(),
new CommandHandler(commandHelper, Commands.BASE_COMMAND_NAME, false)
new CommandHandler(commandHelper, Commands.BASE_COMMAND_NAME, false),
channelRegistration
);
}

Expand Down

0 comments on commit f7d228d

Please sign in to comment.