From 87140ce5f6df1c512927215f656590a459d33fba Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Wed, 27 May 2020 21:55:21 +0200 Subject: [PATCH 01/12] Api initialized. --- .../java/de/jeter/chatex/ChatListener.java | 10 ++- .../java/de/jeter/chatex/api/ChatExAPI.java | 15 +++++ .../jeter/chatex/api/events/ChatExEvent.java | 4 ++ .../chatex/api/events/ChatExEventHandler.java | 9 +++ .../chatex/api/events/ChatExListener.java | 4 ++ .../jeter/chatex/api/events/EventManager.java | 9 +++ .../chatex/api/events/IRegisteredEvent.java | 8 +++ .../MessageBlockedByAntiSpamManagerEvent.java | 66 +++++++++++++++++++ .../chatex/api/events/RegisteredEvent.java | 33 ++++++++++ .../chatex/events/DefaultEventManager.java | 50 ++++++++++++++ 10 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/jeter/chatex/api/ChatExAPI.java create mode 100644 src/main/java/de/jeter/chatex/api/events/ChatExEvent.java create mode 100644 src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java create mode 100644 src/main/java/de/jeter/chatex/api/events/ChatExListener.java create mode 100644 src/main/java/de/jeter/chatex/api/events/EventManager.java create mode 100644 src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java create mode 100644 src/main/java/de/jeter/chatex/api/events/MessageBlockedByAntiSpamManagerEvent.java create mode 100644 src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java create mode 100644 src/main/java/de/jeter/chatex/events/DefaultEventManager.java diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index 6260756..e3b81c7 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -1,5 +1,7 @@ package de.jeter.chatex; +import de.jeter.chatex.api.events.MessageBlockedByAntiSpamManagerEvent; +import de.jeter.chatex.events.DefaultEventManager; import de.jeter.chatex.plugins.PluginManager; import de.jeter.chatex.utils.*; import de.jeter.chatex.utils.adManager.AdManager; @@ -29,8 +31,12 @@ public void onChat(final AsyncPlayerChatEvent event) { } if (!AntiSpamManager.isAllowed(event.getPlayer())) { - event.getPlayer().sendMessage(Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", AntiSpamManager.getRemaingSeconds(event.getPlayer()) + "")); - event.setCancelled(true); + String message = Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", AntiSpamManager.getRemaingSeconds(event.getPlayer()) + ""); + long remainingTime = AntiSpamManager.getRemaingSeconds(event.getPlayer()); + event.getPlayer().sendMessage(message); + MessageBlockedByAntiSpamManagerEvent messageBlockedByAntiSpamManagerEvent = new MessageBlockedByAntiSpamManagerEvent(event.getPlayer(),message, event.getMessage(),remainingTime); + DefaultEventManager.getInstance().handleEvent(messageBlockedByAntiSpamManagerEvent); + event.setCancelled(messageBlockedByAntiSpamManagerEvent.isCancelled()); return; } AntiSpamManager.put(event.getPlayer()); diff --git a/src/main/java/de/jeter/chatex/api/ChatExAPI.java b/src/main/java/de/jeter/chatex/api/ChatExAPI.java new file mode 100644 index 0000000..bddd500 --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/ChatExAPI.java @@ -0,0 +1,15 @@ +package de.jeter.chatex.api; + +import de.jeter.chatex.api.events.EventManager; +import de.jeter.chatex.events.DefaultEventManager; + +public class ChatExAPI { + public ChatExAPI() { + } + + public EventManager getEventManager() { + return DefaultEventManager.getInstance(); + } + + +} diff --git a/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java b/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java new file mode 100644 index 0000000..48da38e --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java @@ -0,0 +1,4 @@ +package de.jeter.chatex.api.events; + +public abstract class ChatExEvent { +} diff --git a/src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java b/src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java new file mode 100644 index 0000000..3da3938 --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java @@ -0,0 +1,9 @@ +package de.jeter.chatex.api.events; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.EventListener; + +@Retention(RetentionPolicy.RUNTIME) +public @interface ChatExEventHandler { +} diff --git a/src/main/java/de/jeter/chatex/api/events/ChatExListener.java b/src/main/java/de/jeter/chatex/api/events/ChatExListener.java new file mode 100644 index 0000000..5071de6 --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/ChatExListener.java @@ -0,0 +1,4 @@ +package de.jeter.chatex.api.events; + +public interface ChatExListener { +} diff --git a/src/main/java/de/jeter/chatex/api/events/EventManager.java b/src/main/java/de/jeter/chatex/api/events/EventManager.java new file mode 100644 index 0000000..f16065a --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/EventManager.java @@ -0,0 +1,9 @@ +package de.jeter.chatex.api.events; + +import de.jeter.chatex.api.events.ChatExEvent; +import de.jeter.chatex.api.events.ChatExListener; + +public interface EventManager { + void registerEvents(ChatExListener chatExListener); + T handleEvent(T event); +} diff --git a/src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java b/src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java new file mode 100644 index 0000000..78c7405 --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java @@ -0,0 +1,8 @@ +package de.jeter.chatex.api.events; + +import java.lang.reflect.Method; + +public interface IRegisteredEvent { + Object getInstance(); + Method getMethod(); +} diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAntiSpamManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAntiSpamManagerEvent.java new file mode 100644 index 0000000..21f82d8 --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAntiSpamManagerEvent.java @@ -0,0 +1,66 @@ +package de.jeter.chatex.api.events; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +public class MessageBlockedByAntiSpamManagerEvent extends ChatExEvent implements Cancellable { + private Player player; + private String message; + private long remainingTime; + private String playerMessage; + private boolean canceled = true; + + public MessageBlockedByAntiSpamManagerEvent(Player player, String message, String playerMessage, long remaining) { + this.player = player; + this.message = message; + this.playerMessage = playerMessage; + this.remainingTime = remaining; + } + + /** + * @return the player which fired this event + */ + public Player getPlayer() { + return player; + } + + /** + * @return Return the message which the player will receive. + */ + public String getMessage() { + return message; + } + + + /** + * @param message the message which the player will receive. + */ + public void setMessage(String message) { + this.message = message; + } + + /** + * @return the message which the player would have written. + */ + public String getPlayerMessage() { + return playerMessage; + } + + + /** + * @return the remaining time a player is muted + */ + public long getRemainingTime() { + return remainingTime; + } + + @Override + public boolean isCancelled() { + return canceled; + } + + @Override + public void setCancelled(boolean b) { + canceled = b; + } +} diff --git a/src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java b/src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java new file mode 100644 index 0000000..4609e4d --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java @@ -0,0 +1,33 @@ +package de.jeter.chatex.api.events; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class RegisteredEvent implements IRegisteredEvent { + private Method method; + private Object instance; + + public RegisteredEvent(Object obj, Method meth){ + instance = obj; + method = meth; + } + + @Override + public Object getInstance() { + return instance; + } + + @Override + public Method getMethod() { + return method; + } + + public void fireEvent(ChatExEvent chatExEvent){ + method.setAccessible(true); + try { + method.invoke(getInstance(), chatExEvent); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/de/jeter/chatex/events/DefaultEventManager.java b/src/main/java/de/jeter/chatex/events/DefaultEventManager.java new file mode 100644 index 0000000..aca02ef --- /dev/null +++ b/src/main/java/de/jeter/chatex/events/DefaultEventManager.java @@ -0,0 +1,50 @@ +package de.jeter.chatex.events; + +import de.jeter.chatex.api.events.EventManager; +import de.jeter.chatex.api.events.ChatExEvent; +import de.jeter.chatex.api.events.ChatExEventHandler; +import de.jeter.chatex.api.events.ChatExListener; +import de.jeter.chatex.api.events.RegisteredEvent; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +public class DefaultEventManager implements EventManager { + private static DefaultEventManager instance = new DefaultEventManager(); + private final Map, HashSet> eventClassMap = new HashMap<>(); + + private DefaultEventManager() { + } + + public static DefaultEventManager getInstance() { + return instance; + } + + public void registerEvents(ChatExListener chatExListener) { + for (Method method : chatExListener.getClass().getDeclaredMethods()) { + ChatExEventHandler cEEH = method.getAnnotation(ChatExEventHandler.class); + Class checkClass; + if (cEEH == null) continue; + if (method.getParameterTypes().length != 1 || !ChatExEvent.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) + continue; + Class eventClass = checkClass.asSubclass(ChatExEvent.class); + + if (!eventClassMap.containsKey(eventClass)) { + eventClassMap.put(eventClass, new HashSet<>()); + } + ChatExEventHandler chatExEventHandler = method.getAnnotation(ChatExEventHandler.class); + eventClassMap.get(eventClass).add(new RegisteredEvent(chatExListener, method)); + } + } + + public T handleEvent(T event) { + if (!eventClassMap.containsKey(event.getClass())) return event; + for (RegisteredEvent registeredEvent : eventClassMap.get(event.getClass())) { + registeredEvent.fireEvent(event); + } + return event; + } + +} From f75d0c16b3764ff5b2b33ac9078fd31c2caad134 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Wed, 27 May 2020 21:57:58 +0200 Subject: [PATCH 02/12] Api update --- src/main/java/de/jeter/chatex/ChatListener.java | 2 +- src/main/java/de/jeter/chatex/api/ChatExAPI.java | 2 +- .../chatex/{ => api}/events/DefaultEventManager.java | 8 +------- 3 files changed, 3 insertions(+), 9 deletions(-) rename src/main/java/de/jeter/chatex/{ => api}/events/DefaultEventManager.java (85%) diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index e3b81c7..b183358 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -1,7 +1,7 @@ package de.jeter.chatex; import de.jeter.chatex.api.events.MessageBlockedByAntiSpamManagerEvent; -import de.jeter.chatex.events.DefaultEventManager; +import de.jeter.chatex.api.events.DefaultEventManager; import de.jeter.chatex.plugins.PluginManager; import de.jeter.chatex.utils.*; import de.jeter.chatex.utils.adManager.AdManager; diff --git a/src/main/java/de/jeter/chatex/api/ChatExAPI.java b/src/main/java/de/jeter/chatex/api/ChatExAPI.java index bddd500..fef0cef 100644 --- a/src/main/java/de/jeter/chatex/api/ChatExAPI.java +++ b/src/main/java/de/jeter/chatex/api/ChatExAPI.java @@ -1,7 +1,7 @@ package de.jeter.chatex.api; import de.jeter.chatex.api.events.EventManager; -import de.jeter.chatex.events.DefaultEventManager; +import de.jeter.chatex.api.events.DefaultEventManager; public class ChatExAPI { public ChatExAPI() { diff --git a/src/main/java/de/jeter/chatex/events/DefaultEventManager.java b/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java similarity index 85% rename from src/main/java/de/jeter/chatex/events/DefaultEventManager.java rename to src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java index aca02ef..7857db9 100644 --- a/src/main/java/de/jeter/chatex/events/DefaultEventManager.java +++ b/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java @@ -1,10 +1,4 @@ -package de.jeter.chatex.events; - -import de.jeter.chatex.api.events.EventManager; -import de.jeter.chatex.api.events.ChatExEvent; -import de.jeter.chatex.api.events.ChatExEventHandler; -import de.jeter.chatex.api.events.ChatExListener; -import de.jeter.chatex.api.events.RegisteredEvent; +package de.jeter.chatex.api.events; import java.lang.reflect.Method; import java.util.HashMap; From b594c6cb7fb2cd9c2a501710ba7b1cbf95a0bf91 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Wed, 27 May 2020 22:17:12 +0200 Subject: [PATCH 03/12] Api update --- .../java/de/jeter/chatex/ChatListener.java | 18 +++--- .../jeter/chatex/api/events/ChatExEvent.java | 4 +- .../MessageBlockedByAdManagerEvent.java | 58 +++++++++++++++++++ ... => MessageBlockedBySpamManagerEvent.java} | 28 ++++----- 4 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java rename src/main/java/de/jeter/chatex/api/events/{MessageBlockedByAntiSpamManagerEvent.java => MessageBlockedBySpamManagerEvent.java} (68%) diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index b183358..79cabf4 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -1,6 +1,7 @@ package de.jeter.chatex; -import de.jeter.chatex.api.events.MessageBlockedByAntiSpamManagerEvent; +import de.jeter.chatex.api.events.MessageBlockedByAdManagerEvent; +import de.jeter.chatex.api.events.MessageBlockedBySpamManagerEvent; import de.jeter.chatex.api.events.DefaultEventManager; import de.jeter.chatex.plugins.PluginManager; import de.jeter.chatex.utils.*; @@ -33,10 +34,10 @@ public void onChat(final AsyncPlayerChatEvent event) { if (!AntiSpamManager.isAllowed(event.getPlayer())) { String message = Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", AntiSpamManager.getRemaingSeconds(event.getPlayer()) + ""); long remainingTime = AntiSpamManager.getRemaingSeconds(event.getPlayer()); - event.getPlayer().sendMessage(message); - MessageBlockedByAntiSpamManagerEvent messageBlockedByAntiSpamManagerEvent = new MessageBlockedByAntiSpamManagerEvent(event.getPlayer(),message, event.getMessage(),remainingTime); - DefaultEventManager.getInstance().handleEvent(messageBlockedByAntiSpamManagerEvent); - event.setCancelled(messageBlockedByAntiSpamManagerEvent.isCancelled()); + MessageBlockedBySpamManagerEvent messageBlockedBySpamManagerEvent = new MessageBlockedBySpamManagerEvent(event.getPlayer(),message, event.getMessage(),remainingTime); + DefaultEventManager.getInstance().handleEvent(messageBlockedBySpamManagerEvent); + event.getPlayer().sendMessage(messageBlockedBySpamManagerEvent.getPluginMessage()); + event.setCancelled(!messageBlockedBySpamManagerEvent.isCancelled()); return; } AntiSpamManager.put(event.getPlayer()); @@ -46,8 +47,11 @@ public void onChat(final AsyncPlayerChatEvent event) { String chatMessage = event.getMessage(); if (adManager.checkForAds(chatMessage, player)) { - event.getPlayer().sendMessage(Locales.MESSAGES_AD.getString(null).replaceAll("%perm", "chatex.bypassads")); - event.setCancelled(true); + String message = Locales.MESSAGES_AD.getString(null).replaceAll("%perm", "chatex.bypassads"); + MessageBlockedByAdManagerEvent messageBlockedByAdManagerEvent = new MessageBlockedByAdManagerEvent(player, chatMessage, message); + DefaultEventManager.getInstance().handleEvent(messageBlockedByAdManagerEvent); + event.getPlayer().sendMessage(messageBlockedByAdManagerEvent.getPluginMessage()); + event.setCancelled(!messageBlockedByAdManagerEvent.isCancelled()); return; } diff --git a/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java b/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java index 48da38e..9c3d823 100644 --- a/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java @@ -1,4 +1,6 @@ package de.jeter.chatex.api.events; -public abstract class ChatExEvent { +import org.bukkit.event.Cancellable; + +public abstract class ChatExEvent implements Cancellable { } diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java new file mode 100644 index 0000000..2307a31 --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java @@ -0,0 +1,58 @@ +package de.jeter.chatex.api.events; + +import org.bukkit.entity.Player; + +public class MessageBlockedByAdManagerEvent extends ChatExEvent { + private boolean canceled; + private Player player; + private String message; + private String pluginMessage; + + public MessageBlockedByAdManagerEvent(Player player, String message, String pluginMessage) { + this.player = player; + this.message = message; + this.pluginMessage = pluginMessage; + } + + /** + * @return the player which fired this event + */ + public Player getPlayer() { + return player; + } + + /** + * @return the message which the player would have written. + */ + public String getMessage() { + return message; + } + + /** + * @return Return the message which the player will receive. + */ + public String getPluginMessage() { + return pluginMessage; + } + + /** + * @param pluginMessage the message the player will receive + */ + public void setPluginMessage(String pluginMessage) { + this.pluginMessage = pluginMessage; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public boolean isCancelled() { + return canceled; + } + + @Override + public void setCancelled(boolean b) { + canceled = b; + } +} diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAntiSpamManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java similarity index 68% rename from src/main/java/de/jeter/chatex/api/events/MessageBlockedByAntiSpamManagerEvent.java rename to src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java index 21f82d8..a12ed6f 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAntiSpamManagerEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java @@ -1,19 +1,18 @@ package de.jeter.chatex.api.events; import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -public class MessageBlockedByAntiSpamManagerEvent extends ChatExEvent implements Cancellable { +public class MessageBlockedBySpamManagerEvent extends ChatExEvent { private Player player; private String message; private long remainingTime; - private String playerMessage; + private String pluginMessage; private boolean canceled = true; - public MessageBlockedByAntiSpamManagerEvent(Player player, String message, String playerMessage, long remaining) { + public MessageBlockedBySpamManagerEvent(Player player, String message, String pluginMessage, long remaining) { this.player = player; this.message = message; - this.playerMessage = playerMessage; + this.pluginMessage = pluginMessage; this.remainingTime = remaining; } @@ -25,27 +24,30 @@ public Player getPlayer() { } /** - * @return Return the message which the player will receive. + * @return the message which the player would have written. */ + public String getMessage() { return message; } - - /** - * @param message the message which the player will receive. - */ public void setMessage(String message) { this.message = message; } /** - * @return the message which the player would have written. + * @return Return the message which the player will receive. */ - public String getPlayerMessage() { - return playerMessage; + public String getPluginMessage() { + return pluginMessage; } + /** + * @param pluginMessage the message the player will receive + */ + public void setPluginMessage(String pluginMessage) { + this.pluginMessage = pluginMessage; + } /** * @return the remaining time a player is muted From 7fa9eb8c890e06c7a9873152ce2680db479048cf Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Wed, 27 May 2020 22:48:37 +0200 Subject: [PATCH 04/12] [ADD] MessageContainsBlockedWordEvent.java [MOD] ChatListener.java Added trigger for MessageContainsBlockedWordEvent [REM] DefaultEventManager.java unused variable --- .../java/de/jeter/chatex/ChatListener.java | 8 ++- .../api/events/DefaultEventManager.java | 4 +- .../MessageContainsBlockedWordEvent.java | 60 +++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index 79cabf4..0bbebc2 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -3,6 +3,7 @@ import de.jeter.chatex.api.events.MessageBlockedByAdManagerEvent; import de.jeter.chatex.api.events.MessageBlockedBySpamManagerEvent; import de.jeter.chatex.api.events.DefaultEventManager; +import de.jeter.chatex.api.events.MessageContainsBlockedWordEvent; import de.jeter.chatex.plugins.PluginManager; import de.jeter.chatex.utils.*; import de.jeter.chatex.utils.adManager.AdManager; @@ -56,8 +57,11 @@ public void onChat(final AsyncPlayerChatEvent event) { } if (Utils.checkForBlocked(event.getMessage())) { - event.setCancelled(true); - event.getPlayer().sendMessage(Locales.MESSAGES_BLOCKED.getString(null)); + String message = Locales.MESSAGES_BLOCKED.getString(null); + MessageContainsBlockedWordEvent messageContainsBlockedWordEvent = new MessageContainsBlockedWordEvent(player, chatMessage, message); + DefaultEventManager.getInstance().handleEvent(messageContainsBlockedWordEvent); + event.setCancelled(!messageContainsBlockedWordEvent.isCancelled()); + event.getPlayer().sendMessage(messageContainsBlockedWordEvent.getPluginMessage()); return; } diff --git a/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java b/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java index 7857db9..305afb8 100644 --- a/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java +++ b/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java @@ -15,7 +15,7 @@ private DefaultEventManager() { public static DefaultEventManager getInstance() { return instance; } - + public void registerEvents(ChatExListener chatExListener) { for (Method method : chatExListener.getClass().getDeclaredMethods()) { ChatExEventHandler cEEH = method.getAnnotation(ChatExEventHandler.class); @@ -28,7 +28,7 @@ public void registerEvents(ChatExListener chatExListener) { if (!eventClassMap.containsKey(eventClass)) { eventClassMap.put(eventClass, new HashSet<>()); } - ChatExEventHandler chatExEventHandler = method.getAnnotation(ChatExEventHandler.class); + eventClassMap.get(eventClass).add(new RegisteredEvent(chatExListener, method)); } } diff --git a/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java new file mode 100644 index 0000000..bb0cd5d --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java @@ -0,0 +1,60 @@ +package de.jeter.chatex.api.events; + +import org.bukkit.entity.Player; + +public class MessageContainsBlockedWordEvent extends ChatExEvent { + private Player player; + private String message; + private String pluginMessage; + private boolean canceled = true; + + public MessageContainsBlockedWordEvent(Player player, String message, String pluginMessage) { + this.player = player; + this.message = message; + this.pluginMessage = pluginMessage; + } + + /** + * @return the player which fired this event + */ + public Player getPlayer() { + return player; + } + + /** + * @return the message which the player would have written. + */ + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + /** + * @return Return the message which the player will receive. + */ + public String getPluginMessage() { + return pluginMessage; + } + + /** + * @param pluginMessage the message the player will receive + */ + public void setPluginMessage(String pluginMessage) { + this.pluginMessage = pluginMessage; + } + + + @Override + public void setCancelled(boolean b) { + canceled = b; + } + + @Override + public boolean isCancelled() { + return false; + } + +} From e269dde79085dd67c6beb5bd49add8aafaf67214 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Wed, 27 May 2020 23:23:03 +0200 Subject: [PATCH 05/12] [MOD] ChatExAPI.java added every PluginManager function. --- .../java/de/jeter/chatex/api/ChatExAPI.java | 27 +++++++++-- .../jeter/chatex/plugins/PluginManager.java | 46 +++++++++++-------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/jeter/chatex/api/ChatExAPI.java b/src/main/java/de/jeter/chatex/api/ChatExAPI.java index fef0cef..2746629 100644 --- a/src/main/java/de/jeter/chatex/api/ChatExAPI.java +++ b/src/main/java/de/jeter/chatex/api/ChatExAPI.java @@ -1,15 +1,36 @@ package de.jeter.chatex.api; -import de.jeter.chatex.api.events.EventManager; import de.jeter.chatex.api.events.DefaultEventManager; +import de.jeter.chatex.api.events.EventManager; +import de.jeter.chatex.plugins.PluginManager; +import org.bukkit.entity.Player; public class ChatExAPI { - public ChatExAPI() { + public String getPermissionHandlerName() { + return PluginManager.getInstance().getName(); } public EventManager getEventManager() { return DefaultEventManager.getInstance(); } + public String getPrefix(Player p) { + return PluginManager.getInstance().getPrefix(p); + } + + public String getSuffix(Player p) { + return PluginManager.getInstance().getSuffix(p); + } + + public String[] getGroupNames(Player p) { + return PluginManager.getInstance().getGroupNames(p); + } -} + public String getMessageFormat(Player p) { + return PluginManager.getInstance().getMessageFormat(p); + } + + public String getGlobalMessageFormat(Player p) { + return PluginManager.getInstance().getGlobalMessageFormat(p); + } +} \ No newline at end of file diff --git a/src/main/java/de/jeter/chatex/plugins/PluginManager.java b/src/main/java/de/jeter/chatex/plugins/PluginManager.java index 77b3e51..92e2409 100644 --- a/src/main/java/de/jeter/chatex/plugins/PluginManager.java +++ b/src/main/java/de/jeter/chatex/plugins/PluginManager.java @@ -1,14 +1,28 @@ +/* + * This file is part of ChatEx + * Copyright (C) 2020 ChatEx Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package de.jeter.chatex.plugins; import de.jeter.chatex.ChatEx; import de.jeter.chatex.utils.HookManager; import de.jeter.chatex.utils.Utils; -import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.entity.Player; -/** - * @author TheJeterLP - */ public class PluginManager implements PermissionsPlugin { private static PermissionsPlugin handler; @@ -26,15 +40,15 @@ public static void load() { handler = new Nothing(); } ChatEx.getInstance().getLogger().info("Successfully hooked into: " + handler.getName()); + + if (HookManager.checkPlaceholderAPI()) { + ChatEx.getInstance().getLogger().info("Hooked into PlaceholderAPI"); + } } @Override public String getName() { - if (!HookManager.checkPlaceholderAPI()) { - return handler.getName(); - } else { - return handler.getName() + ", PlaceholderAPI"; - } + return handler.getName(); } @Override @@ -54,19 +68,11 @@ public String[] getGroupNames(Player p) { @Override public String getMessageFormat(Player p) { - if (!HookManager.checkPlaceholderAPI()) { - return Utils.replaceColors(handler.getMessageFormat(p)); - } else { - return Utils.replaceColors(PlaceholderAPI.setPlaceholders(p, handler.getMessageFormat(p))); - } + return Utils.replaceColors(handler.getMessageFormat(p)); } @Override public String getGlobalMessageFormat(Player p) { - if (!HookManager.checkPlaceholderAPI()) { - return Utils.replaceColors(handler.getGlobalMessageFormat(p)); - } else { - return Utils.replaceColors(PlaceholderAPI.setPlaceholders(p, handler.getGlobalMessageFormat(p))); - } + return Utils.replaceColors(handler.getGlobalMessageFormat(p)); } -} +} \ No newline at end of file From 8e540509d624ed2ff3a78ead2ba78c70ae7be9e9 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Wed, 27 May 2020 23:45:59 +0200 Subject: [PATCH 06/12] [MOD] Changed to Bukkit event api. --- .../java/de/jeter/chatex/ChatListener.java | 8 ++-- .../java/de/jeter/chatex/api/ChatExAPI.java | 6 --- .../jeter/chatex/api/events/ChatExEvent.java | 3 +- .../chatex/api/events/ChatExEventHandler.java | 9 ---- .../chatex/api/events/ChatExListener.java | 4 -- .../api/events/DefaultEventManager.java | 44 ------------------- .../jeter/chatex/api/events/EventManager.java | 9 ---- .../chatex/api/events/IRegisteredEvent.java | 8 ---- .../MessageBlockedByAdManagerEvent.java | 10 +++++ .../MessageBlockedBySpamManagerEvent.java | 11 +++++ .../MessageContainsBlockedWordEvent.java | 10 +++++ .../chatex/api/events/RegisteredEvent.java | 33 -------------- 12 files changed, 37 insertions(+), 118 deletions(-) delete mode 100644 src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java delete mode 100644 src/main/java/de/jeter/chatex/api/events/ChatExListener.java delete mode 100644 src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java delete mode 100644 src/main/java/de/jeter/chatex/api/events/EventManager.java delete mode 100644 src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java delete mode 100644 src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index 0bbebc2..73b1ac9 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -2,13 +2,13 @@ import de.jeter.chatex.api.events.MessageBlockedByAdManagerEvent; import de.jeter.chatex.api.events.MessageBlockedBySpamManagerEvent; -import de.jeter.chatex.api.events.DefaultEventManager; import de.jeter.chatex.api.events.MessageContainsBlockedWordEvent; import de.jeter.chatex.plugins.PluginManager; import de.jeter.chatex.utils.*; import de.jeter.chatex.utils.adManager.AdManager; import de.jeter.chatex.utils.adManager.SimpleAdManager; import de.jeter.chatex.utils.adManager.SmartAdManager; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -36,7 +36,7 @@ public void onChat(final AsyncPlayerChatEvent event) { String message = Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", AntiSpamManager.getRemaingSeconds(event.getPlayer()) + ""); long remainingTime = AntiSpamManager.getRemaingSeconds(event.getPlayer()); MessageBlockedBySpamManagerEvent messageBlockedBySpamManagerEvent = new MessageBlockedBySpamManagerEvent(event.getPlayer(),message, event.getMessage(),remainingTime); - DefaultEventManager.getInstance().handleEvent(messageBlockedBySpamManagerEvent); + Bukkit.getPluginManager().callEvent(messageBlockedBySpamManagerEvent); event.getPlayer().sendMessage(messageBlockedBySpamManagerEvent.getPluginMessage()); event.setCancelled(!messageBlockedBySpamManagerEvent.isCancelled()); return; @@ -50,7 +50,7 @@ public void onChat(final AsyncPlayerChatEvent event) { if (adManager.checkForAds(chatMessage, player)) { String message = Locales.MESSAGES_AD.getString(null).replaceAll("%perm", "chatex.bypassads"); MessageBlockedByAdManagerEvent messageBlockedByAdManagerEvent = new MessageBlockedByAdManagerEvent(player, chatMessage, message); - DefaultEventManager.getInstance().handleEvent(messageBlockedByAdManagerEvent); + Bukkit.getPluginManager().callEvent(messageBlockedByAdManagerEvent); event.getPlayer().sendMessage(messageBlockedByAdManagerEvent.getPluginMessage()); event.setCancelled(!messageBlockedByAdManagerEvent.isCancelled()); return; @@ -59,7 +59,7 @@ public void onChat(final AsyncPlayerChatEvent event) { if (Utils.checkForBlocked(event.getMessage())) { String message = Locales.MESSAGES_BLOCKED.getString(null); MessageContainsBlockedWordEvent messageContainsBlockedWordEvent = new MessageContainsBlockedWordEvent(player, chatMessage, message); - DefaultEventManager.getInstance().handleEvent(messageContainsBlockedWordEvent); + Bukkit.getPluginManager().callEvent(messageContainsBlockedWordEvent); event.setCancelled(!messageContainsBlockedWordEvent.isCancelled()); event.getPlayer().sendMessage(messageContainsBlockedWordEvent.getPluginMessage()); return; diff --git a/src/main/java/de/jeter/chatex/api/ChatExAPI.java b/src/main/java/de/jeter/chatex/api/ChatExAPI.java index 2746629..675d161 100644 --- a/src/main/java/de/jeter/chatex/api/ChatExAPI.java +++ b/src/main/java/de/jeter/chatex/api/ChatExAPI.java @@ -1,7 +1,5 @@ package de.jeter.chatex.api; -import de.jeter.chatex.api.events.DefaultEventManager; -import de.jeter.chatex.api.events.EventManager; import de.jeter.chatex.plugins.PluginManager; import org.bukkit.entity.Player; @@ -10,10 +8,6 @@ public String getPermissionHandlerName() { return PluginManager.getInstance().getName(); } - public EventManager getEventManager() { - return DefaultEventManager.getInstance(); - } - public String getPrefix(Player p) { return PluginManager.getInstance().getPrefix(p); } diff --git a/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java b/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java index 9c3d823..4b1d2f6 100644 --- a/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/ChatExEvent.java @@ -1,6 +1,7 @@ package de.jeter.chatex.api.events; import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; -public abstract class ChatExEvent implements Cancellable { +public abstract class ChatExEvent extends Event implements Cancellable { } diff --git a/src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java b/src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java deleted file mode 100644 index 3da3938..0000000 --- a/src/main/java/de/jeter/chatex/api/events/ChatExEventHandler.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.jeter.chatex.api.events; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.util.EventListener; - -@Retention(RetentionPolicy.RUNTIME) -public @interface ChatExEventHandler { -} diff --git a/src/main/java/de/jeter/chatex/api/events/ChatExListener.java b/src/main/java/de/jeter/chatex/api/events/ChatExListener.java deleted file mode 100644 index 5071de6..0000000 --- a/src/main/java/de/jeter/chatex/api/events/ChatExListener.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.jeter.chatex.api.events; - -public interface ChatExListener { -} diff --git a/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java b/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java deleted file mode 100644 index 305afb8..0000000 --- a/src/main/java/de/jeter/chatex/api/events/DefaultEventManager.java +++ /dev/null @@ -1,44 +0,0 @@ -package de.jeter.chatex.api.events; - -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; - -public class DefaultEventManager implements EventManager { - private static DefaultEventManager instance = new DefaultEventManager(); - private final Map, HashSet> eventClassMap = new HashMap<>(); - - private DefaultEventManager() { - } - - public static DefaultEventManager getInstance() { - return instance; - } - - public void registerEvents(ChatExListener chatExListener) { - for (Method method : chatExListener.getClass().getDeclaredMethods()) { - ChatExEventHandler cEEH = method.getAnnotation(ChatExEventHandler.class); - Class checkClass; - if (cEEH == null) continue; - if (method.getParameterTypes().length != 1 || !ChatExEvent.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) - continue; - Class eventClass = checkClass.asSubclass(ChatExEvent.class); - - if (!eventClassMap.containsKey(eventClass)) { - eventClassMap.put(eventClass, new HashSet<>()); - } - - eventClassMap.get(eventClass).add(new RegisteredEvent(chatExListener, method)); - } - } - - public T handleEvent(T event) { - if (!eventClassMap.containsKey(event.getClass())) return event; - for (RegisteredEvent registeredEvent : eventClassMap.get(event.getClass())) { - registeredEvent.fireEvent(event); - } - return event; - } - -} diff --git a/src/main/java/de/jeter/chatex/api/events/EventManager.java b/src/main/java/de/jeter/chatex/api/events/EventManager.java deleted file mode 100644 index f16065a..0000000 --- a/src/main/java/de/jeter/chatex/api/events/EventManager.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.jeter.chatex.api.events; - -import de.jeter.chatex.api.events.ChatExEvent; -import de.jeter.chatex.api.events.ChatExListener; - -public interface EventManager { - void registerEvents(ChatExListener chatExListener); - T handleEvent(T event); -} diff --git a/src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java b/src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java deleted file mode 100644 index 78c7405..0000000 --- a/src/main/java/de/jeter/chatex/api/events/IRegisteredEvent.java +++ /dev/null @@ -1,8 +0,0 @@ -package de.jeter.chatex.api.events; - -import java.lang.reflect.Method; - -public interface IRegisteredEvent { - Object getInstance(); - Method getMethod(); -} diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java index 2307a31..c126faa 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java @@ -1,8 +1,10 @@ package de.jeter.chatex.api.events; import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; public class MessageBlockedByAdManagerEvent extends ChatExEvent { + private static final HandlerList handlers = new HandlerList(); private boolean canceled; private Player player; private String message; @@ -55,4 +57,12 @@ public boolean isCancelled() { public void setCancelled(boolean b) { canceled = b; } + + @Override + public HandlerList getHandlers() { + return handlers; + } + public static HandlerList getHandlerList() { + return handlers; + } } diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java index a12ed6f..5639962 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java @@ -1,8 +1,10 @@ package de.jeter.chatex.api.events; import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; public class MessageBlockedBySpamManagerEvent extends ChatExEvent { + private static final HandlerList handlers = new HandlerList(); private Player player; private String message; private long remainingTime; @@ -65,4 +67,13 @@ public boolean isCancelled() { public void setCancelled(boolean b) { canceled = b; } + + @Override + public HandlerList getHandlers() { + return handlers; + } + public static HandlerList getHandlerList() { + return handlers; + } + } diff --git a/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java index bb0cd5d..58e76f3 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java @@ -1,8 +1,10 @@ package de.jeter.chatex.api.events; import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; public class MessageContainsBlockedWordEvent extends ChatExEvent { + private static final HandlerList handlers = new HandlerList(); private Player player; private String message; private String pluginMessage; @@ -57,4 +59,12 @@ public boolean isCancelled() { return false; } + @Override + public HandlerList getHandlers() { + return handlers; + } + public static HandlerList getHandlerList() { + return handlers; + } + } diff --git a/src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java b/src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java deleted file mode 100644 index 4609e4d..0000000 --- a/src/main/java/de/jeter/chatex/api/events/RegisteredEvent.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.jeter.chatex.api.events; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -public class RegisteredEvent implements IRegisteredEvent { - private Method method; - private Object instance; - - public RegisteredEvent(Object obj, Method meth){ - instance = obj; - method = meth; - } - - @Override - public Object getInstance() { - return instance; - } - - @Override - public Method getMethod() { - return method; - } - - public void fireEvent(ChatExEvent chatExEvent){ - method.setAccessible(true); - try { - method.invoke(getInstance(), chatExEvent); - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } - } -} From fef07fae189e5d5e4be2281c5b41a22e52b38132 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Wed, 27 May 2020 23:51:41 +0200 Subject: [PATCH 07/12] newest api Snapshot release. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 9fbd1b1..7019414 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ ChatEx ChatEx 2020 - 2.4.6 + 2.5.0-api-SNAPSHOT ChatManagement plugin for Bukkit From 37c8accaad81c883b5b8027d0bb60c48e1b2e191 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Thu, 28 May 2020 15:02:23 +0200 Subject: [PATCH 08/12] [ADD] long AntiSpamManager.java#getRemainingMillis() [ADD] AntiSpamManager ChatExAPI.java#getAntiSpamManager() [MOD] AntiSpamManager.java is now a Singleton. --- .../java/de/jeter/chatex/ChatListener.java | 6 ++--- .../java/de/jeter/chatex/api/ChatExAPI.java | 5 ++++ .../jeter/chatex/utils/AntiSpamManager.java | 27 +++++++++++++++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index 73b1ac9..b90f022 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -32,9 +32,9 @@ public void onChat(final AsyncPlayerChatEvent event) { return; } - if (!AntiSpamManager.isAllowed(event.getPlayer())) { - String message = Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", AntiSpamManager.getRemaingSeconds(event.getPlayer()) + ""); - long remainingTime = AntiSpamManager.getRemaingSeconds(event.getPlayer()); + if (!AntiSpamManager.getInstance().isAllowed(event.getPlayer())) { + long remainingTime = AntiSpamManager.getInstance().getRemaingSeconds(event.getPlayer()); + String message = Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", remainingTime + ""); MessageBlockedBySpamManagerEvent messageBlockedBySpamManagerEvent = new MessageBlockedBySpamManagerEvent(event.getPlayer(),message, event.getMessage(),remainingTime); Bukkit.getPluginManager().callEvent(messageBlockedBySpamManagerEvent); event.getPlayer().sendMessage(messageBlockedBySpamManagerEvent.getPluginMessage()); diff --git a/src/main/java/de/jeter/chatex/api/ChatExAPI.java b/src/main/java/de/jeter/chatex/api/ChatExAPI.java index 675d161..9bfdf4c 100644 --- a/src/main/java/de/jeter/chatex/api/ChatExAPI.java +++ b/src/main/java/de/jeter/chatex/api/ChatExAPI.java @@ -1,6 +1,7 @@ package de.jeter.chatex.api; import de.jeter.chatex.plugins.PluginManager; +import de.jeter.chatex.utils.AntiSpamManager; import org.bukkit.entity.Player; public class ChatExAPI { @@ -8,6 +9,10 @@ public String getPermissionHandlerName() { return PluginManager.getInstance().getName(); } + public AntiSpamManager getAntiSpamManager(){ + return AntiSpamManager.getInstance(); + } + public String getPrefix(Player p) { return PluginManager.getInstance().getPrefix(p); } diff --git a/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java b/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java index 7ead602..7873be7 100644 --- a/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java +++ b/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java @@ -9,12 +9,20 @@ public class AntiSpamManager { private static final Map map = new HashMap<>(); - + private static AntiSpamManager instance; public static void put(Player chatter) { map.put(chatter, System.currentTimeMillis()); } - public static boolean isAllowed(Player chatter) { + private AntiSpamManager(){ + + } + + public static AntiSpamManager getInstance() { + return instance; + } + + public boolean isAllowed(Player chatter) { if (!map.containsKey(chatter) || !Config.ANTISPAM_ENABLED.getBoolean() || chatter.hasPermission("chatex.antispam.bypass")) { return true; } @@ -25,7 +33,7 @@ public static boolean isAllowed(Player chatter) { return current > lastChat; } - public static long getRemaingSeconds(Player chatter) { + public long getRemaingSeconds(Player chatter) { if (isAllowed(chatter)) { return 0; } @@ -34,8 +42,17 @@ public static long getRemaingSeconds(Player chatter) { long current = System.currentTimeMillis(); long diff = lastChat - current; - long seconds = TimeUnit.MILLISECONDS.toSeconds(diff); - return seconds; + return TimeUnit.MILLISECONDS.toSeconds(diff); + } + + public long getRemainingMillis(Player chatter){ + if (isAllowed(chatter)) { + return 0; + } + long lastChat = map.get(chatter) + (Config.ANTISPAM_SECONDS.getInt() * 1000); + long current = System.currentTimeMillis(); + + return lastChat - current; } } From 0b6a89979fa451294d8deb569a8c7cdc21d3a112 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Thu, 28 May 2020 15:21:02 +0200 Subject: [PATCH 09/12] [MOD] ChatListener.java don't return if event was canceled [MOD] *Events.java added initialization docs. --- src/main/java/de/jeter/chatex/ChatListener.java | 13 ++++++++++--- .../api/events/MessageBlockedByAdManagerEvent.java | 10 ++++++++++ .../events/MessageBlockedBySpamManagerEvent.java | 12 +++++++++++- .../api/events/MessageContainsBlockedWordEvent.java | 10 ++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index b90f022..ab6c8a7 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -39,7 +39,9 @@ public void onChat(final AsyncPlayerChatEvent event) { Bukkit.getPluginManager().callEvent(messageBlockedBySpamManagerEvent); event.getPlayer().sendMessage(messageBlockedBySpamManagerEvent.getPluginMessage()); event.setCancelled(!messageBlockedBySpamManagerEvent.isCancelled()); - return; + if(!messageBlockedBySpamManagerEvent.isCancelled()){ + return; + } } AntiSpamManager.put(event.getPlayer()); @@ -52,8 +54,11 @@ public void onChat(final AsyncPlayerChatEvent event) { MessageBlockedByAdManagerEvent messageBlockedByAdManagerEvent = new MessageBlockedByAdManagerEvent(player, chatMessage, message); Bukkit.getPluginManager().callEvent(messageBlockedByAdManagerEvent); event.getPlayer().sendMessage(messageBlockedByAdManagerEvent.getPluginMessage()); + event.setMessage(messageBlockedByAdManagerEvent.getMessage()); event.setCancelled(!messageBlockedByAdManagerEvent.isCancelled()); - return; + if(!messageBlockedByAdManagerEvent.isCancelled()){ + return; + } } if (Utils.checkForBlocked(event.getMessage())) { @@ -62,7 +67,9 @@ public void onChat(final AsyncPlayerChatEvent event) { Bukkit.getPluginManager().callEvent(messageContainsBlockedWordEvent); event.setCancelled(!messageContainsBlockedWordEvent.isCancelled()); event.getPlayer().sendMessage(messageContainsBlockedWordEvent.getPluginMessage()); - return; + if(!messageContainsBlockedWordEvent.isCancelled()){ + return; + } } boolean global = false; diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java index c126faa..f297b7a 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java @@ -10,6 +10,12 @@ public class MessageBlockedByAdManagerEvent extends ChatExEvent { private String message; private String pluginMessage; + /** + * + * @param player the player which fired the event + * @param message the message of the player + * @param pluginMessage the message which the plugin sends to the player. + */ public MessageBlockedByAdManagerEvent(Player player, String message, String pluginMessage) { this.player = player; this.message = message; @@ -44,6 +50,10 @@ public void setPluginMessage(String pluginMessage) { this.pluginMessage = pluginMessage; } + /** + * + * @param message set the message which the player writes. + */ public void setMessage(String message) { this.message = message; } diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java index 5639962..839a19a 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java @@ -11,6 +11,13 @@ public class MessageBlockedBySpamManagerEvent extends ChatExEvent { private String pluginMessage; private boolean canceled = true; + /** + * + * @param player the player which fired the event + * @param message the message of the player + * @param pluginMessage the message which the plugin sends to the player. + * @param remaining the remaining time in seconds. + */ public MessageBlockedBySpamManagerEvent(Player player, String message, String pluginMessage, long remaining) { this.player = player; this.message = message; @@ -28,11 +35,14 @@ public Player getPlayer() { /** * @return the message which the player would have written. */ - public String getMessage() { return message; } + /** + * + * @param message set the message which the player writes. + */ public void setMessage(String message) { this.message = message; } diff --git a/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java index 58e76f3..888290e 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java @@ -10,6 +10,12 @@ public class MessageContainsBlockedWordEvent extends ChatExEvent { private String pluginMessage; private boolean canceled = true; + /** + * + * @param player the player which fired the event + * @param message the message of the player + * @param pluginMessage the message which the plugin sends to the player. + */ public MessageContainsBlockedWordEvent(Player player, String message, String pluginMessage) { this.player = player; this.message = message; @@ -30,6 +36,10 @@ public String getMessage() { return message; } + /** + * + * @param message set the message which the player writes. + */ public void setMessage(String message) { this.message = message; } From 8c18a140b81506663a6e2673261c532a4444a11c Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Thu, 28 May 2020 15:29:37 +0200 Subject: [PATCH 10/12] [ADD] PlayerUsesRangeModeEvent.java fires when a player uses range mode [MOD] ChatListener.java to use this event --- .../java/de/jeter/chatex/ChatListener.java | 9 +++- .../api/events/PlayerUsesRangeModeEvent.java | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index ab6c8a7..4663431 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -3,6 +3,7 @@ import de.jeter.chatex.api.events.MessageBlockedByAdManagerEvent; import de.jeter.chatex.api.events.MessageBlockedBySpamManagerEvent; import de.jeter.chatex.api.events.MessageContainsBlockedWordEvent; +import de.jeter.chatex.api.events.PlayerUsesRangeModeEvent; import de.jeter.chatex.plugins.PluginManager; import de.jeter.chatex.utils.*; import de.jeter.chatex.utils.adManager.AdManager; @@ -99,8 +100,12 @@ public void onChat(final AsyncPlayerChatEvent event) { } if (global && Config.BUNGEECORD.getBoolean()) { - String msgToSend = Utils.replacePlayerPlaceholders(player, format.replaceAll("%message", Utils.translateColorCodes(chatMessage, player))); - ChannelHandler.getInstance().sendMessage(player, msgToSend); + PlayerUsesRangeModeEvent playerUsesRangeModeEvent = new PlayerUsesRangeModeEvent(player, chatMessage); + Bukkit.getPluginManager().callEvent(playerUsesRangeModeEvent); + if(!playerUsesRangeModeEvent.isCancelled()){ + String msgToSend = Utils.replacePlayerPlaceholders(player, format.replaceAll("%message", Utils.translateColorCodes(playerUsesRangeModeEvent.getMessage(), player))); + ChannelHandler.getInstance().sendMessage(player, msgToSend); + } } format = format.replace("%message", "%2$s"); diff --git a/src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java b/src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java new file mode 100644 index 0000000..881c365 --- /dev/null +++ b/src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java @@ -0,0 +1,52 @@ +package de.jeter.chatex.api.events; + +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; + +public class PlayerUsesRangeModeEvent extends ChatExEvent { + private static final HandlerList handlers = new HandlerList(); + private String message; + private Player player; + + public PlayerUsesRangeModeEvent(Player player, String message){ + this.player = player; + this.message = message; + } + + /** + * @return the player which fired this event + */ + public Player getPlayer() { + return player; + } + + /** + * @return the message which the player would have written. + */ + public String getMessage() { + return message; + } + + /** + * + * @param message set the message which the player writes. + */ + public void setMessage(String message) { + this.message = message; + } + + @Override + public HandlerList getHandlers() { + return null; + } + + @Override + public boolean isCancelled() { + return false; + } + + @Override + public void setCancelled(boolean b) { + + } +} From 084524a5eb757399eade5bacf320218f6600104b Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Thu, 28 May 2020 15:50:26 +0200 Subject: [PATCH 11/12] [FIX] AntiSpamManager.java put is not static anymore and instance gets Initialized. --- src/main/java/de/jeter/chatex/ChatListener.java | 2 +- src/main/java/de/jeter/chatex/utils/AntiSpamManager.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index 4663431..af7706c 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -44,7 +44,7 @@ public void onChat(final AsyncPlayerChatEvent event) { return; } } - AntiSpamManager.put(event.getPlayer()); + AntiSpamManager.getInstance().put(event.getPlayer()); String format = PluginManager.getInstance().getMessageFormat(event.getPlayer()); Player player = event.getPlayer(); diff --git a/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java b/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java index 7873be7..0f0fd98 100644 --- a/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java +++ b/src/main/java/de/jeter/chatex/utils/AntiSpamManager.java @@ -8,9 +8,9 @@ public class AntiSpamManager { - private static final Map map = new HashMap<>(); - private static AntiSpamManager instance; - public static void put(Player chatter) { + private final Map map = new HashMap<>(); + private static AntiSpamManager instance = new AntiSpamManager(); + public void put(Player chatter) { map.put(chatter, System.currentTimeMillis()); } From cc0ee90355c51a2a4007178e0af20141b7c83959 Mon Sep 17 00:00:00 2001 From: worldOneo <62426504+worldOneo@users.noreply.github.com> Date: Thu, 28 May 2020 16:41:09 +0200 Subject: [PATCH 12/12] [FIX] Sending player messages if canceled [FIX] Mixed up PluginMessage and PlayerMessage in the spam event. [FIX] Cancels --- src/main/java/de/jeter/chatex/ChatListener.java | 8 ++++---- .../chatex/api/events/MessageBlockedByAdManagerEvent.java | 2 +- .../api/events/MessageBlockedBySpamManagerEvent.java | 2 +- .../api/events/MessageContainsBlockedWordEvent.java | 2 +- .../jeter/chatex/api/events/PlayerUsesRangeModeEvent.java | 7 ++++--- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/jeter/chatex/ChatListener.java b/src/main/java/de/jeter/chatex/ChatListener.java index af7706c..9b5dfe9 100644 --- a/src/main/java/de/jeter/chatex/ChatListener.java +++ b/src/main/java/de/jeter/chatex/ChatListener.java @@ -36,11 +36,11 @@ public void onChat(final AsyncPlayerChatEvent event) { if (!AntiSpamManager.getInstance().isAllowed(event.getPlayer())) { long remainingTime = AntiSpamManager.getInstance().getRemaingSeconds(event.getPlayer()); String message = Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", remainingTime + ""); - MessageBlockedBySpamManagerEvent messageBlockedBySpamManagerEvent = new MessageBlockedBySpamManagerEvent(event.getPlayer(),message, event.getMessage(),remainingTime); + MessageBlockedBySpamManagerEvent messageBlockedBySpamManagerEvent = new MessageBlockedBySpamManagerEvent(event.getPlayer(),event.getMessage(), message,remainingTime); Bukkit.getPluginManager().callEvent(messageBlockedBySpamManagerEvent); - event.getPlayer().sendMessage(messageBlockedBySpamManagerEvent.getPluginMessage()); event.setCancelled(!messageBlockedBySpamManagerEvent.isCancelled()); if(!messageBlockedBySpamManagerEvent.isCancelled()){ + event.getPlayer().sendMessage(messageBlockedBySpamManagerEvent.getPluginMessage()); return; } } @@ -54,10 +54,10 @@ public void onChat(final AsyncPlayerChatEvent event) { String message = Locales.MESSAGES_AD.getString(null).replaceAll("%perm", "chatex.bypassads"); MessageBlockedByAdManagerEvent messageBlockedByAdManagerEvent = new MessageBlockedByAdManagerEvent(player, chatMessage, message); Bukkit.getPluginManager().callEvent(messageBlockedByAdManagerEvent); - event.getPlayer().sendMessage(messageBlockedByAdManagerEvent.getPluginMessage()); event.setMessage(messageBlockedByAdManagerEvent.getMessage()); event.setCancelled(!messageBlockedByAdManagerEvent.isCancelled()); if(!messageBlockedByAdManagerEvent.isCancelled()){ + event.getPlayer().sendMessage(messageBlockedByAdManagerEvent.getPluginMessage()); return; } } @@ -67,8 +67,8 @@ public void onChat(final AsyncPlayerChatEvent event) { MessageContainsBlockedWordEvent messageContainsBlockedWordEvent = new MessageContainsBlockedWordEvent(player, chatMessage, message); Bukkit.getPluginManager().callEvent(messageContainsBlockedWordEvent); event.setCancelled(!messageContainsBlockedWordEvent.isCancelled()); - event.getPlayer().sendMessage(messageContainsBlockedWordEvent.getPluginMessage()); if(!messageContainsBlockedWordEvent.isCancelled()){ + event.getPlayer().sendMessage(messageContainsBlockedWordEvent.getPluginMessage()); return; } } diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java index f297b7a..4bc77e5 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedByAdManagerEvent.java @@ -5,7 +5,7 @@ public class MessageBlockedByAdManagerEvent extends ChatExEvent { private static final HandlerList handlers = new HandlerList(); - private boolean canceled; + private boolean canceled = false; private Player player; private String message; private String pluginMessage; diff --git a/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java index 839a19a..4d24369 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageBlockedBySpamManagerEvent.java @@ -9,7 +9,7 @@ public class MessageBlockedBySpamManagerEvent extends ChatExEvent { private String message; private long remainingTime; private String pluginMessage; - private boolean canceled = true; + private boolean canceled = false; /** * diff --git a/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java index 888290e..bb1af95 100644 --- a/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/MessageContainsBlockedWordEvent.java @@ -8,7 +8,7 @@ public class MessageContainsBlockedWordEvent extends ChatExEvent { private Player player; private String message; private String pluginMessage; - private boolean canceled = true; + private boolean canceled = false; /** * diff --git a/src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java b/src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java index 881c365..7fb3d54 100644 --- a/src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java +++ b/src/main/java/de/jeter/chatex/api/events/PlayerUsesRangeModeEvent.java @@ -7,6 +7,7 @@ public class PlayerUsesRangeModeEvent extends ChatExEvent { private static final HandlerList handlers = new HandlerList(); private String message; private Player player; + private boolean canceled = false; public PlayerUsesRangeModeEvent(Player player, String message){ this.player = player; @@ -37,16 +38,16 @@ public void setMessage(String message) { @Override public HandlerList getHandlers() { - return null; + return handlers; } @Override public boolean isCancelled() { - return false; + return canceled; } @Override public void setCancelled(boolean b) { - + canceled = b; } }