RuneHive-Game
Loading...
Searching...
No Matches
ProducingSkillAction.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl;
2
3import com.runehive.net.packet.out.SendMessage;
4import com.runehive.game.world.entity.mob.player.Player;
5import com.runehive.content.skill.SkillAction;
6import com.runehive.game.world.items.Item;
7import com.runehive.game.world.items.containers.ItemContainer;
8import com.runehive.game.world.position.Position;
9import com.runehive.util.StringUtils;
10
11import java.util.Optional;
12
13/**
14 * The skill action that represents an action where one item in an inventory is
15 * replaced with a new one. This type of skill action is somewhat basic and
16 * requires that a player have the item to be removed.
17 * <p>
18 * <p>
19 * The skills that may use this type skill action include, but are not limited
20 * to {@code COOKING}.
21 * @author lare96 <http://github.com/lare96>
22 * @see SkillAction
23 * @see DestructionSkillAction
24 * @see HarvestingSkillAction
25 */
26public abstract class ProducingSkillAction extends SkillAction {
27
28 /**
29 * Creates a new {@link ProducingSkillAction}.
30 * @param player the player this skill action is for.
31 * @param position the position the player should face.
32 * @param instant determines if this action should be ran instantly.
33 */
34 public ProducingSkillAction(Player player, Optional<Position> position, boolean instant) {
35 super(player, position, instant);
36 }
37
38 /**
39 * Creates a new {@link ProducingSkillAction}.
40 * @param player the player this skill action is for.
41 * @param position the position the player should face.
42 * @param delay the delay between these producing skill actions.
43 * @param instant determines if this action should be ran instantly.
44 */
45 public ProducingSkillAction(Player player, Optional<Position> position, int delay, boolean instant) {
46 super(player, position, delay, instant);
47 }
48
49 @Override
50 public final boolean canRun() {
51 Optional<Item[]> removeItem = removeItem();
52
53 if(!removeItem.isPresent())
54 return true;
55
56 ItemContainer container = getMob().getPlayer().inventory.copy();
57 if(container.removeAll(removeItem.get()) && !container.hasCapacityFor(produceItem().get())) {
58 container.fireCapacityExceededEvent();
59 return false;
60 }
61 if(getMob().getPlayer().inventory.containsAll(removeItem.get()))
62 return true;
63
64 if(!message().isPresent()) {
65 for(Item item : removeItem.get()) {
66 if(!getMob().getPlayer().inventory.contains(item)) {
67 String anyOrEnough = item.getAmount() == 1 ? "any" : "enough";
68 getMob().getPlayer().send(new SendMessage("You don't have " + anyOrEnough + " " + StringUtils.appendPluralCheck(item.getName()) + " left."));
69 return false;
70 }
71 }
72 } else {
73 getMob().getPlayer().send(new SendMessage(message().get()));
74 }
75
76 onProduce(false);
77 return false;
78 }
79
80 @Override
81 public final void onExecute() {
82 getMob().skills.addExperience(skill(), experience());
83 removeItem().ifPresent(getMob().getPlayer().inventory::removeAll);
84 produceItem().ifPresent(getMob().getPlayer().inventory::addAll);
85 onProduce(true);
86 }
87
88 /**
89 * The method executed upon production of an item.
90 * @param success determines if the production was successful or not.
91 */
92 public void onProduce(boolean success) {
93
94 }
95
96 /**
97 * The item that will be removed upon production.
98 * @return the item that will be removed.
99 */
100 public abstract Optional<Item[]> removeItem();
101
102 /**
103 * The item that will be added upon production.
104 * @return the item that will be added.
105 */
106 public abstract Optional<Item[]> produceItem();
107
108 /**
109 * The message that will be sent when the player doesn't
110 * have the items required.
111 * @return the alphabetic value which represents the message.
112 */
113 public Optional<String> message() {
114 return Optional.empty();
115 }
116
117}
SkillAction(Mob mob, Optional< Position > position, int delay, boolean instant)
Creates a new Action randomevent.
final Optional< Position > position
The position we should face.
abstract double experience()
The experience given from this action.
abstract int skill()
The skill we should hand to experience to.
abstract Optional< Item[]> produceItem()
The item that will be added upon production.
ProducingSkillAction(Player player, Optional< Position > position, boolean instant)
Creates a new ProducingSkillAction.
Optional< String > message()
The message that will be sent when the player doesn't have the items required.
final void onExecute()
The method which is called on intervals of the specified #delay;.
final boolean canRun()
Determines if the task can be ran.
void onProduce(boolean success)
The method executed upon production of an item.
ProducingSkillAction(Player player, Optional< Position > position, int delay, boolean instant)
Creates a new ProducingSkillAction.
abstract Optional< Item[]> removeItem()
The item that will be removed upon production.
T getMob()
Gets the player.
Definition Action.java:44
final boolean instant
If execution happens instantly upon being scheduled.
Definition Task.java:14
int delay
The cyclic delay.
Definition Task.java:17
This class represents a character controlled by a player.
Definition Player.java:125
The container class that represents an item that can be interacted with.
Definition Item.java:21
An abstraction game representing a group of Items.
final boolean hasCapacityFor(Item... item)
Determines if this container has the capacity for item.
final void fireCapacityExceededEvent()
Fires the ItemContainerListener.capacityExceeded(ItemContainer) event.
boolean removeAll(Collection<? extends Item > items)
Attempts to withdraw items in bulk from this container.
final ItemContainer copy()
Creates a copy of the underlying item container.
The OutgoingPacket that sends a message to a Players chatbox in the client.
static String appendPluralCheck(String thing)
Appends the determined plural check to thing.