Channel connect fixes

Group ID is now required
This commit is contained in:
Norbi Peti 2018-06-08 18:51:37 +02:00
parent c94eaebf5a
commit a691278ea9
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
11 changed files with 66 additions and 63 deletions

View file

@ -1,13 +0,0 @@
<component name="libraryTable">
<library name="Maven: com.github.TBMCPlugins.ButtonCore:Towny:master-v1.0-g4519d5f-237">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/github/TBMCPlugins/ButtonCore/Towny/master-v1.0-g4519d5f-237/Towny-master-v1.0-g4519d5f-237.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/github/TBMCPlugins/ButtonCore/Towny/master-v1.0-g4519d5f-237/Towny-master-v1.0-g4519d5f-237-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/github/TBMCPlugins/ButtonCore/Towny/master-v1.0-g4519d5f-237/Towny-master-v1.0-g4519d5f-237-sources.jar!/" />
</SOURCES>
</library>
</component>

View file

@ -1,13 +1,13 @@
<component name="libraryTable">
<library name="Maven: net.md-5:bungeecord-chat:1.12-SNAPSHOT">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.12-SNAPSHOT/bungeecord-chat-1.12-20180513.014306-84.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.12-SNAPSHOT/bungeecord-chat-1.12-20180607.053457-86.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.12-SNAPSHOT/bungeecord-chat-1.12-20180513.014306-84-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.12-SNAPSHOT/bungeecord-chat-1.12-20180607.053457-86-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.12-SNAPSHOT/bungeecord-chat-1.12-20180513.014306-84-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.12-SNAPSHOT/bungeecord-chat-1.12-20180607.053457-86-sources.jar!/" />
</SOURCES>
</library>
</component>

View file

@ -13,6 +13,7 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />

View file

@ -51,9 +51,9 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onSystemChat(TBMCSystemChatEvent event) {
if (event.isHandled())
return; // Only handle here if ButtonChat couldn't
Bukkit.getOnlinePlayers().stream().filter(p -> event.shouldSendTo(p))
.forEach(p -> p.sendMessage(event.getChannel().DisplayName.substring(0, 2) + event.getMessage()));
}
if (event.isHandled())
return; // Only handle here if ButtonChat couldn't
Bukkit.getOnlinePlayers().stream().filter(p -> event.shouldSendTo(p))
.forEach(p -> p.sendMessage(event.getChannel().DisplayName.substring(0, 2) + event.getMessage()));
}
}

View file

@ -2,7 +2,6 @@ package buttondevteam.lib;
import buttondevteam.lib.chat.Channel;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
@ -12,18 +11,14 @@ import javax.annotation.Nullable;
/**
* The purpose of this event is to determine which group the given channel belongs to
* or to validate that they have access to the given group chat.<br>
* It's mainly meant to be called from DiscordPlugin and listened for in ButtonChat
* and the groups are towns/nations.
* It's not meant to be called from any plugin - it should be only created to use the helper methods
*/
@Getter
public class TBMCChannelConnectEvent extends TBMCChatEventBase implements Cancellable {
public class TBMCChannelConnectFakeEvent extends TBMCChatEventBase implements Cancellable {
private static final HandlerList handlers = new HandlerList();
@Nullable
private final CommandSender sender;
@Nullable
@Setter
private String groupid; //Town name etc.
/**
* Using this the group will be determined based on the sender.
@ -31,8 +26,8 @@ public class TBMCChannelConnectEvent extends TBMCChatEventBase implements Cancel
* @param sender The sender to get the group from
* @param channel The channel to use
*/
public TBMCChannelConnectEvent(CommandSender sender, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1);
public TBMCChannelConnectFakeEvent(CommandSender sender, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1, null);
this.sender = sender;
}
@ -42,9 +37,8 @@ public class TBMCChannelConnectEvent extends TBMCChatEventBase implements Cancel
* @param groupid The group to use, for example the name of a town or nation
* @param channel The channel to use
*/
public TBMCChannelConnectEvent(String groupid, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1);
this.groupid = groupid;
public TBMCChannelConnectFakeEvent(String groupid, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1, groupid);
this.sender = null;
}

View file

@ -13,8 +13,8 @@ import org.bukkit.event.HandlerList;
*/
@Getter
public class TBMCChatEvent extends TBMCChatEventBase {
public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score, boolean fromcmd) {
super(channel, message, score);
public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score, boolean fromcmd, String groupid) {
super(channel, message, score, groupid);
this.sender = sender;
this.fromcmd = fromcmd;
}

View file

@ -10,13 +10,22 @@ import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import javax.annotation.Nullable;
@Getter
@RequiredArgsConstructor
public abstract class TBMCChatEventBase extends Event implements Cancellable {
private final Channel channel;
private @NonNull String message;
private @Setter boolean cancelled;
private final int score;
/**
* The sender's score.
*/
private final int score;
/**
* The sender's group ID.
*/
private final String groupID;
/**
* Note: Errors are sent to the sender automatically
@ -31,10 +40,21 @@ public abstract class TBMCChatEventBase extends Event implements Cancellable {
/**
* 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;
}
public int getMCScore(CommandSender sender) {
if (channel.filteranderrormsg == null)
return 0;
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null ? result.score : -1;
}
/**
* Note: Errors are sent to the sender automatically
*/
@Nullable
public String getGroupID(CommandSender sender) {
if (channel.filteranderrormsg == null)
return null;
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null ? result.groupID : null;
}
}

View file

@ -19,8 +19,8 @@ public class TBMCSystemChatEvent extends TBMCChatEventBase {
handled = true;
}
public TBMCSystemChatEvent(Channel channel, String message, int score) { // TODO: RIch message
super(channel, message, score);
public TBMCSystemChatEvent(Channel channel, String message, int score, String groupid) { // TODO: Rich message
super(channel, message, score, groupid);
}
private static final HandlerList handlers = new HandlerList();

View file

@ -77,12 +77,12 @@ public class Channel {
public static Function<CommandSender, RecipientTestResult> noScoreResult(Predicate<CommandSender> filter,
String errormsg) {
return s -> filter.test(s) ? new RecipientTestResult(0) : new RecipientTestResult(errormsg);
return s -> filter.test(s) ? new RecipientTestResult(0, "everyone") : new RecipientTestResult(errormsg);
}
public static <T extends Channel> BiFunction<T, CommandSender, RecipientTestResult> noScoreResult(
BiPredicate<T, CommandSender> filter, String errormsg) {
return (this_, s) -> filter.test(this_, s) ? new RecipientTestResult(0) : new RecipientTestResult(errormsg);
return (this_, s) -> filter.test(this_, s) ? new RecipientTestResult(0, "everyone") : new RecipientTestResult(errormsg);
}
public static Channel GlobalChat;
@ -97,6 +97,7 @@ public class Channel {
public static class RecipientTestResult {
public String errormessage = null;
public int score = -1; // Anything below 0 is "never send"
public String groupID = null;
/**
* Creates a result that indicates an <b>error</b>
@ -111,9 +112,11 @@ public class Channel {
* Creates a result that indicates a <b>success</b>
*
* @param score The score that identifies the target group. For example, the index of the town or nation to send to.
* @param groupID The ID of the target group.
*/
public RecipientTestResult(int score) {
public RecipientTestResult(int score, String groupID) {
this.score = score;
this.groupID = groupID;
}
}
}

View file

@ -15,11 +15,11 @@ public class ChatRoom extends Channel {
public void joinRoom(CommandSender sender) {
usersInRoom.add(sender);
TBMCChatAPI.SendSystemMessage(this, 0, sender.getName() + " joined the room");
TBMCChatAPI.SendSystemMessage(this, 0, "everyone", sender.getName() + " joined the room");
}
public void leaveRoom(CommandSender sender) {
usersInRoom.remove(sender);
TBMCChatAPI.SendSystemMessage(this, 0, sender.getName() + " left the room");
TBMCChatAPI.SendSystemMessage(this, 0, "everyone", sender.getName() + " left the room");
}
}

View file

@ -221,7 +221,8 @@ public class TBMCChatAPI {
}
/**
* Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.
* Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.<br>
* This will also send the error message to the sender, if they can't send the message.
*
* @param channel The channel to send to
* @param sender The sender to send from
@ -236,10 +237,11 @@ public class TBMCChatAPI {
Bukkit.getPluginManager().callEvent(eventPre);
if (eventPre.isCancelled())
return true;
int score = getScoreOrSendError(channel, sender);
if (score == -1)
RecipientTestResult rtr = getScoreOrSendError(channel, sender);
int score = rtr.score;
if (score == -1 || rtr.groupID == null)
return true;
TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand);
TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID);
Bukkit.getPluginManager().callEvent(event);
return event.isCancelled();
}
@ -255,27 +257,23 @@ public class TBMCChatAPI {
* The message to send
* @return The event cancelled state
*/
public static boolean SendSystemMessage(Channel channel, int score, String message) {
public static boolean SendSystemMessage(Channel channel, int score, String groupid, String message) {
if (!Channel.getChannels().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, score);
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, score, groupid);
Bukkit.getPluginManager().callEvent(event);
return event.isCancelled();
}
private static int getScoreOrSendError(Channel channel, CommandSender sender) {
int score;
private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) {
if (channel.filteranderrormsg == null)
score = 0;
return new RecipientTestResult(0, "everyone");
else {
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
if (result.errormessage != null) {
if (result.errormessage != null)
sender.sendMessage("§c" + result.errormessage);
return -1;
}
score = result.score;
return result;
}
return score;
}
/**