RuneHive-Game
Loading...
Searching...
No Matches
HarvestingSkillAction.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl;
2
3import com.google.common.base.Preconditions;
4import com.runehive.net.packet.out.SendMessage;
5import com.runehive.game.action.Action;
6import com.runehive.game.action.policy.WalkablePolicy;
7import com.runehive.game.world.entity.mob.Mob;
8import com.runehive.game.world.entity.mob.player.Player;
9import com.runehive.content.skill.SkillAction;
10import com.runehive.game.world.items.Item;
11import com.runehive.game.world.position.Position;
12import com.runehive.util.RandomGen;
13import com.runehive.util.StringUtils;
14
15import java.util.Optional;
16
17/**
18 * Holds functionality for skills such as woodcutting and mining.
19 *
20 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand Up</a>
21 * @since 19-12-2016.
22 */
23public abstract class HarvestingSkillAction extends SkillAction {
24
25 /**
26 * The double onReward types.
27 */
28 public enum DoubleReward {
30 }
31
32 /**
33 * The factor boost that determines the success rate for harvesting based on
34 * skill level. The higher the number the less frequently harvest will be
35 * obtained. A value higher than {@code 99} or lower than {@code 0} will
36 * throw an {@link IllegalStateException}.
37 */
38 private static final int SUCCESS_FACTOR = 10;
39
40 /**
41 * The random generator instance that will generate random numbers.
42 */
43 private final RandomGen random = new RandomGen();
44
45 /**
46 * Creates a new {@link Action} randomevent.
47 *
48 * @param mob {@link #mob}.
49 * @param position {@link #position}.
50 * @param instant {@link #instant}.
51 */
52 public HarvestingSkillAction(Mob mob, Optional<Position> position, boolean instant) {
53 super(mob, position, instant);
54 }
55
56 /**
57 * Creates a new {@link Action} randomevent.
58 *
59 * @param mob {@link #mob}.
60 * @param position {@link #position}.
61 * @param delay {@link #delay}.
62 * @param instant {@link #instant}.
63 */
64 public HarvestingSkillAction(Mob mob, Optional<Position> position, int delay, boolean instant) {
65 super(mob, position, delay, instant);
66 }
67
68 /**
69 * The method executed upon harvest of the items.
70 *
71 * @param items the items being harvested.
72 * @param success determines if the harvest was successful or not.
73 */
74 public void onHarvest(Item[] items, boolean success) {
75
76 }
77
78 /**
79 * If mob will get a double onReward.
80 *
81 * @return If mob will get a double onReward.
82 */
84 return DoubleReward.NONE;
85 }
86
87 /**
88 * The success factor for the harvest. The higher the number means the more
89 * frequently harvest will be obtained.
90 *
91 * @return the success factor.
92 */
93 public abstract double successFactor();
94
95 /**
96 * The items to be removed upon a successful harvest.
97 *
98 * @return the items to be removed.
99 */
100 public abstract Optional<Item[]> removeItems();
101
102 /**
103 * The items to be harvested upon a successful harvest.
104 *
105 * @return the items to be harvested.
106 */
107 public abstract Item[] harvestItems();
108
109 @Override
110 public final boolean canRun() {
111 if (getMob().isPlayer()) {
112 Player player = getMob().getPlayer();
113 if (getMob().isPlayer() && getMob().getPlayer().inventory.remaining() < 1) {
114 getMob().getPlayer().send(new SendMessage("You do not have any space left in your inventory."));
115 return false;
116 }
117 if (removeItems().isPresent()) {
118 Item[] remove = removeItems().get();
119
120 if (!player.inventory.containsAll(remove)) {
121 for (Item item : remove) {
122 if (!player.inventory.contains(item)) {
123 player.send(new SendMessage("You don't have " + StringUtils.appendIndefiniteArticle(item.getName() + ".")));
124 break;
125 }
126 }
127 return false;
128 }
129 }
130 }
131 return true;
132 }
133
134 @Override
135 public void onExecute() {
136 Preconditions.checkState(SUCCESS_FACTOR >= 0 && SUCCESS_FACTOR <= 99, "Invalid success factor for harvesting!");
137 int factor = (getMob().skills.getSkills()[skill()].getLevel() / SUCCESS_FACTOR);
138 double boost = (factor * 0.01);
139 if (random.success(successFactor() + boost)) {
140 Optional<Item[]> removeItems = removeItems();
142
143 if (getMob().isPlayer()) {
144 removeItems.ifPresent(getMob().getPlayer().inventory::removeAll);
145 if (harvestItems != null)
146 getMob().getPlayer().inventory.addAll(harvestItems);
147 if (experience() != -1)
148 getMob().skills.addExperience(skill(), experience());
149 onHarvest(harvestItems, true);
150
151 if (doubleReward() == DoubleReward.ITEM) {
152 getMob().getPlayer().inventory.addAll(harvestItems);
153 } else if (doubleReward() == DoubleReward.EXPERIENCE) {
154 getMob().skills.addExperience(skill(), experience());
155 } else if (doubleReward() == DoubleReward.ALL) {
156 getMob().getPlayer().inventory.addAll(harvestItems);
157 getMob().skills.addExperience(skill(), experience());
158 }
159 }
160 } else {
161 onHarvest(null, false);
162 }
163 }
164
165 /**
166 * Determines if this action is prioritized.
167 * <p>When making an action prioritized, the next action will be ignored
168 * if not queued.</p>
169 *
170 * @return {@code true} if this action is prioritized, {@code false} otherwise.
171 */
172 @Override
173 public boolean prioritized() {
174 return false;
175 }
176
177 /**
178 * Gets the WalkablePolicy of this action.
179 *
180 * @return The walkable policy of this action.
181 */
182 @Override
186}
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.
boolean prioritized()
Determines if this action is prioritized.
static final int SUCCESS_FACTOR
The factor boost that determines the success rate for harvesting based on skill level.
WalkablePolicy getWalkablePolicy()
Gets the WalkablePolicy of this action.
abstract Item[] harvestItems()
The items to be harvested upon a successful harvest.
final boolean canRun()
Determines if the task can be ran.
DoubleReward doubleReward()
If mob will get a double onReward.
final RandomGen random
The random generator instance that will generate random numbers.
abstract Optional< Item[]> removeItems()
The items to be removed upon a successful harvest.
void onExecute()
The method which is called on intervals of the specified #delay;.
HarvestingSkillAction(Mob mob, Optional< Position > position, boolean instant)
Creates a new Action randomevent.
void onHarvest(Item[] items, boolean success)
The method executed upon harvest of the items.
abstract double successFactor()
The success factor for the harvest.
HarvestingSkillAction(Mob mob, Optional< Position > position, int delay, boolean instant)
Creates a new Action randomevent.
T getMob()
Gets the player.
Definition Action.java:44
final T mob
The Mob associated with this ActionEvent.
Definition Action.java:15
final boolean instant
If execution happens instantly upon being scheduled.
Definition Task.java:14
int delay
The cyclic delay.
Definition Task.java:17
Handles the mob class.
Definition Mob.java:66
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
boolean contains(int id)
Determines if this container contains id.
final boolean containsAll(int... identifiers)
Determines if this container contains all identifiers.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The ThreadLocalRandom wrapper that provides additional functionality for generating pseudo-random num...
static String appendIndefiniteArticle(String thing)
Appends the determined indefinite article to thing.
A queue policy determines whether the action can occur while walking.
NON_WALKABLE
This indicates actions cannot occur while walking.