RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
GroundItem.java
1package com.osroyale.game.world.items.ground;
2
3import com.osroyale.game.event.impl.PickupItemEvent;
4import com.osroyale.game.event.impl.log.DropItemLogEvent;
5import com.osroyale.game.plugin.PluginManager;
6import com.osroyale.game.world.World;
7import com.osroyale.game.world.entity.Entity;
8import com.osroyale.game.world.entity.EntityType;
9import com.osroyale.game.world.entity.mob.player.Player;
10import com.osroyale.game.world.entity.mob.player.PlayerRight;
11import com.osroyale.game.world.items.Item;
12import com.osroyale.game.world.position.Position;
13import com.osroyale.game.world.region.Region;
14import com.osroyale.net.packet.out.SendGroundItem;
15import com.osroyale.net.packet.out.SendMessage;
16import com.osroyale.net.packet.out.SendRemoveGroundItem;
17
18import java.util.Objects;
19
64
65public final class GroundItem extends Entity {
66
68 public final Item item;
69
71 public final Player player;
72
74 public final GroundItemEvent event = new GroundItemEvent(this);
75
78
79 public boolean canIronMenPickThisItemUp = true;
80
82 public static void createGlobal(Player player, Item item) {
83 GroundItem groundItem = new GroundItem(player, item, player.getPosition());
84 groundItem.policy = GroundItemPolicy.GLOBAL;
85 groundItem.instance = player.instance;
86 groundItem.register();
87 World.getDataBus().publish(new DropItemLogEvent(player, groundItem));
88 }
89
91 public static void createGlobal(Player player, Item item, Position position) {
92 GroundItem groundItem = new GroundItem(player, item, position);
93 groundItem.policy = GroundItemPolicy.GLOBAL;
94 groundItem.instance = player.instance;
95 groundItem.register();
96 }
97
99 public static GroundItem create(Player player, Item item) {
100 GroundItem groundItem = new GroundItem(player, item, player.getPosition());
101 groundItem.policy = GroundItemPolicy.ONLY_OWNER;
102 groundItem.instance = player.instance;
103 groundItem.register();
104 World.getDataBus().publish(new DropItemLogEvent(player, groundItem));
105 return groundItem;
106 }
107
109 public static GroundItem create(Player player, Item item, Position position) {
110 GroundItem groundItem = new GroundItem(player, item, position);
111 groundItem.policy = GroundItemPolicy.ONLY_OWNER;
112 groundItem.instance = player.instance;
113 groundItem.register();
114 return groundItem;
115 }
116
118 private GroundItem(Player player, Item item, Position position) {
119 super(position);
120 this.item = item;
121 this.player = player;
122 this.instance = player.instance;
123 }
124
125 public boolean canSee(Player other) {
126 if (item.isTradeable() && policy.equals(GroundItemPolicy.GLOBAL) && instance == other.instance) {
127 return true;
128 }
129 return player.usernameLong == other.usernameLong;
130 }
131
133 public static void pickup(Player player, Item item, Position position) {
134 GroundItem result = position.getRegion().getGroundItem(item.getId(), position);
135
136 if (result == null) {
137 return;
138 }
139
140 if (PlayerRight.isIronman(player) && (!result.canIronMenPickThisItemUp || result.player.usernameLong != player.usernameLong)) {
141 player.send(new SendMessage("As an iron man you may not pick up this item."));
142 return;
143 }
144
145 if (!player.inventory.hasCapacityFor(item)) {
146 player.send(new SendMessage("You don't have enough inventory space."));
147 return;
148 }
149
150 result.event.cancel();
151 player.inventory.add(result.item);
152 PluginManager.getDataBus().publish(player, new PickupItemEvent(result));
153 }
154
155 @Override
156 public void register() {
157 if (isRegistered()) {
158 return;
159 }
160
161 setRegistered(true);
162 setPosition(getPosition());
163 }
164
165 @Override
166 public void unregister() {
167 if (!isRegistered()) {
168 return;
169 }
170
171 setRegistered(false);
172 removeFromRegion(getRegion());
173 }
174
175 @Override
176 public void addToRegion(Region region) {
177 if (item.getId() == 11283) {
178 item.setId(11284);
179 player.dragonfireCharges = 0;
180 }
181 /* System.out.println("item id: "+item.getId());
182 System.out.println("item amt: "+item.getAmount());*/
183 GroundItem groundItem = region.getGroundItem(item.getId(), getPosition());
184 if (groundItem != null && groundItem.item.isStackable()
185 && policy.equals(groundItem.policy) && (policy == GroundItemPolicy.GLOBAL || player.usernameLong == groundItem.player.usernameLong)) {
186 groundItem.event.cancel();
187 item.incrementAmountBy(groundItem.item.getAmount());
188 }
189 Region[] regions = World.getRegions().getSurroundingRegions(getPosition());
190 for (Region reg : regions) {
191 reg.getPlayers(getHeight()).forEach(player -> {
192 if (canSee(player)) {
193 player.send(new SendGroundItem(this));
194 }
195 });
196 }
197
198 region.addGroundItem(this);
200 }
201
202 @Override
203 public void removeFromRegion(Region region) {
204 Region[] regions = World.getRegions().getSurroundingRegions(getPosition());
205 for (Region reg : regions) {
206 reg.getPlayers(getHeight()).forEach(player -> {
207 if (canSee(player)) {
208 player.send(new SendRemoveGroundItem(this));
209 }
210 });
211 }
212 region.removeGroundItem(this);
213 }
214
215 @Override
216 public String getName() {
217 return item.getName();
218 }
219
220 @Override
222 return EntityType.GROUND_ITEM;
223 }
224
225 @Override
226 public boolean equals(Object obj) {
227 if (obj == this) return true;
228 if (obj instanceof GroundItem) {
229 GroundItem other = (GroundItem) obj;
230 return other.getPosition().equals(getPosition())
231 && other.item.equals(item)
232 && player.usernameLong == other.player.usernameLong;
233 }
234 return false;
235 }
236
237 @Override
238 public int hashCode() {
239 return Objects.hash(getPosition(), getIndex());
240 }
241
242 @Override
243 public String toString() {
244 return "GroundItem[owner=" + player.getUsername() + ", position=" + getPosition() + ", index=" + getIndex() + ", item=" + item.getName() + "]";
245 }
246}
synchronized final void cancel()
Definition Task.java:147
static void schedule(Task task)
Definition World.java:284
static void pickup(Player player, Item item, Position position)
static GroundItem create(Player player, Item item, Position position)
static void createGlobal(Player player, Item item)
static void createGlobal(Player player, Item item, Position position)
static GroundItem create(Player player, Item item)
void addGroundItem(GroundItem item)
Definition Region.java:187
void removeGroundItem(GroundItem item)
Definition Region.java:192
Region[] getSurroundingRegions(Position position)