Merge pull request #44 from TBMCPlugins/dev

Update 2
This commit is contained in:
Norbi Peti 2018-06-15 18:39:24 +02:00 committed by GitHub
commit 6cf28e5c9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 136 additions and 66 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"> <component name="libraryTable">
<library name="Maven: net.md-5:bungeecord-chat:1.12-SNAPSHOT"> <library name="Maven: net.md-5:bungeecord-chat:1.12-SNAPSHOT">
<CLASSES> <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> </CLASSES>
<JAVADOC> <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> </JAVADOC>
<SOURCES> <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> </SOURCES>
</library> </library>
</component> </component>

View file

@ -13,6 +13,7 @@
<orderEntry type="sourceFolder" forTests="false" /> <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="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: 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: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" 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) @EventHandler(priority = EventPriority.HIGHEST)
public void onSystemChat(TBMCSystemChatEvent event) { public void onSystemChat(TBMCSystemChatEvent event) {
if (event.isHandled()) if (event.isHandled())
return; // Only handle here if ButtonChat couldn't return; // Only handle here if ButtonChat couldn't
Bukkit.getOnlinePlayers().stream().filter(p -> event.shouldSendTo(p)) Bukkit.getOnlinePlayers().stream().filter(p -> event.shouldSendTo(p))
.forEach(p -> p.sendMessage(event.getChannel().DisplayName.substring(0, 2) + event.getMessage())); .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 buttondevteam.lib.chat.Channel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; 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 * 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> * 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 * It's not meant to be called from any plugin - it should be only created to use the helper methods
* and the groups are towns/nations.
*/ */
@Getter @Getter
public class TBMCChannelConnectEvent extends TBMCChatEventBase implements Cancellable { public class TBMCChannelConnectFakeEvent extends TBMCChatEventBase implements Cancellable {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
@Nullable @Nullable
private final CommandSender sender; private final CommandSender sender;
@Nullable
@Setter
private String groupid; //Town name etc.
/** /**
* Using this the group will be determined based on the sender. * 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 sender The sender to get the group from
* @param channel The channel to use * @param channel The channel to use
*/ */
public TBMCChannelConnectEvent(CommandSender sender, Channel channel) { public TBMCChannelConnectFakeEvent(CommandSender sender, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1); super(channel, "Channel connecting message. One of the things users should never see in action.", -1, null);
this.sender = sender; 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 groupid The group to use, for example the name of a town or nation
* @param channel The channel to use * @param channel The channel to use
*/ */
public TBMCChannelConnectEvent(String groupid, Channel channel) { public TBMCChannelConnectFakeEvent(String groupid, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1); super(channel, "Channel connecting message. One of the things users should never see in action.", -1, groupid);
this.groupid = groupid;
this.sender = null; this.sender = null;
} }

View file

@ -5,6 +5,8 @@ import lombok.Getter;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import javax.annotation.Nullable;
/** /**
* Make sure to only send the message to users where {@link #shouldSendTo(CommandSender)} returns true. * Make sure to only send the message to users where {@link #shouldSendTo(CommandSender)} returns true.
* *
@ -13,19 +15,59 @@ import org.bukkit.event.HandlerList;
*/ */
@Getter @Getter
public class TBMCChatEvent extends TBMCChatEventBase { public class TBMCChatEvent extends TBMCChatEventBase {
public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score, boolean fromcmd) { public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score, boolean fromcmd, String groupid) {
super(channel, message, score); super(channel, message, score, groupid);
this.sender = sender; this.sender = sender;
this.fromcmd = fromcmd; this.fromcmd = fromcmd;
} this.ignoreSenderPermissions = false;
}
public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score, boolean fromcmd, String groupid, boolean ignoreSenderPermissions) {
super(channel, message, score, groupid);
this.sender = sender;
this.fromcmd = fromcmd;
this.ignoreSenderPermissions = ignoreSenderPermissions;
}
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private CommandSender sender; private CommandSender sender;
private boolean fromcmd; private boolean fromcmd;
private final boolean ignoreSenderPermissions;
// TODO: Message object with data? // TODO: Message object with data?
@Override /**
* This will allow the sender of the message if {@link #isIgnoreSenderPermissions()} is true.
*/
@Override
public boolean shouldSendTo(CommandSender sender) {
if (isIgnoreSenderPermissions() && sender.equals(this.sender))
return true; //Allow sending the message no matter what
return super.shouldSendTo(sender);
}
/**
* This will allow the sender of the message if {@link #isIgnoreSenderPermissions()} is true.
*/
@Override
public int getMCScore(CommandSender sender) {
if (isIgnoreSenderPermissions() && sender.equals(this.sender))
return getScore(); //Send in the correct group no matter what
return super.getMCScore(sender);
}
/**
* This will allow the sender of the message if {@link #isIgnoreSenderPermissions()} is true.
*/
@Nullable
@Override
public String getGroupID(CommandSender sender) {
if (isIgnoreSenderPermissions() && sender.equals(this.sender))
return getGroupID(); //Send in the correct group no matter what
return super.getGroupID(sender);
}
@Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;
} }

View file

@ -10,13 +10,22 @@ import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import javax.annotation.Nullable;
@Getter @Getter
@RequiredArgsConstructor @RequiredArgsConstructor
public abstract class TBMCChatEventBase extends Event implements Cancellable { public abstract class TBMCChatEventBase extends Event implements Cancellable {
private final Channel channel; private final Channel channel;
private @NonNull String message; private @NonNull String message;
private @Setter boolean cancelled; 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 * Note: Errors are sent to the sender automatically
@ -31,10 +40,23 @@ public abstract class TBMCChatEventBase extends Event implements Cancellable {
/** /**
* Note: Errors are sent to the sender automatically * Note: Errors are sent to the sender automatically
*/ */
public int getMCScore(CommandSender sender) { public int getMCScore(CommandSender sender) {
if (channel.filteranderrormsg == null) if (channel.filteranderrormsg == null)
return 0; return 0;
RecipientTestResult result = channel.filteranderrormsg.apply(sender); RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null ? result.score : -1; return result.errormessage == null ? result.score : -1;
} }
/**
* Note: Errors are sent to the sender automatically<br>
*
* Null means don't send
*/
@Nullable
public String getGroupID(CommandSender sender) {
if (channel.filteranderrormsg == null)
return "everyone";
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; handled = true;
} }
public TBMCSystemChatEvent(Channel channel, String message, int score) { // TODO: RIch message public TBMCSystemChatEvent(Channel channel, String message, int score, String groupid) { // TODO: Rich message
super(channel, message, score); super(channel, message, score, groupid);
} }
private static final HandlerList handlers = new HandlerList(); 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, public static Function<CommandSender, RecipientTestResult> noScoreResult(Predicate<CommandSender> filter,
String errormsg) { 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( public static <T extends Channel> BiFunction<T, CommandSender, RecipientTestResult> noScoreResult(
BiPredicate<T, CommandSender> filter, String errormsg) { 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; public static Channel GlobalChat;
@ -97,6 +97,7 @@ public class Channel {
public static class RecipientTestResult { public static class RecipientTestResult {
public String errormessage = null; public String errormessage = null;
public int score = -1; // Anything below 0 is "never send" public int score = -1; // Anything below 0 is "never send"
public String groupID = null;
/** /**
* Creates a result that indicates an <b>error</b> * 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> * 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 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.score = score;
this.groupID = groupID;
} }
} }
} }

View file

@ -15,11 +15,11 @@ public class ChatRoom extends Channel {
public void joinRoom(CommandSender sender) { public void joinRoom(CommandSender sender) {
usersInRoom.add(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) { public void leaveRoom(CommandSender sender) {
usersInRoom.remove(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 channel The channel to send to
* @param sender The sender to send from * @param sender The sender to send from
@ -230,20 +231,44 @@ public class TBMCChatAPI {
* @return The event cancelled state * @return The event cancelled state
*/ */
public static boolean SendChatMessage(Channel channel, CommandSender sender, String message, boolean fromcommand) { public static boolean SendChatMessage(Channel channel, CommandSender sender, String message, boolean fromcommand) {
return sendChatMessageInternal(channel, sender, message, fromcommand, sender);
}
private static boolean sendChatMessageInternal(Channel channel, CommandSender sender, String message, boolean fromcommand, CommandSender permcheck) {
if (!Channel.getChannels().contains(channel)) if (!Channel.getChannels().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!"); throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
RecipientTestResult rtr = getScoreOrSendError(channel, permcheck);
int score = rtr.score;
if (score == -1 || rtr.groupID == null)
return true;
TBMCChatPreprocessEvent eventPre = new TBMCChatPreprocessEvent(sender, channel, message); TBMCChatPreprocessEvent eventPre = new TBMCChatPreprocessEvent(sender, channel, message);
Bukkit.getPluginManager().callEvent(eventPre); Bukkit.getPluginManager().callEvent(eventPre);
if (eventPre.isCancelled()) if (eventPre.isCancelled())
return true; return true;
int score = getScoreOrSendError(channel, sender); TBMCChatEvent event;
if (score == -1) if (permcheck == sender)
return true; event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID);
TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand); else
event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID, true);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return event.isCancelled(); return event.isCancelled();
} }
/**
* Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.<br>
* This will not check if the sender has permission.
*
* @param channel The channel to send to
* @param sender The sender to send from
* @param message The message to send
* @param fromcommand Whether this message comes from running a command, used to determine whether to delete Discord messages for example
* @param permcheck The sender to check permissions
* @return The event cancelled state
*/
public static boolean SendChatMessageDontCheckSender(Channel channel, CommandSender sender, String message, boolean fromcommand, CommandSender permcheck) {
return sendChatMessageInternal(channel, sender, message, fromcommand, permcheck);
}
/** /**
* Sends a regular message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}. * Sends a regular message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.
* *
@ -255,27 +280,23 @@ public class TBMCChatAPI {
* The message to send * The message to send
* @return The event cancelled state * @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)) if (!Channel.getChannels().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!"); 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); Bukkit.getPluginManager().callEvent(event);
return event.isCancelled(); return event.isCancelled();
} }
private static int getScoreOrSendError(Channel channel, CommandSender sender) { private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) {
int score;
if (channel.filteranderrormsg == null) if (channel.filteranderrormsg == null)
score = 0; return new RecipientTestResult(0, "everyone");
else { else {
RecipientTestResult result = channel.filteranderrormsg.apply(sender); RecipientTestResult result = channel.filteranderrormsg.apply(sender);
if (result.errormessage != null) { if (result.errormessage != null)
sender.sendMessage("§c" + result.errormessage); sender.sendMessage("§c" + result.errormessage);
return -1; return result;
}
score = result.score;
} }
return score;
} }
/** /**