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!

Offline Mail System!

Joined
Oct 25, 2008
Messages
1,372
Reaction score
604
Hello good people of RaGEZONE. I coded this a while back for a server I used to code for. To be perfectly honest, it's pretty useless. I coded it to learn and experiment.

Basically what this is, is it lets players send mail to each others, that works offline and online. Let's get started.

Add these methods in MapleCharacter.java
Code:
  public void setSeenAllMail() {
        try {
            PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("UPDATE mail SET `read` = 1 WHERE MailReciever = ?");
            ps.setString(1, getName());
            ps.executeUpdate();
            ps.close();
        } catch (SQLException e) {
            return;
        }
    }
Code:
public int newMail() {
        int mail = 0;
        PreparedStatement ps = null;
        Connection con = DatabaseConnection.getConnection();
        try {
            ps = con.prepareStatement("SELECT COUNT(*) as c FROM mail where mailreciever = ? and `read` = 0 and deleted = 0");
            ps.setString(1, getName());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
             mail = rs.getInt("c");
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException ex) {
            }
        }
        return mail;
    }
Code:
public void sendMail(String Reciever, String message) throws SQLException {
		Connection con = DatabaseConnection.getConnection();
		PreparedStatement ps = con.prepareStatement("INSERT INTO mail (`MailSender`, `MailReciever`, `Message`) VALUES (?, ?, ?)");
		ps.setString(1, getName());
		ps.setString(2, Reciever);
		ps.setString(3, message);
		ps.executeUpdate();
		ps.close();
	}

Open up your player commands file and add these commands.
Code:
} else if (splitted[0].equals("@checkallmail")) {
                       c.getPlayer().setSeenAllMail();
             ResultSet rs = getAllMail(c.getPlayer().getName());
                           try { 
                               mc.dropMessage("Your MailBox:");
                               while(rs.next()){

                                  mc.dropMessage(rs.getString("MailSender") + ": " + rs.getString("Message"));
                               }
                            } catch(Exception ex) {}
Code:
} else if (splitted[0].equals("@checknewmail")) {
                   if (c.getPlayer().newMail() > 0) {
                     
             ResultSet rs = getNewMail(c.getPlayer().getName());
                           try {
                               mc.dropMessage("Your New Messages:");
                               while(rs.next()){

                                  mc.dropMessage(rs.getString("MailSender") + ": " + rs.getString("Message"));
                               }
                            } catch(Exception ex) {}
 c.getPlayer().setSeenAllMail();
           } else {
                  mc.dropMessage("You have no new messages. Use @checkallmail to see your entire inbox.");
           }
Code:
} else if (splitted[0].equalsIgnoreCase("@sendmail")) {
                    if (!player.getCheatTracker().Spam(60000, 2)) {
                    if (splitted.length <= 2) {
mc.dropMessage("Please use @sendmail <Mail Reciever> <Message>.");
} else {
     MapleCharacter victim = c.getChannelServer().getPlayerStorage().getCharacterByName(splitted[1]);
     String Reciever = splitted[1];
     String message = StringUtil.joinStringFrom(splitted, 2);
     c.getPlayer().sendMail(Reciever, message);
     mc.dropMessage("You have sent " + Reciever + " this message: " + message);
     victim.dropMessage("[Notice]: " + c.getPlayer().getName() + " has just sent you a message! Use @checknewmail to see what it was.");
}
                    } else {
                    mc.dropMessage("You cannot send a message more than once per minute.");
                }

Add these at the bottom of your player commands file:
Code:
public static ResultSet getAllMail(String Reciever) {
		try {
			Connection con = DatabaseConnection.getConnection();
		PreparedStatement ps = con.prepareStatement("SELECT MailSender, Message FROM mail WHERE MailReciever = ? and Deleted = 0");
                ps.setString(1, Reciever);
                 return ps.executeQuery();

		} catch (Exception ex) {}

		return null;
	}
Code:
public static ResultSet getNewMail(String Reciever) {
		try {
			Connection con = DatabaseConnection.getConnection();
		PreparedStatement ps = con.prepareStatement("SELECT MailSender, Message FROM mail WHERE MailReciever = ? and Deleted = 0 and `Read` = 0");
                ps.setString(1, Reciever);
                 return ps.executeQuery();

		} catch (Exception ex) {}

		return null;
	}

Open up your PlayerLoggedinHandler.java and add this near the bottom:
Code:
                if (c.getPlayer().newMail() > 0) {
                    player.dropMessage("[Notice]: You have " + c.getPlayer().newMail() + " unread messages.");
                }

Run this MySQL: (Edit TABLENAMEHERE to your server's table name.)
Code:
CREATE TABLE `TABLENAMEHERE`.`Mail` (
  `MessageID` INT(10) NOT NULL AUTO_INCREMENT,
  `MailSender` VARCHAR(13) NOT NULL,
  `MailReciever` VARCHAR(13) NOT NULL,
  `Message` VARCHAR(128) NOT NULL,
  `Read` TINYINT(4) NOT NULL DEFAULT 0,
  `Deleted` TINYINT(4) UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`MessageID`)
)
ENGINE = InnoDB;

And you're done! This works but I'm not sure if I got all the pieces together.

Here's a screen of it in action:
SharpAceX - Offline Mail System! - RaGEZONE Forums


Have fun!
 
Joined
Oct 25, 2008
Messages
1,372
Reaction score
604
Initiate Mage
Joined
Aug 20, 2009
Messages
36
Reaction score
3
Sydney's was somewhat like this. You logged in it tells you if your inbox, new messages blah blah blah, you read and you reply
 
may web.very maple.pls.
Member
Joined
Aug 12, 2009
Messages
1,810
Reaction score
606
that never worked in v75 for me xD
Ontopic looks nice great release ;)
 
may web.very maple.pls.
Member
Joined
Aug 12, 2009
Messages
1,810
Reaction score
606
Added to Library, Great work on this :D
 
Back
Top