/
MMOChatTitle.java
113 lines (104 loc) · 3.57 KB
/
MMOChatTitle.java
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
/*
* This file is part of mmoChatTitle <http://github.com/mmoMinecraftDev/mmoChatTitle>.
*
* mmoChatTitle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mmo.Chat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import mmo.Core.ChatAPI.MMOChatEvent;
import mmo.Core.MMOPlugin.Support;
import mmo.Core.util.EnumBitSet;
import mmo.Core.MMO;
import mmo.Core.MMOPlugin;
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.getspout.spoutapi.player.SpoutPlayer;
public class MMOChatTitle extends MMOPlugin implements Listener {
public static boolean config_always_show = false;
public static int config_max_titles = 1;
public static String config_stop_prefix = "!!!";
public static List<String> config_default = new ArrayList<String>();
public static Map<String, String> default_perms = new LinkedHashMap<String, String>();
@Override
public EnumBitSet mmoSupport(EnumBitSet support) {
support.set(Support.MMO_AUTO_EXTRACT);
return support;
}
@Override
public void loadConfiguration(final FileConfiguration cfg) {
config_always_show = cfg.getBoolean("always_show", config_always_show);
config_max_titles = cfg.getInt("max_titles", config_max_titles);
config_stop_prefix = cfg.getString("stop_prefix", config_stop_prefix);
if (cfg.isList("default")) {
config_default = cfg.getStringList("default");
}
default_perms.clear();
for (String arg : config_default) {
String[] perm = arg.split("=");
if (perm.length == 2) {
default_perms.put(perm[0], perm[1]);
}
}
}
@Override
public void onEnable() {
super.onEnable();
pm.registerEvents(this, this);
}
@EventHandler (priority = EventPriority.HIGH)
public void onMMOChat(final MMOChatEvent event) {
if (config_always_show || event.hasFilter("Title")) {
final List<String> titles = new LinkedList<String>();
final Player from = event.getPlayer();
final Map<String, String> perms = new LinkedHashMap<String, String>(default_perms);
for (String arg : event.getArgs("Title")) {
final String[] perm = arg.split("=");
if (perm.length == 2) {
perms.put(perm[0], perm[1]);
}
}
for (String arg : perms.keySet()) {
boolean end = false;
if (from.hasPermission(arg)) {
String title = perms.get(arg);
if (!config_stop_prefix.isEmpty() && title.startsWith(config_stop_prefix)) {
end = true;
title = title.substring(config_stop_prefix.length()).trim();
}
if (!title.isEmpty()) {
titles.add(title);
}
if (config_max_titles > 0 && titles.size() >= config_max_titles) {
end = true;
}
}
if (end) {
break;
}
}
if (!titles.isEmpty()) {
for (Player to : event.getRecipients()) {
event.setFormat(to, event.getFormat(to).replaceAll("%2\\$s", MMO.join(titles, " ") + " %2\\$s"));
}
}
}
}
}