Support for default channels on player join

This commit is contained in:
Marco Cunha committed 2012-08-25 19:51:56 +02:00
1 parent 06c67f50e0
commit 019ea0cb7b
4 files changed
+62 -34

No files matched your search

@@ -17,6 +17,7 @@ import com.palmergames.bukkit.towny.object.TownyUniverse;
public class ChannelsHolder {
private Chat plugin;
private Channel defaultChan = null;
/** Constructor
*
@@ -27,6 +28,13 @@ public class ChannelsHolder {
this.plugin = plugin;
}
public void setDefaultChannel(Channel channel) {
defaultChan = channel;
}
public Channel getDefaultChannel() {
return defaultChan;
}
// Container for all channels
private Map<String,Channel> channels = new HashMap<String,Channel>();
@@ -7,8 +7,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.palmergames.bukkit.TownyChat.Chat;
import com.palmergames.bukkit.TownyChat.channels.Channel;
import com.palmergames.bukkit.TownyChat.channels.StandardChannel;
@@ -98,6 +96,21 @@ public class ConfigurationHandler {
}
}
if (key.equalsIgnoreCase("default")) {
boolean set = false;
if (element instanceof Boolean) {
set = (element != null && ((Boolean)element) == true);
} else if (element instanceof String) {
set = Boolean.parseBoolean((String)element);
} else if (element instanceof Integer) {
set = (element != null && ((Integer)element) != 0);
}
if (set) {
plugin.getChannelsHandler().setDefaultChannel(channel);
plugin.getLogger().info("Default Channel set to " + channel.getName());
}
}
if (key.equalsIgnoreCase("channeltag"))
if (element instanceof String)
channel.setChannelTag(element.toString());
@@ -16,6 +16,7 @@ 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.listener.LocalTownyChatEvent;
import com.palmergames.bukkit.TownyChat.tasks.onPlayerJoinTask;
import com.palmergames.bukkit.TownyChat.Chat;
import com.palmergames.bukkit.TownyChat.TownyChatFormatter;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
@@ -35,6 +36,38 @@ public class TownyChatPlayerListener implements Listener {
this.plugin = instance;
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(final PlayerJoinEvent event) {
Player player = event.getPlayer();
String name = player.getName();
for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) {
// If the channel is auto join, they will be added
// If the channel is not auto join, they will marked as absent
// TODO: Only do this for channels the user has permissions for
channel.forgetPlayer(name);
}
Channel channel = plugin.getChannelsHandler().getDefaultChannel();
if (channel != null) {
// See if we have permissions for the default channel
channel = plugin.getChannelsHandler().getChannel(player, channel.getCommands().get(0));
if (channel != null) {
// 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);
}
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerQuit(final PlayerQuitEvent event) {
String name = event.getPlayer().getName();
for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) {
// If the channel is auto join, they will be added
// If the channel is not auto join, they will marked as absent
channel.forgetPlayer(name);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
@@ -53,10 +86,9 @@ public class TownyChatPlayerListener implements Listener {
if (split.length > 1)
message = StringMgmt.join(StringMgmt.remFirstArg(split), " ");
// Check if they used a channel command or alias
Channel channel = plugin.getChannelsHandler().getChannel(player, command);
if (channel != null) {
/*
* If there is no message toggle the chat mode.
*/
@@ -87,29 +119,10 @@ public class TownyChatPlayerListener implements Listener {
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerQuit(final PlayerQuitEvent event) {
String name = event.getPlayer().getName();
for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) {
channel.forgetPlayer(name);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(final PlayerJoinEvent event) {
String name = event.getPlayer().getName();
for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) {
// If the channel is auto join, they will be added
// If the channel is not auto join, they will marked as absent
channel.forgetPlayer(name);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
String playerName = player.getName();
// Check if essentials has this player muted.
if (!isMuted(player)) {
@@ -133,11 +146,7 @@ public class TownyChatPlayerListener implements Listener {
event.setCancelled(true);
return;
}
// If player sends a message to a channel it had left
// tell the channel to add the player back
if (channel.isAbsent(playerName)) {
channel.forgetPlayer(playerName);
}
channel.chatProcess(event);
return;
}
@@ -177,7 +186,6 @@ public class TownyChatPlayerListener implements Listener {
channel.chatProcess(event);
return;
}
}
/*
@@ -249,4 +257,4 @@ public class TownyChatPlayerListener implements Listener {
}
return false;
}
}
}
@@ -18,16 +18,15 @@ public class TownyUtil {
}
List<String> modes = towny.getPlayerMode(player);
int pos = 0;
int newmodecount = modes.size();
Iterator<String> iter = modes.iterator();
while (iter.hasNext()) {
String s = iter.next();
if (mode.equalsIgnoreCase(s)) {
modes.remove(pos);
towny.setPlayerMode(player, modes.toArray(new String[modes.size()]), notify);
iter.remove();
towny.setPlayerMode(player, modes.toArray(new String[newmodecount-1]), notify);
return true;
}
pos++;
}
return false;
}