Search in sources :

Example 1 with BuddylistEntry

use of client.BuddylistEntry 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)

Example 2 with BuddylistEntry

use of client.BuddylistEntry in project HeavenMS by ronancpl.

the class World method updateBuddies.

private void updateBuddies(int characterId, int channel, int[] buddies, boolean offline) {
    PlayerStorage playerStorage = getPlayerStorage();
    for (int buddy : buddies) {
        MapleCharacter chr = playerStorage.getCharacterById(buddy);
        if (chr != null) {
            BuddylistEntry ble = chr.getBuddylist().get(characterId);
            if (ble != null && ble.isVisible()) {
                int mcChannel;
                if (offline) {
                    ble.setChannel((byte) -1);
                    mcChannel = -1;
                } else {
                    ble.setChannel(channel);
                    mcChannel = (byte) (channel - 1);
                }
                chr.getBuddylist().put(ble);
                chr.getClient().announce(MaplePacketCreator.updateBuddyChannel(ble.getCharacterId(), mcChannel));
            }
        }
    }
}
Also used : MapleCharacter(client.MapleCharacter) BuddylistEntry(client.BuddylistEntry) PlayerStorage(net.server.PlayerStorage)

Example 3 with BuddylistEntry

use of client.BuddylistEntry in project HeavenMS by ronancpl.

the class World method buddyChanged.

public void buddyChanged(int cid, int cidFrom, String name, int channel, BuddyOperation operation) {
    MapleCharacter addChar = getPlayerStorage().getCharacterById(cid);
    if (addChar != null) {
        BuddyList buddylist = addChar.getBuddylist();
        switch(operation) {
            case ADDED:
                if (buddylist.contains(cidFrom)) {
                    buddylist.put(new BuddylistEntry(name, "Default Group", cidFrom, channel, true));
                    addChar.getClient().announce(MaplePacketCreator.updateBuddyChannel(cidFrom, (byte) (channel - 1)));
                }
                break;
            case DELETED:
                if (buddylist.contains(cidFrom)) {
                    buddylist.put(new BuddylistEntry(name, "Default Group", cidFrom, (byte) -1, buddylist.get(cidFrom).isVisible()));
                    addChar.getClient().announce(MaplePacketCreator.updateBuddyChannel(cidFrom, (byte) -1));
                }
                break;
        }
    }
}
Also used : MapleCharacter(client.MapleCharacter) BuddylistEntry(client.BuddylistEntry) BuddyList(client.BuddyList)

Example 4 with BuddylistEntry

use of client.BuddylistEntry in project HeavenMS by ronancpl.

the class PlayerLoggedinHandler method handlePacket.

@Override
public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
    final int cid = slea.readInt();
    final Server server = Server.getInstance();
    MapleCharacter player = c.getWorldServer().getPlayerStorage().getCharacterById(cid);
    boolean newcomer = false;
    if (player == null) {
        try {
            player = MapleCharacter.loadCharFromDB(cid, c, true);
            newcomer = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    } else {
        player.newClient(c);
    }
    if (player == null) {
        // If you are still getting null here then please just uninstall the game >.>, we dont need you fucking with the logs
        c.disconnect(true, false);
        return;
    }
    c.setPlayer(player);
    c.setAccID(player.getAccountID());
    int state = c.getLoginState();
    boolean allowLogin = true;
    Channel cserv = c.getChannelServer();
    if (state == MapleClient.LOGIN_SERVER_TRANSITION || state == MapleClient.LOGIN_NOTLOGGEDIN) {
        for (String charName : c.loadCharacterNames(c.getWorld())) {
            for (Channel ch : c.getWorldServer().getChannels()) {
                if (ch.isConnected(charName)) {
                    allowLogin = false;
                }
            }
            break;
        }
    }
    if (state != MapleClient.LOGIN_SERVER_TRANSITION || !allowLogin) {
        c.setPlayer(null);
        c.announce(MaplePacketCreator.getAfterLoginError(7));
        return;
    }
    c.updateLoginState(MapleClient.LOGIN_LOGGEDIN);
    cserv.addPlayer(player);
    List<PlayerBuffValueHolder> buffs = server.getPlayerBuffStorage().getBuffsFromStorage(cid);
    if (buffs != null) {
        List<Pair<Long, PlayerBuffValueHolder>> timedBuffs = getLocalStartTimes(buffs);
        player.silentGiveBuffs(timedBuffs);
    }
    c.announce(MaplePacketCreator.getCharInfo(player));
    if (!player.isHidden()) {
        player.toggleHide(true);
    }
    player.sendKeymap();
    player.sendMacros();
    if (player.getKeymap().get(91) != null)
        player.announce(MaplePacketCreator.sendAutoHpPot(player.getKeymap().get(91).getAction()));
    if (player.getKeymap().get(92) != null)
        player.announce(MaplePacketCreator.sendAutoMpPot(player.getKeymap().get(92).getAction()));
    player.getMap().addPlayer(player);
    World world = server.getWorld(c.getWorld());
    world.getPlayerStorage().addPlayer(player);
    player.setAwayFromWorld(false);
    int[] buddyIds = player.getBuddylist().getBuddyIds();
    world.loggedOn(player.getName(), player.getId(), c.getChannel(), buddyIds);
    for (CharacterIdChannelPair onlineBuddy : server.getWorld(c.getWorld()).multiBuddyFind(player.getId(), buddyIds)) {
        BuddylistEntry ble = player.getBuddylist().get(onlineBuddy.getCharacterId());
        ble.setChannel(onlineBuddy.getChannel());
        player.getBuddylist().put(ble);
    }
    c.announce(MaplePacketCreator.updateBuddylist(player.getBuddylist().getBuddies()));
    c.announce(MaplePacketCreator.loadFamily(player));
    if (player.getFamilyId() > 0) {
        MapleFamily f = world.getFamily(player.getFamilyId());
        if (f == null) {
            f = new MapleFamily(player.getId());
            world.addFamily(player.getFamilyId(), f);
        }
        player.setFamily(f);
        c.announce(MaplePacketCreator.getFamilyInfo(f.getMember(player.getId())));
    }
    if (player.getGuildId() > 0) {
        MapleGuild playerGuild = server.getGuild(player.getGuildId(), player.getWorld(), player);
        if (playerGuild == null) {
            player.deleteGuild(player.getGuildId());
            player.getMGC().setGuildId(0);
        } else {
            playerGuild.getMGC(player.getId()).setCharacter(player);
            player.setMGC(playerGuild.getMGC(player.getId()));
            server.setGuildMemberOnline(player, true, c.getChannel());
            c.announce(MaplePacketCreator.showGuildInfo(player));
            int allianceId = player.getGuild().getAllianceId();
            if (allianceId > 0) {
                MapleAlliance newAlliance = server.getAlliance(allianceId);
                if (newAlliance == null) {
                    newAlliance = MapleAlliance.loadAlliance(allianceId);
                    if (newAlliance != null) {
                        server.addAlliance(allianceId, newAlliance);
                    } else {
                        player.getGuild().setAllianceId(0);
                    }
                }
                if (newAlliance != null) {
                    c.announce(MaplePacketCreator.updateAllianceInfo(newAlliance, c));
                    c.announce(MaplePacketCreator.allianceNotice(newAlliance.getId(), newAlliance.getNotice()));
                    if (newcomer) {
                        server.allianceMessage(allianceId, MaplePacketCreator.allianceMemberOnline(player, true), player.getId(), -1);
                    }
                }
            }
        }
    }
    player.showNote();
    if (player.getParty() != null) {
        MaplePartyCharacter pchar = player.getMPC();
        // Use this in case of enabling party HPbar HUD when logging in, however "you created a party" will appear on chat.
        // c.announce(MaplePacketCreator.partyCreated(pchar));
        pchar.setChannel(c.getChannel());
        pchar.setMapId(player.getMapId());
        pchar.setOnline(true);
        world.updateParty(player.getParty().getId(), PartyOperation.LOG_ONOFF, pchar);
        player.updatePartyMemberHP();
    }
    if (player.getInventory(MapleInventoryType.EQUIPPED).findById(1122017) != null) {
        player.equipPendantOfSpirit();
    }
    c.announce(MaplePacketCreator.updateBuddylist(player.getBuddylist().getBuddies()));
    CharacterNameAndId pendingBuddyRequest = c.getPlayer().getBuddylist().pollPendingRequest();
    if (pendingBuddyRequest != null) {
        c.announce(MaplePacketCreator.requestBuddylistAdd(pendingBuddyRequest.getId(), c.getPlayer().getId(), pendingBuddyRequest.getName()));
    }
    if (newcomer) {
        for (MaplePet pet : player.getPets()) {
            if (pet != null)
                world.registerPetHunger(player, player.getPetIndex(pet));
        }
        player.reloadQuestExpirations();
    }
    c.announce(MaplePacketCreator.updateGender(player));
    player.checkMessenger();
    c.announce(MaplePacketCreator.enableReport());
    player.changeSkillLevel(SkillFactory.getSkill(10000000 * player.getJobType() + 12), (byte) (player.getLinkedLevel() / 10), 20, -1);
    player.checkBerserk(player.isHidden());
    player.buffExpireTask();
    player.diseaseExpireTask();
    player.skillCooldownTask();
    player.expirationTask();
    player.questExpirationTask();
    if (GameConstants.hasSPTable(player.getJob()) && player.getJob().getId() != 2001) {
        player.createDragon();
    }
    player.commitExcludedItems();
    if (newcomer) {
        /*
            if (!c.hasVotedAlready()){
            	player.announce(MaplePacketCreator.earnTitleMessage("You can vote now! Vote and earn a vote point!"));
            }
                    */
        if (player.isGM()) {
            Server.getInstance().broadcastGMMessage(c.getWorld(), MaplePacketCreator.earnTitleMessage((player.gmLevel() < 6 ? "GM " : "Admin ") + player.getName() + " has logged in"));
        }
    }
    showDueyNotification(c, player);
    if (player.getMap().getHPDec() > 0)
        player.resetHpDecreaseTask();
    player.resetPlayerRates();
    if (ServerConstants.USE_ADD_RATES_BY_LEVEL == true)
        player.setPlayerRates();
    player.setWorldRates();
    player.updateCouponRates();
    player.receivePartyMemberHP();
}
Also used : MapleCharacter(client.MapleCharacter) PlayerBuffValueHolder(net.server.PlayerBuffValueHolder) CharacterIdChannelPair(net.server.channel.CharacterIdChannelPair) Server(net.server.Server) SQLException(java.sql.SQLException) MapleFamily(client.MapleFamily) Channel(net.server.channel.Channel) World(net.server.world.World) BuddylistEntry(client.BuddylistEntry) MapleGuild(net.server.guild.MapleGuild) MapleAlliance(net.server.guild.MapleAlliance) MaplePet(client.inventory.MaplePet) CharacterIdChannelPair(net.server.channel.CharacterIdChannelPair) Pair(tools.Pair) MaplePartyCharacter(net.server.world.MaplePartyCharacter) CharacterNameAndId(client.CharacterNameAndId)

Example 5 with BuddylistEntry

use of client.BuddylistEntry in project HeavenMS by ronancpl.

the class MaplePacketCreator method updateBuddylist.

public static byte[] updateBuddylist(Collection<BuddylistEntry> buddylist) {
    final MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
    mplew.writeShort(SendOpcode.BUDDYLIST.getValue());
    mplew.write(7);
    mplew.write(buddylist.size());
    for (BuddylistEntry buddy : buddylist) {
        if (buddy.isVisible()) {
            // cid
            mplew.writeInt(buddy.getCharacterId());
            mplew.writeAsciiString(getRightPaddedStr(buddy.getName(), '\0', 13));
            // opposite status
            mplew.write(0);
            mplew.writeInt(buddy.getChannel() - 1);
            mplew.writeAsciiString(getRightPaddedStr(buddy.getGroup(), '\0', 13));
            // mapid?
            mplew.writeInt(0);
        }
    }
    for (int x = 0; x < buddylist.size(); x++) {
        // mapid?
        mplew.writeInt(0);
    }
    return mplew.getPacket();
}
Also used : MaplePacketLittleEndianWriter(tools.data.output.MaplePacketLittleEndianWriter) BuddylistEntry(client.BuddylistEntry) Point(java.awt.Point)

Aggregations

BuddylistEntry (client.BuddylistEntry)5 MapleCharacter (client.MapleCharacter)4 BuddyList (client.BuddyList)2 SQLException (java.sql.SQLException)2 World (net.server.world.World)2 BuddyAddResult (client.BuddyList.BuddyAddResult)1 CharacterNameAndId (client.CharacterNameAndId)1 MapleFamily (client.MapleFamily)1 MaplePet (client.inventory.MaplePet)1 Point (java.awt.Point)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 PlayerBuffValueHolder (net.server.PlayerBuffValueHolder)1 PlayerStorage (net.server.PlayerStorage)1 Server (net.server.Server)1 Channel (net.server.channel.Channel)1 CharacterIdChannelPair (net.server.channel.CharacterIdChannelPair)1 MapleAlliance (net.server.guild.MapleAlliance)1 MapleGuild (net.server.guild.MapleGuild)1