Skip to content

Commit

Permalink
Merge pull request #217 from beanbeanjuice/94-disabled-servers
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice authored Aug 31, 2024
2 parents e245a0d + be1fbc7 commit b9e0c8e
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 5 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,28 @@ send-previous-messages-on-switch:

# These require a restart in order to take place.
commands:
reload-aliases:
- "spcreload"
chat-toggle-aliases:
- "chattoggle"
ban-aliases:
- "spcban"
unban-aliases:
- "spcunban"
whisper-aliases:
- "spc-msg"
reply-aliases:
- "spc-r"
broadcast-aliases:
- "spc-bc"
- "broadcast"

# Disable servers from sending/receiving proxy messages.
disabled-servers:
- example1

# DO NOT TOUCH THIS
file-version: 13
file-version: 15
```
**messages.yml**
Expand Down
2 changes: 1 addition & 1 deletion projects/main-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

version = "0.5.6"
version = "0.5.7"

java {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public void runProxyLeaveMessage(String playerName, UUID playerUUID, String serv
// Log to Console
if (config.get(ConfigKey.CONSOLE_LEAVE).asBoolean()) plugin.log(message);


// Log to Discord
if (config.get(ConfigKey.DISCORD_LEAVE_ENABLED).asBoolean()) {
EmbedBuilder embedBuilder = simpleAuthorEmbedBuilder(playerUUID, discordMessage).setColor(Color.RED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigKey;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.beanbeanjuice.simpleproxychat.utility.listeners.MessageType;
import com.beanbeanjuice.simpleproxychat.utility.listeners.bungee.BungeeServerListener;
import lombok.Getter;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.config.ServerInfo;
Expand Down Expand Up @@ -69,6 +70,7 @@ public void chatSendToAllOtherPlayers(String parsedMessage) {
if (!plugin.getConfig().get(ConfigKey.USE_PERMISSIONS).asBoolean()) return true;
return streamPlayer.hasPermission(Permission.READ_CHAT_MESSAGE.getPermissionNode());
})
.filter((streamPlayer) -> !BungeeServerListener.playerIsInDisabledServer(streamPlayer, plugin))
.forEach((streamPlayer) -> streamPlayer.sendMessage(ChatMessageType.CHAT, convertToBungee(parsedMessage)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigKey;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.beanbeanjuice.simpleproxychat.utility.listeners.MessageType;
import com.beanbeanjuice.simpleproxychat.utility.listeners.velocity.VelocityServerListener;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import lombok.Getter;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void chatSendToAllOtherPlayers(String parsedMessage) {
if (!plugin.getConfig().get(ConfigKey.USE_PERMISSIONS).asBoolean()) return true;
return streamPlayer.hasPermission(Permission.READ_CHAT_MESSAGE.getPermissionNode());
})
.filter((streamPlayer) -> !VelocityServerListener.playerIsInDisabledServer(streamPlayer, plugin))
.forEach((streamPlayer) -> streamPlayer.sendMessage(component));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum ConfigKey {
WHISPER_ALIASES (ConfigFileType.CONFIG, "commands.whisper-aliases", List.class),
REPLY_ALIASES (ConfigFileType.CONFIG, "commands.reply-aliases", List.class),
BROADCAST_ALIASES (ConfigFileType.CONFIG, "commands.broadcast-aliases", List.class),
DISABLED_SERVERS (ConfigFileType.CONFIG, "disabled-servers", List.class),

// MESSAGES
PLUGIN_PREFIX (ConfigFileType.MESSAGES, "plugin-prefix", String.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.beanbeanjuice.simpleproxychat.SimpleProxyChatBungee;
import com.beanbeanjuice.simpleproxychat.chat.ChatHandler;
import com.beanbeanjuice.simpleproxychat.socket.bungee.BungeeChatMessageData;
import com.beanbeanjuice.simpleproxychat.utility.ISimpleProxyChat;
import com.beanbeanjuice.simpleproxychat.utility.helper.Helper;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.beanbeanjuice.simpleproxychat.utility.listeners.MessageType;
Expand Down Expand Up @@ -37,12 +38,17 @@ public BungeeServerListener(SimpleProxyChatBungee plugin, ChatHandler chatHandle
startServerStatusDetection();
}

public static boolean playerIsInDisabledServer(ProxiedPlayer player, ISimpleProxyChat plugin) {
return plugin.getSPCConfig().get(ConfigKey.DISABLED_SERVERS).asList().contains(player.getServer().getInfo().getName());
}

@EventHandler (priority = EventPriority.HIGHEST)
public void onProxyChatEvent(ChatEvent event) {
if (event.isCancelled()) return;
if (event.isCommand() || event.isProxyCommand()) return;

ProxiedPlayer player = (ProxiedPlayer) event.getSender();
if (playerIsInDisabledServer(player, plugin)) return;

if (plugin.isVanishAPIEnabled() && BungeeVanishAPI.isInvisible(player)) {
// TODO: If is allowed to speak in vanish then continue.
if (!event.getMessage().endsWith("/")) {
Expand All @@ -67,6 +73,7 @@ public void onProxyChatEvent(ChatEvent event) {
*/
@EventHandler
public void onPlayerLeaveServer(ServerDisconnectEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;
previousServerHandler.put(event.getPlayer().getName(), event.getTarget());
}

Expand All @@ -76,13 +83,15 @@ public void onPlayerLeaveServer(ServerDisconnectEvent event) {
*/
@EventHandler
public void onPlayerKick(ServerKickEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;
if (event.getState() == ServerKickEvent.State.CONNECTING) return;
if (!event.getPlayer().getGroups().contains("successful-connection")) return;
previousServerHandler.put(event.getPlayer().getName(), event.getKickedFrom());
}

@EventHandler
public void onPlayerLeaveProxy(PlayerDisconnectEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;
if (!event.getPlayer().getGroups().contains("successful-connection")) return;
if (!event.getPlayer().getGroups().contains("not-first-join")) return;
if (plugin.isVanishAPIEnabled() && BungeeVanishAPI.isInvisible(event.getPlayer())) return; // Ignore if invisible.
Expand Down Expand Up @@ -125,6 +134,8 @@ public void onPostLogin(PostLoginEvent event) {

@EventHandler
public void onPlayerJoinProxy(ServerConnectedEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;

if (event.getPlayer().getGroups().contains("not-first-join")) return; // If not first join, don't do anything.
if (!event.getPlayer().getGroups().contains("successful-connection")) return;
event.getPlayer().addGroups("not-first-join");
Expand Down Expand Up @@ -157,6 +168,8 @@ public void join(ProxiedPlayer player, @Nullable Server server, boolean isFake)
public void onPlayerServerSwitch(ServerSwitchEvent event) {
ProxiedPlayer player = event.getPlayer();

if (playerIsInDisabledServer(player, plugin)) return;

if (plugin.isVanishAPIEnabled() && BungeeVanishAPI.isInvisible(player)) return; // Ignore if player is invisible.
if (event.getFrom() == null) return; // This means the player just joined the network.

Expand Down Expand Up @@ -203,6 +216,9 @@ private void sendToAllServers(String parsedMessage, Permission permission) {
if (player.getServer() == null || player.getServer().getInfo() == null) return false;
return !Helper.serverHasChatLocked(plugin, player.getServer().getInfo().getName());
})
.filter((player) -> { // Players IN the server will not receive the message.
return !playerIsInDisabledServer(player, plugin);
})
.forEach((player) -> player.sendMessage(ChatMessageType.CHAT, Helper.convertToBungee(parsedMessage)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.beanbeanjuice.simpleproxychat.SimpleProxyChatVelocity;
import com.beanbeanjuice.simpleproxychat.chat.ChatHandler;
import com.beanbeanjuice.simpleproxychat.socket.velocity.VelocityChatMessageData;
import com.beanbeanjuice.simpleproxychat.utility.ISimpleProxyChat;
import com.beanbeanjuice.simpleproxychat.utility.helper.Helper;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.beanbeanjuice.simpleproxychat.utility.listeners.MessageType;
Expand All @@ -16,7 +17,9 @@
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import de.myzelyam.api.vanish.VelocityVanishAPI;
import lombok.Getter;
import net.kyori.adventure.text.Component;
Expand All @@ -43,8 +46,22 @@ public void initializeVelocityVanishListener() {
this.velocityVanishListener.startVanishListener();
}

public static boolean playerIsInDisabledServer(Player player, ISimpleProxyChat plugin) {
return player.getCurrentServer()
.map(ServerConnection::getServerInfo)
.map(ServerInfo::getName)
.map(plugin.getSPCConfig().get(ConfigKey.DISABLED_SERVERS).asList()::contains)
.orElse(false);
}

public static boolean playerIsInDisabledServer(ServerInfo serverInfo, ISimpleProxyChat plugin) {
return plugin.getSPCConfig().get(ConfigKey.DISABLED_SERVERS).asList().contains(serverInfo.getName());
}

@Subscribe(order = PostOrder.LAST)
public void onPlayerChat(PlayerChatEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;

String playerMessage = event.getMessage();
Player player = event.getPlayer();
if (plugin.isVanishAPIEnabled() && VelocityVanishAPI.isInvisible(player)) {
Expand All @@ -67,13 +84,16 @@ public void onPlayerChat(PlayerChatEvent event) {

@Subscribe
public void onDisconnect(DisconnectEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;
if (plugin.isVanishAPIEnabled() && VelocityVanishAPI.isInvisible(event.getPlayer())) return; // Ignore if invisible.

leave(event.getPlayer());
}

@Subscribe
public void kickedFromServerEvent(KickedFromServerEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;

KickedFromServerEvent.ServerKickResult result = event.getResult();
if (result.toString().contains("velocity.error.cant-connect")) return;
if (event.getServerKickReason().isEmpty()) return;
Expand Down Expand Up @@ -124,10 +144,13 @@ public void onPreLoginEvent(PreLoginEvent event) {

@Subscribe
public void onServerConnected(ServerConnectedEvent event) {
if (playerIsInDisabledServer(event.getPlayer(), plugin)) return;
if (plugin.isVanishAPIEnabled() && VelocityVanishAPI.isInvisible(event.getPlayer())) return;

// First Join
if (event.getPreviousServer().isEmpty()) {
if (playerIsInDisabledServer(event.getServer().getServerInfo(), plugin)) return;

join(event.getPlayer(), event.getServer().getServerInfo().getName());
return;
}
Expand Down Expand Up @@ -166,6 +189,7 @@ private void sendToAllServers(String message, Permission permission) {
return player.hasPermission(permission.getPermissionNode());
return true;
})
.filter((player) -> !playerIsInDisabledServer(player, plugin))
.forEach((player) -> player.sendMessage(MiniMessage.miniMessage().deserialize(message)));
}

Expand Down
6 changes: 5 additions & 1 deletion projects/main-app/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,9 @@ commands:
- "spc-bc"
- "broadcast"

# Disable servers from sending/receiving proxy messages.
disabled-servers:
- example1

# DO NOT TOUCH THIS
file-version: 14
file-version: 15

0 comments on commit b9e0c8e

Please sign in to comment.