Skip to content

Commit

Permalink
Fixed: Bungeecord console message parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram1903 committed Jun 20, 2024
1 parent 63354d8 commit 6ca5f0d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,30 @@

package com.deathmotion.playercrasher;

import com.deathmotion.playercrasher.Util.MessageSender;
import com.deathmotion.playercrasher.commands.BungeeCrashCommand;
import com.deathmotion.playercrasher.commands.BungeeCrashInfoCommand;
import com.deathmotion.playercrasher.interfaces.Scheduler;
import io.github.retrooper.packetevents.adventure.serializer.legacy.LegacyComponentSerializer;
import io.github.retrooper.packetevents.bstats.Metrics;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin;

import java.util.UUID;
import java.util.regex.Pattern;


@Getter
public class BungeePlayerCrasher extends PCPlatform<Plugin> {

private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)&[0-9A-FK-ORX]|\\u25cf");
public final MessageSender messageSender;
private final PCBungee plugin;

public BungeePlayerCrasher(PCBungee plugin) {
this.plugin = plugin;
this.messageSender = new MessageSender(plugin);
}

@Override
Expand All @@ -64,7 +68,8 @@ public boolean hasPermission(UUID sender, String permission) {

@Override
public void sendConsoleMessage(Component message) {
ProxyServer.getInstance().getConsole().sendMessage(LegacyComponentSerializer.legacySection().serialize(message));
String legacyMessage = STRIP_COLOR_PATTERN.matcher(LegacyComponentSerializer.legacyAmpersand().serialize(message)).replaceAll("").trim();
ProxyServer.getInstance().getConsole().sendMessage(legacyMessage);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@

package com.deathmotion.playercrasher.Util;

import com.deathmotion.playercrasher.PCBungee;
import com.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.adventure.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;

public class MessageSender {

public static void sendMessages(CommandSender sender, Component message) {
private final PCBungee plugin;

public MessageSender(PCBungee plugin) {
this.plugin = plugin;
}

public void sendMessages(CommandSender sender, Component message) {
if (sender instanceof ProxiedPlayer) {
PacketEvents.getAPI().getPlayerManager().getUser(sender).sendMessage(message);
} else {
sender.sendMessage(LegacyComponentSerializer.legacySection().serialize(message));
plugin.getPc().sendConsoleMessage(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
package com.deathmotion.playercrasher.commands;

import com.deathmotion.playercrasher.PCBungee;
import com.deathmotion.playercrasher.Util.MessageSender;
import com.deathmotion.playercrasher.data.CommonSender;
import com.deathmotion.playercrasher.enums.CrashMethod;
import com.deathmotion.playercrasher.util.CommandUtil;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import io.github.retrooper.packetevents.adventure.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
Expand All @@ -38,45 +37,47 @@
public class BungeeCrashCommand extends Command implements TabExecutor {

private final PCBungee plugin;
private final MessageSender messageSender;

public BungeeCrashCommand(PCBungee plugin) {
super("Crash", "PlayerCrasher.Crash", "");

this.plugin = plugin;
this.messageSender = plugin.getPc().messageSender;
plugin.getProxy().getPluginManager().registerCommand(plugin, this);
}

@Override
public void execute(CommandSender sender, String[] args) {
if (!sender.hasPermission("PlayerCrasher.Crash")) {
sendMessages(sender, CommandUtil.noPermission);
messageSender.sendMessages(sender, CommandUtil.noPermission);
return;
}

if (args.length == 0) {
sendMessages(sender, CommandUtil.invalidCommand);
messageSender.sendMessages(sender, CommandUtil.invalidCommand);
return;
}

ProxiedPlayer targetPlayer = plugin.getProxy().getPlayer(args[0]);
if (targetPlayer == null) {
sendMessages(sender, CommandUtil.playerNotFound);
messageSender.sendMessages(sender, CommandUtil.playerNotFound);
return;
}

User target = PacketEvents.getAPI().getPlayerManager().getUser(targetPlayer);
if (target == null) {
sendMessages(sender, CommandUtil.playerNotFound);
messageSender.sendMessages(sender, CommandUtil.playerNotFound);
return;
}

if (targetPlayer == sender) {
sendMessages(sender, CommandUtil.selfCrash);
messageSender.sendMessages(sender, CommandUtil.selfCrash);
return;
}

if (targetPlayer.hasPermission("PlayerCrasher.Bypass")) {
sendMessages(sender, CommandUtil.playerBypass);
messageSender.sendMessages(sender, CommandUtil.playerBypass);
return;
}

Expand All @@ -91,12 +92,12 @@ public void execute(CommandSender sender, String[] args) {
try {
method = CrashMethod.valueOf(args[1].toUpperCase());
} catch (IllegalArgumentException e) {
sendMessages(sender, CommandUtil.invalidMethod);
messageSender.sendMessages(sender, CommandUtil.invalidMethod);
return;
}
}

sendMessages(sender, CommandUtil.crashSent(target.getName()));
messageSender.sendMessages(sender, CommandUtil.crashSent(target.getName()));
plugin.getPc().crashPlayer(createCommonUser(sender), target, method);
}

Expand All @@ -120,14 +121,6 @@ public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
return suggestions;
}

private void sendMessages(CommandSender sender, Component message) {
if (sender instanceof ProxiedPlayer) {
PacketEvents.getAPI().getPlayerManager().getUser(sender).sendMessage(message);
} else {
sender.sendMessage(LegacyComponentSerializer.legacySection().serialize(message));
}
}

private CommonSender createCommonUser(CommandSender sender) {
if (sender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) sender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import io.github.retrooper.packetevents.adventure.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
Expand All @@ -35,24 +34,26 @@
public class BungeeCrashInfoCommand extends Command implements TabExecutor {

private final PCBungee plugin;
private final MessageSender messageSender;

public BungeeCrashInfoCommand(PCBungee plugin) {
super("CrashInfo", "PlayerCrasher.CrashInfo", "brand");

this.plugin = plugin;
this.messageSender = plugin.getPc().messageSender;
plugin.getProxy().getPluginManager().registerCommand(plugin, this);
}

@Override
public void execute(CommandSender sender, String[] args) {
if (!sender.hasPermission("PlayerCrasher.CrashInfo")) {
MessageSender.sendMessages(sender, CommandUtil.noPermission);
messageSender.sendMessages(sender, CommandUtil.noPermission);
return;
}

if (args.length == 0) {
if (!(sender instanceof ProxiedPlayer)) {
sender.sendMessage(LegacyComponentSerializer.legacySection().serialize(CommandUtil.specifyPlayer));
plugin.getPc().sendConsoleMessage(CommandUtil.specifyPlayer);
return;
} else {
User user = PacketEvents.getAPI().getPlayerManager().getUser(sender);
Expand All @@ -66,15 +67,15 @@ public void execute(CommandSender sender, String[] args) {
ProxiedPlayer playerToCheck = plugin.getProxy().getPlayer(args[0]);

if (playerToCheck == null) {
MessageSender.sendMessages(sender, CommandUtil.playerNotFound);
messageSender.sendMessages(sender, CommandUtil.playerNotFound);
return;
}

User userToCheck = PacketEvents.getAPI().getPlayerManager().getUser(playerToCheck);
String clientBrand = plugin.getPc().getClientBrand(userToCheck.getUUID());
ClientVersion clientVersion = userToCheck.getClientVersion();

MessageSender.sendMessages(sender, CommandUtil.playerBrand(playerToCheck.getName(), clientBrand, clientVersion.getReleaseName()));
messageSender.sendMessages(sender, CommandUtil.playerBrand(playerToCheck.getName(), clientBrand, clientVersion.getReleaseName()));
}

@Override
Expand Down

0 comments on commit 6ca5f0d

Please sign in to comment.