summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/chat/UsernameUtil.java
blob: 3df615652891a7f950eba35c34e29f35a8cffc07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.earth2me.essentials.update.chat;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;


public final class UsernameUtil
{
	private static final Pattern CB_PATTERN = Pattern.compile("git-Bukkit-([0-9]+).([0-9]+).([0-9]+)-[0-9]+-[0-9a-z]+-b([0-9]+)jnks.*");

	private UsernameUtil()
	{
	}

	public static String createUsername(final Player player)
	{
		final StringBuilder nameBuilder = new StringBuilder();
		final Server server = Bukkit.getServer();
		nameBuilder.append(player.getName());

		addCraftBukkitVersion(server, nameBuilder);
		addEssentialsVersion(server, nameBuilder);
		addGroupManagerVersion(server, nameBuilder);
		addPermissionsExVersion(server, nameBuilder);
		addPermissionsBukkitVersion(server, nameBuilder);
		addBPermissionsVersion(server, nameBuilder);
		addPermissionsVersion(server, nameBuilder);

		return nameBuilder.toString();
	}

	private static void addPermissionsVersion(final Server server, final StringBuilder nameBuilder)
	{
		final Plugin perm = server.getPluginManager().getPlugin("Permissions");
		if (perm != null)
		{
			nameBuilder.append(" P");
			if (!perm.isEnabled())
			{
				nameBuilder.append('!');
			}
			nameBuilder.append(perm.getDescription().getVersion());
		}
	}

	private static void addBPermissionsVersion(final Server server, final StringBuilder nameBuilder)
	{
		final Plugin bperm = server.getPluginManager().getPlugin("bPermissions");
		if (bperm != null)
		{
			nameBuilder.append(" BP");
			if (!bperm.isEnabled())
			{
				nameBuilder.append('!');
			}
			nameBuilder.append(bperm.getDescription().getVersion());
		}
	}

	private static void addPermissionsBukkitVersion(final Server server, final StringBuilder nameBuilder)
	{
		final Plugin permb = server.getPluginManager().getPlugin("PermissionsBukkit");
		if (permb != null)
		{
			nameBuilder.append(" PB");
			if (!permb.isEnabled())
			{
				nameBuilder.append('!');
			}
			nameBuilder.append(permb.getDescription().getVersion());
		}
	}

	private static void addPermissionsExVersion(final Server server, final StringBuilder nameBuilder)
	{
		final Plugin pex = server.getPluginManager().getPlugin("PermissionsEx");
		if (pex != null)
		{
			nameBuilder.append(" PEX");
			if (!pex.isEnabled())
			{
				nameBuilder.append('!');
			}
			nameBuilder.append(pex.getDescription().getVersion());
		}
	}

	private static void addGroupManagerVersion(final Server server, final StringBuilder nameBuilder)
	{
		final Plugin groupManager = server.getPluginManager().getPlugin("GroupManager");
		if (groupManager != null)
		{
			nameBuilder.append(" GM");
			if (!groupManager.isEnabled())
			{
				nameBuilder.append('!');
			}
		}
	}

	private static void addEssentialsVersion(final Server server, final StringBuilder nameBuilder)
	{
		final Plugin essentials = server.getPluginManager().getPlugin("Essentials");
		if (essentials != null)
		{
			nameBuilder.append(" ESS");
			nameBuilder.append(essentials.getDescription().getVersion());
		}
	}

	private static void addCraftBukkitVersion(final Server server, final StringBuilder nameBuilder)
	{
		final Matcher versionMatch = CB_PATTERN.matcher(server.getVersion());
		if (versionMatch.matches())
		{
			nameBuilder.append(" CB");
			nameBuilder.append(versionMatch.group(4));
		}
	}
}