Started chat rooms and annotation processing, priority and precision and other fixes #36

Merged
NorbiPeti merged 5 commits from dev into master 2017-08-17 15:42:18 +00:00
13 changed files with 214 additions and 153 deletions
Showing only changes of commit 89d88ef952 - Show all commits

View file

@ -1 +0,0 @@
buttondevteam.lib.AnnotationProcessor

14
pom.xml
View file

@ -28,7 +28,6 @@
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
@ -138,6 +137,19 @@
<artifactId>mockito-core</artifactId>
<version>2.7.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TBMCPlugins</groupId>
<artifactId>ButtonProcessor</artifactId>
<version>master-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<organization>
<name>TBMCPlugins</name>

View file

@ -7,6 +7,7 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.ChatRoom;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.player.TBMCPlayerBase;
@ -35,11 +36,11 @@ public class MainPlugin extends JavaPlugin {
TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class);
TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fg§f", Color.White, "g", null));
TBMCChatAPI.RegisterChatChannel(
Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", Channel.filteranderrormsg(null)));
Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", Channel.inGroupFilter(null)));
TBMCChatAPI.RegisterChatChannel(
Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "mod", Channel.filteranderrormsg("mod")));
TBMCChatAPI
.RegisterChatChannel(new Channel("§6DEV§", Color.Gold, "dev", Channel.filteranderrormsg("developer")));
Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "mod", Channel.inGroupFilter("mod")));
TBMCChatAPI.RegisterChatChannel(new Channel("§6DEV§", Color.Gold, "dev", Channel.inGroupFilter("developer")));
TBMCChatAPI.RegisterChatChannel(new ChatRoom("§cRED", Color.DarkRed, "red"));
logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ") Test: " + Test + ".");
}

View file

@ -1,11 +1,13 @@
package buttondevteam.core;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.player.TBMCPlayerBase;
public class PlayerListener implements Listener {
@ -19,4 +21,12 @@ public class PlayerListener implements Listener {
public void OnPlayerLeave(PlayerQuitEvent event) {
TBMCPlayerBase.quitPlayer(event.getPlayer());
}
@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()));
}
}

View file

@ -1,33 +0,0 @@
package buttondevteam.lib;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import buttondevteam.lib.player.ChromaGamerEnforcer;
/** * A simple session bean type annotation processor. The implementation * is based on the standard annotation processing API in Java 6. */
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class AnnotationProcessor extends AbstractProcessor {
/** * Check if both @Stateful and @Stateless are present in an * session bean. If so, emits a warning message. */
@Override
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) { // TODO: SEparate JAR
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(ChromaGamerEnforcer.class);
for (Element element : elements) {
System.out.println("Processing " + element);
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
System.out.println("Annotations: " + annotationMirrors);
for (AnnotationMirror annotation : annotationMirrors) {
String type = annotation.getAnnotationType().toString();
System.out.println("Type: " + type);
}
}
return true; // claim the annotations
}
}

View file

@ -1,12 +1,10 @@
package buttondevteam.lib;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
import lombok.Getter;
/**
* Make sure to only send the message to users who {@link #shouldSendTo(CommandSender)} returns true.
@ -14,37 +12,17 @@ import buttondevteam.lib.chat.Channel.RecipientTestResult;
* @author NorbiPeti
*
*/
public class TBMCChatEvent extends Event implements Cancellable {
@Getter
public class TBMCChatEvent extends TBMCChatEventBase {
public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score) {
super(channel, message, score);
this.sender = sender;
}
private static final HandlerList handlers = new HandlerList();
private Channel channel;
private CommandSender sender;
private String message;
private boolean cancelled;
private int score;
public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score) {
this.sender = sender;
this.channel = channel;
this.message = message; // TODO: Message object with data?
this.score = score;
}
/*
* public TBMCPlayer getPlayer() { return TBMCPlayer.getPlayer(sender); // TODO: Get Chroma user }
*/
public Channel getChannel() {
return channel;
}
public CommandSender getSender() {
return sender;
}
public String getMessage() {
return message;
}
// TODO: Message object with data?
@Override
public HandlerList getHandlers() {
@ -54,34 +32,4 @@ public class TBMCChatEvent extends Event implements Cancellable {
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean 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

@ -0,0 +1,40 @@
package buttondevteam.lib;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@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;
/**
* 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

@ -3,6 +3,9 @@ package buttondevteam.lib;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* <p>
* This event gets called (ideally) each time an exception occurs in a TBMC plugin. To call it, use {@link TBMCCoreAPI#SendException(String, Exception)}.
@ -11,50 +14,17 @@ import org.bukkit.event.HandlerList;
* @author Norbi
*
*/
@Getter
@RequiredArgsConstructor
public class TBMCExceptionEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private String sourcemsg;
private Throwable exception;
private final String sourceMessage;
private final Throwable exception;
private boolean handled;
TBMCExceptionEvent(String sourcemsg, Throwable exception) {
this.sourcemsg = sourcemsg;
this.exception = exception;
}
/**
* Gets the source message (where did this exception occur, etc.)
*
* @return The message
*/
public String getSourceMessage() {
return sourcemsg;
}
/**
* Gets the exception
*
* @return The exception
*/
public Throwable getException() {
return exception;
}
/**
* Gets if this event was handled
*
* @return True if it was handled
*/
public boolean isHandled() {
return handled;
}
/**
* Flags the event as handled
*/
public void setHandled() {
this.handled = true;
handled = true;
}
@Override

View file

@ -0,0 +1,37 @@
package buttondevteam.lib;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import buttondevteam.lib.chat.Channel;
import lombok.Getter;
/**
* Make sure to only send the message to users who {@link #shouldSendTo(CommandSender)} returns true.
*
* @author NorbiPeti
*
*/
@Getter
public class TBMCSystemChatEvent extends TBMCChatEventBase {
private boolean handled;
public void setHandled() {
handled = true;
}
public TBMCSystemChatEvent(Channel channel, String message, int score) { // TODO: RIch message
super(channel, message, score);
}
private static final HandlerList handlers = new HandlerList();
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View file

@ -2,7 +2,10 @@ package buttondevteam.lib.chat;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -25,7 +28,7 @@ public class Channel {
* Creates a channel.
*
* @param displayname
* The name that should appear at the start of the message
* The name that should appear at the start of the message. <b>A chat color is expected at the beginning (§9).</b>
* @param color
* The default color of the messages sent in the channel
* @param command
@ -42,6 +45,20 @@ public class Channel {
this.filteranderrormsg = filteranderrormsg;
}
/**
* Must be only called from a subclass - otherwise it'll throw an exception.
*
* @see Channel#Channel(String, Color, String, Function)
*/
@SuppressWarnings("unchecked")
protected <T extends Channel> Channel(String displayname, Color color, String command,
BiFunction<T, CommandSender, RecipientTestResult> filteranderrormsg) {
DisplayName = displayname;
this.color = color;
ID = command;
this.filteranderrormsg = s -> filteranderrormsg.apply((T) this, s);
}
public static List<Channel> getChannels() {
return channels;
}
@ -54,12 +71,21 @@ public class Channel {
* The group that can access the channel or <b>null</b> to only allow OPs.
* @return
*/
public static Function<CommandSender, RecipientTestResult> filteranderrormsg(String permgroup) {
return s -> s.isOp() || (permgroup != null
? s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, permgroup) : false)
? new RecipientTestResult(0) //
: new RecipientTestResult("You need to be a(n) " + (permgroup != null ? permgroup : "OP")
+ " to use this channel.");
public static Function<CommandSender, RecipientTestResult> inGroupFilter(String permgroup) {
return noScoreResult(
s -> s.isOp() || (permgroup != null
? s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, permgroup) : false),
"You need to be a(n) " + (permgroup != null ? permgroup : "OP") + " to use this channel.");
}
public static Function<CommandSender, RecipientTestResult> noScoreResult(Predicate<CommandSender> filter,
String errormsg) {
return s -> filter.test(s) ? new RecipientTestResult(0) : 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);
}
public static Channel GlobalChat;

View file

@ -0,0 +1,24 @@
package buttondevteam.lib.chat;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.command.CommandSender;
public class ChatRoom extends Channel {
private List<CommandSender> usersInRoom = new ArrayList<>();
public ChatRoom(String displayname, Color color, String command) {
<ChatRoom>super(displayname, color, command, noScoreResult((this_, s) -> this_.usersInRoom.contains(s),
"Not implemented yet. Please report it to the devs along with which platform you're trying to talk from."));
}
public void joinRoom(CommandSender sender) {
usersInRoom.add(sender);
TBMCChatAPI.SendSystemMessage(this, 0, sender.getName() + " joined the room");
}
public void leaveRoom(CommandSender sender) {
usersInRoom.remove(sender);
TBMCChatAPI.SendSystemMessage(this, 0, sender.getName() + " left the room");
}
}

View file

@ -22,6 +22,7 @@ import buttondevteam.core.MainPlugin;
import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.TBMCChatPreprocessEvent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
public class TBMCChatAPI {
@ -223,20 +224,46 @@ public class TBMCChatAPI {
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);
Bukkit.getPluginManager().callEvent(event);
return event.isCancelled();
}
/**
* Sends a regular message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.
*
* @param channel
* The channel to send to
* @param score
* The score to use to find the group - use 0 if the channel doesn't have scores
* @param message
* The message to send
* @return The event cancelled state
*/
public static boolean SendSystemMessage(Channel channel, int score, String message) {
if (!Channel.getChannels().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, score);
Bukkit.getPluginManager().callEvent(event);
return event.isCancelled();
}
private static int getScoreOrSendError(Channel channel, CommandSender sender) {
int score;
if (channel.filteranderrormsg == null)
score = -1;
score = 0;
else {
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
if (result.errormessage != null) {
sender.sendMessage("§c" + result.errormessage);
return true;
return -1;
}
score = result.score;
}
TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score);
Bukkit.getPluginManager().callEvent(event);
return event.isCancelled();
return score;
}
/**

View file

@ -2,7 +2,6 @@ package buttondevteam.lib.player;
import java.io.File;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
@ -11,6 +10,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import com.google.common.collect.HashBiMap;
import buttondevteam.lib.TBMCCoreAPI;
import lombok.val;
@ChromaGamerEnforcer
public abstract class ChromaGamerBase implements AutoCloseable {
@ -131,7 +131,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
Consumer<YamlConfiguration> sync = sourcedata -> {
final String sourcefolder = sourcedata == plugindata ? ownFolder : userFolder;
final String id = sourcedata.getString(sourcefolder + "_id");
for (Entry<Class<? extends ChromaGamerBase>, String> entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones
for (val entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones
if (entry.getKey() == getClass() || entry.getKey() == user.getClass())
continue;
final String otherid = sourcedata.getString(entry.getValue() + "_id");
@ -139,7 +139,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
continue;
try (ChromaGamerBase cg = getUser(otherid, entry.getKey())) {
cg.plugindata.set(sourcefolder + "_id", id); // Set new IDs
for (Entry<Class<? extends ChromaGamerBase>, String> item : playerTypes.entrySet())
for (val item : playerTypes.entrySet())
if (sourcedata.contains(item.getValue() + "_id"))
cg.plugindata.set(item.getValue() + "_id", sourcedata.getString(item.getValue() + "_id")); // Set all existing IDs
} catch (Exception e) {