Chat channels probably done, needs testing

This commit is contained in:
Norbi Peti 2017-05-06 19:55:21 +02:00
parent 308006d624
commit 9a61047cd2
3 changed files with 47 additions and 52 deletions

View file

@ -6,7 +6,14 @@ import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
/**
* Make sure to only send the message to users who {@link #shouldSendTo(CommandSender)} returns true.
*
* @author NorbiPeti
*
*/
public class TBMCChatEvent extends Event implements Cancellable { public class TBMCChatEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
@ -14,11 +21,13 @@ public class TBMCChatEvent extends Event implements Cancellable {
private CommandSender sender; private CommandSender sender;
private String message; private String message;
private boolean cancelled; private boolean cancelled;
private int score;
public TBMCChatEvent(CommandSender sender, Channel channel, String message) { public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score) {
this.sender = sender; this.sender = sender;
this.channel = channel; this.channel = channel;
this.message = message; // TODO: Message object with data? this.message = message; // TODO: Message object with data?
this.score = score;
} }
/* /*
@ -56,4 +65,23 @@ public class TBMCChatEvent extends Event implements Cancellable {
this.cancelled = cancelled; this.cancelled = cancelled;
} }
/**
* Note: Errors are sent to the sender automatically
*/
public boolean shouldSendTo(CommandSender sender) {
if (channel.filteranderrormsg == null)
return true;
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null && score == result.score;
}
/**
* Note: Errors are sent to the sender automatically
*/
public int getMCScore(CommandSender sender) {
if (channel.filteranderrormsg == null)
return 0;
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null ? result.score : -1;
}
} }

View file

@ -35,7 +35,7 @@ public class Channel {
* @param command * @param command
* The command to be used for the channel <i>without /</i>. For example "mod". It's also used for scoreboard objective names. * The command to be used for the channel <i>without /</i>. For example "mod". It's also used for scoreboard objective names.
* @param filteranderrormsg * @param filteranderrormsg
* Checks all senders against the criteria provided here and sends the message returned if it has an index - otherwise displays the error.<br> * Checks all senders against the criteria provided here and sends the message if the index matches the sender's - if no score at all, displays the error.<br>
* May be null to send to everyone. * May be null to send to everyone.
*/ */
public Channel(String displayname, Color color, String command, public Channel(String displayname, Color color, String command,
@ -48,8 +48,6 @@ public class Channel {
static { static {
RegisterChannel(GlobalChat = new Channel("§fg§f", Color.White, "g", null)); RegisterChannel(GlobalChat = new Channel("§fg§f", Color.White, "g", null));
RegisterChannel(TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false)));
RegisterChannel(NationChat = new Channel("§6NC§f", Color.Gold, "nc", s -> checkTownNationChat(s, true)));
RegisterChannel(AdminChat = new Channel("§cADMIN§f", Color.Red, "a", s -> s.isOp() ? new RecipientTestResult(0) RegisterChannel(AdminChat = new Channel("§cADMIN§f", Color.Red, "a", s -> s.isOp() ? new RecipientTestResult(0)
: new RecipientTestResult("You need to be an admin to use this channel."))); : new RecipientTestResult("You need to be an admin to use this channel.")));
RegisterChannel(ModChat = new Channel("§9MOD§f", Color.Blue, "mod", RegisterChannel(ModChat = new Channel("§9MOD§f", Color.Blue, "mod",
@ -63,8 +61,6 @@ public class Channel {
} }
public static Channel GlobalChat; public static Channel GlobalChat;
public static Channel TownChat;
public static Channel NationChat;
public static Channel AdminChat; public static Channel AdminChat;
public static Channel ModChat; public static Channel ModChat;
@ -73,49 +69,6 @@ public class Channel {
Bukkit.getPluginManager().callEvent(new ChatChannelRegisterEvent(channel)); Bukkit.getPluginManager().callEvent(new ChatChannelRegisterEvent(channel));
} }
/**
* 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 = MainPlugin.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; // TODO: Move all this to the event in ButtonChat along with the channel definitions and make scores only for the sender...
if (nationchat) {
if (town.hasNation())
nation = town.getNation();
if (nation == null)
return new RecipientTestResult("Your town isn't in a nation.");
index = MainPlugin.Nations.indexOf(nation);
if (index < 0) {
MainPlugin.Nations.add(nation);
index = MainPlugin.Nations.size() - 1;
}
} else {
index = MainPlugin.Towns.indexOf(town);
if (index < 0) {
MainPlugin.Towns.add(town);
index = MainPlugin.Towns.size() - 1;
}
}
return new RecipientTestResult(index);
} catch (NotRegisteredException e) {
return new RecipientTestResult("You (probably) aren't knwon by Towny! (Not in a town)");
}
}
public static class RecipientTestResult { public static class RecipientTestResult {
public String errormessage; public String errormessage;
public int score; public int score;
@ -138,6 +91,6 @@ public class Channel {
*/ */
public RecipientTestResult(int score) { public RecipientTestResult(int score) {
this.score = score; this.score = score;
} // TODO: Set score of players, if they get one, otherwise (on error) set score to -1 }
} }
} }

View file

@ -20,6 +20,7 @@ import buttondevteam.core.CommandCaller;
import buttondevteam.core.MainPlugin; import buttondevteam.core.MainPlugin;
import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
public class TBMCChatAPI { public class TBMCChatAPI {
@ -200,7 +201,7 @@ public class TBMCChatAPI {
} }
/** /**
* Sends a chat message to Minecraft * Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.
* *
* @param channel * @param channel
* The channel to send to * The channel to send to
@ -211,7 +212,20 @@ public class TBMCChatAPI {
* @return The event cancelled state * @return The event cancelled state
*/ */
public static boolean SendChatMessage(Channel channel, CommandSender sender, String message) { public static boolean SendChatMessage(Channel channel, CommandSender sender, String message) {
TBMCChatEvent event = new TBMCChatEvent(sender, channel, message); if (!Channel.getChannels().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
int score;
if (channel.filteranderrormsg == null)
score = -1;
else {
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
if (result.errormessage != null) {
sender.sendMessage("§c" + result.errormessage);
return true;
}
score = result.score;
}
TBMCChatEvent event = new TBMCChatEvent(sender, channel, message, score);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return event.isCancelled(); return event.isCancelled();
} }