[Release] Pets save after logging out and they spawn when you relog/cc (Odin)

Status
Not open for further replies.
You've got my respect!
Joined
Apr 8, 2008
Messages
508
Reaction score
147
This basically makes sure your pet does not rollback when logging out, and that the pet will spawn when you log back in.

In PlayerLoggedinHandler.java
Add these to your imports:
Code:
import net.sf.odinms.client.MaplePet;
import net.sf.odinms.client.MapleStat;
import net.sf.odinms.provider.MapleData;
import net.sf.odinms.provider.MapleDataProviderFactory;
import net.sf.odinms.provider.MapleDataTool;
import net.sf.odinms.tools.Pair;
import net.sf.odinms.client.MapleInventoryType;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import net.sf.odinms.database.DatabaseConnection;
import java.util.List;
import java.io.File;
Add this:
Code:
int petid = -1;
                byte petpos = 0;
                try {
                    Connection con = DatabaseConnection.getConnection(); // Get a connection to the database
                    PreparedStatement ps = con.prepareStatement("SELECT petid FROM characters WHERE id = ? LIMIT 1"); // Get pet details..
                    ps.setInt(1, c.getPlayer().getId());
                    ResultSet rs = ps.executeQuery();
                    if(rs.next())
                        petid = rs.getInt("petid");
                    rs.close();
                    ps.close();
                    ps = con.prepareStatement("SELECT position FROM inventoryitems WHERE petid = ?");
                    ps.setInt(1, petid);
                    rs = ps.executeQuery();
                    if(rs.next())
                        petpos = (byte)rs.getInt("position");
                    rs.close();
                    ps.close();
                } catch (SQLException e) {} // no pet or something messed up, leave it
                	
                if (c.getPlayer().getPet() == null && petid != -1 && petid != 0) {

			// New instance of MaplePet - using the item ID and unique pet ID
                    try{
                        if(c.getPlayer().getInventory(MapleInventoryType.CASH).getItem(petpos).getItemId() != 5000028)
                        {
                            MaplePet pet = MaplePet.loadFromDb(c.getPlayer().getInventory(MapleInventoryType.CASH).getItem(petpos).getItemId(), petpos, c.getPlayer().getInventory(MapleInventoryType.CASH).getItem(petpos).getPetId());

                            // Assign the pet to the player, set stats
                            c.getPlayer().setPet(pet);		

                            log.info("showPet: {}", MaplePacketCreator.showPet(c.getPlayer(), c.getPlayer().getPet()));

                            // Broadcast packet to the map...
                            c.getPlayer().getMap().broadcastMessage(c.getPlayer(), MaplePacketCreator.showPet(c.getPlayer(), c.getPlayer().getPet()), true);

                            // Find the pet's unique ID
                            int uniqueid = c.getPlayer().getPet().getUniqueId();

                            // Make a new List for the stat update
                            List<Pair<MapleStat, Integer>> stats = new ArrayList<Pair<MapleStat, Integer>>();
                            stats.add(new Pair<MapleStat, Integer>(MapleStat.PET, Integer.valueOf(uniqueid)));

                            log.info("statUpdate1: {}", MaplePacketCreator.updatePlayerStats(stats, false, true));
                            log.info("statUpdate2: {}", MaplePacketCreator.enableActions());

                            // Write the stat update to the player...
                            c.getSession().write(MaplePacketCreator.updatePlayerStats(stats, false, true));
                            c.getSession().write(MaplePacketCreator.enableActions());

                            // Get the data
                            MapleData petData = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/Item.wz")).getData("Pet/" + String.valueOf(pet.getItemId()) + ".img");
                            MapleData hungerData = petData.getChildByPath("info/hungry");

                            // Start the fullness schedule
                            c.getPlayer().startFullnessSchedule(MapleDataTool.getInt(hungerData));
                        }
                    } catch (Exception e) { } 
		}
After this:
Code:
CharacterNameAndId pendingBuddyRequest = player.getBuddylist().pollPendingRequest();
        if (pendingBuddyRequest != null) {
            player.getBuddylist().put(new BuddylistEntry(pendingBuddyRequest.getName(), pendingBuddyRequest.getId(), -1, false));
            c.getSession().write(MaplePacketCreator.requestBuddylistAdd(pendingBuddyRequest.getId(), pendingBuddyRequest.getName()));
        }
In MapleCharacter.java
Add:
Code:
public void savePet()
        {
            Connection con = DatabaseConnection.getConnection();
            try {
                PreparedStatement ps = con.prepareStatement("UPDATE characters SET petid = ? WHERE id = ?");
                if(pet != null)
                    ps.setInt(1, pet.getUniqueId());
                else
                    ps.setInt(1, 0);
                ps.setInt(2, id);
                ps.executeUpdate();
                ps.close();
                if (getPet() != null) {
                        ps = con.prepareStatement("UPDATE pets SET "
                                                + "name = ?, level = ?, "
                                                + "closeness = ?, fullness = ? "
                                                + "WHERE petid = ?"); // Prepare statement...

                        ps.setString(1, getPet().getName());    // Set name
                        ps.setInt(2, getPet().getLevel());    // Set Level
                        ps.setInt(3, getPet().getCloseness());    // Set Closeness
                        ps.setInt(4, getPet().getFullness());    // Set Fullness
                        ps.setInt(5, getPet().getUniqueId());    // Set ID

                        ps.executeUpdate();            // Execute statement
                        ps.close();
                }
                ps.close();
            } catch (SQLException e) {} // forget it
        }
Before:
Code:
public void saveToDB(boolean update) {
In MapleClient.java
Add:
Code:
getPlayer().savePet();
After:
Code:
if (getPlayer().getPet() != null) {

And execute this SQL:
Code:
ALTER TABLE characters ADD petid int(10) DEFAULT 0;
And then you're done.

CREDITS:
Leifde
Me
Serp/Matze

Feel free to improve upon this, I know I rushed it.

Also, feel free to take:
Code:
if (getPet() != null) {
                        ps = con.prepareStatement("UPDATE pets SET "
                                                + "name = ?, level = ?, "
                                                + "closeness = ?, fullness = ? "
                                                + "WHERE petid = ?"); // Prepare statement...

                        ps.setString(1, getPet().getName());    // Set name
                        ps.setInt(2, getPet().getLevel());    // Set Level
                        ps.setInt(3, getPet().getCloseness());    // Set Closeness
                        ps.setInt(4, getPet().getFullness());    // Set Fullness
                        ps.setInt(5, getPet().getUniqueId());    // Set ID

                        ps.executeUpdate();            // Execute statement
                        ps.close();
                }
Out of of the saveToDb function, as savePet does it
 
Last edited:
init:
deps-jar:
Compiling 2 source files to C:\Documents and Settings\Kira\Desktop\SeanPack V.5.9 Beta\SeanSource V.5.9\build\classes
C:\Documents and Settings\Kira\Desktop\SeanPack V.5.9 Beta\SeanSource V.5.9\src\net\sf\odinms\net\channel\handler\PlayerLoggedinHandler.java:194: '(' or '[' expected
List> stats = new ArrayList>();
C:\Documents and Settings\Kira\Desktop\SeanPack V.5.9 Beta\SeanSource V.5.9\src\net\sf\odinms\net\channel\handler\PlayerLoggedinHandler.java:194: illegal start of expression
List> stats = new ArrayList>();
2 errors
BUILD FAILED (total time: 1 second)

can't solve it
 
init:
deps-jar:
Warning: net\sf\odinms\net\channel\handler\PlayerLoggedinHandler.java modified in the future.
Compiling 2 source files to C:\Documents and Settings\Kira\Desktop\SeanPack V.5.9 Beta\SeanSource V.5.9\build\classes
C:\Documents and Settings\Kira\Desktop\SeanPack V.5.9 Beta\SeanSource V.5.9\src\net\sf\odinms\net\channel\handler\PlayerLoggedinHandler.java:194: cannot find symbol
symbol : variable PET
location: class net.sf.odinms.client.MapleStat
stats.add(new Pair<MapleStat, Integer>(MapleStat.PET, Integer.valueOf(uniqueid)));
1 error

i got this error this time
 
Status
Not open for further replies.
Back
Top