From c3badba73ba6f758540ce7c2322bbca8cdcf8b26 Mon Sep 17 00:00:00 2001 From: P0nk Date: Thu, 30 Mar 2023 06:52:40 +0200 Subject: [PATCH] Convert ShopItem to record --- src/main/java/server/shop/Shop.java | 18 +++++++-------- src/main/java/server/shop/ShopItem.java | 30 ++----------------------- src/main/java/tools/PacketCreator.java | 14 ++++++------ 3 files changed, 18 insertions(+), 44 deletions(-) diff --git a/src/main/java/server/shop/Shop.java b/src/main/java/server/shop/Shop.java index b420d5c71c..622a16bce5 100644 --- a/src/main/java/server/shop/Shop.java +++ b/src/main/java/server/shop/Shop.java @@ -62,7 +62,7 @@ public class Shop { public void buy(Client c, short slot, int itemId, short quantity) { ShopItem item = findBySlot(slot); if (item != null) { - if (item.getItemId() != itemId) { + if (item.itemId() != itemId) { log.warn("Wrong slot number in shop {}", id); return; } @@ -70,18 +70,18 @@ public class Shop { return; } ItemInformationProvider ii = ItemInformationProvider.getInstance(); - if (item.getPrice() > 0) { - int amount = (int) Math.min((float) item.getPrice() * quantity, Integer.MAX_VALUE); + if (item.price() > 0) { + int amount = (int) Math.min((float) item.price() * quantity, Integer.MAX_VALUE); if (c.getPlayer().getMeso() >= amount) { if (InventoryManipulator.checkSpace(c, itemId, quantity, "")) { if (!ItemConstants.isRechargeable(itemId)) { //Pets can't be bought from shops InventoryManipulator.addById(c, itemId, quantity, "", -1); c.getPlayer().gainMeso(-amount, false); } else { - short slotMax = ii.getSlotMax(c, item.getItemId()); + short slotMax = ii.getSlotMax(c, item.itemId()); quantity = slotMax; InventoryManipulator.addById(c, itemId, quantity, "", -1); - c.getPlayer().gainMeso(-item.getPrice(), false); + c.getPlayer().gainMeso(-item.price(), false); } c.sendPacket(PacketCreator.shopTransaction((byte) 0)); } else { @@ -92,8 +92,8 @@ public class Shop { c.sendPacket(PacketCreator.shopTransaction((byte) 2)); } - } else if (item.getPitch() > 0) { - int amount = (int) Math.min((float) item.getPitch() * quantity, Integer.MAX_VALUE); + } else if (item.pitch() > 0) { + int amount = (int) Math.min((float) item.pitch() * quantity, Integer.MAX_VALUE); if (c.getPlayer().getInventory(InventoryType.ETC).countById(ItemId.PERFECT_PITCH) >= amount) { if (InventoryManipulator.checkSpace(c, itemId, quantity, "")) { @@ -101,7 +101,7 @@ public class Shop { InventoryManipulator.addById(c, itemId, quantity, "", -1); InventoryManipulator.removeById(c, InventoryType.ETC, ItemId.PERFECT_PITCH, amount, false, false); } else { - short slotMax = ii.getSlotMax(c, item.getItemId()); + short slotMax = ii.getSlotMax(c, item.itemId()); quantity = slotMax; InventoryManipulator.addById(c, itemId, quantity, "", -1); InventoryManipulator.removeById(c, InventoryType.ETC, ItemId.PERFECT_PITCH, amount, false, false); @@ -115,7 +115,7 @@ public class Shop { } else if (c.getPlayer().getInventory(InventoryType.CASH).countById(token) != 0) { int amount = c.getPlayer().getInventory(InventoryType.CASH).countById(token); int value = amount * tokenvalue; - int cost = item.getPrice() * quantity; + int cost = item.price() * quantity; if (c.getPlayer().getMeso() + value >= cost) { int cardreduce = value - cost; int diff = cardreduce + c.getPlayer().getMeso(); diff --git a/src/main/java/server/shop/ShopItem.java b/src/main/java/server/shop/ShopItem.java index 7649c0e871..06d595c980 100644 --- a/src/main/java/server/shop/ShopItem.java +++ b/src/main/java/server/shop/ShopItem.java @@ -23,33 +23,7 @@ package server.shop; /** * @author Matze + * @author Ponk */ -public class ShopItem { - private final short buyable; - private final int itemId; - private final int price; - private final int pitch; - - public ShopItem(short buyable, int itemId, int price, int pitch) { - this.buyable = buyable; - this.itemId = itemId; - this.price = price; - this.pitch = pitch; - } - - public short getBuyable() { - return buyable; - } - - public int getItemId() { - return itemId; - } - - public int getPrice() { - return price; - } - - public int getPitch() { - return pitch; - } +public record ShopItem(short buyable, int itemId, int price, int pitch) { } diff --git a/src/main/java/tools/PacketCreator.java b/src/main/java/tools/PacketCreator.java index 74d3d46591..cfa759a3e4 100644 --- a/src/main/java/tools/PacketCreator.java +++ b/src/main/java/tools/PacketCreator.java @@ -2370,19 +2370,19 @@ public class PacketCreator { p.writeInt(sid); p.writeShort(items.size()); // item count for (ShopItem item : items) { - p.writeInt(item.getItemId()); - p.writeInt(item.getPrice()); - p.writeInt(item.getPrice() == 0 ? item.getPitch() : 0); //Perfect Pitch + p.writeInt(item.itemId()); + p.writeInt(item.price()); + p.writeInt(item.price() == 0 ? item.pitch() : 0); //Perfect Pitch p.writeInt(0); //Can be used x minutes after purchase p.writeInt(0); //Hmm - if (!ItemConstants.isRechargeable(item.getItemId())) { + if (!ItemConstants.isRechargeable(item.itemId())) { p.writeShort(1); // stacksize o.o - p.writeShort(item.getBuyable()); + p.writeShort(item.buyable()); } else { p.writeShort(0); p.writeInt(0); - p.writeShort(doubleToShortBits(ii.getUnitPrice(item.getItemId()))); - p.writeShort(ii.getSlotMax(c, item.getItemId())); + p.writeShort(doubleToShortBits(ii.getUnitPrice(item.itemId()))); + p.writeShort(ii.getSlotMax(c, item.itemId())); } } return p;