Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[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:
Elite Diviner
Joined
Apr 24, 2008
Messages
416
Reaction score
2
2nd post~ arl0ws frantier happy national day xD
 
Elite Diviner
Joined
May 25, 2008
Messages
454
Reaction score
0
Is there some errors with your script?

eg: Pet_, shouldn't it be jus pet?
 
You've got my respect!
Joined
Apr 8, 2008
Messages
508
Reaction score
147
Is there some errors with your script?

eg: Pet_, shouldn't it be jus pet?
I fixed that, when I was making the script I had to do some things but I fixed those.
Also I fixed the post thanks.
 
Elite Diviner
Joined
Apr 24, 2008
Messages
416
Reaction score
2
frantier did u see the pm on how 2 change title i send u?
 
Elite Diviner
Joined
May 25, 2008
Messages
454
Reaction score
0
I got "List" and "File" unresolved in PlayyerLoggedInHandler.java "-_-

Any1 help pl0x?
 
Junior Spellweaver
Joined
Jul 6, 2008
Messages
104
Reaction score
15
... I did Ctrl + Shift + I, and Now it compiles fine lolz o_0 Now time to test it =P
 
Initiate Mage
Joined
Apr 4, 2008
Messages
19
Reaction score
0
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
 
Experienced Elementalist
Joined
Jun 18, 2008
Messages
216
Reaction score
0
this is an absolute fail at the moment
 
Legendary Battlemage
Joined
May 23, 2008
Messages
628
Reaction score
4
How is it absolute fail...
This is another step closer to GMS...
In GMS pets stay with you forever until you either click on it or you don't feed it
 
Initiate Mage
Joined
Apr 4, 2008
Messages
19
Reaction score
0
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