diff --git a/Server/src/main/java/core/game/content/global/shop/Shop.kt b/Server/src/main/java/core/game/content/global/shop/Shop.kt index 3b3f30b6c..1ae9e865e 100644 --- a/Server/src/main/java/core/game/content/global/shop/Shop.kt +++ b/Server/src/main/java/core/game/content/global/shop/Shop.kt @@ -1,20 +1,34 @@ -package core.game.content.global.shop; +package core.game.content.global.shop -import api.ContentAPI; -import core.cache.def.impl.ItemDefinition; -import core.game.container.Container; -import core.game.container.ContainerType; -import org.rs09.consts.Items; -import core.game.node.entity.player.Player; -import core.game.node.entity.player.link.diary.DiaryType; -import core.game.node.item.Item; -import rs09.game.system.SystemLogger; -import rs09.game.system.config.ItemConfigParser; -import rs09.game.world.GameWorld; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import rs09.game.world.GameWorld.ticks +import api.ContentAPI.amountInInventory +import rs09.game.system.SystemLogger.logInfo +import core.game.node.entity.player.link.diary.DiaryType +import core.cache.def.impl.ItemDefinition +import core.game.container.Container +import core.game.container.ContainerType +import core.game.node.entity.player.Player +import core.game.node.item.Item +import org.rs09.consts.Items.BLACK_CHAINBODY_1107 +import org.rs09.consts.Items.BOWL_1923 +import org.rs09.consts.Items.BUCKET_1925 +import org.rs09.consts.Items.CAKE_TIN_1887 +import org.rs09.consts.Items.CANDLE_36 +import org.rs09.consts.Items.CHAOS_RUNE_562 +import org.rs09.consts.Items.CHISEL_1755 +import org.rs09.consts.Items.DEATH_RUNE_560 +import org.rs09.consts.Items.EMPTY_POT_1931 +import org.rs09.consts.Items.HAMMER_2347 +import org.rs09.consts.Items.JUG_1935 +import org.rs09.consts.Items.NEWCOMER_MAP_550 +import org.rs09.consts.Items.SECURITY_BOOK_9003 +import org.rs09.consts.Items.SHEARS_1735 +import org.rs09.consts.Items.TINDERBOX_590 +import org.rs09.consts.Items.TOKKUL_6529 +import rs09.game.system.config.ItemConfigParser +import java.lang.IndexOutOfBoundsException +import java.util.Arrays +import java.util.ArrayList /** * A class representing a shop. @@ -22,120 +36,140 @@ import java.util.List; * @author 'Vexia * @author Jamix77 */ -public class Shop { - - /** - * Represents the general store items. - */ - public final static Item[] GENERAL_STORE_ITEMS = new Item[]{ - new Item(Items.EMPTY_POT_1931,5), - new Item(Items.JUG_1935,5), - new Item(Items.SHEARS_1735,2), - new Item(Items.BUCKET_1925,3), - new Item(Items.BOWL_1923,2), - new Item(Items.CAKE_TIN_1887,2), - new Item(Items.TINDERBOX_590,2), - new Item(Items.CHISEL_1755,2), - new Item(Items.HAMMER_2347,5), - new Item(Items.NEWCOMER_MAP_550,5), - new Item(Items.SECURITY_BOOK_9003,5) - }; - /** - * Represents the coins item. - */ - private static final int COINS = 995; - /** - * Represents the tokkul item id. - */ - private static final int TOKKUL = 6529; - - /** - * Represents the archery ticket item id - */ - private static final int ARCHERY_TICKET = 1464; - - /** - * Represents the shop containers. - */ - private final Container[] containers = new Container[]{new Container(40, ContainerType.SHOP), new Container(40, ContainerType.SHOP)}; - /** - * Represents the list of shop viewers. - */ - private final List viewers = new ArrayList<>(20); - +open class Shop @JvmOverloads constructor( /** * Represents the title of the shop. */ - private final String title; - + val title: String, /** * Represents the items in the store. */ - private Item[] items; - + var items: Array, /** * Represents if it's a general store. */ - private final boolean general; - + val isGeneral: Boolean, /** * Represents the currency the shop allows. */ - private final int currency; - + val currency: Int = COINS, /** * If the shop buys for high alch. */ - private final boolean highAlch; - + val isHighAlch: Boolean = false +) { + /** + * Gets the containers. + * + * @return The containers. + */ + /** + * Represents the shop containers. + */ + val containers = arrayOf(Container(40, ContainerType.SHOP), Container(40, ContainerType.SHOP)) + /** + * Gets the viewers. + * + * @return The viewers. + */ + /** + * Represents the list of shop viewers. + */ + val viewers: List = ArrayList(20) + /** + * Gets the title. + * + * @return The title. + */ + /** + * Gets the items. + * + * @return The items. + */ + /** + * Gets the general. + * + * @return The general. + */ + /** + * Gets the currency. + * + * @return The currency. + */ + /** + * Gets the bhighAlch. + * + * @return the highAlch + */ + /** + * Gets the SellAllFor value. + * + * @return the sellAllFor + */ /** * Sell price for all shop items, if needed. */ - private int sellAllFor; - + var sellAllFor = 0 + private set + /** + * Gets the lastRestock. + * + * @return the lastRestock + */ + /** + * Sets the balastRestock. + * + * @param lastRestock the lastRestock to set. + */ /** * The last restock. */ - private int lastRestock; - + var lastRestock = 0 + /** + * Check if shop should restock. + * + * @return the restock + */ + /** + * Sets the restock. + * + * @param reStock + */ /** * If the shop should restock. */ - private boolean restock; - + var isRestock = false + /** + * Gets the pointShop. + * + * @return the pointShop + */ + /** + * Sets the pointShop. + * + * @param pointShop the pointShop to set. + */ /** * If it's a point shop. */ - private boolean pointShop; - + var isPointShop = false + /** + * Gets the npcs. + * + * @return the npcs + */ + /** + * Sets the banpcs. + * + * @param npcs the npcs to set. + */ /** * The npcs of the shop. */ - private int[] npcs; + var npcs: IntArray = intArrayOf() /** - * Constructs a new {@code Shop} {@code Object}. - * - * @param title the title. - * @param items the items. - * @param general the general. - * @param currency the currency. - * @param highAlch if high alch. - */ - public Shop(String title, Item[] items, boolean general, int currency, boolean highAlch) { - this.title = title; - this.items = items; - this.general = general; - this.currency = currency; - this.highAlch = highAlch; - this.getContainer(0).add(items); - this.setRestock(true); - lastRestock = GameWorld.getTicks() + 100; - } - - - /** - * Constructs a new {@code Shop} {@code Object}. + * Constructs a new `Shop` `Object`. * * @param title the title. * @param items the items. @@ -144,104 +178,122 @@ public class Shop { * @param highAlch if high alch. * @param restock if restock. */ - public Shop(String title, Item[] items, boolean general, int currency, boolean highAlch, boolean restock, int sellAllFor) { - this(title, items, general, currency, highAlch); - this.sellAllFor = sellAllFor; - this.setRestock(restock); + constructor( + title: String, + items: Array, + general: Boolean, + currency: Int, + highAlch: Boolean, + restock: Boolean, + sellAllFor: Int + ) : this(title, items, general, currency, highAlch) { + this.sellAllFor = sellAllFor + isRestock = restock } /** - * Constructs a new {@code Shop} {@code Object} + * Constructs a new `Shop` `Object` * * @param title the shop title * @param items items the shop can handle * @param general is this a general store * @param currency what currency is used */ - public Shop(String title, Item[] items, int[] npcs, boolean general, int currency) { - this(title, items, general, currency, false); - this.setNpcs(npcs); + constructor(title: String, items: Array, npcs: IntArray, general: Boolean, currency: Int) : this( + title, + items, + general, + currency, + false + ) { + this.npcs = npcs } /** - * Constructs a new {@code Shop} {@code Object}. - * - * @param title the title. - * @param items the items. - * @param general the general. - */ - public Shop(String title, Item[] items, boolean general) { - this(title, items, general, COINS, false); - } - - /** - * Constructs a new {@code Shop} {@code Object}. + * Constructs a new `Shop` `Object`. * * @param title the title. * @param general the general. */ - public Shop(String title, boolean general) { - this(title, GENERAL_STORE_ITEMS, general); - } + constructor(title: String, general: Boolean) : this(title, GENERAL_STORE_ITEMS, general) {} /** - * Constructs a new {@code Shop} {@code Object}. + * Constructs a new `Shop` `Object`. * * @param title the title. * @param general the general. * @param highAlch if highAlch. */ - public Shop(String title, boolean general, boolean highAlch) { - this(title, GENERAL_STORE_ITEMS, general, COINS, highAlch); + constructor(title: String, general: Boolean, highAlch: Boolean) : this( + title, + GENERAL_STORE_ITEMS, + general, + COINS, + highAlch + ) { } /** - * Constructs a new {@code Shop} {@code Object}. + * Constructs a new `Shop` `Object`. * * @param title the title. * @param general the general. * @param highAlch if highAlch. */ - public Shop(String title, boolean general, int currency, boolean highAlch) { - this(title, GENERAL_STORE_ITEMS, general, currency, highAlch); + constructor(title: String, general: Boolean, currency: Int, highAlch: Boolean) : this( + title, + GENERAL_STORE_ITEMS, + general, + currency, + highAlch + ) { } /** - * Constructs a new {@code Shop} {@code Object}. + * Constructs a new `Shop` `Object`. * * @param title the title. * @param items the items. * @param npcs the npcs. * @param general the general. */ - public Shop(String title, Item[] items, int[] npcs, boolean general) { - this(title, items, general, COINS, false); - this.setNpcs(npcs); + constructor(title: String, items: Array, npcs: IntArray, general: Boolean) : this( + title, + items, + general, + COINS, + false + ) { + this.npcs = npcs } /** - * Constructs a new {@code Shop} {@code Object}. + * Constructs a new `Shop` `Object`. * * @param title the title. * @param npcs the npcs. * @param general the general. */ - public Shop(String title, int[] npcs, boolean general) { - this(title, GENERAL_STORE_ITEMS, npcs, general); - this.setNpcs(npcs); + constructor(title: String, npcs: IntArray, general: Boolean) : this(title, GENERAL_STORE_ITEMS, npcs, general) { + this.npcs = npcs } /** - * Constructs a new {@code Shop} {@code Object}. + * Constructs a new `Shop` `Object`. * * @param title the title. * @param npcs the npcs. * @param general the general. * @param highAlch if highAlch. */ - public Shop(String title, int[] npcs, boolean general, boolean highAlch) { - this(title, GENERAL_STORE_ITEMS, general, 995, highAlch); - this.setNpcs(npcs); + constructor(title: String, npcs: IntArray, general: Boolean, highAlch: Boolean) : this( + title, + GENERAL_STORE_ITEMS, + general, + 995, + highAlch + ) { + this.npcs = npcs } /** @@ -249,38 +301,35 @@ public class Shop { * * @param player the shop. */ - public void open(final Player player) { - ShopViewer.extend(player, this).open(); + fun open(player: Player) { + ShopViewer.extend(player, this).open() // Browse the Lumbridge General Store - if (getTitle().equalsIgnoreCase("Lumbridge General Store")) { - player.getAchievementDiaryManager().finishTask(player, DiaryType.LUMBRIDGE, 0, 18); + if (title.equals("Lumbridge General Store", ignoreCase = true)) { + player.achievementDiaryManager.finishTask(player, DiaryType.LUMBRIDGE, 0, 18) } // Browse through Oziach's Armour Shop - if (getTitle().equalsIgnoreCase("Oziach's Armour")) { - player.getAchievementDiaryManager().finishTask(player, DiaryType.VARROCK, 1, 20); + if (title.equals("Oziach's Armour", ignoreCase = true)) { + player.achievementDiaryManager.finishTask(player, DiaryType.VARROCK, 1, 20) } } - public void give(Player player, final int slot, int amount, int tabIndex) { - final Container container = getContainer(tabIndex); - final Item item = container.get(slot); - if (item == null) { - return; + fun give(player: Player, slot: Int, amount: Int, tabIndex: Int) { + val container = getContainer(tabIndex) + val item = container[slot] ?: return + val add = Item(item.id, amount) + if (add.amount < 1 || !player.inventory.hasSpaceFor(add)) { + player.packetDispatch.sendMessage("You have no inventory space at the moment and cannot get anything.") + return } - final Item add = new Item(item.getId(), amount); - if (add.getAmount() < 1 || !player.getInventory().hasSpaceFor(add)) { - player.getPacketDispatch().sendMessage("You have no inventory space at the moment and cannot get anything."); - return; + add.amount = getAmount(player, add) + if (add.amount < 1 || !player.inventory.hasSpaceFor(add)) { + player.packetDispatch.sendMessage("You have no inventory space at the moment and cannot get anything.") + return } - add.setAmount(getAmount(player, add)); - if (add.getAmount() < 1 || !player.getInventory().hasSpaceFor(add)) { - player.getPacketDispatch().sendMessage("You have no inventory space at the moment and cannot get anything."); - return; - } - player.getInventory().add(add); - update(); + player.inventory.add(add) + update() } /** @@ -289,68 +338,69 @@ public class Shop { * @param slot the slot. * @param amount the amount. */ - public void buy(Player player, final int slot, int amount, int tabIndex) { - if (tabIndex == 1 && player.getIronmanManager().checkRestriction()) { - return; + fun buy(player: Player, slot: Int, amount: Int, tabIndex: Int) { + var amount = amount + if (tabIndex == 1 && player.ironmanManager.checkRestriction()) { + return } - final Container container = getContainer(tabIndex); - final Item item = container.get(slot); - if (item == null) { - return; + val container = getContainer(tabIndex) + val item = container[slot] ?: return + if (item.amount == 0) { + player.packetDispatch.sendMessage("There is no stock of that item at the moment.") + return } - if (item.getAmount() == 0) { - player.getPacketDispatch().sendMessage("There is no stock of that item at the moment."); - return; + if (amount > item.amount && item.amount != -1) { + amount = item.amount } - if (amount > item.getAmount() && !(item.getAmount() == -1)) { - amount = item.getAmount(); + val add = Item(item.id, amount) + if (player.inventory.getMaximumAdd(add) < amount) { + add.amount = player.inventory.getMaximumAdd(add) } - final Item add = new Item(item.getId(), amount); - if (player.getInventory().getMaximumAdd(add) < amount) { - add.setAmount(player.getInventory().getMaximumAdd(add)); + if (add.amount < 1 || !player.inventory.hasSpaceFor(add)) { + player.packetDispatch.sendMessage("You have no inventory space at the moment and cannot buy anything.") + return } - if (add.getAmount() < 1 || !player.getInventory().hasSpaceFor(add)) { - player.getPacketDispatch().sendMessage("You have no inventory space at the moment and cannot buy anything."); - return; + add.amount = getAmount(player, add) + if (add.amount < 1 || !player.inventory.hasSpaceFor(add)) { + player.packetDispatch.sendMessage("You have no inventory space at the moment and cannot buy anything.") + return } - add.setAmount(getAmount(player, add)); - if (add.getAmount() < 1 || !player.getInventory().hasSpaceFor(add)) { - player.getPacketDispatch().sendMessage("You have no inventory space at the moment and cannot buy anything."); - return; - } - int price = add.getAmount() * getBuyPrice(item, player); - final Item currency = new Item(getCurrency(), price); + val price = add.amount * getBuyPrice(item, player) + val currency = Item(currency, price) if (!canBuy(player, item, price, currency)) { - return; + return } if (handleBuy(player, currency)) { - if (pointShop) { - decrementPoints(player, price); + if (isPointShop) { + decrementPoints(player, price) } if (tabIndex == 0) { - if(!(container.getAmount(item) == -1)) - container.replace(new Item(item.getId(), container.getAmount(item) - add.getAmount()), slot, true); + if (container.getAmount(item) != -1) container.replace( + Item( + item.id, + container.getAmount(item) - add.amount + ), slot, true + ) } else { - container.remove(add); - container.shift(); + container.remove(add) + container.shift() } // Achievement Diary Handlers - if (add.getId() == Items.BLACK_CHAINBODY_1107 && getTitle().equalsIgnoreCase("Wayne's Chains") && !player.getAttribute("diary:falador:black-chain-bought", false)) { - player.setAttribute("/save:diary:falador:black-chain-bought", true); + if (add.id == BLACK_CHAINBODY_1107 && title.equals("Wayne's Chains", ignoreCase = true) && !player.getAttribute("diary:falador:black-chain-bought", false)) { + player.setAttribute("/save:diary:falador:black-chain-bought", true) } - if (add.getId() == 12622 && getTitle().equalsIgnoreCase("Sarah's Farming Shop")) { - player.getAchievementDiaryManager().finishTask(player, DiaryType.FALADOR, 0, 0); + if (add.id == 12622 && title.equals("Sarah's Farming Shop", ignoreCase = true)) { + player.achievementDiaryManager.finishTask(player, DiaryType.FALADOR, 0, 0) } - if (add.getId() == Items.CANDLE_36 && getTitle().equalsIgnoreCase("Candle Shop")) { - player.getAchievementDiaryManager().finishTask(player, DiaryType.SEERS_VILLAGE, 0, 9); + if (add.id == CANDLE_36 && title.equals("Candle Shop", ignoreCase = true)) { + player.achievementDiaryManager.finishTask(player, DiaryType.SEERS_VILLAGE, 0, 9) } - if (getTitle().equalsIgnoreCase("Ranging Guild Ticket Exchange")) { - player.getAchievementDiaryManager().finishTask(player, DiaryType.SEERS_VILLAGE, 1, 8); + if (title.equals("Ranging Guild Ticket Exchange", ignoreCase = true)) { + player.achievementDiaryManager.finishTask(player, DiaryType.SEERS_VILLAGE, 1, 8) } - - player.getInventory().add(add); - update(); + player.inventory.add(add) + update() } } @@ -360,55 +410,54 @@ public class Shop { * @param slot the slot. * @param amount the amount. */ - public void sell(Player player, final int slot, int amount, int tabIndex) { - final Item item = player.getInventory().get(slot); - if (item == null) { - return; - } - final ItemDefinition def = ItemDefinition.forId(item.getId()); + fun sell(player: Player, slot: Int, amount: Int, tabIndex: Int) { + var amount = amount + var tabIndex = tabIndex + val item = player.inventory[slot] ?: return + val def = ItemDefinition.forId(item.id) if (!canSell(player, item, def)) { - return; + return } - final Container container = getContainer(item); - if (amount > player.getInventory().getAmount(item)) { - amount = player.getInventory().getAmount(item); + val container = getContainer(item) + if (amount > player.inventory.getAmount(item)) { + amount = player.inventory.getAmount(item) } - Item add = new Item(item.getId(), amount); - if (add.getAmount() > container.getMaximumAdd(add)) { - add.setAmount(container.getMaximumAdd(add)); + var add = Item(item.id, amount) + if (add.amount > container.getMaximumAdd(add)) { + add.amount = container.getMaximumAdd(add) } - player.debug("" + add.getAmount()); - if (!container.hasSpaceFor(add) || add.getAmount() < 1) { - player.getPacketDispatch().sendMessage("The shop has ran out of space."); - return; + player.debug("" + add.amount) + if (!container.hasSpaceFor(add) || add.amount < 1) { + player.packetDispatch.sendMessage("The shop has ran out of space.") + return } - final Item currency = new Item(getCurrency(), getSellingValue(add, player)); - if(item.getDefinition().isStackable()){ - if (!player.getInventory().hasSpaceFor(currency)) { - player.getPacketDispatch().sendMessage("You don't have enough space for that many " + currency.getName().toLowerCase() + "."); - return; + val currency = Item(currency, getSellingValue(add, player)) + if (item.definition.isStackable) { + if (!player.inventory.hasSpaceFor(currency)) { + player.packetDispatch.sendMessage("You don't have enough space for that many " + currency.name.toLowerCase() + ".") + return } } - player.debug("Selling item"); - if (player.getInventory().remove(add, slot, true)) { - if (currency.getAmount() > player.getInventory().getMaximumAdd(currency)) { - currency.setAmount(player.getInventory().getMaximumAdd(currency)); + player.debug("Selling item") + if (player.inventory.remove(add, slot, true)) { + if (currency.amount > player.inventory.getMaximumAdd(currency)) { + currency.amount = player.inventory.getMaximumAdd(currency) } - if (!add.getDefinition().isUnnoted()) { - add = new Item(add.getNoteChange(), add.getAmount()); + if (!add.definition.isUnnoted) { + add = Item(add.noteChange, add.amount) } - if (container.getAmount(add.getId()) == -1 || container.add(add)) { - if (currency.getAmount() > 0) { - player.debug("Adding coins to inventory"); - player.getInventory().add(currency); + if (container.getAmount(add.id) == -1 || container.add(add)) { + if (currency.amount > 0) { + player.debug("Adding coins to inventory") + player.inventory.add(currency) } - final ShopViewer viewer = player.getExtension(ShopViewer.class); - tabIndex = container == getContainers()[0] ? 0 : 1; - sendStock(player, tabIndex); + val viewer = player.getExtension(ShopViewer::class.java) + tabIndex = if (container === containers[0]) 0 else 1 + sendStock(player, tabIndex) if (viewer != null) { - viewer.setTabIndex(tabIndex); + viewer.tabIndex = tabIndex } - update(); + update() } } } @@ -421,25 +470,30 @@ public class Shop { * @param item the item. * @param sell the sell. */ - public void value(Player player, ShopViewer viewer, Item item, boolean sell) { + fun value(player: Player, viewer: ShopViewer, item: Item, sell: Boolean) { if (sell) { - if (pointShop || item.getId() == viewer.getShop().getCurrency() || !item.getDefinition().isTradeable() || !viewer.getShop().itemAllowed(item.getId())) { - player.getPacketDispatch().sendMessage("You can't sell this item."); - return; + if (isPointShop || item.id == viewer.shop.currency || !item.definition.isTradeable || !viewer.shop.itemAllowed( + item.id + ) + ) { + player.packetDispatch.sendMessage("You can't sell this item.") + return } - final int value = viewer.getShop().getSellingValue(new Item(item.getId(), 1), player); - String currency = pointShop ? getPointsName() : ItemDefinition.forId(viewer.getShop().getCurrency()).getName().toLowerCase(); - if (value == 1 && currency.charAt(currency.length() - 1) == 's') { - currency = currency.substring(0, currency.length() - 1); + val value = viewer.shop.getSellingValue(Item(item.id, 1), player) + var currency = + if (isPointShop) pointsName else ItemDefinition.forId(viewer.shop.currency).name.toLowerCase() + if (value == 1 && currency[currency.length - 1] == 's') { + currency = currency.substring(0, currency.length - 1) } - player.getPacketDispatch().sendMessage(item.getName() + ": shop will buy for " + value + " " + currency + "."); + player.packetDispatch.sendMessage(item.name + ": shop will buy for " + value + " " + currency + ".") } else { - int value = viewer.getShop().getBuyPrice(item, player); - String name = pointShop ? getPointsName() + "s" : ItemDefinition.forId(viewer.getShop().getCurrency()).getName().toLowerCase(); - if (value == 1 && (name.charAt(name.length() - 1) == 's')) { - name = name.substring(0, name.length() - 1); + val value = viewer.shop.getBuyPrice(item, player) + var name = + if (isPointShop) pointsName + "s" else ItemDefinition.forId(viewer.shop.currency).name.toLowerCase() + if (value == 1 && name[name.length - 1] == 's') { + name = name.substring(0, name.length - 1) } - player.getPacketDispatch().sendMessage("" + item.getName() + ": currently costs " + value + " " + name + "."); + player.packetDispatch.sendMessage("" + item.name + ": currently costs " + value + " " + name + ".") } } @@ -449,58 +503,55 @@ public class Shop { * @param player * @param tabIndex */ - public void sendStock(final Player player, int tabIndex) { - final boolean main = tabIndex == 0; - player.getPacketDispatch().sendInterfaceConfig(620, 23, !main); - player.getPacketDispatch().sendInterfaceConfig(620, 24, main); - player.getPacketDispatch().sendInterfaceConfig(620, 29, !main); - player.getPacketDispatch().sendInterfaceConfig(620, 25, main); - player.getPacketDispatch().sendInterfaceConfig(620, 27, main); - player.getPacketDispatch().sendInterfaceConfig(620, 26, false); - player.getPacketDispatch().sendAccessMask(1278, main ? 23 : 24, 620, 0, 40); + fun sendStock(player: Player, tabIndex: Int) { + val main = tabIndex == 0 + player.packetDispatch.sendInterfaceConfig(620, 23, !main) + player.packetDispatch.sendInterfaceConfig(620, 24, main) + player.packetDispatch.sendInterfaceConfig(620, 29, !main) + player.packetDispatch.sendInterfaceConfig(620, 25, main) + player.packetDispatch.sendInterfaceConfig(620, 27, main) + player.packetDispatch.sendInterfaceConfig(620, 26, false) + player.packetDispatch.sendAccessMask(1278, if (main) 23 else 24, 620, 0, 40) } /** * Method used to update the viewers. */ - public void update() { - for (ShopViewer viewer : viewers) { - viewer.update(); + fun update() { + for (viewer in viewers) { + viewer.update() } } /** * Method used to restock the shop. */ - public void restock() { - for (Container container : containers) { - for (int i = 0; i < container.toArray().length; i++) { - final boolean main = (container == containers[0]); - final Item item = container.toArray()[i]; - if (item == null) { - continue; - } - boolean reduce = !main; + fun restock() { + for (container in containers) { + for (i in container.toArray().indices) { + val main = container === containers[0] + val item = container.toArray()[i] ?: continue + var reduce = !main if (main) { - if (item.getAmount() < items[i].getAmount()) { - item.setAmount(item.getAmount() + 1); + if (item.amount < items[i].amount) { + item.amount = item.amount + 1 } - reduce = item.getAmount() > items[i].getAmount(); + reduce = item.amount > items[i].amount } if (reduce) { - int amount = item.getAmount() - 1; + val amount = item.amount - 1 if (amount < 1 && !main) { - container.remove(item); + container.remove(item) } else { - item.setAmount(amount); + item.amount = amount } if (!main) { - container.shift(); + container.shift() } } } } - update(); + update() } /** @@ -509,18 +560,18 @@ public class Shop { * @param player the player. * @param item the item. * @param def the def. - * @return {@code True} if so. + * @return `True` if so. */ - public boolean canSell(Player player, Item item, ItemDefinition def) { - if (pointShop || item.getDefinition().hasDestroyAction() || !def.isTradeable() || !itemAllowed(item.getId())) { - player.getPacketDispatch().sendMessage("You can't sell this item to this shop."); - return false; + open fun canSell(player: Player, item: Item, def: ItemDefinition): Boolean { + if (isPointShop || item.definition.hasDestroyAction() || !def.isTradeable || !itemAllowed(item.id)) { + player.packetDispatch.sendMessage("You can't sell this item to this shop.") + return false } - if (item.getId() == getCurrency()) { - player.getPacketDispatch().sendMessage("You can't sell " + item.getName().toLowerCase() + " to a shop."); - return false; + if (item.id == currency) { + player.packetDispatch.sendMessage("You can't sell " + item.name.toLowerCase() + " to a shop.") + return false } - return true; + return true } /** @@ -530,8 +581,8 @@ public class Shop { * @param add the added item. * @return the amount. */ - public int getAmount(Player player, Item add) { - return add.getAmount(); + fun getAmount(player: Player?, add: Item): Int { + return add.amount } /** @@ -539,10 +590,10 @@ public class Shop { * * @param player the player. * @param currency the currency. - * @return {@code True} if so. + * @return `True` if so. */ - public boolean handleBuy(Player player, Item currency) { - return pointShop || player.getInventory().remove(currency); + fun handleBuy(player: Player, currency: Item?): Boolean { + return isPointShop || player.inventory.remove(currency) } /** @@ -552,18 +603,20 @@ public class Shop { * @param item the item. * @param price the price. * @param currency the currency. - * @return {@code True} if they can buy. + * @return `True` if they can buy. */ - public boolean canBuy(Player player, Item item, int price, Item currency) { - if (!pointShop && !player.getInventory().containsItem(currency)) { - player.getPacketDispatch().sendMessage("You don't have enough " + ItemDefinition.forId(getCurrency()).getName().toLowerCase() + "."); - return false; + fun canBuy(player: Player, item: Item, price: Int, currency: Item): Boolean { + if (!isPointShop && !player.inventory.containsItem(currency)) { + player.packetDispatch.sendMessage( + "You don't have enough " + ItemDefinition.forId(currency.id).name.toLowerCase() + "." + ) + return false } - if (pointShop && getPoints(player) < price) { - player.sendMessage("You don't have enough " + getPointsName() + "s."); - return false; + if (isPointShop && getPoints(player) < price) { + player.sendMessage("You don't have enough " + pointsName + "s.") + return false } - return true; + return true } /** @@ -572,8 +625,8 @@ public class Shop { * @param player the player. * @return the points. */ - public int getPoints(Player player) { - return 0; + fun getPoints(player: Player?): Int { + return 0 } /** @@ -582,18 +635,15 @@ public class Shop { * @param player the player. * @param decrement the decrementation. */ - public void decrementPoints(Player player, int decrement) { - - } + fun decrementPoints(player: Player?, decrement: Int) {} /** * Gets the points name. * * @return the name. */ - public String getPointsName() { - return ""; - } + val pointsName: String + get() = "" /** * Gets the value gained for selling this item to a certain shop. @@ -602,17 +652,17 @@ public class Shop { * @param player the player. * @return The value. */ - public int getSellingValue(Item item, Player player) { - if (!item.getDefinition().isUnnoted()) { - player.setAttribute("shop:originalId",item.getId()); - item = new Item(item.getNoteChange(), item.getAmount()); + fun getSellingValue(item: Item, player: Player): Int { + var item = item + if (!item.definition.isUnnoted) { + player.setAttribute("shop:originalId", item.id) + item = Item(item.noteChange, item.amount) } - int amount = getContainer(1).getAmount(item); + var amount = getContainer(1).getAmount(item) if (amount < 1) { - amount = getContainer(0).getAmount(item); + amount = getContainer(0).getAmount(item) } - int value = getSellValue(player, amount, item); - return value; + return getSellingValue(player, amount, item) } /** @@ -622,31 +672,31 @@ public class Shop { * @param item the item. * @return the selling value. */ - private int getSellValue(Player player, int amount, Item item) { - int id = player.getAttribute("shop:originalId",item.getId()); - if(item.getAmount() > ContentAPI.amountInInventory(player, id)){ - item.setAmount(ContentAPI.amountInInventory(player, id)); - player.removeAttribute("shop:originalId"); + private fun getSellingValue(player: Player, amount: Int, item: Item): Int { + val id = player.getAttribute("shop:originalId", item.id) + if (item.amount > amountInInventory(player, id)) { + item.amount = amountInInventory(player, id) + player.removeAttribute("shop:originalId") } - double diff = item.getDefinition().isStackable() ? 0.005 : 0.05; - double maxMod = 1.0 - (amount * diff); + val diff = if (item.definition.isStackable) 0.005 else 0.05 + var maxMod = 1.0 - amount * diff if (maxMod < 0.25) { - maxMod = 0.25; + maxMod = 0.25 } - double minMod = maxMod - ((item.getAmount() - 1) * diff); + var minMod = maxMod - (item.amount - 1) * diff if (minMod < 0.25) { - minMod = 0.25; + minMod = 0.25 } - double mod = (maxMod + minMod) / 2; - SystemLogger.logInfo("" + item.getDefinition().getAlchemyValue(highAlch) + " " + mod + " " + item.getAmount()); - int baseValue = item.getDefinition().getAlchemyValue(highAlch); - int value = (int) (baseValue * mod * item.getAmount()); - if(getCurrency() == Items.TOKKUL_6529 && item.getId() == Items.CHAOS_RUNE_562) value = (13 * item.getAmount()); - if(getCurrency() == Items.TOKKUL_6529 && item.getId() == Items.DEATH_RUNE_560) value = (27 * item.getAmount()); - if(item.getId() == 12183){ - value = 25 * item.getAmount(); + val mod = (maxMod + minMod) / 2 + logInfo("" + item.definition.getAlchemyValue(isHighAlch) + " " + mod + " " + item.amount) + val baseValue = item.definition.getAlchemyValue(isHighAlch) + var value = (baseValue * mod * item.amount).toInt() + if (currency == TOKKUL_6529 && item.id == CHAOS_RUNE_562) value = 13 * item.amount + if (currency == TOKKUL_6529 && item.id == DEATH_RUNE_560) value = 27 * item.amount + if (item.id == 12183) { + value = 25 * item.amount } - return value; + return value } /** @@ -655,54 +705,55 @@ public class Shop { * @param item the item. * @return the price. */ - public int getBuyPrice(Item item, Player player) { - item = new Item(item.getId(), 1); - int price = item.getDefinition().getMaxValue(); - int sellVal = getSellingValue(item, player); + open fun getBuyPrice(item: Item, player: Player): Int { + var item = item + item = Item(item.id, 1) + var price = item.definition.maxValue + val sellVal = getSellingValue(item, player) if (price < sellVal) { - price = getSellValue(player, 0, item) + sellVal - (sellVal - item.getDefinition().getMaxValue()); + price = getSellingValue(player, 0, item) + sellVal - (sellVal - item.definition.maxValue) } if (price < 0) { - price = 1; + price = 1 } - if (getCurrency() == TOKKUL) { - int tokkul = item.getDefinition().getConfiguration("tokkul_price", -1); + if (currency == TOKKUL) { + val tokkul = item.definition.getConfiguration("tokkul_price", -1) if (tokkul > 0) { - price = tokkul; + price = tokkul } - if (player.getAchievementDiaryManager().getKaramjaGlove() != -1) { - price *= 0.86666666667; + if (player.achievementDiaryManager.karamjaGlove != -1) { + price = kotlin.math.floor(price * 0.87).toInt() } } - if (getCurrency() == ARCHERY_TICKET) { - int tickets = item.getDefinition().getConfiguration(ItemConfigParser.ARCHERY_TICKET_PRICE,-1); - if (tickets > 0) { - price = tickets; - } + if (currency == ARCHERY_TICKET) { + val tickets = item.definition.getConfiguration(ItemConfigParser.ARCHERY_TICKET_PRICE, -1) + if (tickets > 0) { + price = tickets + } } - return (getSellAllFor() > 0 ? getSellAllFor() : price); + return if (sellAllFor > 0) sellAllFor else price } /** * Checks if the item is allowed to be sold to the shop. * * @param itemId the item id. - * @return {@code True} if so. + * @return `True` if so. */ - public boolean itemAllowed(int itemId) { - if (general) { - return true; + fun itemAllowed(itemId: Int): Boolean { + if (isGeneral) { + return true } - int noteId = ItemDefinition.forId(itemId).getNoteId(); - if (!ItemDefinition.forId(itemId).isUnnoted()) { - noteId = ItemDefinition.forId(noteId).getNoteId(); + var noteId = ItemDefinition.forId(itemId).noteId + if (!ItemDefinition.forId(itemId).isUnnoted) { + noteId = ItemDefinition.forId(noteId).noteId } - for (Item id : items) { - if (itemId == id.getId() || (noteId > -1 && noteId == ItemDefinition.forId(id.getId()).getNoteId())) { - return true; + for (id in items) { + if (itemId == id.id || noteId > -1 && noteId == ItemDefinition.forId(id.id).noteId) { + return true } } - return false; + return false } /** @@ -711,18 +762,18 @@ public class Shop { * @param item the item. * @return the container. */ - public Container getContainer(Item item) { - int itemId = item.getId(); - int noteId = ItemDefinition.forId(itemId).getNoteId(); - if (!ItemDefinition.forId(itemId).isUnnoted()) { - noteId = ItemDefinition.forId(noteId).getNoteId(); + fun getContainer(item: Item): Container { + val itemId = item.id + var noteId = ItemDefinition.forId(itemId).noteId + if (!ItemDefinition.forId(itemId).isUnnoted) { + noteId = ItemDefinition.forId(noteId).noteId } - for (Item i : items) { - if (i.getId() == item.getId() || (noteId > -1 && noteId == ItemDefinition.forId(i.getId()).getNoteId())) { - return getContainer(0); + for (i in items) { + if (i.id == item.id || noteId > -1 && noteId == ItemDefinition.forId(i.id).noteId) { + return getContainer(0) } } - return getContainer(1); + return getContainer(1) } /** @@ -730,8 +781,8 @@ public class Shop { * * @return the shop. */ - public Shop copy() { - return new Shop(title, items, general, currency, highAlch); + fun copy(): Shop { + return Shop(title, items, isGeneral, currency, isHighAlch) } /** @@ -740,174 +791,75 @@ public class Shop { * @param tabIndex the tab index. * @return the container. */ - public Container getContainer(int tabIndex) { - if (tabIndex > containers.length) { - throw new IndexOutOfBoundsException("Error! Shop tab index out of bounds."); + fun getContainer(tabIndex: Int): Container { + if (tabIndex > containers.size) { + throw IndexOutOfBoundsException("Error! Shop tab index out of bounds.") } - return containers[tabIndex]; + return containers[tabIndex] } + override fun toString(): String { + return "Shop [containers=" + Arrays.toString(containers) + ", viewers=" + viewers + ", title=" + title + ", items=" + Arrays.toString( + items + ) + ", general=" + isGeneral + ", currency=" + currency + ", highAlch=" + isHighAlch + "]" + } + + fun setItems(items: ArrayList) { + this.items = items.toTypedArray() + } + + companion object { + /** + * Represents the general store items. + */ + val GENERAL_STORE_ITEMS = arrayOf( + Item(EMPTY_POT_1931, 5), + Item(JUG_1935, 5), + Item(SHEARS_1735, 2), + Item(BUCKET_1925, 3), + Item(BOWL_1923, 2), + Item(CAKE_TIN_1887, 2), + Item(TINDERBOX_590, 2), + Item(CHISEL_1755, 2), + Item(HAMMER_2347, 5), + Item(NEWCOMER_MAP_550, 5), + Item(SECURITY_BOOK_9003, 5) + ) + + /** + * Represents the coins item. + */ + private const val COINS = 995 + + /** + * Represents the tokkul item id. + */ + private const val TOKKUL = 6529 + + /** + * Represents the archery ticket item id + */ + private const val ARCHERY_TICKET = 1464 + } /** - * Gets the viewers. + * Constructs a new `Shop` `Object`. * - * @return The viewers. + * @param title the title. + * @param items the items. + * @param isGeneral the general. + * @param currency the currency. + * @param isHighAlch if high alch. */ - public List getViewers() { - return viewers; - } - /** - * Gets the title. + * Constructs a new `Shop` `Object`. * - * @return The title. + * @param title the title. + * @param items the items. + * @param general the general. */ - public String getTitle() { - return title; + init { + this.getContainer(0).add(*items) + isRestock = true + lastRestock = ticks + 100 } - - /** - * Gets the items. - * - * @return The items. - */ - public Item[] getItems() { - return items; - } - - /** - * Sets the items. - * - * @return true if the items were changed. - */ - public boolean setItems(Item... item) { - return items == item; - } - - /** - * Gets the general. - * - * @return The general. - */ - public boolean isGeneral() { - return general; - } - - /** - * Gets the currency. - * - * @return The currency. - */ - public int getCurrency() { - return currency; - } - - /** - * Gets the containers. - * - * @return The containers. - */ - public Container[] getContainers() { - return containers; - } - - /** - * Gets the bhighAlch. - * - * @return the highAlch - */ - public boolean isHighAlch() { - return highAlch; - } - - @Override - public String toString() { - return "Shop [containers=" + Arrays.toString(containers) + ", viewers=" + viewers + ", title=" + title + ", items=" + Arrays.toString(items) + ", general=" + general + ", currency=" + currency + ", highAlch=" + highAlch + "]"; - } - - /** - * Gets the lastRestock. - * - * @return the lastRestock - */ - public int getLastRestock() { - return lastRestock; - } - - /** - * Sets the balastRestock. - * - * @param lastRestock the lastRestock to set. - */ - public void setLastRestock(int lastRestock) { - this.lastRestock = lastRestock; - } - - /** - * Gets the npcs. - * - * @return the npcs - */ - public int[] getNpcs() { - return npcs; - } - - /** - * Sets the banpcs. - * - * @param npcs the npcs to set. - */ - public void setNpcs(int[] npcs) { - this.npcs = npcs; - } - - /** - * Gets the pointShop. - * - * @return the pointShop - */ - public boolean isPointShop() { - return pointShop; - } - - /** - * Sets the pointShop. - * - * @param pointShop the pointShop to set. - */ - public void setPointShop(boolean pointShop) { - this.pointShop = pointShop; - } - - /** - * Check if shop should restock. - * - * @return the restock - */ - public boolean isRestock() { - return restock; - } - - /** - * Sets the restock. - * - * @param reStock - */ - public void setRestock(boolean reStock) { - restock = reStock; - } - - - /** - * Gets the SellAllFor value. - * - * @return the sellAllFor - */ - public int getSellAllFor() { - return sellAllFor; - } - - public void setItems(ArrayList items){ - this.items = items.toArray(new Item[0]); - } - } \ No newline at end of file