Fix mode setting when switching out of default channel

This commit is contained in:
Marco Cunha committed 2012-08-25 23:26:35 +02:00
1 parent e2efed0b01
commit 0c68f59d1e
3 files changed
+37 -5

No files matched your search

@@ -91,9 +91,7 @@ public class LeaveCommand implements CommandExecutor {
if (plugin.getTowny().hasPlayerMode(player, chan.getName())) {
if (plugin.getChannelsHandler().getDefaultChannel() != null && plugin.getChannelsHandler().getDefaultChannel().isPresent(player.getName())) {
nextChannel = plugin.getChannelsHandler().getDefaultChannel();
if (TownyUtil.removePlayerMode(plugin.getTowny(), player, chan.getName(), false)) {
TownyUtil.addPlayerMode(plugin.getTowny(), player, nextChannel.getName(), true);
}
TownyUtil.removeAndSetPlayerMode(plugin.getTowny(), player, chan.getName(), nextChannel.getName(), true);
}
}
if (nextChannel == null) {
@@ -95,13 +95,21 @@ public class TownyChatPlayerListener implements Listener {
*/
if (message.isEmpty()) {
if (plugin.getTowny().hasPlayerMode(player, channel.getName())) {
plugin.getTowny().removePlayerMode(player);
// Find what the next channel is if any
Channel nextChannel = null;
if (plugin.getChannelsHandler().getDefaultChannel() != null && plugin.getChannelsHandler().getDefaultChannel().isPresent(player.getName())) {
nextChannel = plugin.getChannelsHandler().getDefaultChannel();
TownyUtil.addPlayerMode(plugin.getTowny(), player, nextChannel.getName(), true);
if (!nextChannel.getName().equalsIgnoreCase(channel.getName())) {
TownyUtil.removeAndSetPlayerMode(plugin.getTowny(), player, channel.getName(), nextChannel.getName(), true);
} else {
// They're talking on default channel and want to leave but can't because they'll be put default again
// Their only option out is to leave the channel if they have permission to do so.
TownyMessaging.sendErrorMsg(player, "[TownyChat] You are already talking in the default channel. To switch to another channel use that channel's command.");
}
} else {
plugin.getTowny().removePlayerMode(player);
}
if (nextChannel == null) {
nextChannel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL);
}
@@ -164,6 +172,7 @@ public class TownyChatPlayerListener implements Listener {
return;
}
plugin.getLogger().info("onPlayerChat: Processing directed message for " + channel.getName());
channel.chatProcess(event);
return;
}
@@ -41,4 +41,29 @@ public class TownyUtil {
towny.setPlayerMode(player, modes.toArray(new String[modes.size()]), notify);
return true;
}
public static boolean removeAndSetPlayerMode(Towny towny, Player player, String removeMode, String addMode, boolean notify) {
List<String> modes = towny.getPlayerMode(player);
boolean modesChanged = false;
if (removeMode != null && towny.hasPlayerMode(player, removeMode)) {
Iterator<String> iter = modes.iterator();
while (iter.hasNext()) {
String s = iter.next();
if (s == null) continue;
if (s.equalsIgnoreCase(removeMode)) {
iter.remove();
modesChanged = true;
}
}
}
if (addMode != null) {
modes.add(addMode);
modesChanged = true;
}
if (modesChanged) {
towny.setPlayerMode(player, modes.toArray(new String[modes.size()]), notify);
return true;
}
return false;
}
}