MaplePet.java
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:7k
- /*
- This file is part of the OdinMS Maple Story Server
- Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
- Matthias Butz <matze@odinms.de>
- Jan Christian Meyer <vimes@odinms.de>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License version 3
- as published by the Free Software Foundation. You may not use, modify
- or distribute this program under any other version of the
- GNU Affero General Public License.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.sf.odinms.client;
- import java.awt.Point;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import net.sf.odinms.database.DatabaseConnection;
- import net.sf.odinms.server.MapleItemInformationProvider;
- import net.sf.odinms.server.movement.AbsoluteLifeMovement;
- import net.sf.odinms.server.movement.LifeMovement;
- import net.sf.odinms.server.movement.LifeMovementFragment;
- /**
- *
- * @author Matze
- */
- public class MaplePet extends Item {
- private String name;
- private int uniqueid;
- private int closeness = 0;
- private int level = 1;
- private int fullness = 100;
- private int Fh;
- private Point pos;
- private int stance;
- private MaplePet(int id, byte position, int uniqueid) {
- super(id, position, (short) 1);
- this.uniqueid = uniqueid;
- }
- public static MaplePet loadFromDb(int itemid, byte position, int petid) {
- try {
- MaplePet ret = new MaplePet(itemid, position, petid);
- Connection con = DatabaseConnection.getConnection(); // Get a connection to the database
- PreparedStatement ps = con.prepareStatement("SELECT * FROM pets WHERE petid = ?"); // Get pet details..
- ps.setInt(1, petid);
- ResultSet rs = ps.executeQuery();
- rs.next();
- ret.setName(rs.getString("name"));
- ret.setCloseness(rs.getInt("closeness"));
- ret.setLevel(rs.getInt("level"));
- ret.setFullness(rs.getInt("fullness"));
- rs.close();
- ps.close();
- return ret;
- } catch (SQLException ex) {
- Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- public void saveToDb() {
- try {
- Connection con = DatabaseConnection.getConnection(); // Get a connection to the database
- PreparedStatement ps = con.prepareStatement("UPDATE pets SET name = ?, level = ?, closeness = ?, fullness = ? WHERE petid = ?");
- ps.setString(1, getName()); // Set name
- if (this.level > 30) {
- this.level = 30;
- }
- ps.setInt(2, getLevel()); // Set Level
- ps.setInt(3, getCloseness()); // Set Closeness
- ps.setInt(4, getFullness()); // Set Fullness
- ps.setInt(5, getUniqueId()); // Set ID
- ps.executeUpdate(); // Execute statement
- ps.close();
- } catch (SQLException ex) {
- Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void deletePet(int itemid, MapleClient c) {
- try {
- String sql = "DELETE FROM pets WHERE `petid` = ?";
- IItem player = c.getPlayer().getInventory(MapleInventoryType.CASH).findById(itemid);
- Connection con = DatabaseConnection.getConnection();
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setInt(1, player.getPetId());
- ps.executeUpdate();
- ps.close();
- } catch (SQLException ex) {
- Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static int createPet(int itemid) {
- try {
- MapleItemInformationProvider mii = MapleItemInformationProvider.getInstance();
- Connection con = DatabaseConnection.getConnection();
- PreparedStatement ps = con.prepareStatement("INSERT INTO pets (name, level, closeness, fullness) VALUES (?, ?, ?, ?)");
- ps.setString(1, mii.getName(itemid));
- ps.setInt(2, 1);
- ps.setInt(3, 0);
- ps.setInt(4, 100);
- ps.executeUpdate();
- ResultSet rs = ps.getGeneratedKeys();
- rs.next();
- int ret = rs.getInt(1);
- rs.close();
- ps.close();
- return ret;
- } catch (SQLException ex) {
- Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex);
- return -1;
- }
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name.length()>12?name.substring(0,12):name;
- }
- public int getUniqueId() {
- return uniqueid;
- }
- public void setUniqueId(int id) {
- this.uniqueid = id;
- }
- public int getCloseness() {
- return closeness;
- }
- public void setCloseness(int closeness) {
- this.closeness = closeness;
- }
- public int getLevel() {
- return level;
- }
- public void setLevel(int level) {
- this.level = level;
- }
- public int getFullness() {
- return fullness;
- }
- public void setFullness(int fullness) {
- this.fullness = fullness;
- }
- public int getFh() {
- return Fh;
- }
- public void setFh(int Fh) {
- this.Fh = Fh;
- }
- public Point getPos() {
- return pos;
- }
- public void setPos(Point pos) {
- this.pos = pos;
- }
- public int getStance() {
- return stance;
- }
- public void setStance(int stance) {
- this.stance = stance;
- }
- public boolean canConsume(int itemId) {
- MapleItemInformationProvider mii = MapleItemInformationProvider.getInstance();
- for (int petId : mii.petsCanConsume(itemId)) {
- if (petId == this.getItemId()) {
- return true;
- }
- }
- return false;
- }
- public void updatePosition(List<LifeMovementFragment> movement) {
- for (LifeMovementFragment move : movement) {
- if (move instanceof LifeMovement) {
- if (move instanceof AbsoluteLifeMovement) {
- Point position = ((LifeMovement) move).getPosition();
- this.setPos(position);
- }
- this.setStance(((LifeMovement) move).getNewstate());
- }
- }
- }
- }