RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
TradeSession.java
1package com.osroyale.game.world.entity.mob.player.exchange.trade;
2
3import com.osroyale.game.event.impl.log.TradeLogEvent;
4import com.osroyale.game.world.InterfaceConstants;
5import com.osroyale.game.world.World;
6import com.osroyale.game.world.entity.mob.player.Player;
7import com.osroyale.game.world.entity.mob.player.PlayerRight;
8import com.osroyale.game.world.entity.mob.player.exchange.ExchangeCompletionType;
9import com.osroyale.game.world.entity.mob.player.exchange.ExchangeSession;
10import com.osroyale.game.world.entity.mob.player.exchange.ExchangeSessionType;
11import com.osroyale.game.world.items.Item;
12import com.osroyale.game.world.items.containers.pricechecker.PriceType;
13import com.osroyale.net.packet.out.SendItemOnInterface;
14import com.osroyale.net.packet.out.SendMessage;
15import com.osroyale.net.packet.out.SendMinimapState;
16import com.osroyale.net.packet.out.SendString;
17import com.osroyale.util.MessageColor;
18import com.osroyale.util.Stopwatch;
19import com.osroyale.util.Utility;
20
59
60public final class TradeSession extends ExchangeSession {
61
66 private final Stopwatch lastOfferModification = Stopwatch.start();
67
77
78 @Override
79 public boolean onRequest() {
80 if(player.playTime < 6000) {
81 player.send(new SendMessage("You must have over one hour of play time to trade."));
82 return false;
83 }
84 if(player.getUsername().contains("nicholas") || player.getUsername().contains("muntuna")) {
85 player.send(new SendMessage("You cannot trade as a " +player.getPlayer().right));
86 return false;
87 }
88 this.player.face(other.getPosition());
89 this.player.exchangeSession.requested_players.add(other);
90 if (!other.exchangeSession.requested_players.contains(player)) {
91 player.send(new SendMessage("Sending trade request..."));
92 other.send(new SendMessage(player.getName() + ":tradereq:", MessageColor.PURPLE));
93 return false;
94 }
95 SESSIONS.add(this);
96 this.forEach(p -> {
97 p.exchangeSession.resetRequests();
99 p.attributes.set("TRADE_KEY", true);
100 });
101 updateMainComponents("FIRST_SCREEN");
102 return true;
103 }
104
105 @Override
106 public boolean canAddItem(Player player, Item item, int slot) {
107 return true;
108 }
109
110 @Override
111 public boolean canRemoveItem(Player player, Item item, int slot) {
112 return true;
113 }
114
115 @Override
116 public boolean onButtonClick(Player p, int button) {
117 switch (button) {
118
119 case -32513:
120 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.FIRST_TRADE_SCREEN)) {
121 return false;
122 }
123
124 depositeAll(p);
125 return true;
126
127 case -32510:
128 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.FIRST_TRADE_SCREEN)) {
129 return false;
130 }
131
132 withdrawAll(p);
133 return true;
134
135 case -32524:
136 case -32530:
137 case -32324:
138 case -32332:
139 p.exchangeSession.reset(ExchangeSessionType.TRADE);
140 return true;
141
142 case -32527:
143 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.FIRST_TRADE_SCREEN)) {
144 return false;
145 }
146
147 accept(p, "OFFER_ITEMS");
148 return true;
149
150 case -32327:
151 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.SECOND_TRADE_SCREEN)) {
152 return false;
153 }
154
155 accept(p, "CONFIRM_DECISION");
156 return true;
157
158 }
159 return false;
160 }
161
162 @Override
163 public void accept(Player player, String component) {
165 switch (component) {
166 case "OFFER_ITEMS":
167 if (!lastOfferModification.elapsed(1_000)) {
168 player.send(new SendString("@red@Trade has been modified!", 33030));
169 return;
170 }
171 if (!player.inventory.hasCapacityFor(item_containers.get(other).toArray())) {
172 player.send(new SendMessage("You don't have enough free slots for this many items.", MessageColor.RED));
173 break;
174 }
175
176 if (!other.inventory.hasCapacityFor(item_containers.get(player).toArray())) {
177 String username = other.getName();
178 player.send(new SendMessage(username + " doesn't have enough free slots for this many items", MessageColor.RED));
179 break;
180 }
181 if (hasAttachment() && getAttachment() != player) {
182 this.setAttachment(null);
183 updateMainComponents("SECOND_SCREEN");
184 return;
185 }
186 setAttachment(player);
187 player.send(new SendString("Waiting for other player...", 33029));
188 other.send(new SendString("Other player has accepted", 33029));
189 break;
190 case "CONFIRM_DECISION":
191 if (hasAttachment() && getAttachment() != player) {
192 this.setAttachment(null);
193 accept(player, "FINALIZE");
194 return;
195 }
197 other.send(new SendString("Other player has accepted.", 33202));
198 player.send(new SendString("Waiting for other player...", 33202));
199 break;
200
201 case "FINALIZE":
202 if (other.isRegistered() && player.isRegistered()) {
203
204 final Item[] playerItems = this.item_containers.get(player).toArray();
205 final Item[] otherItems = this.item_containers.get(other).toArray();
206
207 player.inventory.addAll(otherItems);
208 other.inventory.addAll(playerItems);
209
210 World.getDataBus().publish(new TradeLogEvent(player, playerItems, other, otherItems));
211
212 forEach(p -> p.send(new SendMessage("Trade successfully completed with " + this.getOther(p).getName(), MessageColor.RED)));
214 }
215 break;
216
217 }
218 }
219
220 @Override
221 public void updateMainComponents(String component) {
222 switch (component) {
223 case "FIRST_SCREEN":
225 break;
226 case "SECOND_SCREEN":
227 forEach(p -> {
228 Player recipient = p.getName().equals(player.getName()) ? this.other : this.player;
229
230 p.send(new SendString("<col=65535>Are you sure you want to make this trade?", 33202));
231 p.send(new SendItemOnInterface(InterfaceConstants.INVENTORY_INTERFACE, p.inventory.toArray()));
232
233 p.send(new SendString(getItemNames(p, this.item_containers.get(p).toArray()), 33221));
234 p.send(new SendString(getItemNames(recipient, this.item_containers.get(recipient).toArray()), 33251));
235 p.send(new SendString("<col=65535>Trading With:", 33207));
236 p.send(new SendString(String.format("%s <col=65535>%s", PlayerRight.getCrown(recipient), Utility.formatName(recipient.getName())), 33208));
237 p.interfaceManager.openInventory(InterfaceConstants.SECOND_TRADE_SCREEN, 3213);
238 });
239 break;
240 }
241 }
242
243 @Override
244 public void updateOfferComponents() {
245 this.lastOfferModification.reset();
246 this.setAttachment(null);
247 forEach(p -> {
248 Player recipient = p.getName().equals(player.getName()) ? this.other : this.player;
249 int remaining = recipient.inventory.remaining();
250
252 this.item_containers.get(p).refresh(recipient, InterfaceConstants.OTHER_TRADE_CONTAINER);
253
254 p.send(new SendItemOnInterface(3322, p.inventory.toArray()));
255 p.send(new SendString(String.format("Trading with: %s %s", PlayerRight.getCrown(recipient), Utility.formatName(recipient.getName())), 33002));
256 p.send(new SendString(Utility.formatName(recipient.getName()), 33003));
257 p.send(new SendString("has " + remaining + " free", 33004));
258 p.send(new SendString("inventory spaces", 33005));
259
260 // p = current player being looped
261 // recipient = the other player (not the player being looped)
262 // don't use player and other object because those are the cached values.
263// long difference = this.item_containers.get(p).containerValue(PriceType.VALUE) - this.item_containers.get(recipient).containerValue(PriceType.VALUE);
264// System.out.println("difference = " + difference);FIXME
265// p.send(new SendString(difference == 0 ? "<col=ffffff> Absolutely nothing!" : "<col=ffffff> " + difference, 33018));
266
267 p.send(new SendString(this.item_containers.get(p).containerValue(PriceType.VALUE) == 0 ? "Nothing." : Utility.formatDigits(this.item_containers.get(p).containerValue(PriceType.VALUE)) + " gp", 33019));
268 p.send(new SendString(this.item_containers.get(recipient).containerValue(PriceType.VALUE) == 0 ? "Nothing." : Utility.formatDigits(this.item_containers.get(recipient).containerValue(PriceType.VALUE)) + " gp", 33020));
269
270 p.send(new SendString("", 33029));
271 p.send(new SendString("", 33030));
272
273 p.interfaceManager.openInventory(InterfaceConstants.FIRST_TRADE_SCREEN, 3321);
274 });
275 }
276
277 @Override
278 public void onReset() {
279 forEach(p -> {
280 p.attributes.set("TRADE_KEY", false);
281 p.interfaceManager.close();
282 p.resetFace();
284 });
285 }
286
291 private String getItemNames(Player player, Item[] items) {
292 String tradeItems = "Absolutely nothing!";
293 String tradeAmount;
294 int count = 0;
295 for (Item item : items) {
296 if (item == null || tradeItems.contains(item.getName())) {
297 continue;
298 }
299 int amount = this.item_containers.get(player).computeAmountForId(item.getId());
300 tradeAmount = item.isStackable() ? amount >= 1000 && amount < 1000000 ? "@cya@" + (amount / 1000) + "K @whi@" + "(" + amount + ")" : amount >= 1000000 ? "@gre@" + (amount / 1000000) + " " + "million @whi@(" + amount + ")" : "" + amount : "(x" + amount + ")";
301 tradeItems = count == 0 ? item.getName() : tradeItems + "\\n" + item.getName();
302 tradeItems = tradeItems + (item.isStackable() ? " x " : " ") + tradeAmount;
303 count++;
304 }
305 return tradeItems;
306 }
307}
ExchangeSession(Player player, Player other, ExchangeSessionType type)
static String formatDigits(final int amount)
Definition Utility.java:78