What you are adding is a simple a Pet system!
Difficulty: 1/10
Knownledge: Copy & Paste
Base: PI
What are you adding!
Go to src.server.model.npcs and add FOLDER called pet
2. make Class on it Called DropPet.java, Pet.java, PetData
DropPet.java
Code:
package server.model.npcs.pet;
import server.model.players.Client;
/**
* Drop the pet.
* @author MGT Madness, created on 11-12-2013.
*/
public class DropPet
{
/**
* The class instance.
*/
private static final DropPet instance = new DropPet();
/**
* Returns a visible encapsulation of the class instance.
*
* @return The returned encapsulated instance.
*/
public final static DropPet getInstance()
{
return instance;
}
/**
* Check if the player has requirements to drop the pet.
* @param player
* The player dropping the pet.
* @param itemId
* The pet item identity being dropped.
* @param slot
* The slot of the item being dropped.
*/
public void dropPetRequirements(Client player, int itemId, int slot)
{
if (player.getPetSummoned())
{
player.sendMessage("You already have a pet following you.");
return;
}
for (int i = 0; i < PetData.petData.length; i++)
{
if (PetData.petData[i][1] == itemId)
{
dropPet(player, itemId, slot);
}
}
}
/**
* Summon the pet and delete inventory pet item.
* @param player
* The player dropping the pet.
* @param itemId
* The pet identity item in inventory.
* @param slot
* The slot of the pet item in inventory.
*/
private void dropPet(Client player, int itemId, int slot)
{
for (int i = 0; i < PetData.petData.length; i++)
{
if (PetData.petData[i][1] == itemId)
{
player.sendMessage("You drop your pet and it starts following you.");
Pet.summonPet(player, PetData.petData[i][0], player.absX, player.absY - 1, player.heightLevel);
}
}
player.getItems().deleteItem(itemId, slot, player.playerItemsN[slot]);
}
}
Pet.java
Code:
package server.model.npcs.pet;
import server.model.npcs.NPC;
import server.model.npcs.NPCHandler;
import server.model.players.Client;
import server.model.players.Player;
/**
* @author Jordon
* @author Animeking1120
* @author Erick
* @author MGT Madness, started editing at 11-12-2013.
* <p>
* Pet system.
*/
public class Pet
{
/**
* Find if this NPC is a pet to pickup.
* @param player
* The player interacting with the the NPC.
* @param npcType
* The NPC type being interacted with.
*/
public static void pickUpPetRequirements(Client player, int npcType)
{
for (int i = 0; i < PetData.petData.length; i++)
{
if (PetData.petData[i][0] == npcType)
{
if (NPCHandler.npcs[player.npcClickIndex].summonedBy == player.playerId)
{
if (player.getItems().freeSlots() > 0)
{
pickUpPet(player, npcType);
}
else
{
player.sendMessage("Not enough space in your inventory.");
}
}
else
{
player.sendMessage("This is not your pet.");
}
}
}
}
/**
* Pick up the pet and place in inventory of the player.
* @param player
* The player picking up the pet.
* @param pet
* The identity of the pet being picked up
*/
public static void pickUpPet(Client player, int pet)
{
player.sendMessage("You pick up your pet.");
player.startAnimation(827);
for (int i = 0; i < PetData.petData.length; i++)
{
if (PetData.petData[i][0] == pet)
{
player.getItems().addItem(PetData.petData[i][1], 1);
}
}
for (int i = 0; i < NPCHandler.maxNPCs; i++)
{
if (NPCHandler.npcs[i] == null)
{
continue;
}
if (NPCHandler.npcs[i].summonedBy == player.playerId)
{
deletePet(NPCHandler.npcs[i]);
}
}
player.setPetSummoned(false);
player.petID = 0;
}
/**
* Summon the pet.
* @param player
* The player who summoned the pet.
* @param npcType
* The pet being summoned.
* @param x
* The x coord of the pet.
* @param y
* The x coord of the pet.
* @param heightLevel
* The height of the pet.
*/
public static void summonPet(Player player, int npcType, int x, int y, int heightLevel)
{
int slot = -1;
for (int i = 1; i < NPCHandler.maxNPCs; i++)
{
if (NPCHandler.npcs[i] == null)
{
slot = i;
break;
}
}
if (slot == -1)
{
return;
}
NPC newNPC = new NPC(slot, npcType);
newNPC.absX = x;
newNPC.absY = y;
newNPC.makeX = x;
newNPC.makeY = y;
newNPC.heightLevel = heightLevel;
newNPC.walkingType = 0;
newNPC.HP = 0;
newNPC.MaxHP = 0;
newNPC.maxHit = 0;
newNPC.attack = 0;
newNPC.defence = 0;
newNPC.spawnedBy = player.getId();
newNPC.underAttack = true;
newNPC.facePlayer(player.playerId);
newNPC.summoned = true;
newNPC.summonedBy = player.playerId;
player.petID = npcType;
player.setPetSummoned(true);
NPCHandler.npcs[slot] = newNPC;
}
/**
* Delete the current pet to summon a new one.
* This is for cases such as the Player is too far away from Pet, so the pet gets deleted and summoned close to player. Or if pet is picked up.
*/
public static void deletePet(NPC pet)
{
pet.absX = -1;
pet.absY = -1;
pet.makeX = -1;
pet.makeY = -1;
pet.heightLevel = -1;
pet.walkingType = -1;
pet.HP = -1;
pet.MaxHP = -1;
pet.maxHit = -1;
pet.attack = -1;
pet.defence = -1;
pet.isDead = true;
pet.applyDead = true;
}
/**
* Spawn the pet for the player that just logged in
* @param player
* The associated player.
*/
public static void ownerLoggedIn(Client player)
{
if (player.getPetSummoned() && player.petID > 0)
{
Pet.summonPet(player, player.petID, player.absX, player.absY - 1, player.heightLevel);
player.sendMessage("Your loyal pet finds you!");
}
}
}
Code:
package server.model.npcs.pet;
/**
* Handle large pet data.
* @author MGT Madness, created on 12-12-2013.
*/
public class PetData
{
/**
* NPC spawn identity and Pet identity in inventory.
*/
public static int[][] petData = {
{
498, 12653 // King Black Dragon. // F
},
{
130, 12649 // Kree arra. // F
},
{
2573, 12650 // General Graardor. // F
},
{
857, 12652 // K'ril Tsutsaroth. // F
},
{
858, 12651 // Commander Zilyana. // F
},
{
454, 12694 // Chaos elemental. // F
},
{
859, 12643 // Supreme // F
},
{
860, 12644 // Prime // F
},
{
861, 12645 // Rex // F
},
{
6305, 12655 // Kraken
},
{
25, 15574 // Vet'ion // 90% working - Walking anim needed F
},
{
1593, 15573 // Callisto Added F
},
{
1552, 14073 // Santa guy pet
},
{
4020, 15571 // Venenatis
},
{
4021, 15572 // Scorpia
},
{
864, 16672 // Kalphite queen Jr
},
{
863, 17672 // Baby mole
},
{
761, 1555 // Pet kitten.
},
{
762, 1556 // Pet kitten.
},
{
763, 1557 // Pet kitten.
},
{
764, 1558 // Pet kitten.
},
{
765, 1559 // Pet kitten.
},
{
766, 1560 // Pet kitten.
},
{
768, 1561 // Pet cat.
},
{
769, 1562 // Pet cat.
},
{
770, 1563 // Pet cat.
},
{
771, 1564 // Pet cat.
},
{
772, 1565 // Pet cat.
},
{
773, 1566 // Pet cat.
},
{
80, 7001 // Camel.
},
{
3505, 7583 // Hell-kitten.
},
{
3506, 7584 // Lazy hellcat.
},
{
3507, 7585 // Wily hellcat.
},
{
6900, 12469 // Red Hatchling dragon.
},
{
6901, 12470 // Red Baby Dragon.
},
{
6902, 12471 // Blue Hatchling dragon.
},
{
6903, 12472 // Blue Baby Dragon.
},
{
6904, 12473 // Green Hatchling Dragon.
},
{
6905, 12474 // Green Baby Dragon.
},
{
6906, 12475 // Black Hatchling Dragon.
},
{
6907, 12476 // Black Baby Dragon.
},
{
6908, 12481 // Baby Penguin.
},
{
6909, 12482 // Penguin.
},
{
6910, 12762 // Penguin.
},
{
6911, 12484 // Raven Chick.
},
{
6912, 12485 // Raven.
},
{
6913, 12486 // Baby Raccoon.
},
{
6914, 12487 // Raccoon.
},
{
6915, 12488 // Orange Baby Gecko.
},
{
6916, 12489 // Orange Gecko.
},
{
6919, 12490 // Baby Squirrel.
},
{
6920, 12491 // Squirrel.
},
{
6922, 12492 // Baby Chameleon (Green).
},
{
6923, 12493 // Chameleon (Green).
},
{
6942, 12496 // Baby Monkey.
},
{
6943, 12497 // Monkey.
},
{
6945, 12498 // Baby Vulture.
},
{
6946, 12499 // Vulture.
},
{
6947, 12500 // Baby Giant Crab.
},
{
6948, 12501 // Giant Crab.
},
{
6949, 12503 // Saradomin chick.
},
{
6950, 12504 // Saradomin bird.
},
{
6951, 12505 // Saradomin owl.
},
{
6952, 12506 // Zamorak chick.
},
{
6953, 12507 // Zamorak bird.
},
{
6954, 12508 // Zamorak hawk.
},
{
6955, 12509 // Guthix chick.
},
{
6956, 12510 // Guthix bird.
},
{
6957, 12511 // Guthix raptor.
},
{
6958, 12512 // Terrier Puppy.
},
{
6959, 12513 // Terrier.
},
{
6960, 12514 // Greyhound puppy.
},
{
6961, 12515 // Greyhound.
},
{
6962, 12516 // Labrador puppy.
},
{
6963, 12517 // Labrador.
},
{
6964, 12518 // Dalmatian puppy.
},
{
6965, 12519 // Dalmatian.
},
{
6966, 12520 // Sheepdog puppy.
},
{
6967, 12521 // Sheepdog.
},
{
6968, 12522 // Bulldog puppy.
},
{
6969, 12523 // Bulldog.
}
};
}
At Client.java Imoport
Code:
import server.model.npcs.pet.Pet;
import server.model.players.Pets;
and under public boolean usingCarpet = false; add
Code:
private Pets pets = new Pets();
add this somewhere
Code:
private static final Pets Pets = null;
on
public void initialize() {
add
Code:
Pet.ownerLoggedIn(this);
some where on initazlize add
Code:
if (hasNpc == true) {
if (summonId == 498) {
getItems().addItem(12653, 1);
}
if (summonId == 4003) {
getItems().addItem(12694, 1);
}
if (summonId == 454) {
getItems().addItem(12650, 1);
}
if (summonId == 130) {
getItems().addItem(12649, 1);
}
if (summonId == 857) {
getItems().addItem(12652, 1);
}
if (summonId == 858) {
getItems().addItem(12651, 1);
}
if (summonId == 859) {
getItems().addItem(12643, 1);
}
if (summonId == 860) {
getItems().addItem(12644, 1);
}
if (summonId == 861) {
getItems().addItem(12645, 1);
}
if (summonId == 859) {
getItems().addItem(12655, 1);
}
// Server.npcHandler.spawnNpc3(this, summonId, absX, absY-1,
// heightLevel, 0, 120, 25, 200, 200, true, false, true);
summonId = 0;
hasNpc = false;
this.sendMessage("Your pet has been added to your inventory.");
}
under
public Crafting getCrafting() {
return crafting;
}
add
Code:
public Pets getSummon() {
return Pets;
}
We are done from Client.java Save n close!
Npchandler.java import:
Code:
import server.model.npcs.pet.Pet;
at public void process() {
add
Code:
for (int i = 0; i < maxNPCs; i++) {
if (npcs[i] != null) {
NPC npc = npcs[i];
Client petOwner = (Client) PlayerHandler.players[npc.summonedBy];
if (petOwner == null && npc.summoned)
{
Pet.deletePet(npc);
}
if (petOwner != null && petOwner.isDead)
{
Pet.deletePet(npc);
}
if (petOwner != null && petOwner.getPetSummoned() && npc.summoned)
{
if (petOwner.goodDistance(npc.getX(), npc.getY(), petOwner.absX, petOwner.absY, 15))
{
Server.npcHandler.followPlayer(i, petOwner.playerId);
}
else
{
Pet.deletePet(npc);
Pet.summonPet(petOwner, petOwner.petID, petOwner.absX, petOwner.absY - 1, petOwner.heightLevel);
}
}
}
}
We are from Npchandler.java
Actionhandler.java Import
Code:
import server.model.npcs.pet.Pet;
import server.model.npcs.pet.Pet;
and at public void firstClickNpc(int npcType) {
add
Code:
Pet.pickUpPetRequirements(c, npcType);
We are done from Actionhandler.java
At dropItem.java import
Code:
import server.model.npcs.pet.DropPet;
import server.model.npcs.pet.PetData;
import server.model.players.Pets;
add
Code:
for (int i = 0; i < PetData.petData.length; i++)
{
if (PetData.petData[i][1] == itemId)
{
if (c.getPetSummoned())
{
droppable = false;
break;
}
}
}
DropPet.getInstance().dropPetRequirements(c, itemId, slot);
if (c.playerItemsN[slot] == 0 || itemId < 0
|| c.playerItems[slot] != itemId + 1) {
return;
}
Save n close it
At Player.java under public byte getChatTextSize() {
return chatTextSize;
}
add
Code:
//Pets
public boolean petSummoned;
/**
* Change the state of petSummoned.
* @param state
* The state of petSummoned.
*/
public int petID;
public void setPetSummoned(boolean state)
{
petSummoned = state;
}
public boolean getPetSummoned()
{
return petSummoned;
}
At PlayerSave.java
Under
} else if (token.equals("special-amount")) {
p.specAmount = Double.parseDouble(token2);
add
Code:
} else if (token.equals("petID")) {
p.petID = Integer.parseInt(token2);
} else if (token.equals("petSummoned")) {
p.setPetSummoned(Boolean.parseBoolean(token2));
and under characterfile.write("character-rights = ", 0, 19);
characterfile.write(Integer.toString(p.playerRight s), 0, Integer.toString(p.playerRights).length());
characterfile.newLine();
add
Code:
characterfile.write("petSummoned = ", 0, 14);
characterfile.write(Boolean.toString(p.getPetSummoned()), 0, Boolean.toString(p.getPetSummoned()).length());
characterfile.newLine();
characterfile.write("petID = ", 0, 8);
characterfile.write(Integer.toString(p.petID), 0, Integer.toString(p.petID).length());
characterfile.newLine();
THIS BEEN TESTED MANY TIMES AND IT WORKS FINE!
Thanks Enjoy it!