Search in sources :

Example 1 with BuddyAddResult

use of client.BuddyList.BuddyAddResult in project HeavenMS by ronancpl.

the class BuddylistModifyHandler method handlePacket.

@Override
public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
    int mode = slea.readByte();
    MapleCharacter player = c.getPlayer();
    BuddyList buddylist = player.getBuddylist();
    if (mode == 1) {
        // add
        String addName = slea.readMapleAsciiString();
        String group = slea.readMapleAsciiString();
        if (group.length() > 16 || addName.length() < 4 || addName.length() > 13) {
            // hax.
            return;
        }
        BuddylistEntry ble = buddylist.get(addName);
        if (ble != null && !ble.isVisible() && group.equals(ble.getGroup())) {
            c.announce(MaplePacketCreator.serverNotice(1, "You already have \"" + ble.getName() + "\" on your Buddylist"));
        } else if (buddylist.isFull() && ble == null) {
            c.announce(MaplePacketCreator.serverNotice(1, "Your buddylist is already full"));
        } else if (ble == null) {
            try {
                World world = c.getWorldServer();
                CharacterIdNameBuddyCapacity charWithId;
                int channel;
                MapleCharacter otherChar = c.getChannelServer().getPlayerStorage().getCharacterByName(addName);
                if (otherChar != null) {
                    channel = c.getChannel();
                    charWithId = new CharacterIdNameBuddyCapacity(otherChar.getId(), otherChar.getName(), otherChar.getBuddylist().getCapacity());
                } else {
                    channel = world.find(addName);
                    charWithId = getCharacterIdAndNameFromDatabase(addName);
                }
                if (charWithId != null) {
                    BuddyAddResult buddyAddResult = null;
                    if (channel != -1) {
                        buddyAddResult = world.requestBuddyAdd(addName, c.getChannel(), player.getId(), player.getName());
                    } else {
                        Connection con = DatabaseConnection.getConnection();
                        PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as buddyCount FROM buddies WHERE characterid = ? AND pending = 0");
                        ps.setInt(1, charWithId.getId());
                        ResultSet rs = ps.executeQuery();
                        if (!rs.next()) {
                            throw new RuntimeException("Result set expected");
                        } else if (rs.getInt("buddyCount") >= charWithId.getBuddyCapacity()) {
                            buddyAddResult = BuddyAddResult.BUDDYLIST_FULL;
                        }
                        rs.close();
                        ps.close();
                        ps = con.prepareStatement("SELECT pending FROM buddies WHERE characterid = ? AND buddyid = ?");
                        ps.setInt(1, charWithId.getId());
                        ps.setInt(2, player.getId());
                        rs = ps.executeQuery();
                        if (rs.next()) {
                            buddyAddResult = BuddyAddResult.ALREADY_ON_LIST;
                        }
                        rs.close();
                        ps.close();
                        con.close();
                    }
                    if (buddyAddResult == BuddyAddResult.BUDDYLIST_FULL) {
                        c.announce(MaplePacketCreator.serverNotice(1, "\"" + addName + "\"'s Buddylist is full"));
                    } else {
                        int displayChannel;
                        displayChannel = -1;
                        int otherCid = charWithId.getId();
                        if (buddyAddResult == BuddyAddResult.ALREADY_ON_LIST && channel != -1) {
                            displayChannel = channel;
                            notifyRemoteChannel(c, channel, otherCid, ADDED);
                        } else if (buddyAddResult != BuddyAddResult.ALREADY_ON_LIST && channel == -1) {
                            Connection con = DatabaseConnection.getConnection();
                            try (PreparedStatement ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`) VALUES (?, ?, 1)")) {
                                ps.setInt(1, charWithId.getId());
                                ps.setInt(2, player.getId());
                                ps.executeUpdate();
                            }
                            con.close();
                        }
                        buddylist.put(new BuddylistEntry(charWithId.getName(), group, otherCid, displayChannel, true));
                        c.announce(MaplePacketCreator.updateBuddylist(buddylist.getBuddies()));
                    }
                } else {
                    c.announce(MaplePacketCreator.serverNotice(1, "A character called \"" + addName + "\" does not exist"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            ble.changeGroup(group);
            c.announce(MaplePacketCreator.updateBuddylist(buddylist.getBuddies()));
        }
    } else if (mode == 2) {
        // accept buddy
        int otherCid = slea.readInt();
        if (!buddylist.isFull()) {
            try {
                // worldInterface.find(otherCid);
                int channel = c.getWorldServer().find(otherCid);
                String otherName = null;
                MapleCharacter otherChar = c.getChannelServer().getPlayerStorage().getCharacterById(otherCid);
                if (otherChar == null) {
                    Connection con = DatabaseConnection.getConnection();
                    try (PreparedStatement ps = con.prepareStatement("SELECT name FROM characters WHERE id = ?")) {
                        ps.setInt(1, otherCid);
                        try (ResultSet rs = ps.executeQuery()) {
                            if (rs.next()) {
                                otherName = rs.getString("name");
                            }
                        }
                    }
                    con.close();
                } else {
                    otherName = otherChar.getName();
                }
                if (otherName != null) {
                    buddylist.put(new BuddylistEntry(otherName, "Default Group", otherCid, channel, true));
                    c.announce(MaplePacketCreator.updateBuddylist(buddylist.getBuddies()));
                    notifyRemoteChannel(c, channel, otherCid, ADDED);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        nextPendingRequest(c);
    } else if (mode == 3) {
        // delete
        int otherCid = slea.readInt();
        player.deleteBuddy(otherCid);
    }
}
Also used : BuddyAddResult(client.BuddyList.BuddyAddResult) MapleCharacter(client.MapleCharacter) SQLException(java.sql.SQLException) DatabaseConnection(tools.DatabaseConnection) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) BuddyList(client.BuddyList) World(net.server.world.World) BuddylistEntry(client.BuddylistEntry) ResultSet(java.sql.ResultSet)

Aggregations

BuddyList (client.BuddyList)1 BuddyAddResult (client.BuddyList.BuddyAddResult)1 BuddylistEntry (client.BuddylistEntry)1 MapleCharacter (client.MapleCharacter)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 World (net.server.world.World)1 DatabaseConnection (tools.DatabaseConnection)1