add XMPP presence management stack

This commit is contained in:
patjense 2013-10-13 12:57:41 -07:00 committed by KHobbits
parent 12bf4e9dcf
commit 601bdbff46

View file

@ -1,5 +1,6 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.Console;
import com.earth2me.essentials.EssentialsConf;
import com.earth2me.essentials.IConf;
@ -12,12 +13,15 @@ import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class XMPPManager extends Handler implements MessageListener, ChatManagerListener, IConf
@ -124,7 +128,10 @@ public class XMPPManager extends Handler implements MessageListener, ChatManager
try
{
connection.connect();
connection.login(xmppuser, password);
connection.login(xmppuser, password, "Essentials-XMPP");
connection.sendPacket(new Presence(Presence.Type.available, "No one online.", 2, Presence.Mode.available));
connection.getRoster().setSubscriptionMode(SubscriptionMode.accept_all);
chatManager = connection.getChatManager();
chatManager.addChatListener(this);
@ -155,6 +162,27 @@ public class XMPPManager extends Handler implements MessageListener, ChatManager
}
public final void updatePresence()
{
final int usercount;
final StringBuilder stringBuilder = new StringBuilder();
usercount = parent.getServer().getOnlinePlayers().length;
if (usercount == 0) {
final String presenceMsg = "No one online.";
connection.sendPacket(new Presence(Presence.Type.available, presenceMsg, 2, Presence.Mode.available));
}
if (usercount == 1) {
final String presenceMsg = "1 player online.";
connection.sendPacket(new Presence(Presence.Type.available, presenceMsg, 2, Presence.Mode.dnd));
}
if (usercount > 1) {
stringBuilder.append(usercount).append(" players online.");
connection.sendPacket(new Presence(Presence.Type.available, stringBuilder.toString(), 2, Presence.Mode.dnd));
}
}
@Override
public void chatCreated(final Chat chat, final boolean createdLocally)
{