Towny event broadcasting to Discord and masked links #96
4 changed files with 498 additions and 428 deletions
|
@ -257,14 +257,12 @@ public class ChatProcessing {
|
|||
namesb.append(")");
|
||||
StringBuilder nicksb = new StringBuilder("(?i)(");
|
||||
boolean addNickFormatter = false;
|
||||
int index = 0;
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
final String nick = PlayerListener.nicknames.inverse().get(p.getUniqueId());
|
||||
if (nick != null) {
|
||||
nicksb.append(nick).append("|");
|
||||
addNickFormatter = true; //Add it even if there's only 1 player online (it was in the if)
|
||||
}
|
||||
index++;
|
||||
}
|
||||
nicksb.deleteCharAt(nicksb.length() - 1);
|
||||
nicksb.append(")");
|
||||
|
|
|
@ -58,8 +58,8 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
|
||||
public static Scoreboard SB;
|
||||
public static TownyUniverse TU;
|
||||
private static ArrayList<Town> Towns;
|
||||
private static ArrayList<Nation> Nations;
|
||||
private static ArrayList<String> Towns;
|
||||
private static ArrayList<String> Nations;
|
||||
|
||||
public static Channel TownChat;
|
||||
public static Channel NationChat;
|
||||
|
@ -90,8 +90,8 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
|
||||
SB = getServer().getScoreboardManager().getMainScoreboard(); // Main can be detected with @a[score_...]
|
||||
TU = ((Towny) Bukkit.getPluginManager().getPlugin("Towny")).getTownyUniverse();
|
||||
Towns = new ArrayList<>(TU.getTownsMap().values()); // Creates a snapshot of towns, new towns will be added when needed
|
||||
Nations = new ArrayList<>(TU.getNationsMap().values()); // Same here but with nations
|
||||
Towns = TU.getTownsMap().values().stream().map(Town::getName).collect(Collectors.toCollection(ArrayList::new)); // Creates a snapshot of towns, new towns will be added when needed
|
||||
Nations = TU.getNationsMap().values().stream().map(Nation::getName).collect(Collectors.toCollection(ArrayList::new)); // Same here but with nations
|
||||
|
||||
TownColors.keySet().removeIf(t -> !TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns
|
||||
NationColor.keySet().removeIf(n -> !TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations
|
||||
|
@ -406,21 +406,22 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
nation = town.getNation();
|
||||
if (nation == null)
|
||||
return new RecipientTestResult("Your town isn't in a nation.");
|
||||
index = PluginMain.Nations.indexOf(nation);
|
||||
if (index < 0) {
|
||||
PluginMain.Nations.add(nation);
|
||||
index = PluginMain.Nations.size() - 1;
|
||||
}
|
||||
} else {
|
||||
index = PluginMain.Towns.indexOf(town);
|
||||
if (index < 0) {
|
||||
PluginMain.Towns.add(town);
|
||||
index = PluginMain.Towns.size() - 1;
|
||||
}
|
||||
}
|
||||
index = getTownNationIndex(nation.getName(), true);
|
||||
} else
|
||||
index = getTownNationIndex(town.getName(), false);
|
||||
return new RecipientTestResult(index, nationchat ? nation.getName() : town.getName());
|
||||
} catch (NotRegisteredException e) {
|
||||
return new RecipientTestResult("You (probably) aren't knwon by Towny! (Not in a town)");
|
||||
}
|
||||
}
|
||||
|
||||
public static int getTownNationIndex(String name, boolean nation) {
|
||||
val list = nation ? Nations : Towns;
|
||||
int index = list.indexOf(name);
|
||||
if (index < 0) {
|
||||
list.add(name);
|
||||
index = list.size() - 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
package buttondevteam.chat.components;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import com.palmergames.bukkit.towny.TownyLogger;
|
||||
import lombok.val;
|
||||
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class TownyAnnouncer {
|
||||
private static final Pattern LOG_TYPE_PATTERN = Pattern.compile("\\[(\\w+ (?:Msg|Message))] (\\w+):");
|
||||
private static final Handler HANDLER = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
if (logRecord.getMessage() == null) return;
|
||||
val m = LOG_TYPE_PATTERN.matcher(logRecord.getMessage());
|
||||
if (!m.find()) return;
|
||||
String groupID = m.group(2); //The group ID is correctly cased
|
||||
switch (String.valueOf(m.group(1))) { //valueOf: Handles null
|
||||
case "Town":
|
||||
TBMCChatAPI.SendSystemMessage(PluginMain.TownChat,
|
||||
new Channel.RecipientTestResult(PluginMain.getTownNationIndex(groupID, false), groupID),
|
||||
logRecord.getMessage()); //TODO: This will also send it in Minecraft
|
||||
break;
|
||||
case "Nation":
|
||||
TBMCChatAPI.SendSystemMessage(PluginMain.NationChat,
|
||||
new Channel.RecipientTestResult(PluginMain.getTownNationIndex(groupID, true), groupID),
|
||||
logRecord.getMessage()); //TODO: This will also send it in Minecraft
|
||||
break;
|
||||
case "Global": //TODO
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
public static void setup() {
|
||||
TownyLogger.log.addHandler(HANDLER);
|
||||
}
|
||||
|
||||
public static void setdown() {
|
||||
TownyLogger.log.removeHandler(HANDLER);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package buttondevteam.chat.components;
|
||||
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
|
||||
public class TownyComponent extends Component { //TODO: Register component
|
||||
@Override
|
||||
protected void enable() {
|
||||
TownyAnnouncer.setup();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disable() {
|
||||
TownyAnnouncer.setdown();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue