From 948669cbffc6ece74d3b1895a9d3f4eb033a95e4 Mon Sep 17 00:00:00 2001 From: Alejandro <25-alejandro@users.noreply.git.krews.org> Date: Sun, 4 Aug 2019 18:11:11 +0300 Subject: [PATCH] Automatically convert credit redeemables on trade confirm --- .../eu/habbo/habbohotel/rooms/RoomTrade.java | 38 +++++++++++++++++++ .../outgoing/trading/TradeUpdateComposer.java | 12 +----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomTrade.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomTrade.java index ba56a8e1..0b17cbd7 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomTrade.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomTrade.java @@ -8,6 +8,7 @@ import com.eu.habbo.messages.outgoing.inventory.AddHabboItemComposer; import com.eu.habbo.messages.outgoing.inventory.InventoryRefreshComposer; import com.eu.habbo.messages.outgoing.rooms.users.RoomUserStatusComposer; import com.eu.habbo.messages.outgoing.trading.*; +import com.eu.habbo.threading.runnables.QueryDeleteHabboItem; import gnu.trove.set.hash.THashSet; import java.sql.*; @@ -214,6 +215,33 @@ public class RoomTrade { userOne.clearItems(); userTwo.clearItems(); + int creditsForUserTwo = 0; + THashSet creditFurniUserOne = new THashSet<>(); + for (HabboItem item : itemsUserOne) { + int worth = RoomTrade.getCreditsByItem(item); + if (worth > 0) { + creditsForUserTwo += worth; + creditFurniUserOne.add(item); + new QueryDeleteHabboItem(item); + } + } + itemsUserOne.removeAll(creditFurniUserOne); + + int creditsForUserOne = 0; + THashSet creditFurniUserTwo = new THashSet<>(); + for (HabboItem item : itemsUserTwo) { + int worth = RoomTrade.getCreditsByItem(item); + if (worth > 0) { + creditsForUserOne += worth; + creditFurniUserTwo.add(item); + new QueryDeleteHabboItem(item); + } + } + itemsUserTwo.removeAll(creditFurniUserTwo); + + userOne.getHabbo().giveCredits(creditsForUserOne); + userTwo.getHabbo().giveCredits(creditsForUserTwo); + userOne.getHabbo().getInventory().getItemsComponent().addItems(itemsUserTwo); userTwo.getHabbo().getInventory().getItemsComponent().addItems(itemsUserOne); @@ -287,4 +315,14 @@ public class RoomTrade { public List getRoomTradeUsers() { return this.users; } + + public static int getCreditsByItem(HabboItem item) { + if (!item.getBaseItem().getName().startsWith("CF_") && !item.getBaseItem().getName().startsWith("CFC_")) return 0; + + try { + return Integer.valueOf(item.getBaseItem().getName().split("_")[1]); + } catch (Exception e) { + return 0; + } + } } diff --git a/src/main/java/com/eu/habbo/messages/outgoing/trading/TradeUpdateComposer.java b/src/main/java/com/eu/habbo/messages/outgoing/trading/TradeUpdateComposer.java index 87f579d5..3f28f45c 100644 --- a/src/main/java/com/eu/habbo/messages/outgoing/trading/TradeUpdateComposer.java +++ b/src/main/java/com/eu/habbo/messages/outgoing/trading/TradeUpdateComposer.java @@ -39,18 +39,8 @@ public class TradeUpdateComposer extends MessageComposer { } this.response.appendInt(roomTradeUser.getItems().size()); - this.response.appendInt(roomTradeUser.getItems().stream().mapToInt(this::getCreditsByItem).sum()); + this.response.appendInt(roomTradeUser.getItems().stream().mapToInt(RoomTrade::getCreditsByItem).sum()); } return this.response; } - - private int getCreditsByItem(HabboItem item) { - if (!item.getBaseItem().getName().startsWith("CF_") && !item.getBaseItem().getName().startsWith("CFC_")) return 0; - - try { - return Integer.valueOf(item.getBaseItem().getName().split("_")[1]); - } catch (Exception e) { - return 0; - } - } }