Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[MPR] Merge API into master #15

Merged
merged 13 commits into from
May 28, 2020
Merged
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<artifactId>ChatEx</artifactId>
<name>ChatEx</name>
<inceptionYear>2020</inceptionYear>

<version>2.5.0-SNAPSHOT</version>
<description>ChatManagement plugin for Bukkit</description>

Expand Down
61 changes: 44 additions & 17 deletions src/main/java/de/jeter/chatex/ChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
*/
package de.jeter.chatex;

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;
import de.jeter.chatex.utils.adManager.SimpleAdManager;
import de.jeter.chatex.utils.adManager.SmartAdManager;
import org.bukkit.Bukkit;
import java.util.UnknownFormatConversionException;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -45,27 +50,45 @@ public void onChat(final AsyncPlayerChatEvent event) {
return;
}

if (!AntiSpamManager.isAllowed(player)) {
event.getPlayer().sendMessage(Locales.ANTI_SPAM_DENIED.getString(event.getPlayer()).replaceAll("%time%", AntiSpamManager.getRemaingSeconds(event.getPlayer()) + ""));
event.setCancelled(true);
return;
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(),event.getMessage(), message,remainingTime);
Bukkit.getPluginManager().callEvent(messageBlockedBySpamManagerEvent);
event.setCancelled(!messageBlockedBySpamManagerEvent.isCancelled());
if(!messageBlockedBySpamManagerEvent.isCancelled()){
event.getPlayer().sendMessage(messageBlockedBySpamManagerEvent.getPluginMessage());
return;
}
}

AntiSpamManager.put(player);
String format = PluginManager.getInstance().getMessageFormat(player);

AntiSpamManager.getInstance().put(event.getPlayer());

String format = PluginManager.getInstance().getMessageFormat(event.getPlayer());
Player player = event.getPlayer();

String chatMessage = event.getMessage();

if (adManager.checkForAds(chatMessage, player)) {
event.getPlayer().sendMessage(Locales.MESSAGES_AD.getString(null).replaceAll("%perm", "chatex.bypassads"));
event.setCancelled(true);
return;
String message = Locales.MESSAGES_AD.getString(null).replaceAll("%perm", "chatex.bypassads");
MessageBlockedByAdManagerEvent messageBlockedByAdManagerEvent = new MessageBlockedByAdManagerEvent(player, chatMessage, message);
Bukkit.getPluginManager().callEvent(messageBlockedByAdManagerEvent);
event.setMessage(messageBlockedByAdManagerEvent.getMessage());
event.setCancelled(!messageBlockedByAdManagerEvent.isCancelled());
if(!messageBlockedByAdManagerEvent.isCancelled()){
event.getPlayer().sendMessage(messageBlockedByAdManagerEvent.getPluginMessage());
return;
}
}

if (Utils.checkForBlocked(chatMessage)) {
event.setCancelled(true);
event.getPlayer().sendMessage(Locales.MESSAGES_BLOCKED.getString(null));
return;
if (Utils.checkForBlocked(event.getMessage())) {
String message = Locales.MESSAGES_BLOCKED.getString(null);
MessageContainsBlockedWordEvent messageContainsBlockedWordEvent = new MessageContainsBlockedWordEvent(player, chatMessage, message);
Bukkit.getPluginManager().callEvent(messageContainsBlockedWordEvent);
event.setCancelled(!messageContainsBlockedWordEvent.isCancelled());
if(!messageContainsBlockedWordEvent.isCancelled()){
event.getPlayer().sendMessage(messageContainsBlockedWordEvent.getPluginMessage());
return;
}
}

boolean global = false;
Expand Down Expand Up @@ -95,8 +118,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");
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/de/jeter/chatex/api/ChatExAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 {
public String getPermissionHandlerName() {
return PluginManager.getInstance().getName();
}

public AntiSpamManager getAntiSpamManager(){
return AntiSpamManager.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);
}
}
7 changes: 7 additions & 0 deletions src/main/java/de/jeter/chatex/api/events/ChatExEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.jeter.chatex.api.events;

import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;

public abstract class ChatExEvent extends Event implements Cancellable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 = false;
private Player player;
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;
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;
}

/**
*
* @param message set the message which the player writes.
*/
public void setMessage(String message) {
this.message = message;
}

@Override
public boolean isCancelled() {
return canceled;
}

@Override
public void setCancelled(boolean b) {
canceled = b;
}

@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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;
private String pluginMessage;
private boolean canceled = false;

/**
*
* @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;
this.pluginMessage = pluginMessage;
this.remainingTime = remaining;
}

/**
* @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;
}

/**
* @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;
}

/**
* @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;
}

@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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;
private boolean canceled = false;

/**
*
* @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;
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;
}

/**
*
* @param message set the message which the player writes.
*/
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;
}

@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}

}
Loading