RuneHive-Game
Loading...
Searching...
No Matches
HarvestablePatch.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl.farming.patches;
2
3import com.google.gson.JsonObject;
4import com.runehive.Config;
5import com.runehive.content.skillcape.SkillCape;
6import com.runehive.content.skill.impl.farming.FarmingConstants;
7import com.runehive.content.skill.impl.farming.zones.FarmingZone;
8import com.runehive.game.action.Action;
9import com.runehive.game.action.policy.WalkablePolicy;
10import com.runehive.game.world.Interactable;
11import com.runehive.game.world.entity.mob.player.Player;
12import com.runehive.game.world.entity.skill.Skill;
13import com.runehive.util.RandomUtils;
14
15/**
16 * A {@link WaterablePatch} that can harvest produce.
17 *
18 * @author Michael | Chex
19 */
20public abstract class HarvestablePatch extends FarmingPatch {
21
22 /** The harvest amount. */
23 private int harvest;
24
25 /**
26 * Constructs a new {@link HarvestablePatch} object.
27 *
28 * @param player the player that owns this patch
29 * @param zone the zone this patch belongs in
30 * @param boundaries the boundaries of this patch
31 */
35
36 /**
37 * Harvests produce in this patch if the requirements are met.
38 *
39 * @param min the minimum amount of produce to harvest
40 * @param max the maximum amount of produce to harvest
41 * @return {@code true} if this patch has successfully begun to harvest
42 * produce
43 */
44 private boolean harvest(int min, int max) {
45 if (plant == null) {
46 return false;
47 }
48
49 if (!player.inventory.contains(FarmingConstants.SECATEURS) && !player.inventory.contains(FarmingConstants.MAGIC_SECATEURS) &&
50 !player.equipment.contains(FarmingConstants.MAGIC_SECATEURS)) {
51 player.dialogueFactory.sendStatement("You need secateurs to harvest here.").execute();
52 return true;
53 }
54
55 min = min * compost.produceIncrease() / 100;
56 player.action.execute(createHarvestAction(player, min, max));
57 return true;
58 }
59
60 /**
61 * Creates the harvesting action.
62 *
63 * @param player the player harvesting produce
64 * @param min the minimum amount of produce to harvest
65 * @param max the maximum amount of produce to harvest
66 * @return a new {@link Action} specifically designed to harvest produce
67 */
68 private Action<Player> createHarvestAction(Player player, int min, int max) {
69 return new Action<Player>(player, 2) {
70
71 @Override
72 protected boolean canSchedule() {
73 return !player.locking.locked() && player.inventory.getFreeSlots() > 0;
74 }
75
76 @Override
77 protected void onSchedule() {
78 player.locking.lock(2);
79 player.animate(animation());
80 player.message("You begin to harvest the crop for " + plant.getProductType() + ".");
81 }
82
83 @Override
84 public void execute() {
85 if (player.inventory.getFreeSlots() <= 0) {
86 cancel();
87 return;
88 }
89
90 if (harvest == 0) {
91 harvest = RandomUtils.inclusive(min, max);
92
93 if (player.equipment.contains(FarmingConstants.MAGIC_SECATEURS)) {
94 harvest = harvest * 13 / 12;
95 }
96
98 harvest += harvest * 13 / 12;
99 }
100
101 harvest++;
102 }
103
104 if (harvest == 1) {
105 resetPatch();
106 cancel();
107 return;
108 }
109
110 harvest--;
111 if (harvest % 2 == 0)
112 player.animate(animation());
113 player.inventory.add(plant.getHarvestId(), 1);
114 player.skills.addExperience(Skill.FARMING, plant.getHarvestXp() * Config.FARMING_MODIFICATION);
115 }
116
117 @Override
118 public void onCancel(boolean logout) {
119 player.resetAnimation();
120 zone.sendPatchConfigs(player);
121 }
122
123 @Override
124 public WalkablePolicy getWalkablePolicy() {
126 }
127
128 @Override
129 public String getName() {
130 return "Farming Harvest";
131 }
132 };
133 }
134
135 /** @return the minimum amount of produce to harvest */
136 protected abstract int getMinAmount();
137
138 /** @return the maximum amount of produce to harvest */
139 protected abstract int getMaxAmount();
140
141 /** @return the harvesting animation */
142 protected abstract int animation();
143
144 @Override
145 public boolean clickObject(int opcode) {
146 if (opcode == 1 && !isDead() && fullyGrown) {
147 return harvest(getMinAmount(), getMaxAmount());
148 }
149 return super.clickObject(opcode);
150 }
151
152 @Override
153 protected void resetPatch() {
154 super.resetPatch();
155 harvest = 0;
156 }
157
158 @Override
159 public JsonObject toJson() {
160 JsonObject object = super.toJson();
161 object.addProperty("harvest", harvest);
162 return object;
163 }
164
165 @Override
166 public void fromJson(JsonObject object) {
167 harvest = object.get("harvest").getAsInt();
168 super.fromJson(object);
169 }
170
171}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final double FARMING_MODIFICATION
The experience modification for farming.
Definition Config.java:295
FarmingPatch(Player player, FarmingZone zone, Interactable[] boundaries)
HarvestablePatch(Player player, FarmingZone zone, Interactable[] boundaries)
Constructs a new HarvestablePatch object.
boolean harvest(int min, int max)
Harvests produce in this patch if the requirements are met.
Action< Player > createHarvestAction(Player player, int min, int max)
Creates the harvesting action.
Represents an action an entity can execute.
Definition Action.java:12
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 FARMING
The farming skill id.
Definition Skill.java:78
A static-util class that provides additional functionality for generating pseudo-random numbers.
static int inclusive(int min, int max)
Returns a pseudo-random int value between inclusive min and inclusive max.
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.
An object implementing Interactable has uses.