interesting naming to say the least.
Code:
public abstract class SpellHandler {
public static enum SpellType {
LUNARS, ANCIENT, MODERNS;
}
private static final HashMap<Integer, SpellHandler> spell = new HashMap<Integer, SpellHandler>();
static {
spell.put(23, new IceBarrage());
spell.put(37, new Vengeance());
spell.put(25, new WindStrike());
spell.put(28, new WaterStrike());
spell.put(36, new Bind());
}
public static boolean handle(Player player, int spellId) {
SpellHandler castedSpell = spell.get(spellId);
if (castedSpell == null) {
player.getPackets().sendGameMessage("Err: 6913. Oh dear! you've found a spell that is not handled!");
return false;
}
if (castedSpell.otherRequirements(player))
return false;
if (castedSpell.requiredLevel() > player.getSkills().getLevel(Skills.MAGIC)) {
player.getPackets().sendGameMessage(
"You need a magic level of " + castedSpell.requiredLevel() + " to cast this spell.");
return true;
}
player.getCombatDefinitions().setSpell(spellId);
if (castedSpell.isDamageSpell()) {
if (castedSpell.getCastAnimation() != null)
player.setNextAnimation(castedSpell.getCastAnimation());
if (castedSpell.getCastGfx() != null)
player.setNextGraphics(castedSpell.getCastGfx());
player.getCombat().getCombatHit().hitTarget(player, 1, 1, CombatStyle.MAGIC);
} else {
castedSpell.handleSpell(player);
}
return true;
}
protected CombatHit combat = new CombatHit();
public abstract int getBaseXP();
public abstract Animation getCastAnimation();
public abstract Graphics getCastGfx();
public abstract int getCastingSound();
public abstract Graphics getFrozenHitGfx();
public abstract Graphics getHitGfx();
public abstract int getHitSound();
public abstract int getSpellId();
public abstract SpellType getSpellType();
public abstract void handleSpell(Player player);
public abstract boolean isDamageSpell();
public abstract boolean otherRequirements(Player player);
public abstract int requiredLevel();
//public abstract int[] runesRequired();
/*
* ICE_BLITZ(SpellType.ANCIENT, 82, 21, 1978, 366, 15, 46, -1, new int[] {
* 367, 367 }, new int[] {171, 169});
*/
}
the idea behind this is good but the design could be improved. leave the data to be stored in a separate class. the 'SpellHandler' should only be for polymorphism, not actual logic.
also to shorten code and make this easier, ex:
Code:
/**
* @author Tyluur <[email protected]>
* @since 6/23/2017
*/
public interface MagicSpellEvent<T extends MagicSpellContext> extends CombatRegistryEvent {
/**
* The id of the spell
*/
int spellId();
/**
* The delay on the spell
*/
int delay();
/**
* The animation id for the spell
*/
int animationId();
/**
* The id of the graphics applied when the hit lands
*/
int hitGfx();
/**
* The base damage of the spell
*/
int maxHit();
/**
* The base experience of the spell
*/
double exp();
/**
* The book the spell is on
*/
MagicBook book();
/**
* Casts the magic spell
*
* @param player
* The player
* @param context
* The context
*/
void cast(Player player, T context);
/**
* The default height of the hit land gfx
*/
default int gfxHeight() {
return 96;
}
}