Add contexts and towny town support

This commit is contained in:
Draycia committed 2020-07-27 21:24:20 -07:00
1 parent e111416717
commit d5d066fcb6
15 files changed
+313 -135

No files matched your search

@@ -1,5 +1,6 @@
package net.draycia.simplechat;
import net.draycia.simplechat.channels.contexts.impl.DistanceContext;
import net.draycia.simplechat.listeners.*;
import net.draycia.simplechat.managers.*;
import net.draycia.simplechat.storage.UserService;
@@ -24,6 +25,7 @@ public final class SimpleChat extends JavaPlugin {
private ChannelManager channelManager;
private RedisManager redisManager = null;
private AdventureManager adventureManager;
private ContextManager contextManager;
private UserService userService;
@@ -50,6 +52,7 @@ public final class SimpleChat extends JavaPlugin {
pluginMessageManager = new PluginMessageManager(this);
commandManager = new CommandManager(this);
channelManager = new ChannelManager(this);
contextManager = new ContextManager();
if (getConfig().getBoolean("redis.enabled")) {
redisManager = new RedisManager(this);
@@ -69,6 +72,7 @@ public final class SimpleChat extends JavaPlugin {
// Setup listeners
setupListeners();
reloadPatterns();
registerContexts();
}
@Override
@@ -98,6 +102,10 @@ public final class SimpleChat extends JavaPlugin {
}
}
private void registerContexts() {
getContextManager().register("distance", new DistanceContext());
}
public Permission getPermission() {
return permission;
}
@@ -129,4 +137,8 @@ public final class SimpleChat extends JavaPlugin {
public AdventureManager getAdventureManager() {
return adventureManager;
}
public ContextManager getContextManager() {
return contextManager;
}
}
@@ -41,11 +41,6 @@ public abstract class ChatChannel {
public abstract String getAliases();
/**
* @return The distance other players must be within to the sender to see messages in this channel.
*/
public abstract Double getDistance();
/**
* @return The message to be sent to the player when switching to this channel.
*/
@@ -73,6 +68,10 @@ public abstract class ChatChannel {
public abstract Boolean firstMatchingGroup();
public abstract boolean testContext(ChatUser sender, ChatUser target);
public abstract Object getContext(String key);
/**
* @return If the player can use this channel.
*/
@@ -0,0 +1,11 @@
package net.draycia.simplechat.channels.contexts;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser;
public interface MessageContext {
ChatUser getSender();
ChatUser getTarget();
ChatChannel getChatChannel();
Object getValue();
}
@@ -0,0 +1,32 @@
package net.draycia.simplechat.channels.contexts.impl;
import net.draycia.simplechat.channels.contexts.MessageContext;
import org.bukkit.Location;
import java.util.function.Function;
public class DistanceContext implements Function<MessageContext, Boolean> {
@Override
public Boolean apply(MessageContext context) {
if (!context.getSender().isOnline() || !context.getTarget().isOnline()) {
return false;
}
Location senderLocation = context.getSender().asPlayer().getLocation();
Location targetLocation = context.getTarget().asPlayer().getLocation();
if (!senderLocation.getWorld().equals(targetLocation.getWorld())) {
return false;
}
Double value = (Double) context.getValue();
if (value > 0) {
return senderLocation.distance(targetLocation) <= value;
}
return true;
}
}
@@ -0,0 +1,39 @@
package net.draycia.simplechat.channels.contexts.impl;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.channels.contexts.MessageContext;
import net.draycia.simplechat.storage.ChatUser;
public class SimpleMessageContext implements MessageContext {
private final ChatUser sender;
private final ChatUser target;
private final ChatChannel chatChannel;
private final Object value;
public SimpleMessageContext(ChatUser sender, ChatUser target, ChatChannel chatChannel, Object value) {
this.sender = sender;
this.target = target;
this.chatChannel = chatChannel;
this.value = value;
}
@Override
public ChatUser getSender() {
return sender;
}
@Override
public ChatUser getTarget() {
return target;
}
@Override
public ChatChannel getChatChannel() {
return chatChannel;
}
@Override
public Object getValue() {
return value;
}
}
@@ -4,6 +4,7 @@ import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.events.ChatComponentEvent;
import net.draycia.simplechat.events.ChatFormatEvent;
import net.draycia.simplechat.managers.ContextManager;
import net.draycia.simplechat.storage.ChatUser;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
@@ -34,6 +35,11 @@ public class SimpleChatChannel extends ChatChannel {
return simpleChat;
}
@Override
public boolean testContext(ChatUser sender, ChatUser target) {
return simpleChat.getContextManager().testContext(sender, target, this);
}
@Override
public Boolean canPlayerUse(ChatUser user) {
return user.asPlayer().hasPermission("simplechat.channels." + getName() + ".use");
@@ -207,25 +213,6 @@ public class SimpleChatChannel extends ChatChannel {
return false;
}
if (sender.isOnline()) {
if (getDistance() == 0) {
if (!targetPlayer.getWorld().equals(sender.asPlayer().getWorld())) {
return false;
}
} else if (getDistance() > 0) {
Location one = targetPlayer.getLocation();
Location two = sender.asPlayer().getLocation();
if (!one.getWorld().equals(two.getWorld())) {
return false;
}
if (one.distance(two) > getDistance()) {
return false;
}
}
}
if (isIgnorable()) {
if (target.isIgnoringUser(sender) && !targetPlayer.hasPermission("simplechat.ignore.exempt")) {
return false;
@@ -287,11 +274,6 @@ public class SimpleChatChannel extends ChatChannel {
return name == null ? key : name;
}
@Override
public Double getDistance() {
return getDouble("distance");
}
@Override
public String getSwitchMessage() {
return getString("switch-message");
@@ -348,6 +330,29 @@ public class SimpleChatChannel extends ChatChannel {
return itemPatterns;
}
@Override
public Object getContext(String key) {
ConfigurationSection section = config.getConfigurationSection("contexts");
if (section == null) {
ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default");
if (defaultSection == null) {
return null;
}
ConfigurationSection defaultContexts = defaultSection.getConfigurationSection("contexts");
if (defaultContexts == null) {
return null;
}
return defaultContexts.get(key);
}
return section.get(key);
}
private String getString(String key) {
if (config != null && config.contains(key)) {
return config.getString(key);
@@ -0,0 +1,43 @@
package net.draycia.simplechat.managers;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.channels.contexts.MessageContext;
import net.draycia.simplechat.channels.contexts.impl.SimpleMessageContext;
import net.draycia.simplechat.storage.ChatUser;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class ContextManager {
private final Map<String, Function<MessageContext, Boolean>> handlers = new HashMap<>();
public boolean register(String key, Function<MessageContext, Boolean> handler) {
if (this.handlers.containsKey(key)) {
return false;
}
this.handlers.put(key, handler);
return true;
}
public boolean testContext(ChatUser user, ChatUser target, ChatChannel channel) {
for (Map.Entry<String, Function<MessageContext, Boolean>> handler : handlers.entrySet()) {
String key = handler.getKey();
Object value = channel.getContext(key);
if (value == null) {
continue;
}
MessageContext context = new SimpleMessageContext(user, target, channel, value);
return handler.getValue().apply(context);
}
return true;
}
}
@@ -21,12 +21,15 @@ default:
# Also supports PlaceholderAPI
staff: '<gray>{[%vault_prefix%<gray>]} %player_displayname% <gray>» <color><pre><message>'
default: '<gray>[%vault_prefix%<gray>] %player_displayname%<gray>: <color><pre><message>'
contexts:
distance: -1 # -1 to disable, 0 to set to world, 1+ to fully enable
# mcmmo-party only works when the SiumpleChatMCMMO addon is installed.
# Requires both players to be in the same party to see the message.
mcmmo-party: false
color: '#FFFFFF'
# Can players /toggle this channel and hide its messages?
ignorable: true
# -1 = disabled, 0 = world chat, greater than 0 = enabled
distance: -1
# Forces all other servers to use this server's formatting for this channel
forward-format: true
# Enables/disables cross server chat for this channel
should-bungee: true