RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
SpellCasting.java
1package com.osroyale.content.skill.impl.magic.spell;
2
3import com.osroyale.Config;
4import com.osroyale.content.achievement.AchievementHandler;
5import com.osroyale.content.achievement.AchievementKey;
6import com.osroyale.content.activity.Activity;
7import com.osroyale.content.skill.impl.magic.enchant.BoltEchantData;
8import com.osroyale.content.skill.impl.magic.enchant.SpellEnchant;
9import com.osroyale.content.skill.impl.magic.enchant.SpellEnchantData;
10import com.osroyale.game.Graphic;
11import com.osroyale.game.action.impl.SpellAction;
12import com.osroyale.game.world.entity.combat.magic.MagicRune;
13import com.osroyale.game.world.entity.mob.player.Player;
14import com.osroyale.game.world.entity.mob.player.PlayerRight;
15import com.osroyale.game.world.entity.skill.Skill;
16import com.osroyale.game.world.items.Item;
17import com.osroyale.game.world.items.ItemDefinition;
18import com.osroyale.net.packet.out.SendForceTab;
19import com.osroyale.net.packet.out.SendItemOnInterface;
20import com.osroyale.net.packet.out.SendMessage;
21import com.osroyale.net.packet.out.SendString;
22import com.osroyale.util.Stopwatch;
23
64
65public class SpellCasting {
66 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)};
67 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}};
68 private final static int[] LEVELS = {4, 7, 14, 24, 27, 29, 49, 57, 68, 87};
69
71 public Spell spell;
77 private Player player;
78
80 public SpellCasting(Player player) {
81 this.player = player;
82 this.spell = null;
83 }
84
86 public boolean cast(Spell s) {
87 if (player.skills.getLevel(Skill.MAGIC) < s.getLevel()) {
88 player.send(new SendMessage("You need a magic level " + s.getLevel() + " of to cast " + s.getName() + "."));
89 return false;
90 }
91
92 if (!PlayerRight.isDeveloper(player) && !player.isBot && !MagicRune.hasRunes(player, s.getRunes())) {
93 player.send(new SendMessage("You do not have the required runes to cast this spell."));
94 return false;
95 }
96
97 if (Activity.evaluate(player, it -> !it.canSpellCast(player))) {
98 return false;
99 }
100
101 spell = s;
102 player.inventory.removeAll(s.getRunes());
103 return true;
104 }
105
107 public void cast(Spell spell, Item item) {
108 if (player.skills.getLevel(Skill.MAGIC) < spell.getLevel()) {
109 player.send(new SendMessage("You need a Magic level of " + spell.getLevel() + " to do this!"));
110 return;
111 }
112 if (spell.getRunes() != null) {
113 if (!PlayerRight.isDeveloper(player) && !player.isBot && !MagicRune.hasRunes(player, spell.getRunes())) {
114 player.send(new SendMessage("You do not have the required runes to do this!"));
115 return;
116 }
117 }
118
119 if (Activity.evaluate(player, it -> !it.canSpellCast(player))) {
120 return;
121 }
122
123 player.action.execute(new SpellAction(player, spell, item), true);
124 }
125
126 private boolean hasEnchantRunes(int spellID) {
127 SpellEnchant spell = SpellEnchant.forSpell(spellID);
128 return spell != null && player.inventory.containsAll(spell.getRunes());
129 }
130
131 private int getEnchantmentLevel(int spellID) {
132 switch (spellID) {
133 case 1155: //Lvl-1 enchant sapphire
134 return 1;
135 case 1165: //Lvl-2 enchant emerald
136 return 2;
137 case 1176: //Lvl-3 enchant ruby
138 return 3;
139 case 1180: //Lvl-4 enchant diamond
140 return 4;
141 case 1187: //Lvl-5 enchant dragonstone
142 return 5;
143 case 6003: //Lvl-6 enchant onyx
144 return 6;
145 case 40180: //Lvl-7 enchant zenyte
146 return 7;
147 }
148 return 0;
149 }
150
151 public void enchantItem(int itemID, int spellID) {
152 SpellEnchant ens = SpellEnchant.forSpell(spellID);
153
154 if (ens == null) {
155 return;
156 }
157
158 SpellEnchantData enc = SpellEnchantData.forId(itemID);
159
160 if (enc == null) {
161 player.message("You can't enchant this item!");
162 return;
163 }
164
165 if (player.skills.getMaxLevel(Skill.MAGIC) < enc.getLevel()) {
166 player.message("You need a magic level of at least " + enc.getMagicLevel() + " to cast this spell.");
167 return;
168 }
169
170 if (player.skills.getMaxLevel(Skill.CRAFTING) < enc.getCraftingLevel()) {
171 player.message("You need a crafting level of at least " + enc.getCraftingLevel() + " to cast this spell.");
172 return;
173 }
174
175 if (!player.inventory.contains(enc.getUnenchanted(), 1)) {
176 return;
177 }
178
179 if (!hasEnchantRunes(spellID)) {
180 player.message("You do not have enough runes to cast this spell.");
181 return;
182 }
183
184 if (getEnchantmentLevel(spellID) != enc.getLevel()) {
185 player.message("You can only enchant this jewelry using a level-" + enc.getLevel() + " enchantment spell.");
186 return;
187 }
188
189 player.animate(enc.getAnimation());
190 player.inventory.removeAll(ens.getRunes());
191 player.inventory.remove(enc.getUnenchanted(), 1);
192 player.inventory.add(enc.getEnchanted(), 1);
193 player.send(new SendForceTab(Config.MAGIC_TAB));
194 player.graphic(new Graphic(enc.getGFX(), true));
195 player.skills.addExperience(Skill.MAGIC, enc.getXp() * Config.MAGIC_MODIFICATION);
196 }
197
198 public void openBoltEnchant() {
199 for (int index = 0; index < LEVELS.length; index++) {
200 String color = player.skills.get(Skill.MAGIC).getLevel() < LEVELS[index] ? "@red@" : "@gre@";
201 player.send(new SendString(color + "Magic " + LEVELS[index], 42756 + index));
202 }
203 for (int i = 0; i < RUNES.length; i++) {
204 String color = player.inventory.contains(new Item(RUNES[i][0], RUNES[i][1])) ? "@gre@" : "@red@";
205 int amount = player.inventory.computeAmountForId(RUNES[i][0]);
206 player.send(new SendString(color + (amount >= RUNES[i][1] ? RUNES[i][1] + "" : amount) + "/" + RUNES[i][1], 42766 + i));
207 }
208 player.send(new SendItemOnInterface(42752, ITEMS));
209 player.interfaceManager.open(42750);
210 }
211
212 public void enchant(int itemId) {
213 if (!player.interfaceManager.isInterfaceOpen(42750))
214 return;
215 if (!BoltEchantData.forItem(itemId).isPresent())
216 return;
217
218 BoltEchantData enchant = BoltEchantData.forItem(itemId).get();
219 String boltName = ItemDefinition.get(enchant.bolt).getName();
220
221 if (player.skills.get(Skill.MAGIC).getLevel() < enchant.levelRequired) {
222 player.send(new SendMessage("You need a Magic level of " + enchant.levelRequired + " to enchant " + boltName + "."));
223 return;
224 }
225 if (!player.inventory.contains(new Item(enchant.bolt, 10))) {
226 player.send(new SendMessage("You need 10 " + boltName + " to do this!"));
227 return;
228 }
229 if (!player.inventory.containsAll(enchant.runesRequired)) {
230 player.send(new SendMessage("You do not have the required runes to do this!"));
231 return;
232 }
233 player.inventory.removeAll(enchant.runesRequired);
234 player.inventory.remove(enchant.bolt, 10);
235 player.inventory.add(enchant.enchantedBolt, 10);
236 player.skills.addExperience(Skill.MAGIC, 250 * Config.MAGIC_MODIFICATION);
237 player.send(new SendMessage("@red@You have enchanted 10 " + boltName + (boltName.endsWith("bolts") ? "." : " bolts.")));
238 AchievementHandler.activate(player, AchievementKey.ENCHANT_BOLTS, 10);
239 openBoltEnchant();
240 }
241}