Hello!
I have a problem that is delaying me MAJORLY and makes me want to stop using RS2D because I have absolutely NO IDEA how to fix it.
The problem is that when I click to attack an NPC(Packet 72), the system goes through a method which like calls the attackNPC method in a different class, sets the attackers blah blah blah .
The problem is that it gives me a NullPointerException!
I have a class called CombatCaller, which deals with the packets, and I have a class called CombatController which actually manages what happens in combat.
Here, the classes aren't long so I'll just copy paste both of them in here -
Here is the CombatCaller class:
Code:
package net.varek.rs2d.packethandler;
import org.apache.mina.common.IoSession;
import net.varek.rs2d.model.NPC;
import net.varek.rs2d.model.Player;
import net.varek.rs2d.net.ActionSender;
import net.varek.rs2d.net.Packet;
import net.varek.rs2d.scriptengine.Parser;
import net.varek.rs2d.controller.CombatController;
import net.varek.rs2d.manager.NPCManager;
import net.varek.rs2d.manager.ScriptManager;
public class CombatCaller implements PacketHandler {
private static CombatController Combat;
public void handlePacket(Packet p, IoSession session) {
Player player = (Player) session.getAttachment();
ActionSender sender = player.getActionSender();
if (p.getId() == ATTACK_PLAYER) {
handleAttackPlayer(p, player, sender);
}
if (p.getId() == MAGIC_ON_PLAYER) {
handleMagicOnPlayer(p, player, sender);
}
if (p.getId() == ATTACK_NPC) {
handleAttackNPC(p, player, sender);
}
if (p.getId() == MAGIC_ON_NPC) {
handleMagicOnNPC(p, player, sender);
}
}
static void handleAttackPlayer(Packet p, Player player, ActionSender sender) {
int victim = p.readShortA();
Combat.attackPlayer(victim, player);
}
static void handleMagicOnPlayer(Packet p, Player player, ActionSender sender) {
sender.sendMessage("You tried to mage a player!");
}
static void handleAttackNPC(Packet p, Player player, ActionSender sender) {
int victim = p.readShortA();
if (NPCManager.isValid(victim)) {
NPC n = NPCManager.getNPC(victim);
Parser parser = ScriptManager.getScriptManager().getParser(
ScriptManager.GENERAL);
int script = parser.call("attacknpc", "" + n.getId(), player);
if (Parser.isTrue(script)) {
if (Parser.isND(script)) {
// no default action
return;
}
}
System.out.println(player.username + " started attacking an NPC - "
+ n);
player.setEntityFace(victim);
Combat.attackNPC(victim, player);
}
}
static void handleMagicOnNPC(Packet p, Player player, ActionSender sender) {
sender.sendMessage("You tried to mage a NPC!");
}
public static final int ATTACK_PLAYER = 73;
public static final int MAGIC_ON_PLAYER = 249;
public static final int ATTACK_NPC = 72;
public static final int MAGIC_ON_NPC = 131;
}
And the CombatController, which actually manages the combat(Well not yet )
Code:
package net.varek.rs2d.controller;
import net.varek.rs2d.manager.NPCManager;
import net.varek.rs2d.model.Player;
import net.varek.rs2d.model.NPC;
import net.varek.rs2d.model.PlayerAnimation;
import net.varek.rs2d.net.ActionSender;
import net.varek.rs2d.content.skills.ISkill;
import net.varek.rs2d.content.skills.SkillConstants;
import net.varek.rs2d.controller.EquipmentController;
import net.varek.rs2d.manager.ItemManager;
/**
* @author Martin Date - 4/24/09 Controls attacking and other combat-related
* things. Called from the class file CombatController in package:
* net.varek.rs2d.packethandler.
*/
public class CombatController {
private Player player;
private ActionSender sender;
public void attackPlayer(int victim, Player player) { // Handles PVP.
sender.sendMessage("You attacked a player with an ID of: " + victim);
player.setEntityFace(victim);
}
public void attackNPC(int npc, Player player) { // Handles PVN.
NPC n = NPCManager.getNPC(npc);
if (n != null) {
player.setAnimation(new PlayerAnimation(getAttackAnimation(), 0));
int hitAmount = net.varek.rs2d.util.RandomNumber.rand(getMaxHit());
n.decreaseHP(hitAmount);
if (n.isMoveable())
n.setMoveable(false);
}
}
This also happens with players.
I appreciate ANY HELP, seriously.
I will be the bitch of the person who solves this problem for me(Not really)
Thanks in advance, A LOT!
-Deadly Uzi