Search in sources :

Example 1 with PlayerNPCs

use of server.maps.PlayerNPCs in project HeavenMS by ronancpl.

the class MapleCharacter method playerNPC.

public void playerNPC(MapleCharacter v, int scriptId) {
    int npcId;
    try {
        Connection con = DatabaseConnection.getConnection();
        PreparedStatement ps = con.prepareStatement("SELECT id FROM playernpcs WHERE ScriptId = ?");
        ps.setInt(1, scriptId);
        ResultSet rs = ps.executeQuery();
        if (!rs.next()) {
            rs.close();
            ps = con.prepareStatement("INSERT INTO playernpcs (name, hair, face, skin, x, cy, map, ScriptId, Foothold, rx0, rx1) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, v.getName());
            ps.setInt(2, v.getHair());
            ps.setInt(3, v.getFace());
            ps.setInt(4, v.getSkinColor().getId());
            ps.setInt(5, getPosition().x);
            ps.setInt(6, getPosition().y);
            ps.setInt(7, getMapId());
            ps.setInt(8, scriptId);
            ps.setInt(9, getMap().getFootholds().findBelow(getPosition()).getId());
            ps.setInt(10, getPosition().x + 50);
            ps.setInt(11, getPosition().x - 50);
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            rs.next();
            npcId = rs.getInt(1);
            ps.close();
            ps = con.prepareStatement("INSERT INTO playernpcs_equip (NpcId, equipid, equippos) VALUES (?, ?, ?)");
            ps.setInt(1, npcId);
            for (Item equip : getInventory(MapleInventoryType.EQUIPPED)) {
                int position = Math.abs(equip.getPosition());
                if ((position < 12 && position > 0) || (position > 100 && position < 112)) {
                    ps.setInt(2, equip.getItemId());
                    ps.setInt(3, equip.getPosition());
                    ps.addBatch();
                }
            }
            ps.executeBatch();
            ps.close();
            rs.close();
            ps = con.prepareStatement("SELECT * FROM playernpcs WHERE ScriptId = ?");
            ps.setInt(1, scriptId);
            rs = ps.executeQuery();
            rs.next();
            PlayerNPCs pn = new PlayerNPCs(rs);
            for (Channel channel : Server.getInstance().getChannelsFromWorld(world)) {
                MapleMap m = channel.getMapFactory().getMap(getMapId());
                m.broadcastMessage(MaplePacketCreator.spawnPlayerNPC(pn));
                m.broadcastMessage(MaplePacketCreator.getPlayerNPC(pn));
                m.addMapObject(pn);
            }
        }
        ps.close();
        rs.close();
        con.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Also used : MapleMapItem(server.maps.MapleMapItem) Item(client.inventory.Item) MaplePlayerShopItem(server.maps.MaplePlayerShopItem) MapleMap(server.maps.MapleMap) SQLException(java.sql.SQLException) Channel(net.server.channel.Channel) DatabaseConnection(tools.DatabaseConnection) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PlayerNPCs(server.maps.PlayerNPCs) PreparedStatement(java.sql.PreparedStatement) Point(java.awt.Point)

Example 2 with PlayerNPCs

use of server.maps.PlayerNPCs in project HeavenMS by ronancpl.

the class NPCTalkHandler method handlePacket.

@Override
public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
    if (!c.getPlayer().isAlive()) {
        c.announce(MaplePacketCreator.enableActions());
        return;
    }
    if (System.currentTimeMillis() - c.getPlayer().getNpcCooldown() < ServerConstants.BLOCK_NPC_RACE_CONDT) {
        c.announce(MaplePacketCreator.enableActions());
        return;
    }
    int oid = slea.readInt();
    MapleMapObject obj = c.getPlayer().getMap().getMapObject(oid);
    if (obj instanceof MapleNPC) {
        MapleNPC npc = (MapleNPC) obj;
        if (ServerConstants.USE_DEBUG == true)
            c.getPlayer().dropMessage(5, "Talking to NPC " + npc.getId());
        if (npc.getId() == 9010009) {
            // is duey
            c.getPlayer().setNpcCooldown(System.currentTimeMillis());
            c.announce(MaplePacketCreator.sendDuey((byte) 8, DueyHandler.loadItems(c.getPlayer())));
        } else {
            if (c.getCM() != null || c.getQM() != null) {
                c.announce(MaplePacketCreator.enableActions());
                return;
            }
            if (npc.getId() >= 9100100 && npc.getId() <= 9100200) {
                // Custom handling for gachapon scripts to reduce the amount of scripts needed.
                NPCScriptManager.getInstance().start(c, npc.getId(), "gachapon", null);
            } else {
                boolean hasNpcScript = NPCScriptManager.getInstance().start(c, npc.getId(), oid, null);
                if (!hasNpcScript) {
                    if (!npc.hasShop()) {
                        FilePrinter.printError(FilePrinter.NPC_UNCODED, "NPC " + npc.getName() + "(" + npc.getId() + ") is not coded.\r\n");
                        return;
                    } else if (c.getPlayer().getShop() != null) {
                        c.announce(MaplePacketCreator.enableActions());
                        return;
                    }
                    npc.sendShop(c);
                }
            }
        }
    } else if (obj instanceof PlayerNPCs) {
        NPCScriptManager.getInstance().start(c, ((PlayerNPCs) obj).getId(), null);
    }
}
Also used : MapleMapObject(server.maps.MapleMapObject) PlayerNPCs(server.maps.PlayerNPCs) MapleNPC(server.life.MapleNPC)

Aggregations

PlayerNPCs (server.maps.PlayerNPCs)2 Item (client.inventory.Item)1 Point (java.awt.Point)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Channel (net.server.channel.Channel)1 MapleNPC (server.life.MapleNPC)1 MapleMap (server.maps.MapleMap)1 MapleMapItem (server.maps.MapleMapItem)1 MapleMapObject (server.maps.MapleMapObject)1 MaplePlayerShopItem (server.maps.MaplePlayerShopItem)1 DatabaseConnection (tools.DatabaseConnection)1