SpawnPetHandler.java
资源名称:src.rar [点击查看]
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:5k
源码类别:
游戏
开发平台:
Java
- /*
- 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.net.channel.handler;
- import java.awt.Point;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import net.sf.odinms.client.IItem;
- import net.sf.odinms.client.MapleCharacter;
- import net.sf.odinms.client.MapleClient;
- import net.sf.odinms.client.MapleInventoryType;
- import net.sf.odinms.client.MaplePet;
- import net.sf.odinms.client.MapleStat;
- import net.sf.odinms.client.PetDataFactory;
- import net.sf.odinms.client.SkillFactory;
- import net.sf.odinms.net.AbstractMaplePacketHandler;
- import net.sf.odinms.server.MapleInventoryManipulator;
- import net.sf.odinms.tools.MaplePacketCreator;
- import net.sf.odinms.tools.Pair;
- import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;
- public class SpawnPetHandler extends AbstractMaplePacketHandler {
- /* TODO:
- * 1. Move the equpping into a function.
- */
- @Override
- public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
- c.getPlayer().resetAfkTime();
- slea.readInt();
- byte slot = slea.readByte();
- slea.readByte();
- boolean lead = slea.readByte() == 1;
- MapleCharacter player = c.getPlayer();
- IItem item = player.getInventory(MapleInventoryType.CASH).getItem(slot);
- if (item.getItemId() == 5000028 || item.getItemId() == 5000047) {
- boolean done = false;
- int petno;
- int[] pet;
- int[] dragon = {5000029, 5000030, 5000031, 5000032, 5000033};
- int[] robot = {5000048, 5000049, 5000050, 5000051, 5000052, 5000053};
- pet = item.getItemId() == 5000028 ? dragon : robot;
- Random egg = new Random();
- for (int i = 0; i < pet.length && !done; i++) {
- petno = egg.nextInt(pet.length);
- if (!player.haveItem(pet[petno], 1, true, true)) {
- MapleInventoryManipulator.removeFromSlot(c, MapleInventoryType.CASH, item.getPosition(), (short) 1, true, false);
- MapleInventoryManipulator.addById(c, pet[petno], (short) 1, null, MaplePet.createPet(pet[petno]));
- done = true;
- }
- }
- if (!done) {
- player.dropMessage(1, "You currently have all the dragons or robots.");
- return;
- }
- }
- // New instance of MaplePet - using the item ID and unique pet ID
- MaplePet pet = MaplePet.loadFromDb(player.getInventory(MapleInventoryType.CASH).getItem(slot).getItemId(), slot, player.getInventory(MapleInventoryType.CASH).getItem(slot).getPetId());
- // Assign the pet to the player, set stats
- if (player.getPetIndex(pet) != -1) {
- player.unequipPet(pet, true);
- } else {
- if (player.getSkillLevel(SkillFactory.getSkill(8)) == 0 && player.getPet(0) != null) {
- player.unequipPet(player.getPet(0), false);
- }
- if (lead) {
- player.shiftPetsRight();
- }
- Point pos = player.getPosition();
- pos.y -= 12;
- pet.setPos(pos);
- pet.setFh(player.getMap().getFootholds().findBelow(pet.getPos()).getId());
- pet.setStance(0);
- player.addPet(pet);
- // Broadcast packet to the map...
- player.getMap().broadcastMessage(player, MaplePacketCreator.showPet(player, pet, false), true);
- // Find the pet's unique ID
- int uniqueid = pet.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)));
- // Write the stat update to the player...
- c.getSession().write(MaplePacketCreator.petStatUpdate(player));
- c.getSession().write(MaplePacketCreator.enableActions());
- // Get the data
- int hunger = PetDataFactory.getHunger(pet.getItemId());
- // Start the fullness schedule
- player.startFullnessSchedule(hunger, pet, player.getPetIndex(pet));
- }
- }
- }