RuneHive-Game
Loading...
Searching...
No Matches
SpellCasting.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl.magic.spell;
2
3import com.runehive.Config;
4import com.runehive.content.achievement.AchievementHandler;
5import com.runehive.content.achievement.AchievementKey;
6import com.runehive.content.activity.Activity;
7import com.runehive.content.skill.impl.magic.enchant.BoltEchantData;
8import com.runehive.content.skill.impl.magic.enchant.SpellEnchant;
9import com.runehive.content.skill.impl.magic.enchant.SpellEnchantData;
10import com.runehive.game.Graphic;
11import com.runehive.game.action.impl.SpellAction;
12import com.runehive.game.world.entity.combat.magic.MagicRune;
13import com.runehive.game.world.entity.mob.player.Player;
14import com.runehive.game.world.entity.mob.player.PlayerRight;
15import com.runehive.game.world.entity.skill.Skill;
16import com.runehive.game.world.items.Item;
17import com.runehive.game.world.items.ItemDefinition;
18import com.runehive.net.packet.out.SendForceTab;
19import com.runehive.net.packet.out.SendItemOnInterface;
20import com.runehive.net.packet.out.SendMessage;
21import com.runehive.net.packet.out.SendString;
22import com.runehive.util.Stopwatch;
23
24/**
25 * Handles a player casting a non-combat spell (mostly on items).
26 *
27 * @author Daniel
28 */
29public class SpellCasting {
30 private final static Item[] ITEMS = {new Item(9236), new Item(9240), new Item(9237), new Item(9238), new Item(9241), new Item(9239), new Item(9242), new Item(9243), new Item(9244), new Item(9245)};
31 private final static int[][] RUNES = {{564, 1}, {556, 2}, {564, 1}, {555, 1}, {558, 1}, {564, 1}, {557, 2}, {564, 1}, {555, 2}, {564, 1}, {556, 3}, {561, 1}, {564, 1}, {554, 2}, {564, 1}, {554, 5}, {565, 1}, {564, 1}, {557, 10}, {563, 2}, {564, 1}, {557, 15}, {566, 1}, {564, 1}, {554, 20}, {560, 1}};
32 private final static int[] LEVELS = {4, 7, 14, 24, 27, 29, 49, 57, 68, 87};
33
34 /** The casting spell */
35 public Spell spell;
36 /** The casting stopwatch. */
38 /** The vengeance stopwatch. */
40 /** The player spell casting. */
41 private Player player;
42
43 /** Creates the <code>SpellCasting</code>. */
45 this.player = player;
46 this.spell = null;
47 }
48
49 /** Handles casting a combat spell. */
50 public boolean cast(Spell s) {
51 if (player.skills.getLevel(Skill.MAGIC) < s.getLevel()) {
52 player.send(new SendMessage("You need a magic level " + s.getLevel() + " of to cast " + s.getName() + "."));
53 return false;
54 }
55
57 player.send(new SendMessage("You do not have the required runes to cast this spell."));
58 return false;
59 }
60
61 if (Activity.evaluate(player, it -> !it.canSpellCast(player))) {
62 return false;
63 }
64
65 spell = s;
66 player.inventory.removeAll(s.getRunes());
67 return true;
68 }
69
70 /** Handles casting the spell. */
71 public void cast(Spell spell, Item item) {
72 if (player.skills.getLevel(Skill.MAGIC) < spell.getLevel()) {
73 player.send(new SendMessage("You need a Magic level of " + spell.getLevel() + " to do this!"));
74 return;
75 }
76 if (spell.getRunes() != null) {
77 if (!PlayerRight.isDeveloper(player) && !player.isBot && !MagicRune.hasRunes(player, spell.getRunes())) {
78 player.send(new SendMessage("You do not have the required runes to do this!"));
79 return;
80 }
81 }
82
83 if (Activity.evaluate(player, it -> !it.canSpellCast(player))) {
84 return;
85 }
86
87 player.action.execute(new SpellAction(player, spell, item), true);
88 }
89
90 private boolean hasEnchantRunes(int spellID) {
92 return spell != null && player.inventory.containsAll(spell.getRunes());
93 }
94
95 private int getEnchantmentLevel(int spellID) {
96 switch (spellID) {
97 case 1155: //Lvl-1 enchant sapphire
98 return 1;
99 case 1165: //Lvl-2 enchant emerald
100 return 2;
101 case 1176: //Lvl-3 enchant ruby
102 return 3;
103 case 1180: //Lvl-4 enchant diamond
104 return 4;
105 case 1187: //Lvl-5 enchant dragonstone
106 return 5;
107 case 6003: //Lvl-6 enchant onyx
108 return 6;
109 case 40180: //Lvl-7 enchant zenyte
110 return 7;
111 }
112 return 0;
113 }
114
115 public void enchantItem(int itemID, int spellID) {
116 SpellEnchant ens = SpellEnchant.forSpell(spellID);
117
118 if (ens == null) {
119 return;
120 }
121
123
124 if (enc == null) {
125 player.message("You can't enchant this item!");
126 return;
127 }
128
129 if (player.skills.getMaxLevel(Skill.MAGIC) < enc.getLevel()) {
130 player.message("You need a magic level of at least " + enc.getMagicLevel() + " to cast this spell.");
131 return;
132 }
133
134 if (player.skills.getMaxLevel(Skill.CRAFTING) < enc.getCraftingLevel()) {
135 player.message("You need a crafting level of at least " + enc.getCraftingLevel() + " to cast this spell.");
136 return;
137 }
138
139 if (!player.inventory.contains(enc.getUnenchanted(), 1)) {
140 return;
141 }
142
143 if (!hasEnchantRunes(spellID)) {
144 player.message("You do not have enough runes to cast this spell.");
145 return;
146 }
147
148 if (getEnchantmentLevel(spellID) != enc.getLevel()) {
149 player.message("You can only enchant this jewelry using a level-" + enc.getLevel() + " enchantment spell.");
150 return;
151 }
152
153 player.animate(enc.getAnimation());
154 player.inventory.removeAll(ens.getRunes());
155 player.inventory.remove(enc.getUnenchanted(), 1);
156 player.inventory.add(enc.getEnchanted(), 1);
158 player.graphic(new Graphic(enc.getGFX(), true));
159 player.skills.addExperience(Skill.MAGIC, enc.getXp() * Config.MAGIC_MODIFICATION);
160 }
161
162 public void openBoltEnchant() {
163 for (int index = 0; index < LEVELS.length; index++) {
164 String color = player.skills.get(Skill.MAGIC).getLevel() < LEVELS[index] ? "@red@" : "@gre@";
165 player.send(new SendString(color + "Magic " + LEVELS[index], 42756 + index));
166 }
167 for (int i = 0; i < RUNES.length; i++) {
168 String color = player.inventory.contains(new Item(RUNES[i][0], RUNES[i][1])) ? "@gre@" : "@red@";
169 int amount = player.inventory.computeAmountForId(RUNES[i][0]);
170 player.send(new SendString(color + (amount >= RUNES[i][1] ? RUNES[i][1] + "" : amount) + "/" + RUNES[i][1], 42766 + i));
171 }
172 player.send(new SendItemOnInterface(42752, ITEMS));
173 player.interfaceManager.open(42750);
174 }
175
176 public void enchant(int itemId) {
177 if (!player.interfaceManager.isInterfaceOpen(42750))
178 return;
179 if (!BoltEchantData.forItem(itemId).isPresent())
180 return;
181
183 String boltName = ItemDefinition.get(enchant.bolt).getName();
184
185 if (player.skills.get(Skill.MAGIC).getLevel() < enchant.levelRequired) {
186 player.send(new SendMessage("You need a Magic level of " + enchant.levelRequired + " to enchant " + boltName + "."));
187 return;
188 }
189 if (!player.inventory.contains(new Item(enchant.bolt, 10))) {
190 player.send(new SendMessage("You need 10 " + boltName + " to do this!"));
191 return;
192 }
193 if (!player.inventory.containsAll(enchant.runesRequired)) {
194 player.send(new SendMessage("You do not have the required runes to do this!"));
195 return;
196 }
197 player.inventory.removeAll(enchant.runesRequired);
198 player.inventory.remove(enchant.bolt, 10);
199 player.inventory.add(enchant.enchantedBolt, 10);
200 player.skills.addExperience(Skill.MAGIC, 250 * Config.MAGIC_MODIFICATION);
201 player.send(new SendMessage("@red@You have enchanted 10 " + boltName + (boltName.endsWith("bolts") ? "." : " bolts.")));
204 }
205}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final int MAGIC_TAB
Definition Config.java:194
static final double MAGIC_MODIFICATION
The experience modification for magic.
Definition Config.java:268
static void activate(Player player, AchievementKey achievement)
Activates the achievement for the individual player.
A Activity object constructs an in-game activity and sequences it through the start() and finish() me...
Definition Activity.java:31
static boolean evaluate(Mob mob, Predicate< Activity > predicate)
Definition Activity.java:74
void cast(Spell spell, Item item)
Handles casting the spell.
boolean cast(Spell s)
Handles casting a combat spell.
SpellCasting(Player player)
Creates the SpellCasting.
Represents a single graphic that can be used by entities.
Definition Graphic.java:10
Handles the spell casting action.
This class represents a character controlled by a player.
Definition Player.java:125
Represents a trainable and usable skill.
Definition Skill.java:18
static final int CRAFTING
The crafting skill id.
Definition Skill.java:57
static final int MAGIC
The magic skill id.
Definition Skill.java:39
Represents all of an in-game Item's attributes.
static ItemDefinition get(int id)
Gets an item definition.
The container class that represents an item that can be interacted with.
Definition Item.java:21
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
static Stopwatch start()
static Optional< BoltEchantData > forItem(int item)
static boolean hasRunes(Player player, Item[] required)
static boolean isDeveloper(Player player)
Checks if the player has developer status.
The itemcontainer for casting a spell.
Definition Spell.java:11
int getLevel()
Gets the level required to cast spell.
String getName()
Gets the name of the spell.
Item[] getRunes()
Gets the runes required to cast the spell.