TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_nickfilter.java

120 lines
3.7 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.totalfreedom.totalfreedommod.rank.Rank;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = Rank.OP, source = SourceType.BOTH)
@CommandParameters(description = "NickFilter: Prefix any command with this command to replace nicknames in that command with real names. Nicknames should be prefixed with a !.",
usage = "/<command> <other_command> !<playernick>",
aliases = "nf")
public class Command_nickfilter extends FreedomCommand
{
private static Player getPlayerByDisplayName(String needle)
{
needle = needle.toLowerCase().trim();
for (Player player : Bukkit.getOnlinePlayers())
{
if (player.getDisplayName().toLowerCase().trim().contains(needle))
{
return player;
}
}
return null;
}
private static Player getPlayerByDisplayNameAlt(String needle)
{
needle = needle.toLowerCase().trim();
Integer minEditDistance = null;
Player minEditMatch = null;
for (Player player : Bukkit.getOnlinePlayers())
{
String haystack = player.getDisplayName().toLowerCase().trim();
int editDistance = StringUtils.getLevenshteinDistance(needle, haystack.toLowerCase());
if (minEditDistance == null || minEditDistance > editDistance)
{
minEditDistance = editDistance;
minEditMatch = player;
}
}
return minEditMatch;
}
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
boolean nickMatched = false;
final List<String> outputCommand = new ArrayList<>();
if (args.length >= 1)
{
for (String arg : args)
{
Player player = null;
Matcher matcher = Pattern.compile("^!(.+)$").matcher(arg);
if (matcher.find())
{
String displayName = matcher.group(1);
player = getPlayerByDisplayName(displayName);
if (player == null || plugin.al.isVanished(player.getName()) && !plugin.al.isAdmin(sender))
{
player = getPlayerByDisplayNameAlt(displayName);
if (player == null || !plugin.al.isVanished(player.getName()) && !plugin.al.isAdmin(sender))
{
Right, so this change applies only to commands. For the sake of code consistency, I tried to change as many as possible to use `FreedomCommand.msg` instead of `CommandSender.sendMessage` for their messages. Here are a list of the files containing those changes: * Command_adminworld.java * Command_adventure.java * Command_banip.java * Command_blockedit.java * Command_blockpvp.java * Command_cage.java * Command_cartsit.java * Command_clearchat.java * Command_clearinventory.java * Command_commandlist.java * Command_creative.java * Command_deop.java * Command_deopall.java * Command_dispfill.java * Command_doom.java * Command_gcmd.java * Command_hubworld.java * Command_inspect.java * Command_list.java * Command_lockup.java * Command_manageshop.java * Command_manuallyverify.java * Command_masterbuilderworld.java * Command_mbconfig.java * Command_moblimiter.java * Command_mp44.java * Command_mute.java * Command_nickfilter.java * Command_op.java * Command_opall.java * Command_opme.java * Command_potion.java (Also corrected the inconsistent "player not found" message's color) * Command_rank.java * Command_ride.java * Command_saconfig.java * Command_scare.java * Command_setplayerlimit.java * Command_settotalvotes.java * Command_smite.java * Command_spectator.java * Command_survival.java * Command_unblockcmd.java * Command_uncage.java * Command_unmute.java * Command_verifynoadmin.java Here are some commands I added functionality to: * Command_dispfill.java: Added some code that hooks into the CoreProtect API to log the items being removed from and added into the dispensers. * Command_setlever.java: Added some code that hooks into the CoreProtect API to log the levers being interacted with. Here's a command I fixed a critical bug in: * Command_setlever.java
2021-04-05 23:13:26 +00:00
msg("Can't find player by nickname: " + displayName);
return true;
}
}
}
if (player == null)
{
outputCommand.add(arg);
}
else
{
nickMatched = true;
outputCommand.add(player.getName());
}
}
}
if (!nickMatched)
{
Right, so this change applies only to commands. For the sake of code consistency, I tried to change as many as possible to use `FreedomCommand.msg` instead of `CommandSender.sendMessage` for their messages. Here are a list of the files containing those changes: * Command_adminworld.java * Command_adventure.java * Command_banip.java * Command_blockedit.java * Command_blockpvp.java * Command_cage.java * Command_cartsit.java * Command_clearchat.java * Command_clearinventory.java * Command_commandlist.java * Command_creative.java * Command_deop.java * Command_deopall.java * Command_dispfill.java * Command_doom.java * Command_gcmd.java * Command_hubworld.java * Command_inspect.java * Command_list.java * Command_lockup.java * Command_manageshop.java * Command_manuallyverify.java * Command_masterbuilderworld.java * Command_mbconfig.java * Command_moblimiter.java * Command_mp44.java * Command_mute.java * Command_nickfilter.java * Command_op.java * Command_opall.java * Command_opme.java * Command_potion.java (Also corrected the inconsistent "player not found" message's color) * Command_rank.java * Command_ride.java * Command_saconfig.java * Command_scare.java * Command_setplayerlimit.java * Command_settotalvotes.java * Command_smite.java * Command_spectator.java * Command_survival.java * Command_unblockcmd.java * Command_uncage.java * Command_unmute.java * Command_verifynoadmin.java Here are some commands I added functionality to: * Command_dispfill.java: Added some code that hooks into the CoreProtect API to log the items being removed from and added into the dispensers. * Command_setlever.java: Added some code that hooks into the CoreProtect API to log the levers being interacted with. Here's a command I fixed a critical bug in: * Command_setlever.java
2021-04-05 23:13:26 +00:00
msg("No nicknames replaced in command.");
return true;
}
String newCommand = StringUtils.join(outputCommand, " ");
if (plugin.cb.isCommandBlocked(newCommand, sender))
{
// CommandBlocker handles messages and broadcasts
return true;
}
Right, so this change applies only to commands. For the sake of code consistency, I tried to change as many as possible to use `FreedomCommand.msg` instead of `CommandSender.sendMessage` for their messages. Here are a list of the files containing those changes: * Command_adminworld.java * Command_adventure.java * Command_banip.java * Command_blockedit.java * Command_blockpvp.java * Command_cage.java * Command_cartsit.java * Command_clearchat.java * Command_clearinventory.java * Command_commandlist.java * Command_creative.java * Command_deop.java * Command_deopall.java * Command_dispfill.java * Command_doom.java * Command_gcmd.java * Command_hubworld.java * Command_inspect.java * Command_list.java * Command_lockup.java * Command_manageshop.java * Command_manuallyverify.java * Command_masterbuilderworld.java * Command_mbconfig.java * Command_moblimiter.java * Command_mp44.java * Command_mute.java * Command_nickfilter.java * Command_op.java * Command_opall.java * Command_opme.java * Command_potion.java (Also corrected the inconsistent "player not found" message's color) * Command_rank.java * Command_ride.java * Command_saconfig.java * Command_scare.java * Command_setplayerlimit.java * Command_settotalvotes.java * Command_smite.java * Command_spectator.java * Command_survival.java * Command_unblockcmd.java * Command_uncage.java * Command_unmute.java * Command_verifynoadmin.java Here are some commands I added functionality to: * Command_dispfill.java: Added some code that hooks into the CoreProtect API to log the items being removed from and added into the dispensers. * Command_setlever.java: Added some code that hooks into the CoreProtect API to log the levers being interacted with. Here's a command I fixed a critical bug in: * Command_setlever.java
2021-04-05 23:13:26 +00:00
msg("Sending command: \"" + newCommand + "\".");
server.dispatchCommand(sender, newCommand);
return true;
}
2020-07-22 21:40:58 +00:00
}