Chat channels probably done, needs testing

This commit is contained in:
Norbi Peti 2017-05-06 19:55:04 +02:00
parent 9e1bd49cd0
commit 07fccda8d3
4 changed files with 78 additions and 31 deletions

View file

@ -2,10 +2,11 @@ package buttondevteam.chat;
import org.bukkit.Bukkit;
public class AnnouncerThread {
public class AnnouncerThread implements Runnable {
private static int AnnounceMessageIndex = 0;
public static void Run() {
@Override
public void run() {
while (!PluginMain.Instance.stop) {
try {
Thread.sleep(PluginMain.AnnounceTime);

View file

@ -13,17 +13,12 @@ import org.bukkit.scoreboard.Objective;
import com.earth2me.essentials.Essentials;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import buttondevteam.chat.commands.UnlolCommand;
import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
import buttondevteam.chat.formatting.*;
import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
import buttondevteam.lib.chat.TellrawSerializableEnum;
import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerBase;
@ -83,8 +78,10 @@ public class ChatProcessing {
.registerTypeAdapter(boolean.class, new TellrawSerializer.TwBool()).disableHtmlEscaping().create();
}
// Returns e.setCancelled for custom event
public static boolean ProcessChat(Channel channel, CommandSender sender, String message) {
public static boolean ProcessChat(TBMCChatEvent e) {
Channel channel = e.getChannel();
CommandSender sender = e.getSender();
String message = e.getMessage();
long processstart = System.nanoTime();
if (PluginMain.essentials == null)
PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials"));
@ -127,28 +124,20 @@ public class ChatProcessing {
try {
if (channel.filteranderrormsg != null) {
Objective obj = PluginMain.SB.getObjective(channel.ID);
RecipientTestResult result = channel.filteranderrormsg.apply(player);
if (result.errormessage != null)
player.sendMessage("§c" + result.errormessage);
else
for (Player p : Bukkit.getOnlinePlayers()) {
if (p == player)
continue;
result = channel.filteranderrormsg.apply(p);
if (result.errormessage == null)
obj.getScore(p.getName()).setScore(result.score);
else
obj.getScore(p.getName()).setScore(-1);
}
PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console,
String.format("tellraw @a[score_%s=%d,score_%s_min=%d] %s", channel.ID, result.score, jsonstr));
int score;
obj.getScore(player.getUniqueId().toString()).setScore(score = e.getMCScore(player));
for (Player p : Bukkit.getOnlinePlayers()) {
if (player == p)
continue;
obj.getScore(p.getUniqueId().toString()).setScore(e.getMCScore(p));
}
PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String.format(
"tellraw @a[score_%s=%d,score_%s_min=%d] %s", channel.ID, score, channel.ID, score, jsonstr));
} else
PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console,
String.format("tellraw @a %s", jsonstr));
} catch (
Exception e) {
TBMCCoreAPI.SendException("An error occured while sending a chat message!", e);
} catch (Exception ex) {
TBMCCoreAPI.SendException("An error occured while sending a chat message!", ex);
player.sendMessage("§cAn error occured while sending the message.");
return true;
}

View file

@ -5,6 +5,7 @@ import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
@ -17,7 +18,10 @@ import org.htmlcleaner.TagNode;
import buttondevteam.chat.commands.YeehawCommand;
import buttondevteam.chat.listener.PlayerListener;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
import buttondevteam.lib.player.TBMCPlayerBase;
import com.earth2me.essentials.Essentials;
@ -25,7 +29,9 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
@ -54,6 +60,9 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
public static ArrayList<Town> Towns;
public static ArrayList<Nation> Nations;
public static Channel TownChat;
public static Channel NationChat;
/**
* <p>
* This variable is used as a cache for flair state checking when reading the flair thread.
@ -79,12 +88,17 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
Towns = new ArrayList<Town>(TU.getTownsMap().values()); // Creates a snapshot of towns, new towns will be added when needed
Nations = new ArrayList<Nation>(TU.getNationsMap().values()); // Same here but with nations
TBMCChatAPI.RegisterChatChannel(
TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false)));
TBMCChatAPI.RegisterChatChannel(
NationChat = new Channel("§6NC§f", Color.Gold, "nc", s -> checkTownNationChat(s, true)));
setupChat();
setupEconomy();
setupPermissions();
new Thread(() -> FlairGetterThreadMethod()).start();
new Thread(() -> AnnouncerThread.Run()).start();
new Thread(new AnnouncerThread()).start();
}
public Boolean stop = false;
@ -306,4 +320,47 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
return (economy != null);
}
/**
* Return the error message for the message sender if they can't send it and the score
*/
private static RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) {
if (!(sender instanceof Player))
return new RecipientTestResult("§cYou are not a player!");
try {
Resident resident = PluginMain.TU.getResidentMap().get(sender.getName().toLowerCase());
if (resident != null && resident.getModes().contains("spy"))
return null;
/*
* p.sendMessage(String.format("[SPY-%s] - %s: %s", channel.DisplayName, ((Player) sender).getDisplayName(), message));
*/
Town town = null;
if (resident != null && resident.hasTown())
town = resident.getTown();
if (town == null)
return new RecipientTestResult("You aren't in a town.");
Nation nation = null;
int index = -1;
if (nationchat) {
if (town.hasNation())
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;
}
}
return new RecipientTestResult(index);
} catch (NotRegisteredException e) {
return new RecipientTestResult("You (probably) aren't knwon by Towny! (Not in a town)");
}
}
}

View file

@ -274,7 +274,7 @@ public class PlayerListener implements Listener {
try {
if (e.isCancelled())
return;
e.setCancelled(ChatProcessing.ProcessChat(e.getChannel(), e.getSender(), e.getMessage()));
e.setCancelled(ChatProcessing.ProcessChat(e));
} catch (Exception ex) {
for (Player p : Bukkit.getOnlinePlayers())
p.sendMessage("§c!§r["