Replace towny modes usage with player channel map. (#35)

* Replace towny modes usage with player channel map.

Ends TownyChat's usage of Towny's Resident Modes to determine which
channel a player is actively chatting into.

Done as an attempt to fix
https://github.com/TownyAdvanced/Towny/issues/5451 but did not result in
solving the bug, in the end this is probably the correct thing to do
anyways.

* Add temporary debug messaging, make the directChat map store the channel
name instead of the channel command.

* Handle Spigot 1.18.1 config changes. Remove debug messaging.
This commit is contained in:
LlmDl authored and GitHub committed 2021-12-23 11:27:19 -06:00
1 parent 27f46966a6
commit f355807538
14 files changed
+65 -193

No files matched your search

+1 -7
View File
@@ -80,13 +80,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.15.2-R0.1-SNAPSHOT</version>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
+4 -1
View File
@@ -386,4 +386,7 @@ v0.91:
- Implemented sounds for channels and toggling, courtesy of Glare with PR #34.
- Minimum Towny version set to 0.97.2.18.
v0.92:
- Handle channelTag being null.
- Handle channelTag being null.
- Internalize player channel storage.
- Repurpose the display modes on join ChatConfig setting, not displays the channel join message.
- Fix for Spigot 1.18.1 config enabling comments by default.
+13 -1
View File
@@ -17,6 +17,7 @@ import com.palmergames.bukkit.util.Version;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
@@ -27,6 +28,9 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Chat plugin to manage all Towny chat
@@ -51,7 +55,7 @@ public class Chat extends JavaPlugin {
public static boolean usingEssentialsDiscord = false;
boolean chatConfigError = false;
boolean channelsConfigError = false;
private static ConcurrentMap<UUID, Channel> playerChannelMap = new ConcurrentHashMap<>();
@Override
public void onEnable() {
@@ -241,4 +245,12 @@ public class Chat extends JavaPlugin {
public TownyChatPlayerListener getTownyPlayerListener() {
return TownyPlayerListener;
}
public Channel getPlayerChannel(Player player) {
return playerChannelMap.get(player.getUniqueId());
}
public void setPlayerChannel(Player player, Channel channel) {
playerChannelMap.put(player.getUniqueId(), channel);
}
}
@@ -4,7 +4,6 @@ import com.palmergames.bukkit.TownyChat.Chat;
import com.palmergames.bukkit.TownyChat.channels.Channel;
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
import com.palmergames.bukkit.TownyChat.events.PlayerJoinChatChannelEvent;
import com.palmergames.bukkit.TownyChat.util.TownyUtil;
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.command.BaseCommand;
import com.palmergames.bukkit.towny.object.Translatable;
@@ -327,19 +326,16 @@ public class ChannelCommand extends BaseCommand implements CommandExecutor {
// Find what the next channel is if any
Channel nextChannel = null;
if (plugin.getTowny().hasPlayerMode(player, chan.getName())) {
if (plugin.getChannelsHandler().getDefaultChannel() != null && plugin.getChannelsHandler().getDefaultChannel().isPresent(player.getName())) {
nextChannel = plugin.getChannelsHandler().getDefaultChannel();
TownyUtil.removeAndSetPlayerMode(plugin.getTowny(), player, chan.getName(), nextChannel.getName(), true);
}
}
if (nextChannel == null) {
if (plugin.getChannelsHandler().getDefaultChannel() != null && plugin.getChannelsHandler().getDefaultChannel().isPresent(player.getName()))
nextChannel = plugin.getChannelsHandler().getDefaultChannel();
if (nextChannel == null)
nextChannel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL);
}
// If the new channel is not us, announce it
if (nextChannel != null && !chan.getName().equalsIgnoreCase(nextChannel.getName())) {
TownyMessaging.sendMessage(player, Translation.of("tc_you_are_now_talking_in_channel",nextChannel.getName()));
plugin.setPlayerChannel(player, nextChannel);
}
}
@@ -8,7 +8,6 @@ import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.object.Translation;
import com.palmergames.util.StringMgmt;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
@@ -36,7 +35,7 @@ public class ChannelJoinAliasCommand extends BukkitCommand {
message = StringMgmt.join(args, " ");
}
if (message.isEmpty()) {
if (plugin.getTowny().hasPlayerMode(player, channel.getName())) {
if (plugin.getPlayerChannel(player).getName().equals(channel.getName())) {
TownyMessaging.sendMessage(player, String.format(Translation.of("tc_you_are_already_in_channel"), channel.getName()));
return true;
} else {
@@ -51,20 +50,7 @@ public class ChannelJoinAliasCommand extends BukkitCommand {
return true;
}
// Add channel we're moving to.
ArrayList<String> newModes = new ArrayList<String>();
newModes.add(channel.getName());
// Add modes the player already had, except not any current chat channels.
for (String existingMode : plugin.getTowny().getPlayerMode(player))
if (!plugin.getChannelsHandler().isChannel(existingMode))
newModes.add(existingMode);
String[] modes = new String[newModes.size()];
for (int i = 0; i < newModes.size(); i++)
modes[i] = newModes.get(i);
plugin.getTowny().setPlayerMode(player, modes, false);
plugin.setPlayerChannel(player, channel);
TownyMessaging.sendMessage(player, String.format(Translation.of("tc_you_are_now_talking_in_channel"), channel.getName()));
Bukkit.getPluginManager().callEvent(new PlayerJoinChatChannelEvent(player, channel));
return true;
@@ -85,7 +71,7 @@ public class ChannelJoinAliasCommand extends BukkitCommand {
return true;
}
plugin.getTownyPlayerListener().directedChat.put(player, this.getName());
plugin.getTownyPlayerListener().directedChat.put(player, channel.getName().toLowerCase());
final String msg = message;
@@ -59,7 +59,7 @@ public class ChannelsHolder {
if (isChannel(chan.getName()))
channels.remove(chan);
channels.put(chan.getName(), chan);
channels.put(chan.getName().toLowerCase(), chan);
}
public boolean isChannel(String channelName) {
@@ -70,26 +70,9 @@ public class ChannelsHolder {
return channels.get(channelName.toLowerCase());
}
/**
* Get the relevant channel for this command
* if the player is permitted to use it.
*
* @param player
* @param command
* @return channel or null if not permitted
*/
public Channel getChannel(Player player, String command) {
for (Channel channel: channels.values()) {
if (channel.getCommands().contains(command.toLowerCase())) {
if (TownyUniverse.getInstance().getPermissionSource().has(player, channel.getPermission()) || (channel.getPermission().isEmpty()))
return channel;
}
}
return null;
public Map<String, Channel> getChannels() {
return channels;
}
/**
* Find a channel we are able to talk in, and have not left, starting with the greatest range.
*
@@ -390,7 +390,7 @@ public class StandardChannel extends Channel {
// Compile the list of recipients with spy perms
for (Player test : plugin.getServer().getOnlinePlayers()) {
if ((plugin.getTowny().hasPlayerMode(test, "spy")) && !(recipients.contains(test))) {
if (plugin.getTowny().hasPlayerMode(test, "spy") && !(recipients.contains(test))) {
recipients.add(test);
}
@@ -163,11 +163,11 @@ public enum ChatConfigNodes {
"modify_chat.alone_message_string",
"No one in range can hear you or you are alone in this channel.",
"# This allows you to set your alone message."),
DISPLAY_MODES_SET_ON_JOIN(
"display_modes_set_on_join",
DISPLAY_CHANNEL_JOIN_MESSAGE_ON_JOIN(
"display_channel_join_message_on_joining_the_server",
"true",
"",
"# If true players will see [Towny] Modes set: general when they log in."),
"# If true players will see You have joined channel {channel} message on joining the server."),
WORLDS("worlds","","");
@@ -249,7 +249,7 @@ public class ChatSettings extends tag_formats {
/**
* @return the display_modes_set_on_join setting
*/
public static boolean getDisplayeModesSetOnJoin() {
public static boolean getShowChannelMessageOnServerJoin() {
return display_modes_set_on_join;
}
@@ -60,7 +60,13 @@ public class CommentedConfiguration extends YamlConfiguration {
}
public boolean save() {
try {
// Spigot 1.18.1 added SnakeYaml's ability to use Comments in yaml.
// They have it enabled by default, we need to stop it happening.
yamlOptions.setProcessComments(false);
} catch (NoSuchMethodError ignored) {}
boolean saved = true;
// Save the config just like normal
@@ -250,6 +256,7 @@ public class CommentedConfiguration extends YamlConfiguration {
}
}
@SuppressWarnings("deprecation")
@Override
public String saveToString() {
yamlOptions.setIndent(options().indent());
@@ -29,7 +29,7 @@ public class EssentialsDiscordHookListener implements Listener {
if (event.isCancelled()) {
return;
}
Channel channel = plugin.getChannelsHandler().getChannel(event.getPlayer(), directChat);
Channel channel = plugin.getChannelsHandler().getChannel(directChat);
if (channel != null) {
event.setCancelled(channel.getType() != channelTypes.GLOBAL || channel.getRange() != -1);
return;
@@ -37,7 +37,7 @@ public class EssentialsDiscordHookListener implements Listener {
}
for (Channel curChannel : plugin.getChannelsHandler().getAllChannels().values()) {
if (plugin.getTowny().hasPlayerMode(event.getPlayer(), curChannel.getName())) {
if (plugin.getPlayerChannel(event.getPlayer()).getName().equals(curChannel.getName())) {
event.setCancelled( curChannel.getType() != channelTypes.GLOBAL || curChannel.getRange() != -1);
return;
}
@@ -6,7 +6,6 @@ import com.palmergames.bukkit.TownyChat.TownyChatFormatter;
import com.palmergames.bukkit.TownyChat.channels.Channel;
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
import com.palmergames.bukkit.TownyChat.tasks.onPlayerJoinTask;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.exceptions.TownyException;
@@ -47,9 +46,9 @@ public class TownyChatPlayerListener implements Listener {
Channel channel = plugin.getChannelsHandler().getDefaultChannel();
if (channel != null && player.hasPermission(channel.getPermission())) {
// Schedule it as delayed task because Towny may not have processed this just yet
// and would reset the mode otherwise
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new onPlayerJoinTask(plugin, player, channel), 5);
plugin.setPlayerChannel(player, channel);
if (ChatSettings.getShowChannelMessageOnServerJoin())
TownyMessaging.sendMessage(player, String.format(Translation.of("tc_you_are_now_talking_in_channel"), channel.getName()));
}
}
@@ -115,7 +114,7 @@ public class TownyChatPlayerListener implements Listener {
* If this was directed chat send it via the relevant channel
*/
if (directedChat.containsKey(player)) {
Channel channel = plugin.getChannelsHandler().getChannel(player, directedChat.get(player));
Channel channel = plugin.getChannelsHandler().getChannel(directedChat.get(player));
if (channel != null) {
// Notify player he is muted
@@ -142,29 +141,28 @@ public class TownyChatPlayerListener implements Listener {
/*
* Check the player for any channel modes.
*/
for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) {
if (plugin.getTowny().hasPlayerMode(player, channel.getName())) {
// Notify player he is muted
if (channel.isMuted(player.getName())) {
TownyMessaging.sendErrorMsg(player, String.format(Translation.of("tc_err_you_are_currently_muted_in_channel"), channel.getName()));
event.setCancelled(true);
return;
}
if (channel.isSpam(player)) {
event.setCancelled(true);
return;
}
/*
* Channel Chat mode set
* Process the chat
*/
channel.chatProcess(event);
Channel channel = plugin.getChannelsHandler().getChannel(plugin.getPlayerChannel(player).getName());
if (channel != null) {
// Notify player he is muted
if (channel.isMuted(player.getName())) {
TownyMessaging.sendErrorMsg(player, String.format(Translation.of("tc_err_you_are_currently_muted_in_channel"), channel.getName()));
event.setCancelled(true);
return;
}
if (channel.isSpam(player)) {
event.setCancelled(true);
return;
}
/*
* Channel Chat mode set
* Process the chat
*/
channel.chatProcess(event);
return;
}
// Find a global channel this player has permissions for.
Channel channel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL);
channel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL);
if (channel != null) {
// Notify player he is muted
@@ -1,51 +0,0 @@
package com.palmergames.bukkit.TownyChat.tasks;
import com.palmergames.bukkit.TownyChat.Chat;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.entity.Player;
import com.palmergames.bukkit.TownyChat.channels.Channel;
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
import com.palmergames.bukkit.towny.Towny;
/*
*
* Created by RocketRidah
*
*/
public class onPlayerJoinTask implements Runnable {
Chat plugin;
Towny towny;
Player player;
String mode;
public onPlayerJoinTask(Chat plugin, Player player, Channel defaultChannel) {
super();
this.plugin = plugin;
this.towny = plugin.getTowny();
this.player = player;
this.mode = defaultChannel.getName();
}
@Override
public void run() {
if (this.towny.isEnabled()) {
if (!this.towny.isError()) {
if (player != null) {
List<String> modes = new ArrayList<>();
modes.add(mode);
if (!towny.getPlayerMode(player).isEmpty())
modes.addAll(towny.getPlayerMode(player));
towny.setPlayerMode(player, modes.toArray(new String[0]), ChatSettings.getDisplayeModesSetOnJoin());
}
return;
}
}
}
}
@@ -1,56 +0,0 @@
package com.palmergames.bukkit.TownyChat.util;
import com.palmergames.bukkit.towny.Towny;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TownyUtil {
public TownyUtil() {
}
public static boolean removePlayerMode(Towny towny, Player player, String mode, boolean notify) {
if (!towny.hasPlayerMode(player, mode)) {
return false;
}
List<String> modes = towny.getPlayerMode(player);
int newmodecount = modes.size();
Iterator<String> iter = modes.iterator();
while (iter.hasNext()) {
String s = iter.next();
if (mode.equalsIgnoreCase(s)) {
iter.remove();
towny.setPlayerMode(player, modes.toArray(new String[newmodecount-1]), notify);
return true;
}
}
return false;
}
public static void removeAndSetPlayerMode(Towny towny, Player player, String removeMode, String addMode, boolean notify) {
List<String> modes = towny.getPlayerMode(player);
List<String> newModes = new ArrayList<>();
boolean modesChanged = false;
if (removeMode != null && towny.hasPlayerMode(player, removeMode)) {
for (String mode : modes) {
if (!mode.equalsIgnoreCase(removeMode))
newModes.add(mode);
}
modesChanged = true;
}
if (addMode != null) {
newModes.add(addMode);
modesChanged = true;
}
if (modesChanged) {
if (newModes.isEmpty())
towny.removePlayerMode(player);
else
towny.setPlayerMode(player, newModes.toArray(new String[0]), notify);
}
}
}