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">
<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

@ -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

@ -5,6 +5,8 @@ import lombok.Getter;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import javax.annotation.Nullable;
/**
* Make sure to only send the message to users where {@link #shouldSendTo(CommandSender)} returns true.
*
@ -13,18 +15,58 @@ 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;
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 CommandSender sender;
private boolean fromcmd;
private final boolean ignoreSenderPermissions;
// TODO: Message object with data?
/**
* 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() {
return handlers;

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;
/**
* 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
@ -37,4 +46,17 @@ public abstract class TBMCChatEventBase extends Event implements Cancellable {
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
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;
}
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
@ -230,20 +231,44 @@ public class TBMCChatAPI {
* @return The event cancelled state
*/
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))
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);
Bukkit.getPluginManager().callEvent(eventPre);
if (eventPre.isCancelled())
return true;
int score = getScoreOrSendError(channel, sender);
if (score == -1)
return true;
TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand);
TBMCChatEvent event;
if (permcheck == sender)
event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID);
else
event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID, true);
Bukkit.getPluginManager().callEvent(event);
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)}.
*
@ -255,27 +280,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;
return result;
}
score = result.score;
}
return score;
}
/**