RuneHive-Game
Loading...
Searching...
No Matches
GroundItem.java
Go to the documentation of this file.
1package com.runehive.game.world.items.ground;
2
3import com.runehive.game.event.impl.PickupItemEvent;
4import com.runehive.game.event.impl.log.DropItemLogEvent;
5import com.runehive.game.plugin.PluginManager;
6import com.runehive.game.world.World;
7import com.runehive.game.world.entity.Entity;
8import com.runehive.game.world.entity.EntityType;
9import com.runehive.game.world.entity.mob.player.Player;
10import com.runehive.game.world.entity.mob.player.PlayerRight;
11import com.runehive.game.world.items.Item;
12import com.runehive.game.world.position.Position;
13import com.runehive.game.world.region.Region;
14import com.runehive.net.packet.out.SendGroundItem;
15import com.runehive.net.packet.out.SendMessage;
16import com.runehive.net.packet.out.SendRemoveGroundItem;
17
18import java.util.Objects;
19
20/**
21 * Represents a single Ground item on the world map.
22 *
23 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand Up</a>
24 * @author Daniel
25 * @author Michael | Chex
26 * @since 27-12-2016.
27 */
28public final class GroundItem extends Entity {
29
30 /** The item that represents this ground item. */
31 public final Item item;
32
33 /** The optional player whom owns this item. */
34 public final Player player;
35
36 /** The randomevent for ground item. */
37 public final GroundItemEvent event = new GroundItemEvent(this);
38
39 /** The policy for this ground item. */
41
42 public boolean canIronMenPickThisItemUp = true;
43
44 /** Creates a new {@link GroundItem} object for a {@code player} and an {@code item}. */
45 public static void createGlobal(Player player, Item item) {
46 GroundItem groundItem = new GroundItem(player, item, player.getPosition());
47 groundItem.policy = GroundItemPolicy.GLOBAL;
48 groundItem.instance = player.instance;
49 groundItem.register();
51 }
52
53 /** Creates a new {@link GroundItem} object for a {@code player} and an {@code item}. */
55 GroundItem groundItem = new GroundItem(player, item, position);
56 groundItem.policy = GroundItemPolicy.GLOBAL;
57 groundItem.instance = player.instance;
58 groundItem.register();
59 }
60
61 /** Creates a new {@link GroundItem} object for a {@code player} and an {@code item}. */
63 GroundItem groundItem = new GroundItem(player, item, player.getPosition());
64 groundItem.policy = GroundItemPolicy.ONLY_OWNER;
65 groundItem.instance = player.instance;
66 groundItem.register();
68 return groundItem;
69 }
70
71 /** Creates a new {@link GroundItem} object for a {@code player}, an {@code item} and {@code position}. */
73 GroundItem groundItem = new GroundItem(player, item, position);
74 groundItem.policy = GroundItemPolicy.ONLY_OWNER;
75 groundItem.instance = player.instance;
76 groundItem.register();
77 return groundItem;
78 }
79
80 /** Constructs a new {@code GroundItem} object for a {@code player}, an {@code item}, and a {@code position}. */
82 super(position);
83 this.item = item;
84 this.player = player;
85 this.instance = player.instance;
86 }
87
88 public boolean canSee(Player other) {
89 if (item.isTradeable() && policy.equals(GroundItemPolicy.GLOBAL) && instance == other.instance) {
90 return true;
91 }
92 return player.usernameLong == other.usernameLong;
93 }
94
95 /** Attempts to pick the specified {@code item} up. */
96 public static void pickup(Player player, Item item, Position position) {
97 GroundItem result = position.getRegion().getGroundItem(item.getId(), position);
98
99 if (result == null) {
100 return;
101 }
102
103 if (PlayerRight.isIronman(player) && (!result.canIronMenPickThisItemUp || result.player.usernameLong != player.usernameLong)) {
104 player.send(new SendMessage("As an iron man you may not pick up this item."));
105 return;
106 }
107
108 if (!player.inventory.hasCapacityFor(item)) {
109 player.send(new SendMessage("You don't have enough inventory space."));
110 return;
111 }
112
113 result.event.cancel();
114 player.inventory.add(result.item);
116 }
117
118 @Override
119 public void register() {
120 if (isRegistered()) {
121 return;
122 }
123
124 setRegistered(true);
126 }
127
128 @Override
129 public void unregister() {
130 if (!isRegistered()) {
131 return;
132 }
133
134 setRegistered(false);
136 }
137
138 @Override
139 public void addToRegion(Region region) {
140 if (item.getId() == 11283) {
141 item.setId(11284);
142 player.dragonfireCharges = 0;
143 }
144 /* System.out.println("item id: "+item.getId());
145 System.out.println("item amt: "+item.getAmount());*/
146 GroundItem groundItem = region.getGroundItem(item.getId(), getPosition());
147 if (groundItem != null && groundItem.item.isStackable()
148 && policy.equals(groundItem.policy) && (policy == GroundItemPolicy.GLOBAL || player.usernameLong == groundItem.player.usernameLong)) {
149 groundItem.event.cancel();
150 item.incrementAmountBy(groundItem.item.getAmount());
151 }
153 for (Region reg : regions) {
154 reg.getPlayers(getHeight()).forEach(player -> {
155 if (canSee(player)) {
156 player.send(new SendGroundItem(this));
157 }
158 });
159 }
160
161 region.addGroundItem(this);
163 }
164
165 @Override
168 for (Region reg : regions) {
169 reg.getPlayers(getHeight()).forEach(player -> {
170 if (canSee(player)) {
171 player.send(new SendRemoveGroundItem(this));
172 }
173 });
174 }
175 region.removeGroundItem(this);
176 }
177
178 @Override
179 public String getName() {
180 return item.getName();
181 }
182
183 @Override
185 return EntityType.GROUND_ITEM;
186 }
187
188 @Override
189 public boolean equals(Object obj) {
190 if (obj == this) return true;
191 if (obj instanceof GroundItem) {
192 GroundItem other = (GroundItem) obj;
193 return other.getPosition().equals(getPosition())
194 && other.item.equals(item)
195 && player.usernameLong == other.player.usernameLong;
196 }
197 return false;
198 }
199
200 @Override
201 public int hashCode() {
202 return Objects.hash(getPosition(), getIndex());
203 }
204
205 @Override
206 public String toString() {
207 return "GroundItem[owner=" + player.getUsername() + ", position=" + getPosition() + ", index=" + getIndex() + ", item=" + item.getName() + "]";
208 }
209}
void publish(Event event)
Sends an Event to all subscribed listeners.
Definition DataBus.java:92
boolean publish(Player player, Event event)
This class handles how plugins are loaded/unloaded and accessed.
synchronized final void cancel()
Cancels all subsequent executions.
Definition Task.java:113
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
static DataBus getDataBus()
Definition World.java:568
static RegionManager getRegions()
Definition World.java:552
void setRegistered(boolean registered)
Definition Entity.java:98
void setPosition(Position position)
Definition Entity.java:106
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
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
boolean equals(Object obj)
Definition Item.java:475
EntityType getType()
Gets the EntityType.
GroundItemPolicy policy
The policy for this ground item.
final GroundItemEvent event
The randomevent for ground item.
static GroundItem create(Player player, Item item)
Creates a new GroundItem object for a player and an item.
static void createGlobal(Player player, Item item)
Creates a new GroundItem object for a player and an item.
void addToRegion(Region region)
Adds this entity to the specified region.
String getName()
Gets the name of this entity.
static void pickup(Player player, Item item, Position position)
Attempts to pick the specified item up.
final Player player
The optional player whom owns this item.
void unregister()
Unregisters an entity from the World.
static void createGlobal(Player player, Item item, Position position)
Creates a new GroundItem object for a player and an item.
GroundItem(Player player, Item item, Position position)
Constructs a new GroundItem object for a player, an item, and a position.
static GroundItem create(Player player, Item item, Position position)
Creates a new GroundItem object for a player, an item and position.
void removeFromRegion(Region region)
Removes this entity from the specified region.
void register()
Registers an entity to the World.
final Item item
The item that represents this ground item.
Represents a single tile on the game world.
Definition Position.java:14
Represents a single region.
Definition Region.java:21
Region[] getSurroundingRegions(Position position)
Gets the regions surrounding a position.
The OutgoingPacket that sends a message to a Players chatbox in the client.
static boolean isIronman(Player player)
Checks if the player is an ironman.