RuneHive-Game
Loading...
Searching...
No Matches
FarmingZone.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl.farming.zones;
2
3import com.google.gson.JsonArray;
4import com.google.gson.JsonElement;
5import com.google.gson.JsonObject;
6import com.runehive.content.skill.impl.farming.patches.FarmingPatch;
7import com.runehive.game.world.Interactable;
8import com.runehive.game.world.entity.mob.player.Player;
9import com.runehive.game.world.items.Item;
10import com.runehive.game.world.object.GameObject;
11import com.runehive.game.world.position.Position;
12import com.runehive.game.world.region.Region;
13import com.runehive.net.packet.out.SendConfig;
14import com.runehive.util.Utility;
15
16/**
17 * A zone that manages farming patches.
18 *
19 * @author Michael | Chex
20 */
21public class FarmingZone {
22
23 /** The config id of all the patches. */
24 private static final int PATCH_CONFIG = 529;
25
26 /** The boundary of this zone. */
27 private final Interactable boundary;
28
29 /** The patches in this zone. */
30 private final FarmingPatch[] patches;
31
32 /**
33 * Constructs a new farming zone.
34 *
35 * @param size the amount of patches in this zone
36 */
38 this.boundary = boundary;
39 patches = new FarmingPatch[size];
40 }
41
42 /**
43 * Sets a farming patch in this zone.
44 *
45 * @param index the index of the farming patch
46 * @param patch the patch to set
47 */
48 void setPatch(int index, FarmingPatch patch) {
49 patches[index] = patch;
50 }
51
52 /**
53 * Handles clicking a farming patch.
54 *
55 * @param object the object that was clicked
56 * @param opcode the click opcode (starting at 1)
57 * @return {@code true} if the object was a patch
58 */
59 public boolean clickObject(GameObject object, int opcode) {
60 boolean success = false;
61 for (FarmingPatch patch : patches) {
62 if (patch.within(object.getPosition())) {
63 success |= patch.clickObject(opcode);
64 }
65 }
66 return success;
67 }
68
69 /**
70 * Handles using an item on a farming patch.
71 *
72 * @param object the object an item was used on
73 * @param item the item used on the object
74 * @param slot the inventory index of the item
75 * @return {@code true} if the item used on the patch was a farming event
76 */
77 public boolean itemOnObject(GameObject object, Item item, int slot) {
78 boolean success = false;
79 for (FarmingPatch patch : patches) {
80 if (patch.within(object.getPosition())) {
81 success |= patch.itemOnObject(item, slot);
82 }
83 }
84 return success;
85 }
86
87 /**
88 * Sends the configs of this zone to the player's client.
89 *
90 * @param player the player to send the config to
91 */
92 public final void sendPatchConfigs(Player player) {
93 if (!isViewable(player.getPosition())) {
94 return;
95 }
96
97 int config = 0;
98 for (int index = 0; index < patches.length; index++) {
99 config |= patches[index].getConfig() << (index << 3);
100 }
101
102 player.send(new SendConfig(PATCH_CONFIG, config));
103 }
104
105 /** Ticks the farming patches in this zone. */
106 public final void tick() {
107 for (FarmingPatch patch : patches) {
108 patch.tick();
109 }
110 }
111
112 /**
113 * Checks if a position can see this farming zone.
114 *
115 * @param position the position to check
116 * @return {@code true} if the zone is viewable from the position
117 */
118 public boolean isViewable(Position position) {
119 return Utility.inside(boundary, position) || Utility.withinDistance(boundary, position, Region.SIZE);
120 }
121
122 public JsonArray toJson() {
123 JsonArray array = new JsonArray();
124 for (FarmingPatch patch : patches) {
125 array.add(patch.toJson());
126 }
127 return array;
128 }
129
130 public void fromJson(JsonArray array) {
131 int index = 0;
132 for (JsonElement element : array) {
133 patches[index++].fromJson((JsonObject) element);
134 }
135 }
136
137}
void setPatch(int index, FarmingPatch patch)
Sets a farming patch in this zone.
FarmingZone(Interactable boundary, int size)
Constructs a new farming zone.
final void tick()
Ticks the farming patches in this zone.
static final int PATCH_CONFIG
The config id of all the patches.
final FarmingPatch[] patches
The patches in this zone.
boolean itemOnObject(GameObject object, Item item, int slot)
Handles using an item on a farming patch.
boolean clickObject(GameObject object, int opcode)
Handles clicking a farming patch.
boolean isViewable(Position position)
Checks if a position can see this farming zone.
final void sendPatchConfigs(Player player)
Sends the configs of this zone to the player's client.
final Interactable boundary
The boundary of this zone.
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
Represents a single tile on the game world.
Definition Position.java:14
Represents a single region.
Definition Region.java:21
The OutgoingPacket responsible for changing settings on a client.
Handles miscellaneous methods.
Definition Utility.java:27
static boolean inside(Interactable source, Interactable target)
Definition Utility.java:783
static boolean withinDistance(Interactable source, Interactable target, int radius)
Definition Utility.java:470
An object implementing Interactable has uses.