Rework tags (now they won't duplicate when Essentials changes display name).

This commit is contained in:
StevenLawson 2014-08-12 18:25:57 -04:00
parent 891d8d178f
commit 1a50c16d9a
3 changed files with 79 additions and 59 deletions

View File

@ -18,20 +18,30 @@ public class Command_tag extends TFM_Command
{ {
public static final List<String> FORBIDDEN_WORDS = Arrays.asList(new String[] public static final List<String> FORBIDDEN_WORDS = Arrays.asList(new String[]
{ {
"admin", "owner", "moderator", "developer", "&k" "admin", "owner", "moderator", "developer"
}); });
@Override @Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{ {
if (args.length < 1)
{
return false;
}
if (args.length == 1) if (args.length == 1)
{ {
if ("clearall".equals(args[0])) if ("list".equalsIgnoreCase(args[0]))
{
playerMsg("Tags for all online players:");
for (final Player player : server.getOnlinePlayers())
{
final TFM_PlayerData playerdata = TFM_PlayerData.getPlayerData(player);
if (playerdata.getTag() != null)
{
playerMsg(player.getName() + ": " + playerdata.getTag());
}
}
return true;
}
else if ("clearall".equalsIgnoreCase(args[0]))
{ {
if (!TFM_AdminList.isSuperAdmin(sender)) if (!TFM_AdminList.isSuperAdmin(sender))
{ {
@ -40,47 +50,44 @@ public class Command_tag extends TFM_Command
} }
TFM_Util.adminAction(sender.getName(), "Removing all tags", false); TFM_Util.adminAction(sender.getName(), "Removing all tags", false);
int count = 0;
for (Player player : server.getOnlinePlayers())
{
TFM_PlayerData playerdata = TFM_PlayerData.getPlayerData(player);
int count = 0;
for (final Player player : server.getOnlinePlayers())
{
final TFM_PlayerData playerdata = TFM_PlayerData.getPlayerData(player);
if (playerdata.getTag() != null) if (playerdata.getTag() != null)
{ {
count++; count++;
TFM_PlayerData.getPlayerData(player).setTag(null); playerdata.setTag(null);
} }
} }
playerMsg(count + " tag(s) removed."); playerMsg(count + " tag(s) removed.");
return true; return true;
} }
else if ("off".equalsIgnoreCase(args[0]))
if (senderIsConsole)
{ {
playerMsg("Only in-game players can set tags. Use \"/tag clearall\" to reset all tags."); if (senderIsConsole)
{
playerMsg("\"/tag off\" can't be used from the console. Use \"/tag clear <player>\" or \"/tag clearall\" instead.");
}
else
{
TFM_PlayerData.getPlayerData(sender_p).setTag(null);
playerMsg("Your tag has been removed.");
}
return true; return true;
} }
else
if ("off".equals(args[0]))
{ {
TFM_PlayerData.getPlayerData(sender_p).setTag(null); return false;
playerMsg("Your tag has been removed.");
return true;
} }
if (ChatColor.stripColor(TFM_Util.colorize(args[0])).length() > 20)
{
playerMsg("That tag is too long [Max = 20 characters, not including color codes].");
return true;
}
return false;
} }
else if (args.length >= 2)
if (args.length == 2)
{ {
if ("clear".equals(args[0])) if ("clear".equalsIgnoreCase(args[0]))
{ {
if (!TFM_AdminList.isSuperAdmin(sender)) if (!TFM_AdminList.isSuperAdmin(sender))
{ {
@ -98,42 +105,56 @@ public class Command_tag extends TFM_Command
TFM_PlayerData.getPlayerData(player).setTag(null); TFM_PlayerData.getPlayerData(player).setTag(null);
playerMsg("Removed " + player.getName() + "'s tag."); playerMsg("Removed " + player.getName() + "'s tag.");
return true; return true;
} }
} else if ("set".equalsIgnoreCase(args[0]))
if ("set".equals(args[0]))
{
final String tag = StringUtils.join(args, " ", 1, args.length);
if (!TFM_AdminList.isSuperAdmin(sender))
{ {
for (String word : FORBIDDEN_WORDS) final String inputTag = StringUtils.join(args, " ", 1, args.length);
final String outputTag = TFM_Util.colorize(StringUtils.replaceEachRepeatedly(StringUtils.strip(inputTag),
new String[]
{
"" + ChatColor.COLOR_CHAR, "&k"
},
new String[]
{
"", ""
}
)) + ChatColor.RESET;
if (!TFM_AdminList.isSuperAdmin(sender))
{ {
if (!tag.toLowerCase().contains(word)) final String rawTag = ChatColor.stripColor(outputTag).toLowerCase();
if (rawTag.length() > 20)
{ {
continue; playerMsg("That tag is too long (Max is 20 characters).");
return true;
} }
if (word.contains(String.valueOf(ChatColor.COLOR_CHAR))) for (String word : FORBIDDEN_WORDS)
{ {
playerMsg("That tag contains a forbidden color-code."); if (rawTag.contains(word))
{
playerMsg("That tag contains a forbidden word.");
return true;
}
} }
else
{
playerMsg("That tag contains a forbidden word.");
}
return true;
} }
TFM_PlayerData.getPlayerData(sender_p).setTag(outputTag);
playerMsg("Tag set to '" + outputTag + "'.");
return true;
}
else
{
return false;
} }
TFM_PlayerData.getPlayerData(sender_p).setTag(TFM_Util.colorize(tag));
playerMsg("Tag set to " + TFM_Util.colorize(tag));
return true;
} }
else
return false; {
return false;
}
} }
} }

View File

@ -630,9 +630,8 @@ public class TFM_PlayerListener implements Listener
// Set the tag // Set the tag
if (playerdata.getTag() != null) if (playerdata.getTag() != null)
{ {
player.setDisplayName((playerdata.getTag() + " " + player.getDisplayName().replaceAll(" ", ""))); event.setFormat("<" + playerdata.getTag().replaceAll("%", "%%") + " %1$s> %2$s");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -7,6 +7,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import me.StevenLawson.TotalFreedomMod.Bridge.TFM_EssentialsBridge;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@ -72,7 +73,6 @@ public class TFM_PlayerData
return playerdata; return playerdata;
} }
Iterator<Entry<Player, TFM_PlayerData>> it = userinfo.entrySet().iterator(); Iterator<Entry<Player, TFM_PlayerData>> it = userinfo.entrySet().iterator();
while (it.hasNext()) while (it.hasNext())
{ {
@ -391,7 +391,7 @@ public class TFM_PlayerData
player.setOp(false); player.setOp(false);
player.setGameMode(GameMode.SURVIVAL); player.setGameMode(GameMode.SURVIVAL);
player.setFlying(false); player.setFlying(false);
player.setDisplayName(player.getName()); TFM_EssentialsBridge.setNickname(player.getName(), player.getName());
player.closeInventory(); player.closeInventory();
player.setTotalExperience(0); player.setTotalExperience(0);