9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2025-12-19 15:09:23 +00:00

channel mode

This commit is contained in:
XiaoMoMi
2024-02-04 18:43:55 +08:00
parent 7811cea007
commit e450b1429d
18 changed files with 344 additions and 62 deletions

View File

@@ -18,6 +18,7 @@
package net.momirealms.customnameplates.api.manager; package net.momirealms.customnameplates.api.manager;
import net.momirealms.customnameplates.api.mechanic.bubble.Bubble; import net.momirealms.customnameplates.api.mechanic.bubble.Bubble;
import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -26,6 +27,21 @@ import java.util.List;
public interface BubbleManager { public interface BubbleManager {
/**
* Set a custom chat provider
*
* @param provider provider
* @return success or not
*/
boolean setCustomChatProvider(AbstractChatProvider provider);
/**
* Remove a custom chat provider
*
* @return success or not
*/
boolean removeCustomChatProvider();
/** /**
* Register a bubble into map * Register a bubble into map
* *
@@ -118,6 +134,8 @@ public interface BubbleManager {
*/ */
String getDefaultBubble(); String getDefaultBubble();
void onChat(Player player, String text, String channel);
/** /**
* Get all the bubbles' keys * Get all the bubbles' keys
* *

View File

@@ -0,0 +1,25 @@
/*
* Copyright (C) <2022> <XiaoMoMi>
*
* 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 3 of the License, or
* 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, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customnameplates.api.mechanic.bubble;
public enum ChannelMode {
ALL,
JOINED,
CAN_JOIN
}

View File

@@ -15,20 +15,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.momirealms.customnameplates.api.mechanic.bubble.listener; package net.momirealms.customnameplates.api.mechanic.bubble.provider;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
public abstract class AbstractChatListener implements Listener { public abstract class AbstractChatProvider implements Listener {
protected BubbleManager chatBubblesManager; protected BubbleManager chatBubblesManager;
public AbstractChatListener(BubbleManager chatBubblesManager) { public AbstractChatProvider(BubbleManager chatBubblesManager) {
this.chatBubblesManager = chatBubblesManager; this.chatBubblesManager = chatBubblesManager;
} }
public abstract void register(); public abstract void register();
public abstract void unregister(); public abstract void unregister();
public abstract boolean hasJoinedChannel(Player player, String channelID);
public abstract boolean canJoinChannel(Player player, String channelID);
} }

View File

@@ -44,9 +44,11 @@ public class ViewerText {
for (String placeholder : placeholders) { for (String placeholder : placeholders) {
processedText = processedText.replace(placeholder, "%s"); processedText = processedText.replace(placeholder, "%s");
if (placeholder.startsWith("%viewer_")) { if (placeholder.startsWith("%viewer_")) {
this.placeholders[i] = new ClaimedText(null, "%" + placeholder.substring("%viewer_".length())); this.placeholders[i] = new ClaimedText(null, "%" + placeholder.substring("%viewer_".length()), false);
} else if (placeholder.startsWith("%rel_")) {
this.placeholders[i] = new ClaimedText(owner, placeholder, true);
} else { } else {
this.placeholders[i] = new ClaimedText(owner, placeholder); this.placeholders[i] = new ClaimedText(owner, placeholder, false);
} }
i++; i++;
} }
@@ -109,11 +111,13 @@ public class ViewerText {
private final String placeholder; private final String placeholder;
private final Player owner; private final Player owner;
private String latestValue; private String latestValue;
private final boolean relational;
public ClaimedText(Player owner, String placeholder) { public ClaimedText(Player owner, String placeholder, boolean relational) {
this.placeholder = placeholder; this.placeholder = placeholder;
this.owner = owner; this.owner = owner;
this.latestValue = null; this.latestValue = null;
this.relational = relational;
this.update(); this.update();
} }
@@ -125,7 +129,13 @@ public class ViewerText {
public String getValue(Player viewer) { public String getValue(Player viewer) {
return Objects.requireNonNullElseGet( return Objects.requireNonNullElseGet(
latestValue, latestValue,
() -> PlaceholderAPI.setPlaceholders(owner == null ? viewer : owner, placeholder) () -> {
if (relational) {
return PlaceholderAPI.setRelationalPlaceholders(viewer, owner, placeholder);
} else {
return PlaceholderAPI.setPlaceholders(owner == null ? viewer : owner, placeholder);
}
}
); );
} }
} }

View File

@@ -7,7 +7,7 @@ plugins {
allprojects { allprojects {
version = "2.3.2.0" version = "2.3.2.1"
apply<JavaPlugin>() apply<JavaPlugin>()
apply(plugin = "java") apply(plugin = "java")

View File

@@ -25,7 +25,8 @@ import net.momirealms.customnameplates.api.event.BubblesSpawnEvent;
import net.momirealms.customnameplates.api.event.NameplateDataLoadEvent; import net.momirealms.customnameplates.api.event.NameplateDataLoadEvent;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import net.momirealms.customnameplates.api.mechanic.bubble.Bubble; import net.momirealms.customnameplates.api.mechanic.bubble.Bubble;
import net.momirealms.customnameplates.api.mechanic.bubble.listener.AbstractChatListener; import net.momirealms.customnameplates.api.mechanic.bubble.ChannelMode;
import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import net.momirealms.customnameplates.api.mechanic.character.CharacterArranger; import net.momirealms.customnameplates.api.mechanic.character.CharacterArranger;
import net.momirealms.customnameplates.api.mechanic.character.ConfiguredChar; import net.momirealms.customnameplates.api.mechanic.character.ConfiguredChar;
import net.momirealms.customnameplates.api.mechanic.tag.unlimited.EntityTagPlayer; import net.momirealms.customnameplates.api.mechanic.tag.unlimited.EntityTagPlayer;
@@ -38,7 +39,7 @@ import net.momirealms.customnameplates.paper.adventure.AdventureManagerImpl;
import net.momirealms.customnameplates.paper.mechanic.bubble.image.ImageParser; import net.momirealms.customnameplates.paper.mechanic.bubble.image.ImageParser;
import net.momirealms.customnameplates.paper.mechanic.bubble.image.ItemsAdderImageImpl; import net.momirealms.customnameplates.paper.mechanic.bubble.image.ItemsAdderImageImpl;
import net.momirealms.customnameplates.paper.mechanic.bubble.image.OraxenImageImpl; import net.momirealms.customnameplates.paper.mechanic.bubble.image.OraxenImageImpl;
import net.momirealms.customnameplates.paper.mechanic.bubble.listener.*; import net.momirealms.customnameplates.paper.mechanic.bubble.provider.*;
import net.momirealms.customnameplates.paper.setting.CNConfig; import net.momirealms.customnameplates.paper.setting.CNConfig;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@@ -46,6 +47,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -54,9 +57,10 @@ import java.io.File;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class BubbleManagerImpl implements BubbleManager { public class BubbleManagerImpl implements BubbleManager, Listener {
private AbstractChatListener chatListener; private AbstractChatProvider chatProvider;
private AbstractChatProvider customProvider;
private ImageParser imageParser; private ImageParser imageParser;
private final HashMap<String, Bubble> bubbleMap; private final HashMap<String, Bubble> bubbleMap;
private final CustomNameplatesPluginImpl plugin; private final CustomNameplatesPluginImpl plugin;
@@ -73,6 +77,7 @@ public class BubbleManagerImpl implements BubbleManager {
private int lengthPerLine; private int lengthPerLine;
private int subStringIndex; private int subStringIndex;
private String[] blacklistChannels; private String[] blacklistChannels;
private ChannelMode channelMode;
public BubbleManagerImpl(CustomNameplatesPluginImpl plugin) { public BubbleManagerImpl(CustomNameplatesPluginImpl plugin) {
this.plugin = plugin; this.plugin = plugin;
@@ -88,14 +93,16 @@ public class BubbleManagerImpl implements BubbleManager {
if (!CNConfig.bubbleModule) return; if (!CNConfig.bubbleModule) return;
this.loadConfig(); this.loadConfig();
this.loadBubbles(); this.loadBubbles();
this.registerListener(); this.registerChatProvider();
this.registerImageParser(); this.registerImageParser();
Bukkit.getPluginManager().registerEvents(this, plugin);
} }
public void unload() { public void unload() {
this.imageParser = null; this.imageParser = null;
this.bubbleMap.clear(); this.bubbleMap.clear();
if (chatListener != null) chatListener.unregister(); if (chatProvider != null) chatProvider.unregister();
HandlerList.unregisterAll(this);
} }
private void loadConfig() { private void loadConfig() {
@@ -113,6 +120,7 @@ public class BubbleManagerImpl implements BubbleManager {
lengthPerLine = config.getInt("characters-per-line", 30); lengthPerLine = config.getInt("characters-per-line", 30);
startFormat = config.getString("default-format.start", "<gradient:#F5F5F5:#E1FFFF:#F5F5F5><u>"); startFormat = config.getString("default-format.start", "<gradient:#F5F5F5:#E1FFFF:#F5F5F5><u>");
endFormat = config.getString("default-format.end", "<!u></gradient>"); endFormat = config.getString("default-format.end", "<!u></gradient>");
channelMode = ChannelMode.valueOf(config.getString("channel-mode","all").toUpperCase(Locale.ENGLISH));
} }
private void registerImageParser() { private void registerImageParser() {
@@ -124,24 +132,47 @@ public class BubbleManagerImpl implements BubbleManager {
} }
} }
private void registerListener() { private void registerChatProvider() {
if (CNConfig.trChatChannel) { if (this.customProvider != null) {
this.chatListener = new TrChatListener(this); this.chatProvider = customProvider;
} else if (CNConfig.trChatChannel) {
this.chatProvider = new TrChatProvider(this);
} else if (CNConfig.ventureChatChannel) { } else if (CNConfig.ventureChatChannel) {
this.chatListener = new VentureChatListener(this); this.chatProvider = new VentureChatProvider(this);
} else if (CNConfig.huskChatChannel) { } else if (CNConfig.huskChatChannel) {
this.chatListener = new HuskChatListener(this); this.chatProvider = new HuskChatProvider(this);
} else if (CNConfig.carbonChatChannel) { } else if (CNConfig.carbonChatChannel) {
this.chatListener = new CarbonChatListener(this); this.chatProvider = new CarbonChatProvider(this);
} else { } else {
try { try {
Class.forName("io.papermc.paper.event.player.AsyncChatEvent"); Class.forName("io.papermc.paper.event.player.AsyncChatEvent");
this.chatListener = new PaperAsyncChatListener(this); this.chatProvider = new PaperAsyncChatProvider(this);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
this.chatListener = new AsyncChatListener(this); this.chatProvider = new AsyncChatProvider(this);
} }
} }
this.chatListener.register(); this.chatProvider.register();
}
@Override
public boolean setCustomChatProvider(AbstractChatProvider provider) {
if (this.customProvider != null)
return false;
this.customProvider = provider;
if (chatProvider != null) chatProvider.unregister();
this.registerChatProvider();
return true;
}
@Override
public boolean removeCustomChatProvider() {
if (this.customProvider != null) {
this.customProvider.unregister();
this.customProvider = null;
this.registerChatProvider();
return true;
}
return false;
} }
private void loadBubbles() { private void loadBubbles() {
@@ -214,7 +245,13 @@ public class BubbleManagerImpl implements BubbleManager {
return this.bubbleMap.remove(key) != null; return this.bubbleMap.remove(key) != null;
} }
@Override
public void onChat(Player player, String text) { public void onChat(Player player, String text) {
onChat(player, text, null);
}
@Override
public void onChat(Player player, String text, String channel) {
if ( player.getGameMode() == GameMode.SPECTATOR if ( player.getGameMode() == GameMode.SPECTATOR
|| player.hasPotionEffect(PotionEffectType.INVISIBILITY) || player.hasPotionEffect(PotionEffectType.INVISIBILITY)
|| !player.hasPermission("bubbles.use") || !player.hasPermission("bubbles.use")
@@ -270,14 +307,14 @@ public class BubbleManagerImpl implements BubbleManager {
int finalIndex = i; int finalIndex = i;
String finalBubble = bubble; String finalBubble = bubble;
plugin.getScheduler().runTaskAsyncLater( plugin.getScheduler().runTaskAsyncLater(
() -> sendBubble(player, split[finalIndex], bubbleConfig, finalBubble), () -> sendBubble(player, split[finalIndex], bubbleConfig, finalBubble, channel),
(long) i * 250 + 100, (long) i * 250 + 100,
TimeUnit.MILLISECONDS TimeUnit.MILLISECONDS
); );
} }
} }
private void sendBubble(Player player, String text, Bubble bubbleConfig, String key) { private void sendBubble(Player player, String text, Bubble bubbleConfig, String key, @Nullable String channel) {
if (key.equals("none")) { if (key.equals("none")) {
text = startFormat + PlaceholderAPI.setPlaceholders(player, prefix) + text + PlaceholderAPI.setPlaceholders(player, suffix) + endFormat; text = startFormat + PlaceholderAPI.setPlaceholders(player, prefix) + text + PlaceholderAPI.setPlaceholders(player, suffix) + endFormat;
} else { } else {
@@ -291,7 +328,20 @@ public class BubbleManagerImpl implements BubbleManager {
StaticTextEntity entity = tagPlayer.addTag(StaticTextTagSetting.builder() StaticTextEntity entity = tagPlayer.addTag(StaticTextTagSetting.builder()
.leaveRule((p, e) -> true) .leaveRule((p, e) -> true)
.comeRule((p, e) -> true) .comeRule((p, e) -> {
switch (channelMode) {
case ALL -> {
return true;
}
case JOINED -> {
return channel == null || chatProvider.hasJoinedChannel(p, channel);
}
case CAN_JOIN -> {
return channel == null || chatProvider.canJoinChannel(p, channel);
}
}
return false;
})
.verticalOffset(yOffset) .verticalOffset(yOffset)
.defaultText(text) .defaultText(text)
.plugin("bubble") .plugin("bubble")

View File

@@ -15,19 +15,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.momirealms.customnameplates.paper.mechanic.bubble.listener; package net.momirealms.customnameplates.paper.mechanic.bubble.provider;
import net.momirealms.customnameplates.api.CustomNameplatesPlugin; import net.momirealms.customnameplates.api.CustomNameplatesPlugin;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import net.momirealms.customnameplates.api.mechanic.bubble.listener.AbstractChatListener; import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
public class AsyncChatListener extends AbstractChatListener { public class AsyncChatProvider extends AbstractChatProvider {
public AsyncChatListener(BubbleManager chatBubblesManager) { public AsyncChatProvider(BubbleManager chatBubblesManager) {
super(chatBubblesManager); super(chatBubblesManager);
} }
@@ -41,6 +42,17 @@ public class AsyncChatListener extends AbstractChatListener {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }
@Override
public boolean hasJoinedChannel(Player player, String channelID) {
return true;
}
@Override
public boolean canJoinChannel(Player player, String channelID) {
return true;
}
// This event is not async sometimes
@EventHandler (ignoreCancelled = true) @EventHandler (ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent event) { public void onChat(AsyncPlayerChatEvent event) {
CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> chatBubblesManager.onChat(event.getPlayer(), event.getMessage())); CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> chatBubblesManager.onChat(event.getPlayer(), event.getMessage()));

View File

@@ -15,16 +15,21 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.momirealms.customnameplates.paper.mechanic.bubble.listener; package net.momirealms.customnameplates.paper.mechanic.bubble.provider;
import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider; import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEventSubscription; import net.draycia.carbon.api.event.CarbonEventSubscription;
import net.draycia.carbon.api.event.events.CarbonChatEvent; import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.paper.CarbonChatPaper;
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
import net.kyori.adventure.key.Key;
import net.momirealms.customnameplates.api.CustomNameplatesPlugin; import net.momirealms.customnameplates.api.CustomNameplatesPlugin;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import net.momirealms.customnameplates.api.mechanic.bubble.listener.AbstractChatListener; import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import net.momirealms.customnameplates.api.util.LogUtils;
import net.momirealms.customnameplates.paper.util.ReflectionUtils; import net.momirealms.customnameplates.paper.util.ReflectionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -32,19 +37,21 @@ import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class CarbonChatListener extends AbstractChatListener { public class CarbonChatProvider extends AbstractChatProvider {
private final CarbonChat api; private final CarbonChat api;
private CarbonEventSubscription<CarbonChatEvent> subscription; private CarbonEventSubscription<CarbonChatEvent> subscription;
private Method originalMessageMethod; private Method originalMessageMethod;
private Method channelKeyMethod; private Method channelKeyMethod;
private Method getChannelByKeyMethod;
public CarbonChatListener(BubbleManager chatBubblesManager) { public CarbonChatProvider(BubbleManager chatBubblesManager) {
super(chatBubblesManager); super(chatBubblesManager);
this.api = CarbonChatProvider.carbonChat(); this.api = net.draycia.carbon.api.CarbonChatProvider.carbonChat();
try { try {
this.originalMessageMethod = CarbonChatEvent.class.getMethod("originalMessage"); this.originalMessageMethod = CarbonChatEvent.class.getMethod("originalMessage");
this.channelKeyMethod = ChatChannel.class.getMethod("key"); this.channelKeyMethod = ChatChannel.class.getMethod("key");
this.getChannelByKeyMethod = ChannelRegistry.class.getMethod("channel", ReflectionUtils.getKeyClass());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -67,7 +74,7 @@ public class CarbonChatListener extends AbstractChatListener {
return; return;
Object component = getComponentFromEvent(event); Object component = getComponentFromEvent(event);
String message = ReflectionUtils.getMiniMessageTextFromNonShadedComponent(component); String message = ReflectionUtils.getMiniMessageTextFromNonShadedComponent(component);
CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> chatBubblesManager.onChat(player, message)); CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> chatBubblesManager.onChat(player, message, channel));
}); });
} }
@@ -78,6 +85,51 @@ public class CarbonChatListener extends AbstractChatListener {
} }
} }
@Override
public boolean hasJoinedChannel(Player player, String channelID) {
CarbonPlayer cPlayer = null;
for (CarbonPlayer carbonPlayer : api.server().players()) {
if (carbonPlayer.uuid().equals(player.getUniqueId())) {
cPlayer = carbonPlayer;
break;
}
}
if (cPlayer == null) {
return false;
}
ChatChannel selectedChannel = cPlayer.selectedChannel();
if (selectedChannel == null) {
return false;
}
Object key = getChannelKey(selectedChannel);
String str = ReflectionUtils.getKeyAsString(key);
return str.equals(channelID);
}
@Override
public boolean canJoinChannel(Player player, String channelID) {
ChannelRegistry registry = api.channelRegistry();
Object key = ReflectionUtils.getKerFromString(channelID);
if (key == null) {
return false;
}
ChatChannel channel = null;
try {
channel = (ChatChannel) getChannelByKeyMethod.invoke(registry, key);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
if (channel == null) {
LogUtils.warn("Channel " + channelID + " doesn't exist.");
return false;
}
String perm = channel.permission();
if (perm == null) {
return true;
}
return player.hasPermission(perm);
}
private Object getChannelKey(ChatChannel channel) { private Object getChannelKey(ChatChannel channel) {
try { try {
return this.channelKeyMethod.invoke(channel); return this.channelKeyMethod.invoke(channel);

View File

@@ -15,20 +15,26 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.momirealms.customnameplates.paper.mechanic.bubble.listener; package net.momirealms.customnameplates.paper.mechanic.bubble.provider;
import me.arasple.mc.trchat.TrChat;
import me.arasple.mc.trchat.api.TrChatAPI;
import net.momirealms.customnameplates.api.CustomNameplatesPlugin; import net.momirealms.customnameplates.api.CustomNameplatesPlugin;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import net.momirealms.customnameplates.api.mechanic.bubble.listener.AbstractChatListener; import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import net.momirealms.customnameplates.api.util.LogUtils;
import net.william278.huskchat.HuskChat;
import net.william278.huskchat.bukkit.BukkitHuskChat;
import net.william278.huskchat.bukkit.event.ChatMessageEvent; import net.william278.huskchat.bukkit.event.ChatMessageEvent;
import net.william278.huskchat.channel.Channel;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class HuskChatListener extends AbstractChatListener { public class HuskChatProvider extends AbstractChatProvider {
public HuskChatListener(BubbleManager chatBubblesManager) { public HuskChatProvider(BubbleManager chatBubblesManager) {
super(chatBubblesManager); super(chatBubblesManager);
} }
@@ -42,6 +48,30 @@ public class HuskChatListener extends AbstractChatListener {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }
@Override
public boolean hasJoinedChannel(Player player, String channelID) {
String channel = BukkitHuskChat.getInstance().getPlayerCache().getPlayerChannel(player.getUniqueId());
if (channel == null) {
LogUtils.warn("Channel " + channelID + " doesn't exist.");
return false;
}
return channel.equals(channelID);
}
@Override
public boolean canJoinChannel(Player player, String channelID) {
Channel channel = BukkitHuskChat.getInstance().getSettings().getChannels().get(channelID);
if (channel == null) {
LogUtils.warn("Channel " + channelID + " doesn't exist.");
return false;
}
String receivePerm = channel.getReceivePermission();
if (receivePerm == null) {
return true;
}
return player.hasPermission(receivePerm);
}
@EventHandler (ignoreCancelled = true) @EventHandler (ignoreCancelled = true)
public void onHuskChat(ChatMessageEvent event) { public void onHuskChat(ChatMessageEvent event) {
String channel = event.getChannelId(); String channel = event.getChannelId();
@@ -52,7 +82,7 @@ public class HuskChatListener extends AbstractChatListener {
if (player == null || !player.isOnline()) if (player == null || !player.isOnline())
return; return;
CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> { CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> {
chatBubblesManager.onChat(player, event.getMessage()); chatBubblesManager.onChat(player, event.getMessage(), channel);
}); });
} }
} }

View File

@@ -15,25 +15,26 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.momirealms.customnameplates.paper.mechanic.bubble.listener; package net.momirealms.customnameplates.paper.mechanic.bubble.provider;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
import net.momirealms.customnameplates.api.CustomNameplatesPlugin; import net.momirealms.customnameplates.api.CustomNameplatesPlugin;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import net.momirealms.customnameplates.api.mechanic.bubble.listener.AbstractChatListener; import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import net.momirealms.customnameplates.paper.util.ReflectionUtils; import net.momirealms.customnameplates.paper.util.ReflectionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class PaperAsyncChatListener extends AbstractChatListener { public class PaperAsyncChatProvider extends AbstractChatProvider {
private Method messageMethod; private Method messageMethod;
public PaperAsyncChatListener(BubbleManager chatBubblesManager) { public PaperAsyncChatProvider(BubbleManager chatBubblesManager) {
super(chatBubblesManager); super(chatBubblesManager);
try { try {
this.messageMethod = AsyncChatEvent.class.getMethod("message"); this.messageMethod = AsyncChatEvent.class.getMethod("message");
@@ -52,6 +53,16 @@ public class PaperAsyncChatListener extends AbstractChatListener {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }
@Override
public boolean hasJoinedChannel(Player player, String channelID) {
return true;
}
@Override
public boolean canJoinChannel(Player player, String channelID) {
return true;
}
@EventHandler (ignoreCancelled = true) @EventHandler (ignoreCancelled = true)
public void onChat(AsyncChatEvent event) { public void onChat(AsyncChatEvent event) {
Object component = getComponentFromEvent(event); Object component = getComponentFromEvent(event);

View File

@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.momirealms.customnameplates.paper.mechanic.bubble.listener; package net.momirealms.customnameplates.paper.mechanic.bubble.provider;
import me.arasple.mc.trchat.TrChat; import me.arasple.mc.trchat.TrChat;
import me.arasple.mc.trchat.api.event.TrChatEvent; import me.arasple.mc.trchat.api.event.TrChatEvent;
@@ -24,16 +24,18 @@ import me.arasple.mc.trchat.module.internal.filter.FilteredObject;
import me.arasple.mc.trchat.taboolib.platform.BukkitAdapter; import me.arasple.mc.trchat.taboolib.platform.BukkitAdapter;
import net.momirealms.customnameplates.api.CustomNameplatesPlugin; import net.momirealms.customnameplates.api.CustomNameplatesPlugin;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import net.momirealms.customnameplates.api.mechanic.bubble.listener.AbstractChatListener; import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import net.momirealms.customnameplates.api.util.LogUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class TrChatListener extends AbstractChatListener { public class TrChatProvider extends AbstractChatProvider {
private final BukkitAdapter adapter; private final BukkitAdapter adapter;
public TrChatListener(BubbleManager chatBubblesManager) { public TrChatProvider(BubbleManager chatBubblesManager) {
super(chatBubblesManager); super(chatBubblesManager);
this.adapter = new BukkitAdapter(); this.adapter = new BukkitAdapter();
} }
@@ -48,6 +50,27 @@ public class TrChatListener extends AbstractChatListener {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }
@Override
public boolean hasJoinedChannel(Player player, String channelID) {
if (TrChat.INSTANCE.api().getChannelManager().getChannel(channelID) instanceof Channel channel) {
return channel.getListeners().contains(player.getName());
} else {
LogUtils.warn("Channel " + channelID + " doesn't exist.");
return false;
}
}
@Override
public boolean canJoinChannel(Player player, String channelID) {
if (TrChat.INSTANCE.api().getChannelManager().getChannel(channelID) instanceof Channel channel) {
String perm = channel.getSettings().getJoinPermission();
return player.hasPermission(perm);
} else {
LogUtils.warn("Channel " + channelID + " doesn't exist.");
return false;
}
}
@EventHandler (ignoreCancelled = true) @EventHandler (ignoreCancelled = true)
public void onTrChat(TrChatEvent event) { public void onTrChat(TrChatEvent event) {
if (!event.getForward()) return; if (!event.getForward()) return;
@@ -58,7 +81,7 @@ public class TrChatListener extends AbstractChatListener {
} }
FilteredObject object = TrChat.INSTANCE.api().getFilterManager().filter(event.getMessage(), adapter.adaptPlayer(event.getPlayer()), true); FilteredObject object = TrChat.INSTANCE.api().getFilterManager().filter(event.getMessage(), adapter.adaptPlayer(event.getPlayer()), true);
CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> { CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> {
chatBubblesManager.onChat(event.getSession().getPlayer(), object.getFiltered()); chatBubblesManager.onChat(event.getSession().getPlayer(), object.getFiltered(), channelName);
}); });
} }
} }

View File

@@ -15,20 +15,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.momirealms.customnameplates.paper.mechanic.bubble.listener; package net.momirealms.customnameplates.paper.mechanic.bubble.provider;
import mineverse.Aust1n46.chat.api.MineverseChatAPI;
import mineverse.Aust1n46.chat.api.MineverseChatPlayer; import mineverse.Aust1n46.chat.api.MineverseChatPlayer;
import mineverse.Aust1n46.chat.api.events.VentureChatEvent; import mineverse.Aust1n46.chat.api.events.VentureChatEvent;
import mineverse.Aust1n46.chat.channel.ChatChannel;
import net.momirealms.customnameplates.api.CustomNameplatesPlugin; import net.momirealms.customnameplates.api.CustomNameplatesPlugin;
import net.momirealms.customnameplates.api.manager.BubbleManager; import net.momirealms.customnameplates.api.manager.BubbleManager;
import net.momirealms.customnameplates.api.mechanic.bubble.listener.AbstractChatListener; import net.momirealms.customnameplates.api.mechanic.bubble.provider.AbstractChatProvider;
import net.momirealms.customnameplates.api.util.LogUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class VentureChatListener extends AbstractChatListener { public class VentureChatProvider extends AbstractChatProvider {
public VentureChatListener(BubbleManager chatBubblesManager) { public VentureChatProvider(BubbleManager chatBubblesManager) {
super(chatBubblesManager); super(chatBubblesManager);
} }
@@ -42,6 +46,25 @@ public class VentureChatListener extends AbstractChatListener {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }
@Override
public boolean hasJoinedChannel(Player player, String channelID) {
MineverseChatPlayer mcp = MineverseChatAPI.getOnlineMineverseChatPlayer(player);
return mcp.getCurrentChannel().getName().equals(channelID);
}
@Override
public boolean canJoinChannel(Player player, String channelID) {
ChatChannel channel = ChatChannel.getChannel(channelID);
if (channel == null) {
LogUtils.warn("Channel " + channelID + " doesn't exist.");
return false;
}
if (channel.hasPermission()) {
return player.hasPermission(channel.getPermission());
}
return true;
}
@EventHandler (ignoreCancelled = true) @EventHandler (ignoreCancelled = true)
public void onVentureChat(VentureChatEvent event) { public void onVentureChat(VentureChatEvent event) {
String channelName = event.getChannel().getName(); String channelName = event.getChannel().getName();
@@ -53,7 +76,7 @@ public class VentureChatListener extends AbstractChatListener {
return; return;
} }
CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> { CustomNameplatesPlugin.get().getScheduler().runTaskAsync(() -> {
chatBubblesManager.onChat(chatPlayer.getPlayer(), event.getChat()); chatBubblesManager.onChat(chatPlayer.getPlayer(), event.getChat(), channelName);
}); });
} }
} }

View File

@@ -92,7 +92,9 @@ public class UnlimitedEntity implements EntityTagEntity {
} }
staticTags.add(tag); staticTags.add(tag);
for (Player all : nearbyPlayers) { for (Player all : nearbyPlayers) {
tag.addPlayerToViewers(all); if (tag.getComeRule().isPassed(all, entity)) {
tag.addPlayerToViewers(all);
}
} }
} }
@@ -137,7 +139,6 @@ public class UnlimitedEntity implements EntityTagEntity {
} }
} }
public void move(Player receiver, short x, short y, short z, boolean onGround) { public void move(Player receiver, short x, short y, short z, boolean onGround) {
for (StaticTextEntity tag : staticTags) { for (StaticTextEntity tag : staticTags) {
tag.move(receiver, x, y, z, onGround); tag.move(receiver, x, y, z, onGround);

View File

@@ -61,7 +61,9 @@ public class UnlimitedPlayer extends UnlimitedEntity implements EntityTagPlayer
} }
staticTags.add(tag); staticTags.add(tag);
for (Player all : nearbyPlayers) { for (Player all : nearbyPlayers) {
tag.addPlayerToViewers(all); if (tag.getComeRule().isPassed(all, owner)) {
tag.addPlayerToViewers(all);
}
} }
} }

View File

@@ -152,7 +152,6 @@ public class RedisManager extends AbstractStorage {
try (Jedis jedis = jedisPool.getResource()) { try (Jedis jedis = jedisPool.getResource()) {
byte[] key = getRedisKey("cn_data", uuid); byte[] key = getRedisKey("cn_data", uuid);
byte[] data = jedis.get(key); byte[] data = jedis.get(key);
jedis.del(key);
if (data != null) { if (data != null) {
future.complete(Optional.of(plugin.getStorageManager().fromBytes(data))); future.complete(Optional.of(plugin.getStorageManager().fromBytes(data)));
plugin.debug("Redis data retrieved for " + uuid + "; normal data"); plugin.debug("Redis data retrieved for " + uuid + "; normal data");

View File

@@ -55,7 +55,6 @@ public abstract class AbstractHikariDatabase extends AbstractSQLDatabase impleme
} else if (getStorageType() == StorageType.MySQL) { } else if (getStorageType() == StorageType.MySQL) {
try { try {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
LogUtils.warn("It seems that you are not using MySQL 8.0+. It's recommended to update.");
} catch (ClassNotFoundException e2) { } catch (ClassNotFoundException e2) {
LogUtils.warn("No MySQL driver is found"); LogUtils.warn("No MySQL driver is found");
} }

View File

@@ -18,6 +18,7 @@
package net.momirealms.customnameplates.paper.util; package net.momirealms.customnameplates.paper.util;
import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftReflection;
import net.kyori.adventure.key.Key;
import net.momirealms.customnameplates.api.util.LogUtils; import net.momirealms.customnameplates.api.util.LogUtils;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@@ -36,7 +37,9 @@ public class ReflectionUtils {
private static Object emptyComponent; private static Object emptyComponent;
private static Method serializeComponentMethod; private static Method serializeComponentMethod;
private static Method keyAsStringMethod; private static Method keyAsStringMethod;
private static Method keyFromStringMethod;
private static Object miniMessageInstance; private static Object miniMessageInstance;
private static Class<?> keyClass;
public static void load() { public static void load() {
try { try {
@@ -63,8 +66,9 @@ public class ReflectionUtils {
Method miniMessageInstanceGetMethod = miniMessageClass.getMethod("miniMessage"); Method miniMessageInstanceGetMethod = miniMessageClass.getMethod("miniMessage");
miniMessageInstance = miniMessageInstanceGetMethod.invoke(null); miniMessageInstance = miniMessageInstanceGetMethod.invoke(null);
serializeComponentMethod = miniMessageClass.getMethod("serialize", componentClass); serializeComponentMethod = miniMessageClass.getMethod("serialize", componentClass);
Class<?> keyClass = Class.forName("net;kyori;adventure;key;Key".replace(";", ".")); keyClass = Class.forName("net;kyori;adventure;key;Key".replace(";", "."));
keyAsStringMethod = keyClass.getMethod("asString"); keyAsStringMethod = keyClass.getMethod("asString");
keyFromStringMethod = keyClass.getMethod("key", String.class);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
IllegalAccessException ignored) { IllegalAccessException ignored) {
} }
@@ -90,6 +94,10 @@ public class ReflectionUtils {
return emptyComponent; return emptyComponent;
} }
public static Class<?> getKeyClass() {
return keyClass;
}
public static String getKeyAsString(Object key) { public static String getKeyAsString(Object key) {
try { try {
return (String) keyAsStringMethod.invoke(key); return (String) keyAsStringMethod.invoke(key);
@@ -99,6 +107,15 @@ public class ReflectionUtils {
return ""; return "";
} }
public static Object getKerFromString(String key) {
try {
return keyFromStringMethod.invoke(key);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static String getMiniMessageTextFromNonShadedComponent(Object component) { public static String getMiniMessageTextFromNonShadedComponent(Object component) {
try { try {
return (String) serializeComponentMethod.invoke(miniMessageInstance, component); return (String) serializeComponentMethod.invoke(miniMessageInstance, component);

View File

@@ -3,6 +3,11 @@ blacklist-channels:
- Private - Private
- Staff - Staff
# ALL: all the players can see the bubble
# JOINED: players in the same channel can see each other's bubble
# CAN_JOIN: players that have permission to certain channels would see the bubble in that channel
channel-mode: ALL
# Default bubble to display if player's bubble is "none" # Default bubble to display if player's bubble is "none"
default-bubble: 'chat' default-bubble: 'chat'