Rename and clean up MaplePet

This commit is contained in:
P0nk
2021-09-09 20:51:30 +02:00
parent 5ed7cf73d1
commit 6ca5abd896
25 changed files with 121 additions and 113 deletions

View File

@@ -162,7 +162,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
private MapleRockPaperScissor rps;
private MapleMount maplemount;
private MapleParty party;
private MaplePet[] pets = new MaplePet[3];
private Pet[] pets = new Pet[3];
private MaplePlayerShop playerShop = null;
private MapleShop shop = null;
private MapleSkinColor skinColor = MapleSkinColor.NORMAL;
@@ -616,7 +616,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
this.mesosTraded += gain;
}
public void addPet(MaplePet pet) {
public void addPet(Pet pet) {
petLock.lock();
try {
for (int i = 0; i < 3; i++) {
@@ -2962,7 +2962,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
deletedCoupon = true;
}
} else {
MaplePet pet = item.getPet(); // thanks Lame for noticing pets not getting despawned after expiration time
Pet pet = item.getPet(); // thanks Lame for noticing pets not getting despawned after expiration time
if (pet != null) {
unequipPet(pet, true);
}
@@ -5614,7 +5614,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
this.setMessengerPosition(4);
}
public MaplePet[] getPets() {
public Pet[] getPets() {
petLock.lock();
try {
return Arrays.copyOf(pets, pets.length);
@@ -5623,7 +5623,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
}
}
public MaplePet getPet(int index) {
public Pet getPet(int index) {
if (index < 0) {
return null;
}
@@ -5652,7 +5652,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
}
}
public byte getPetIndex(MaplePet pet) {
public byte getPetIndex(Pet pet) {
petLock.lock();
try {
for (byte i = 0; i < 3; i++) {
@@ -7042,7 +7042,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
ret.getInventory(item.getRight()).addItemFromDB(item.getLeft());
Item itemz = item.getLeft();
if (itemz.getPetId() > -1) {
MaplePet pet = itemz.getPet();
Pet pet = itemz.getPet();
if (pet != null && pet.isSummoned()) {
ret.addPet(pet);
}
@@ -7973,7 +7973,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
}
}
public void removePet(MaplePet pet, boolean shift_left) {
public void removePet(Pet pet, boolean shift_left) {
petLock.lock();
try {
int slot = -1;
@@ -8477,7 +8477,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
}
}
List<MaplePet> petList = new LinkedList<>();
List<Pet> petList = new LinkedList<>();
petLock.lock();
try {
for (int i = 0; i < 3; i++) {
@@ -8489,7 +8489,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
petLock.unlock();
}
for (MaplePet pet : petList) {
for (Pet pet : petList) {
pet.saveToDb();
}
@@ -9708,7 +9708,7 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
}
public void runFullnessSchedule(int petSlot) {
MaplePet pet = getPet(petSlot);
Pet pet = getPet(petSlot);
if (pet == null) {
return;
}
@@ -9762,20 +9762,20 @@ public class MapleCharacter extends AbstractMapleCharacterObject {
public void unequipAllPets() {
for (int i = 0; i < 3; i++) {
MaplePet pet = getPet(i);
Pet pet = getPet(i);
if (pet != null) {
unequipPet(pet, true);
}
}
}
public void unequipPet(MaplePet pet, boolean shift_left) {
public void unequipPet(Pet pet, boolean shift_left) {
unequipPet(pet, shift_left, false);
}
public void unequipPet(MaplePet pet, boolean shift_left, boolean hunger) {
public void unequipPet(Pet pet, boolean shift_left, boolean hunger) {
byte petIdx = this.getPetIndex(pet);
MaplePet chrPet = this.getPet(petIdx);
Pet chrPet = this.getPet(petIdx);
if (chrPet != null) {
chrPet.setSummoned(false);

View File

@@ -26,7 +26,7 @@ package client.command.commands.gm2;
import client.MapleCharacter;
import client.MapleClient;
import client.command.Command;
import client.inventory.MaplePet;
import client.inventory.Pet;
import client.inventory.manipulator.MapleInventoryManipulator;
import config.YamlConfig;
import constants.inventory.ItemConstants;
@@ -67,7 +67,7 @@ public class ItemCommand extends Command {
quantity = 1;
long days = Math.max(1, Integer.parseInt(params[1]));
long expiration = System.currentTimeMillis() + (days * 24 * 60 * 60 * 1000);
int petid = MaplePet.createPet(itemId);
int petid = Pet.createPet(itemId);
MapleInventoryManipulator.addById(c, itemId, quantity, player.getName(), petid, expiration);
return;

View File

@@ -28,7 +28,7 @@ import client.MapleClient;
import client.command.Command;
import client.inventory.InventoryType;
import client.inventory.Item;
import client.inventory.MaplePet;
import client.inventory.Pet;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import server.MapleItemInformationProvider;
@@ -68,7 +68,7 @@ public class ItemDropCommand extends Command {
quantity = 1;
long days = Math.max(1, Integer.parseInt(params[1]));
long expiration = System.currentTimeMillis() + (days * 24 * 60 * 60 * 1000);
int petid = MaplePet.createPet(itemId);
int petid = Pet.createPet(itemId);
Item toDrop = new Item(itemId, (short) 0, quantity, petid);
toDrop.setExpiration(expiration);

View File

@@ -26,7 +26,7 @@ package client.command.commands.gm4;
import client.MapleCharacter;
import client.MapleClient;
import client.command.Command;
import client.inventory.MaplePet;
import client.inventory.Pet;
import client.inventory.manipulator.MapleInventoryManipulator;
import server.maps.MapleMapItem;
import server.maps.MapleMapObject;
@@ -59,7 +59,7 @@ public class ForceVacCommand extends Command {
// Add NX to account, show effect and make item disappear
player.getCashShop().gainCash(1, mapItem.getItemId() == 4031865 ? 100 : 250);
} else if (mapItem.getItem().getItemId() >= 5000000 && mapItem.getItem().getItemId() <= 5000100) {
int petId = MaplePet.createPet(mapItem.getItem().getItemId());
int petId = Pet.createPet(mapItem.getItem().getItemId());
if (petId == -1) {
continue;
}

View File

@@ -38,7 +38,7 @@ public class Item implements Comparable<Item> {
private short position;
private short quantity;
private int petid = -1;
private MaplePet pet = null;
private Pet pet = null;
private String owner = "";
protected List<String> log;
private short flag;
@@ -58,7 +58,7 @@ public class Item implements Comparable<Item> {
this.position = position;
this.quantity = quantity;
if (petid > -1) { // issue with null "pet" having petid > -1 found thanks to MedicOP
this.pet = MaplePet.loadFromDb(id, position, petid);
this.pet = Pet.loadFromDb(id, position, petid);
if (this.pet == null) {
petid = -1;
}
@@ -184,7 +184,7 @@ public class Item implements Comparable<Item> {
this.giftFrom = giftFrom;
}
public MaplePet getPet() {
public Pet getPet() {
return pet;
}

View File

@@ -40,10 +40,9 @@ import java.sql.SQLException;
import java.util.List;
/**
*
* @author Matze
*/
public class MaplePet extends Item {
public class Pet extends Item {
private String name;
private int uniqueid;
private int closeness = 0;
@@ -54,13 +53,13 @@ public class MaplePet extends Item {
private int stance;
private boolean summoned;
private int petFlag = 0;
public enum PetFlag {
OWNER_SPEED(0x01);
private int i;
private PetFlag(int i) {
private final int i;
PetFlag(int i) {
this.i = i;
}
@@ -69,14 +68,14 @@ public class MaplePet extends Item {
}
}
private MaplePet(int id, short position, int uniqueid) {
private Pet(int id, short position, int uniqueid) {
super(id, position, (short) 1);
this.uniqueid = uniqueid;
this.pos = new Point(0, 0);
}
public static MaplePet loadFromDb(int itemid, short position, int petid) {
MaplePet ret = new MaplePet(itemid, position, petid);
public static Pet loadFromDb(int itemid, short position, int petid) {
Pet ret = new Pet(itemid, position, petid);
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT name, level, closeness, fullness, summoned, flag FROM pets WHERE petid = ?")) { // Get the pet details...
ps.setInt(1, petid);
@@ -96,7 +95,7 @@ public class MaplePet extends Item {
return null;
}
}
public static void deleteFromDb(MapleCharacter owner, int petid) {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM pets WHERE `petid` = ?")) {
@@ -109,7 +108,7 @@ public class MaplePet extends Item {
ex.printStackTrace();
}
}
public void saveToDb() {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE pets SET name = ?, level = ?, closeness = ?, fullness = ?, summoned = ?, flag = ? WHERE petid = ?")) {
@@ -184,48 +183,55 @@ public class MaplePet extends Item {
public byte getLevel() {
return level;
}
public void gainClosenessFullness(MapleCharacter owner, int incCloseness, int incFullness, int type) {
byte slot = owner.getPetIndex(this);
boolean enjoyed;
//will NOT increase pet's closeness if tried to feed pet with 100% fullness
if (fullness < 100 || incFullness == 0) { //incFullness == 0: command given
int newFullness = fullness + incFullness;
if (newFullness > 100) newFullness = 100;
if (newFullness > 100) {
newFullness = 100;
}
fullness = newFullness;
if (incCloseness > 0 && closeness < 30000) {
int newCloseness = closeness + incCloseness;
if (newCloseness > 30000) newCloseness = 30000;
if (newCloseness > 30000) {
newCloseness = 30000;
}
closeness = newCloseness;
while(newCloseness >= ExpTable.getClosenessNeededForLevel(level)) {
while (newCloseness >= ExpTable.getClosenessNeededForLevel(level)) {
level += 1;
owner.sendPacket(PacketCreator.showOwnPetLevelUp(slot));
owner.getMap().broadcastMessage(PacketCreator.showPetLevelUp(owner, slot));
}
}
enjoyed = true;
} else {
int newCloseness = closeness - 1;
if (newCloseness < 0) newCloseness = 0;
if (newCloseness < 0) {
newCloseness = 0;
}
closeness = newCloseness;
if (level > 1 && newCloseness < ExpTable.getClosenessNeededForLevel(level - 1)) {
level -= 1;
}
enjoyed = false;
}
owner.getMap().broadcastMessage(PacketCreator.petFoodResponse(owner.getId(), slot, enjoyed, false));
saveToDb();
Item petz = owner.getInventory(InventoryType.CASH).getItem(getPosition());
if (petz != null)
if (petz != null) {
owner.forceUpdateItem(petz);
}
}
public void setLevel(byte level) {
@@ -271,31 +277,33 @@ public class MaplePet extends Item {
public void setSummoned(boolean yes) {
this.summoned = yes;
}
public int getPetFlag() {
return this.petFlag;
}
private void setPetFlag(int flag) {
this.petFlag = flag;
}
public void addPetFlag(MapleCharacter owner, PetFlag flag) {
this.petFlag |= flag.getValue();
saveToDb();
Item petz = owner.getInventory(InventoryType.CASH).getItem(getPosition());
if (petz != null)
if (petz != null) {
owner.forceUpdateItem(petz);
}
}
public void removePetFlag(MapleCharacter owner, PetFlag flag) {
this.petFlag &= 0xFFFFFFFF ^ flag.getValue();
saveToDb();
Item petz = owner.getInventory(InventoryType.CASH).getItem(getPosition());
if (petz != null)
if (petz != null) {
owner.forceUpdateItem(petz);
}
}
public Pair<Integer, Boolean> canConsume(int itemId) {

View File

@@ -408,7 +408,7 @@ public class MapleInventoryManipulator {
if (petid > -1) { // thanks Vcoc for finding a d/c issue with equipped pets and pets remaining on DB here
int petIdx = chr.getPetIndex(petid);
if(petIdx > -1) {
MaplePet pet = chr.getPet(petIdx);
Pet pet = chr.getPet(petIdx);
chr.unequipPet(pet, true);
}
@@ -702,7 +702,7 @@ public class MapleInventoryManipulator {
if (petid > -1) {
int petIdx = chr.getPetIndex(petid);
if(petIdx > -1) {
MaplePet pet = chr.getPet(petIdx);
Pet pet = chr.getPet(petIdx);
chr.unequipPet(pet, true);
}
}

View File

@@ -23,7 +23,7 @@ import client.MapleCharacter;
import client.MapleClient;
import client.SkillFactory;
import client.inventory.InventoryType;
import client.inventory.MaplePet;
import client.inventory.Pet;
import client.inventory.manipulator.MapleInventoryManipulator;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
@@ -44,7 +44,7 @@ public class SpawnPetProcessor {
if (c.tryacquireClient()) {
try {
MapleCharacter chr = c.getPlayer();
MaplePet pet = chr.getInventory(InventoryType.CASH).getItem(slot).getPet();
Pet pet = chr.getInventory(InventoryType.CASH).getItem(slot).getPet();
if (pet == null) return;
int petid = pet.getItemId();
@@ -56,7 +56,7 @@ public class SpawnPetProcessor {
return;
} else {
int evolveid = MapleDataTool.getInt("info/evol1", dataRoot.getData("Pet/" + petid + ".img"));
int petId = MaplePet.createPet(evolveid);
int petId = Pet.createPet(evolveid);
if (petId == -1) {
return;
}