Add custom PAPI placeholders

This commit is contained in:
Draycia committed 2020-08-17 06:04:45 -07:00
1 parent 1ec38ec749
commit 34f138a96b
4 files changed
+92

No files matched your search

@@ -1,5 +1,6 @@
package net.draycia.carbon;
import me.clip.placeholderapi.PlaceholderAPI;
import net.draycia.carbon.channels.contexts.impl.DistanceContext;
import net.draycia.carbon.listeners.*;
import net.draycia.carbon.managers.*;
@@ -7,6 +8,7 @@ import net.draycia.carbon.messaging.MessageManager;
import net.draycia.carbon.storage.UserService;
import net.draycia.carbon.storage.impl.JSONUserService;
import net.draycia.carbon.storage.impl.MySQLUserService;
import net.draycia.carbon.util.CarbonPlaceholders;
import net.draycia.carbon.util.ItemStackUtils;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.milkbowl.vault.permission.Permission;
@@ -69,6 +71,8 @@ public final class CarbonChat extends JavaPlugin {
// Setup listeners
setupListeners();
registerContexts();
PlaceholderAPI.registerExpansion(new CarbonPlaceholders(this));
}
@Override
@@ -90,6 +90,8 @@ public abstract class ChatChannel implements ForwardingAudience {
public abstract Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying);
public abstract Boolean canPlayerSee(ChatUser target, boolean checkSpying);
/**
* @return If the channel should forward its formatting / formatted message to other servers
*/
@@ -255,6 +255,7 @@ public class CarbonChatChannel extends ChatChannel {
});
}
@Override
public Boolean canPlayerSee(ChatUser target, boolean checkSpying) {
Player targetPlayer = target.asPlayer();
@@ -0,0 +1,85 @@
package net.draycia.carbon.util;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.storage.ChatUser;
import org.bukkit.entity.Player;
public class CarbonPlaceholders extends PlaceholderExpansion {
private final CarbonChat carbonChat;
public CarbonPlaceholders(CarbonChat carbonChat) {
this.carbonChat = carbonChat;
}
@Override
public String getIdentifier() {
return "carbonchat";
}
@Override
public String getAuthor() {
return "Draycia (Vicarious#0001)";
}
@Override
public String getVersion() {
return "1.0.0";
}
@Override
public boolean persist() {
return true;
}
public String onPlaceholderRequest(final Player player, final String identifier) {
String key = identifier.toLowerCase();
if (key.startsWith("can_use_")) {
String value = key.replace("can_use_", "");
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(value);
if (channel == null) {
return "false";
}
ChatUser user = carbonChat.getUserService().wrap(player);
return channel.canPlayerUse(user) ? "true" : "false";
} else if (key.startsWith("can_see_")) {
String value = key.replace("can_see_", "");
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(value);
if (channel == null) {
return "false";
}
ChatUser user = carbonChat.getUserService().wrap(player);
return channel.canPlayerSee(user, true) ? "true" : "false";
} else if (key.startsWith("ignoring_channel_")) {
String value = key.replace("ignoring_channel_", "");
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(value);
if (channel == null) {
return "false";
}
ChatUser user = carbonChat.getUserService().wrap(player);
return user.getChannelSettings(channel).isIgnored() ? "true" : "false";
} else if (key.startsWith("selected_channel")) {
ChatChannel channel = carbonChat.getUserService().wrap(player).getSelectedChannel();
return channel == null ? carbonChat.getChannelManager().getDefaultChannel().getName() : channel.getName();
}
return null;
}
}