RuneHive-Game
Loading...
Searching...
No Matches
Crafting.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl.crafting;
2
3import com.runehive.Config;
4import com.runehive.content.skillcape.SkillCape;
5import com.runehive.content.activity.randomevent.RandomEventHandler;
6import com.runehive.content.store.Store;
7import com.runehive.net.packet.out.SendInputAmount;
8import com.runehive.net.packet.out.SendChatBoxInterface;
9import com.runehive.net.packet.out.SendItemOnInterfaceZoom;
10import com.runehive.net.packet.out.SendMessage;
11import com.runehive.net.packet.out.SendString;
12import com.runehive.game.Animation;
13import com.runehive.game.action.Action;
14import com.runehive.game.action.policy.WalkablePolicy;
15import com.runehive.game.world.entity.mob.player.Player;
16import com.runehive.content.event.impl.*;
17import com.runehive.content.skill.impl.crafting.impl.*;
18import com.runehive.game.world.entity.skill.Skill;
19import com.runehive.game.world.items.Item;
20import com.runehive.game.world.object.GameObject;
21import com.runehive.util.Utility;
22
23import java.util.HashMap;
24
25/**
26 * Handles the crafting skill.
27 *
28 * @author Daniel
29 */
30public class Crafting extends Skill {
31
32 /** The craftable key. */
33 private static final String CRAFTABLE_KEY = "CRAFTABLE_KEY";
34
35 /** The craftable map. */
36 private final static HashMap<Integer, Craftable> CRAFTABLES = new HashMap<>();
37
38 /** The leather armour data. */
39 private static final Object[][] LEATHER_ARMOR_IDS = {
40 {8635, 1, Leather.LEATHER_BODY}, {8634, 5, Leather.LEATHER_BODY}, {8633, 10, Leather.LEATHER_BODY},
41 {8638, 1, Leather.LEATHER_GLOVES}, {8637, 5, Leather.LEATHER_GLOVES}, {8636, 10, Leather.LEATHER_GLOVES},
42 {8641, 1, Leather.LEATHER_BOOTS}, {8640, 5, Leather.LEATHER_BOOTS}, {8639, 10, Leather.LEATHER_BOOTS},
44 {8647, 1, Leather.LEATHER_CHAPS}, {8646, 5, Leather.LEATHER_CHAPS}, {8645, 10, Leather.LEATHER_CHAPS},
45 {8650, 1, Leather.LEATHER_COIF}, {8649, 5, Leather.LEATHER_COIF}, {8648, 10, Leather.LEATHER_COIF},
46 {8653, 1, Leather.LEATHER_COWL}, {8652, 5, Leather.LEATHER_COWL}, {8651, 10, Leather.LEATHER_COWL},
47 };
48
49 public Crafting(int level, double experience) {
50 super(Skill.CRAFTING, level, experience);
51 }
52
53 public static void addCraftable(Craftable craftable) {
54 if (CRAFTABLES.put(craftable.getWith().getId(), craftable) != null) {
55 System.out.println("[Crafting] Conflicting item values: " + craftable.getWith().getId() + " Type: " + craftable.getName());
56 }
57 }
58
59 private static Craftable getCraftable(int use, int with) {
60 return CRAFTABLES.get(use) == null ? CRAFTABLES.get(with) : CRAFTABLES.get(use);
61 }
62
63 @Override
64 protected boolean clickObject(Player player, ObjectInteractionEvent event) {
65 if (event.getObject().getId() == 4309 && event.getOpcode() == 1) {
66 Spinning.open(player);
67 return true;
68 }
69
70 if (event.getObject().getId() == 11601 && event.getOpcode() == 0) {
71 Jewellery.open(player);
72 return true;
73 }
74
75 return false;
76 }
77
78 @Override
79 protected boolean clickNpc(Player player, NpcInteractionEvent event) {
80 if (event.getNpc().id == 5811) {
81 if (event.getOpcode() == 0) {
82 Tanning.open(player);
83 } else if (event.getOpcode() == 1) {
84 Store.STORES.get("Crafting Store").open(player);
85 }
86 return true;
87 }
88
89 return false;
90 }
91
92 @Override
93 protected boolean useItem(Player player, ItemOnObjectInteractionEvent event) {
94 Item item = event.getItem();
95 GameObject object = event.getObject();
96
97 if (object.getId() == 16469 && (item.getId() == 1783 || item.getId() == 1781)) {
99 return true;
100 }
101
102 return false;
103 }
104
105 @Override
106 protected boolean useItem(Player player, ItemOnItemInteractionEvent event) {
107 Item first = event.getFirst();
108 Item second = event.getSecond();
109
110 if (Stringing.useItem(player, first, second)) {
111 return true;
112 }
113
114 if (first.getId() == 1785 && second.getId() == 1775 || first.getId() == 1775 && second.getId() == 1785) {
115 Glass.open(player);
116 return true;
117 }
118
119 if ((first.getId() == 1733 && second.getId() == 1741) || (first.getId() == 1741 && second.getId() == 1733)) {
120 player.attributes.set(CRAFTABLE_KEY, "HIDE");
121 player.interfaceManager.open(2311);
122 return true;
123 }
124
125 Craftable craftable = getCraftable(first.getId(), second.getId());
126
127 if (craftable == null) {
128 return false;
129 }
130
131 if (!craftable.getUse().equalIds(first) && !craftable.getUse().equalIds(second)) {
132 player.message("You need to use this with " + Utility.getAOrAn(craftable.getUse().getName()) + " " + craftable.getUse().getName().toLowerCase() + " to craft this item.");
133 return true;
134 }
135
136 switch (craftable.getCraftableItems().length) {
137
138 case 1:
139 player.attributes.set(CRAFTABLE_KEY, craftable);
140 player.send(new SendString("\\n \\n \\n \\n" + craftable.getCraftableItems()[0].getProduct().getName(), 2799));
141 player.send(new SendItemOnInterfaceZoom(1746, 170, craftable.getCraftableItems()[0].getProduct().getId()));
142 player.send(new SendChatBoxInterface(4429));
143 return true;
144 case 2:
145 player.attributes.set(CRAFTABLE_KEY, craftable);
146 player.send(new SendItemOnInterfaceZoom(8869, 170, craftable.getCraftableItems()[0].getProduct().getId()));
147 player.send(new SendItemOnInterfaceZoom(8870, 170, craftable.getCraftableItems()[1].getProduct().getId()));
148 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[0].getProduct().getName().replace("d'hide ", "")), 8874));
149 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[1].getProduct().getName().replace("d'hide ", "")), 8878));
150 player.send(new SendChatBoxInterface(8866));
151 return true;
152 case 3:
153 player.attributes.set(CRAFTABLE_KEY, craftable);
154 player.send(new SendItemOnInterfaceZoom(8883, 170, craftable.getCraftableItems()[0].getProduct().getId()));
155 player.send(new SendItemOnInterfaceZoom(8884, 170, craftable.getCraftableItems()[1].getProduct().getId()));
156 player.send(new SendItemOnInterfaceZoom(8885, 170, craftable.getCraftableItems()[2].getProduct().getId()));
157 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[0].getProduct().getName().replace("d'hide ", "")), 8889));
158 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[1].getProduct().getName().replace("d'hide ", "")), 8893));
159 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[2].getProduct().getName().replace("d'hide ", "")), 8897));
160 player.send(new SendChatBoxInterface(8880));
161 return true;
162 case 4:
163 player.attributes.set(CRAFTABLE_KEY, craftable);
164 player.send(new SendItemOnInterfaceZoom(8902, 170, craftable.getCraftableItems()[0].getProduct().getId()));
165 player.send(new SendItemOnInterfaceZoom(8903, 170, craftable.getCraftableItems()[1].getProduct().getId()));
166 player.send(new SendItemOnInterfaceZoom(8904, 170, craftable.getCraftableItems()[2].getProduct().getId()));
167 player.send(new SendItemOnInterfaceZoom(8905, 170, craftable.getCraftableItems()[3].getProduct().getId()));
168 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[0].getProduct().getName().replace("d'hide ", "")), 8909));
169 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[1].getProduct().getName().replace("d'hide ", "")), 8913));
170 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[2].getProduct().getName().replace("d'hide ", "")), 8917));
171 player.send(new SendString("\\n \\n \\n \\n".concat(craftable.getCraftableItems()[3].getProduct().getName().replace("d'hide ", "")), 8921));
172 player.send(new SendChatBoxInterface(8899));
173 return true;
174 case 5:
175 player.attributes.set(CRAFTABLE_KEY, craftable);
176 player.send(new SendItemOnInterfaceZoom(8941, 170, craftable.getCraftableItems()[0].getProduct().getId()));
177 player.send(new SendItemOnInterfaceZoom(8942, 170, craftable.getCraftableItems()[1].getProduct().getId()));
178 player.send(new SendItemOnInterfaceZoom(8943, 170, craftable.getCraftableItems()[2].getProduct().getId()));
179 player.send(new SendItemOnInterfaceZoom(8944, 170, craftable.getCraftableItems()[3].getProduct().getId()));
180 player.send(new SendItemOnInterfaceZoom(8945, 170, craftable.getCraftableItems()[4].getProduct().getId()));
181 player.send(new SendString("\\n \\n \\n \\n".concat("Body"), 8949));
182 player.send(new SendString("\\n \\n \\n \\n".concat("Chaps"), 8953));
183 player.send(new SendString("\\n \\n \\n \\n".concat("Vambraces"), 8957));
184 player.send(new SendString("\\n \\n \\n \\n".concat("Bandana"), 8961));
185 player.send(new SendString("\\n \\n \\n \\n".concat("Boots"), 8965));
186 player.send(new SendChatBoxInterface(8938));
187 return true;
188
189 default:
190 return false;
191 }
192 }
193
194 @Override
196 if (Tanning.click(player, event.getButton())) {
197 return true;
198 }
199
200 if (Glass.click(player, event.getButton())) {
201 return true;
202 }
203
204 if (!player.attributes.has(CRAFTABLE_KEY)) {
205 return false;
206 }
207
208 int button = event.getButton();
209
210 if (button == 2422) {
211 return false;
212 }
213
214 if (String.valueOf(player.attributes.getObject(CRAFTABLE_KEY)).equals("HIDE")) {
215 for (Object[] i : LEATHER_ARMOR_IDS) {
216 if ((int) i[0] == button) {
217 player.interfaceManager.close();
218 start(player, (Craftable) i[2], 0, (int) i[1]);
219 return true;
220 }
221 }
222 }
223
224 Craftable craftable = player.attributes.get(CRAFTABLE_KEY, Craftable.class);
225
226 switch (button) {
227
228 /* Option 1 - Make all */
229 case 1747:
230 start(player, craftable, 0, player.inventory.computeAmountForId(craftable.getWith().getId()));
231 return true;
232
233 /* Option 1 - Make 1 */
234 case 2799:
235 case 8909:
236 case 8874:
237 case 8889:
238 case 8949:
239 start(player, craftable, 0, 1);
240 return true;
241
242 /* Option 1 - Make 5 */
243 case 2798:
244 case 8908:
245 case 8873:
246 case 8888:
247 case 8948:
248 start(player, craftable, 0, 5);
249 return true;
250
251 /* Option 1 - Make 10 */
252 case 1748:
253 case 8907:
254 case 8872:
255 case 8887:
256 case 8947:
257 start(player, craftable, 0, 10);
258 return true;
259
260 /* Option 1 - Make X */
261 case 8906:
262 case 8871:
263 case 8886:
264 case 6212:
265 case 8946:
266 player.send(new SendInputAmount("Enter amount", 10, input -> start(player, craftable, 0, Integer.parseInt(input))));
267 return true;
268
269 /* Option 2 - Make 1 */
270 case 8913:
271 case 8878:
272 case 8893:
273 case 8953:
274 start(player, craftable, 1, 1);
275 return true;
276
277 /* Option 2 - Make 5 */
278 case 8912:
279 case 8877:
280 case 8892:
281 case 8952:
282 start(player, craftable, 1, 5);
283 return true;
284
285 /* Option 2 - Make 10 */
286 case 8911:
287 case 8876:
288 case 8891:
289 case 8951:
290 start(player, craftable, 1, 10);
291 return true;
292
293 /* Option 2 - Make X */
294 case 8910:
295 case 8875:
296 case 8890:
297 case 8950:
298 player.send(new SendInputAmount("Enter amount", 10, input -> start(player, craftable, 1, Integer.parseInt(input))));
299 return true;
300
301 /* Option 3 - Make 1 */
302 case 8917:
303 case 8897:
304 case 8957:
305 start(player, craftable, 2, 1);
306 return true;
307
308 /* Option 3 - Make 5 */
309 case 8916:
310 case 8896:
311 case 8956:
312 start(player, craftable, 2, 5);
313 return true;
314
315 /* Option 3 - Make 10 */
316 case 8915:
317 case 8895:
318 case 8955:
319 start(player, craftable, 2, 10);
320 return true;
321
322 /* Option 3 - Make X */
323 case 8914:
324 case 8894:
325 case 8954:
326 player.send(new SendInputAmount("Enter amount", 10, input -> start(player, craftable, 2, Integer.parseInt(input))));
327 return true;
328
329 /* Option 4 - Make 1 */
330 case 8921:
331 case 8961:
332 start(player, craftable, 3, 1);
333 return true;
334
335 /* Option 4 - Make 5 */
336 case 8920:
337 case 8960:
338 start(player, craftable, 3, 5);
339 return true;
340
341 /* Option 4 - Make 10 */
342 case 8919:
343 case 8959:
344 start(player, craftable, 3, 10);
345 return true;
346
347 /* Option 4 - Make X */
348 case 8918:
349 case 8958:
350 player.send(new SendInputAmount("Enter amount", 10, input -> start(player, craftable, 3, Integer.parseInt(input))));
351 return true;
352
353 /* Option 5 - Make 1 */
354 case 8965:
355 start(player, craftable, 4, 1);
356 return true;
357
358 /* Option 5 - Make 5 */
359 case 8964:
360 start(player, craftable, 4, 5);
361 return true;
362
363 /* Option 5 - Make 10 */
364 case 8963:
365 start(player, craftable, 4, 10);
366 return true;
367
368 /* Option 5 - Make X */
369 case 8962:
370 player.send(new SendInputAmount("Enter amount", 10, input -> start(player, craftable, 4, Integer.parseInt(input))));
371 return true;
372
373 default:
374 return false;
375 }
376 }
377
378 public boolean craft(Player player, int index, int amount) {
379 Craftable craftable = player.attributes.get(CRAFTABLE_KEY);
380 return start(player, craftable, index, amount);
381 }
382
383 public boolean start(Player player, Craftable craftable, int index, int amount) {
384 if (craftable == null) {
385 return false;
386 }
387
389
390 CraftableItem item = craftable.getCraftableItems()[index];
391
392 player.interfaceManager.close();
393
394 if (player.skills.getLevel(Skill.CRAFTING) < item.getLevel()) {
395 player.dialogueFactory.sendStatement("<col=369>You need a Crafting level of " + item.getLevel() + " to do that.").execute();
396 return true;
397 }
398
399 if (!player.inventory.containsAll(craftable.getIngredients(index))) {
400 Item requiredItem = craftable.getCraftableItems()[index].getRequiredItem();
401 Item product = craftable.getCraftableItems()[index].getProduct();
402 String productAmount = "";
403
404 if (product.getName().contains("vamb")) {
405 productAmount = " pair of";
406 } else if (!product.getName().endsWith("s")) {
407 productAmount = " " + Utility.getAOrAn(product.getName());
408 }
409
410 player.send(new SendMessage("You need " + requiredItem.getAmount() + " piece" + (requiredItem.getAmount() > 1 ? "s" : "") + " of " + requiredItem.getName().toLowerCase() + " to make" + productAmount + " " + product.getName().toLowerCase() + "."));
411 return true;
412 }
413
414 player.action.execute(craft(player, craftable, item, index, amount), true);
415 return true;
416 }
417
418 private Action<Player> craft(Player player, Craftable craftable, CraftableItem item, int index, int amount) {
419 return new Action<Player>(player, 2, true) {
420 int iterations = 0;
421
422 @Override
423 public void execute() {
424 player.animate(new Animation(craftable.getAnimation()));
426
427 final boolean saveMaterial = SkillCape.isEquipped(player, SkillCape.CRAFTING) && Utility.random(1, 3) == 1;
428 if (!saveMaterial) {
429 player.inventory.removeAll(craftable.getIngredients(index));
430 }
431 player.inventory.addOrDrop(item.getProduct());
434
435 if (craftable.getProductionMessage() != null) {
436 player.send(new SendMessage(craftable.getProductionMessage()));
437 }
438
439// if (craftable.getName() == "Gem") {
440// AchievementHandler.activateTask(player, AchievementList.CUT_250_GEMS, 1);
441// }
442
443 if (++iterations == amount) {
444 cancel();
445 return;
446 }
447
448 if (!player.inventory.containsAll(craftable.getIngredients(index))) {
449 cancel();
450 player.send(new SendMessage("<col=369>You have run out of materials."));
451 return;
452 }
453 }
454
455 @Override
456 public String getName() {
457 return "Crafting";
458 }
459
460 @Override
461 public boolean prioritized() {
462 return false;
463 }
464
465 @Override
466 public WalkablePolicy getWalkablePolicy() {
468 }
469 };
470 }
471}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final double CRAFTING_MODIFICATION
The experience modification for crafting.
Definition Config.java:253
final DialogueFactory execute()
Retrieves the next dialogue in the chain and executes it.
final DialogueFactory sendStatement(String... lines)
Appends a StatementDialogue to the current dialogue chain.
double getExperience()
Gets the experience rewarded.
static void addCraftable(Craftable craftable)
Definition Crafting.java:53
static final HashMap< Integer, Craftable > CRAFTABLES
The craftable map.
Definition Crafting.java:36
boolean useItem(Player player, ItemOnItemInteractionEvent event)
static final String CRAFTABLE_KEY
The craftable key.
Definition Crafting.java:33
boolean clickNpc(Player player, NpcInteractionEvent event)
Definition Crafting.java:79
boolean clickObject(Player player, ObjectInteractionEvent event)
Definition Crafting.java:64
boolean start(Player player, Craftable craftable, int index, int amount)
static final Object[][] LEATHER_ARMOR_IDS
The leather armour data.
Definition Crafting.java:39
boolean craft(Player player, int index, int amount)
boolean clickButton(Player player, ClickButtonInteractionEvent event)
boolean useItem(Player player, ItemOnObjectInteractionEvent event)
Definition Crafting.java:93
static Craftable getCraftable(int use, int with)
Definition Crafting.java:59
Action< Player > craft(Player player, Craftable craftable, CraftableItem item, int index, int amount)
static boolean click(Player player, int button)
Handles clicking on the interface.
Definition Glass.java:69
static void craft(Player player, GlassData glass, int amount)
Handles crafting the glass.
Definition Glass.java:139
static void open(Player player)
Handles opening the glass crafting interface.
Definition Glass.java:64
static void open(Player player)
Opens the jewellery creation itemcontainer.
Handles spinning items on the spinning wheel.
Definition Spinning.java:21
static void open(Player player)
Handles opening the spinning dialogue.
Definition Spinning.java:74
static boolean useItem(Player player, Item used, Item with)
Handles using item.
static boolean click(Player player, int button)
Handles clicking the tan buttons on the itemcontainer.
Definition Tanning.java:117
static void open(Player player)
Handles opening the tanning itemcontainer.
Definition Tanning.java:67
The class which holds support for further abstraction for shops.
Definition Store.java:20
static Map< String, Store > STORES
A mapping of each shop by it's name.
Definition Store.java:23
Class that models a single animation used by an entity.
Represents an action an entity can execute.
Definition Action.java:12
public< A extends Action<?> > void execute(A action)
final GenericAttributes attributes
Definition Mob.java:95
void open(int identification)
Opens an interface for the player.
This class represents a character controlled by a player.
Definition Player.java:125
static String getName(int skill)
Gets the name for a skill id.
Definition Skill.java:465
double experience
The current skill experience.
Definition Skill.java:179
int level
The current level of the skill.
Definition Skill.java:173
Skill(int skill, int level, double experience)
Constructs a new Skill.
Definition Skill.java:184
void addExperience(int id, double experience)
Adds experience to a given skill.
int getLevel(int id)
Gets the level of a skill.
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int getId()
Gets the identification of this item.
Definition Item.java:324
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
boolean equalIds(Item other)
Definition Item.java:461
final int computeAmountForId(int id)
Computes the total quantity of the Items in this container with id.
boolean removeAll(Collection<? extends Item > items)
Attempts to withdraw items in bulk from this container.
final boolean containsAll(int... identifiers)
Determines if this container contains all identifiers.
void addOrDrop(List< Item > items)
Attempts to deposit an item to the players inventory, if there is no space it'll bank the item instea...
Handles sending the SendItemOnInterfaceZoom packet.
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.
Handles miscellaneous methods.
Definition Utility.java:27
static int random(int bound)
Definition Utility.java:239
static String getAOrAn(String nextWord)
A or an.
Definition Utility.java:116
public< K, E > E get(K key)
Gets a generic attribute.
public< K > Object getObject(K key)
Gets a generic attribute.
public< K > void remove(K key)
Removes a generic attribute.
public< K, E > void set(K key, E attribute)
Sets a generic attribute.
public< K > boolean has(K key)
Checks if a key is in the list of generic attribute.
static boolean isEquipped(Player player, SkillCape cape)
A queue policy determines whether the action can occur while walking.
NON_WALKABLE
This indicates actions cannot occur while walking.
Item[] getIngredients(int index)
Gets the craftable ingredients.
Item getUse()
Gets the craftable used item.
CraftableItem[] getCraftableItems()
Gets the craftable items.
String getName()
Gets the craftable name.
String getProductionMessage()
Gets the craftable production message.
Item getWith()
Gets the craftable item used with.
int getAnimation()
Gets the craftable animation.