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 /msg command on vanished players #82

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions src/main/java/de/myzelyam/supervanish/SuperVanish.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private void registerEvents() {
pluginManager.registerEvents(new GeneralListener(this), this);
pluginManager.registerEvents(new PlayerBlockModifyListener(this), this);
pluginManager.registerEvents(new WorldChangeListener(this), this);
pluginManager.registerEvents(new CommandListener(this), this);
if (versionUtil.isOneDotXOrHigher(10)) {
pluginManager.registerEvents(new TabCompleteListener(this), this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package de.myzelyam.supervanish.listeners;

import de.myzelyam.supervanish.SuperVanish;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

public class CommandListener implements Listener {

private final SuperVanish plugin;
private final FileConfiguration config;

public CommandListener(SuperVanish plugin) {
this.plugin = plugin;
config = plugin.getSettings();
}

@EventHandler(priority = EventPriority.LOW)
private void onCommand(PlayerCommandPreprocessEvent e) {
try {
if (!plugin.getSettings().getBoolean("MessageOptions.DisableMsgCommand", true)) return;
Player p = e.getPlayer();
if (p.hasPermission("sv.bypass")) return;
String[] args = e.getMessage().split(" ");
if (args.length < 3) return;
Player target = Bukkit.getPlayer(args[1]);
if (target == null || !plugin.getVanishStateMgr().isVanished(target.getUniqueId())) return;

String message = plugin.getMessage("FakeNotFound");

String command = e.getMessage().replace("/", "");
String cmd = command.split(" ")[0];
String[] blockedCommands = {"msg", "tell", "w"};
CommandMap commandMap;
try {
commandMap = (CommandMap) Bukkit.getServer().getClass().getMethod("getCommandMap").invoke(Bukkit.getServer());
} catch (Exception ignored) {
return;
}

for (String blockedCommand : blockedCommands) {
if (cmd.equalsIgnoreCase("minecraft:" + blockedCommand)) {
e.getPlayer().sendMessage(message);
e.setCancelled(true);
return;
} else if (cmd.equalsIgnoreCase(blockedCommand) && !(commandMap.getCommand(blockedCommand) instanceof PluginCommand)) {
e.getPlayer().sendMessage(message);
e.setCancelled(true);
return;
}
}
} catch (Exception er) {
plugin.logException(er);
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ MessageOptions:
# Should there only be fake join/quit messages and no admin announcements?
SendMessageOnlyToUsers: false

# Should SV prevent the use of the /msg command on vanished players?
DisableMsgCommand: true

# Should SV hide the real join/leave messages of invisible players?
HideRealJoinQuitMessages: true
# Should SV hide leave messages for invisible players if 'VanishStateFeatures->ReappearOnQuit' is turned on?
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Messages:
InvalidUsage: '&cInvalid usage, you can use &6/sv help&c for a list of commands.'
VanishMessage: '&e%p% left the game'
ReappearMessage: '&e%p% joined the game'
FakeNotFound: '§cNo player was found'
VanishMessageWithPermission: '&a[SV] %p% vanished.'
ReappearMessageWithPermission: '&a[SV] %p% reappeared.'
OnVanish: '&aYou are now invisible!'
Expand Down