use of tic.tac.toe.packet.GameOfferPacket in project Spark by igniterealtime.
the class TicTacToePlugin method initialize.
@Override
public void initialize() {
ClassLoader cl = getClass().getClassLoader();
buttonimg = new ImageIcon(cl.getResource("ttt.button.png"));
_currentInvitations = new HashSet<String>();
ProviderManager.addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE, new GameOfferPacket.Provider());
ProviderManager.addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, new MovePacket.Provider());
ProviderManager.addExtensionProvider(InvalidMove.ELEMENT_NAME, InvalidMove.NAMESPACE, new InvalidMove.Provider());
// Add IQ listener to listen for incoming game invitations.
_gameOfferListener = new StanzaListener() {
public void processPacket(Stanza stanza) {
GameOfferPacket invitation = (GameOfferPacket) stanza;
if (invitation.getType() == IQ.Type.get) {
showInvitationAlert(invitation);
}
}
};
SparkManager.getConnection().addAsyncStanzaListener(_gameOfferListener, new StanzaTypeFilter(GameOfferPacket.class));
addButtonToToolBar();
}
use of tic.tac.toe.packet.GameOfferPacket in project Spark by igniterealtime.
the class TicTacToePlugin method addButtonToToolBar.
/**
* Add the TTT-Button to every opening Chatroom
* and create Listeners for it
*/
private void addButtonToToolBar() {
_chatRoomListener = new ChatRoomListenerAdapter() {
@Override
public void chatRoomOpened(final ChatRoom room) {
if (!(room instanceof ChatRoomImpl)) {
// Don't do anything if this is not a 1on1-Chat
return;
}
final ChatRoomButton sendGameButton = new ChatRoomButton(buttonimg);
room.getToolBar().addChatRoomButton(sendGameButton);
final String opponentJID = ((ChatRoomImpl) room).getJID();
sendGameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_currentInvitations.contains(XmppStringUtils.parseBareJid(opponentJID))) {
return;
}
final GameOfferPacket offer = new GameOfferPacket();
offer.setTo(opponentJID);
offer.setType(IQ.Type.get);
_currentInvitations.add(XmppStringUtils.parseBareJid(opponentJID));
room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.sent"), false, false, Color.BLUE);
try {
SparkManager.getConnection().sendStanza(offer);
} catch (SmackException.NotConnectedException e1) {
Log.warning("Unable to send offer to " + opponentJID, e1);
}
SparkManager.getConnection().addAsyncStanzaListener(new StanzaListener() {
@Override
public void processPacket(Stanza stanza) {
GameOfferPacket answer = (GameOfferPacket) stanza;
answer.setStartingPlayer(offer.isStartingPlayer());
answer.setGameID(offer.getGameID());
if (answer.getType() == IQ.Type.result) {
// ACCEPT
_currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.accept"), false, false, Color.BLUE);
createTTTWindow(answer, opponentJID);
} else {
// DECLINE
room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.decline"), false, false, Color.RED);
_currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
}
}
}, new PacketIDFilter(offer.getPacketID()));
}
});
}
@Override
public void chatRoomClosed(ChatRoom room) {
if (room instanceof ChatRoomImpl) {
ChatRoomImpl cri = (ChatRoomImpl) room;
_currentInvitations.remove(cri.getParticipantJID());
}
}
};
SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
Aggregations