Code:
package org.apollo.game.model.inter.store;
import java.util.ArrayList;
import org.apollo.game.event.impl.UpdateItemsEvent;
import org.apollo.game.event.impl.UpdateSlottedItemsEvent;
import org.apollo.game.model.Inventory;
import org.apollo.game.model.Inventory.StackMode;
import org.apollo.game.model.Item;
import org.apollo.game.model.Player;
import org.apollo.game.model.SlottedItem;
import org.apollo.game.model.inter.store.impl.CoinsShopPaymentType;
/**
* A utility that holds items into a store.
* @author Steve
*/
public final class Shop {
/**
* An enumeration of shop types.
* @author Steve
*/
public static enum ShopType {
/**
* You can only buy if the inventory has the item.
*/
LIMITED_BUY_ONLY,
/**
* You can buy and the inventory item never gets deleted.
*/
UNLIMITED_BUY_ONLY,
/**
* You can only buy and sell if the inventory has the item.
*/
BUY_AND_SELL,
}
/**
* The shop name.
*/
private final String name;
/**
* The shop payment type.
*/
private ShopPaymentType payment = new CoinsShopPaymentType();
/**
* The shop type.
*/
private final ShopType type;
/**
* Holds the shop items.
*/
private final Inventory items = new Inventory(40, StackMode.STACK_ALWAYS);
/**
* The shop's viewers.
*/
private final ArrayList<Player> players = new ArrayList<Player>();
/**
* Create a new shop.
* @param name The shop name.
*/
public Shop(String name) {
this(name, ShopType.BUY_AND_SELL);
}
/**
* Create a new shop.
* @param name The shop name.
* @param type The shop type.
*/
public Shop(String name, ShopType type) {
this.name = name;
this.type = type;
items.addListener(new ShopAmountChangedListener(this));
}
/**
* Adds a bulk item to the store.
* @param item The item to add to the store.
*/
protected void addItem(Item item) {
items.add(item);
}
/**
* Adds a shop viewer to the list.
* @param player The player.
*/
protected void addViewer(Player player) {
if (!players.contains(player)) {
players.add(player);
}
}
/**
* Buys a item from the store.
* @param player The player.
* @param item The item to buy.
*/
public void buyItem(Player player, SlottedItem item) {
if (player.getInventory().freeSlots() >= item.getItem().getAmount()) {
if (payment.buyItem(player, item)) {
player.getInventory().add(item.getItem());
player.send(new UpdateItemsEvent(3823, player.getInventory().getItems()));
if (!type.equals(ShopType.UNLIMITED_BUY_ONLY)) {
items.remove(item.getItem());
}
}
} else {
player.getInventory().forceCapacityExceeded();
}
}
/**
* Gets the buy value of the item.
* @param item The item that is being checked.
* @return The value of the item.
*/
public int buyValue(int item) {
return payment.buyValue(item);
}
/**
* Gets the shop's items.
* @return The shop's items.
*/
public Inventory getItems() {
return items;
}
/**
* Gets the shop's name.
* @return The shop's name.
*/
public String getName() {
return name;
}
/**
* Gets the shop payment type.
* @return The shop payment type.
*/
public ShopPaymentType getPayment() {
return payment;
}
/**
* Refresh the shop for all viewers.
*/
public void refresh() {
for (Player player : players) {
player.send(new UpdateItemsEvent(3900, items.getItems()));
}
}
/**
* Refresh the shop for all viewers.
* @param si The slotted item to refresh.
*/
public void refresh(SlottedItem si) {
for (Player player : players) {
player.send(new UpdateSlottedItemsEvent(3900, si));
}
}
/**
* Removes a shop viewer from the list.
* @param player The player.
*/
protected void removeViewer(Player player) {
if (players.contains(player)) {
players.remove(player);
}
}
/**
* Sells a item to the store.
* @param player The player that is selling this item.
* @param slot The item slot.
* @param item The item.
*/
public void sellItem(Player player, Item item) {
if (type.equals(ShopType.BUY_AND_SELL)) {
if (player.getInventory().contains(item)) {
if (payment.sellItem(player, item)) {
player.getInventory().remove(item);
player.send(new UpdateItemsEvent(3823, player.getInventory().getItems()));
items.add(item);
}
}
} else {
player.sendMessage("You cannot sell items in this shop.");
}
}
/**
* Gets the sell value of the item.
* @param item The item that is being checked.
* @return The value of the item.
*/
public int sellValue(int item) {
if (!type.equals(ShopType.BUY_AND_SELL)) {
return -1;
} else {
return payment.sellValue(item);
}
}
/**
* Sets the shop payment type.
* @param payment The shop payment.
*/
public void setPayment(ShopPaymentType payment) {
this.payment = payment;
}
}
Code:
package org.apollo.game.model.inter.store;
import org.apollo.game.model.Inventory;
import org.apollo.game.model.Item;
import org.apollo.game.model.SlottedItem;
import org.apollo.game.model.inv.InventoryAdapter;
/**
* The listener for the shop amount change.
* @author Steve
*/
public final class ShopAmountChangedListener extends InventoryAdapter {
/**
* The shop.
*/
private final Shop shop;
/**
* Create a new shop amount changed listener.
* @param shop The shop.
*/
public ShopAmountChangedListener(Shop shop) {
this.shop = shop;
}
@Override
public void itemsUpdated(Inventory inventory) {
shop.refresh();
}
@Override
public void itemUpdated(Inventory container, int slot, Item item) {
shop.refresh(new SlottedItem(slot, item));
}
}
Code:
package org.apollo.game.model.inter.store;
import org.apollo.game.model.Player;
import org.apollo.game.model.inter.InterfaceListener;
/**
* An {@link InterfaceListener} for the shops.
* @author Steve
*/
public final class ShopClosedListener implements InterfaceListener {
/**
* The player.
*/
private final Player player;
/**
* The shop.
*/
private final Shop shop;
/**
* Create a shop listener for the player.
* @param player The player.
* @param shop The shop.
*/
public ShopClosedListener(Player player, Shop shop) {
this.player = player;
this.shop = shop;
init();
}
/**
* Adds some shop things.
*/
private void init() {
player.setShop(shop);
shop.addViewer(player);
}
@Override
public void interfaceClosed() {
shop.removeViewer(player);
player.setShop(null);
}
}
Code:
package org.apollo.game.model.inter.store;
import org.apollo.game.model.Item;
import org.apollo.game.model.Player;
import org.apollo.game.model.SlottedItem;
/**
* The {@link Shop} payment type.
* @author Steve
*/
public abstract class ShopPaymentType {
/**
* Buys an item from the store.
* @param player The player that is buying the item.
* @param item The item to buy from the store.
* @return True if the player can purchase the item, false if not.
*/
protected abstract boolean buyItem(Player player, SlottedItem item);
/**
* Gets the buy value of the item.
* @param item The item that is being checked.
* @return The value of the item.
*/
protected abstract int buyValue(int item);
/**
* The name of the payment type.
* @return The name of the payment type.
*/
public abstract String getName();
/**
* Sells a item to the store.
* @param player The player that is selling the item.
* @param item The item to sell to the store.
* @return True if the player can sell the item, false if not.
*/
protected abstract boolean sellItem(Player player, Item item);
/**
* Gets the sell value of the item.
* @param item The item that is being checked.
* @return The value of the item.
*/
protected abstract int sellValue(int item);
}
Code:
package org.apollo.game.model.inter.store;
import java.util.HashMap;
import java.util.Map;
import org.apollo.game.event.impl.SetInterfaceTextEvent;
import org.apollo.game.event.impl.UpdateItemsEvent;
import org.apollo.game.model.Player;
import org.apollo.game.model.World;
/**
* An shop that holds all the {@link World}'s store that a {@link Player} can purchase items in.
* @author Steve
*/
public final class WorldStore {
/**
* Holds the shops.
*/
private final Map<Integer, Shop> shops = new HashMap<Integer, Shop>();
/**
* Adds a shop to the world store.
* @param id The shop id.
* @param shop The shop.
* @return True if shop was added, false if already added.
*/
public boolean addShop(int id, Shop shop) {
if (shops.get(id) == null) {
shops.put(id, shop);
return true;
} else {
return false;
}
}
/**
* Gets the shop.
* @param shop The shop id.
* @return The shop that belongs to the id.
*/
public Shop getShop(int shop) {
return shops.get(shop);
}
/**
* Gets the world shops.
* @return The world shops.
*/
public Map<Integer, Shop> getShops() {
return shops;
}
/**
* Open up a shop.
* @param player The player.
* @param shop The shop.
*/
public void openShop(Player player, int shop) {
if (getShop(shop) != null) {
Shop store = getShop(shop);
player.send(new SetInterfaceTextEvent(3901, store.getName()));
player.send(new UpdateItemsEvent(3823, player.getInventory().getItems()));
player.send(new UpdateItemsEvent(3900, store.getItems().getItems()));
player.getInterfaceSet().openWindowWithSidebar(new ShopClosedListener(player, store), 3824, 3822);
}
}
}
Code:
package org.apollo.game.model.inter.store.impl;
import org.apollo.game.model.Item;
import org.apollo.game.model.Player;
import org.apollo.game.model.SlottedItem;
import org.apollo.game.model.inter.store.ShopPaymentType;
/**
* An {@link ShopPaymentType} that lets players buy with coins.
* @author Steve
*/
public final class CoinsShopPaymentType extends ShopPaymentType {
@Override
public boolean buyItem(Player player, SlottedItem item) {
int price = buyValue(item.getItem().getId()) * item.getItem().getAmount();
if (player.getInventory().contains(995, price)) {
player.getInventory().remove(995, price);
return true;
} else {
return false;
}
}
@Override
public int buyValue(int item) {
return new Item(item).getDefinition().getValue();
}
@Override
public String getName() {
return "coins";
}
@Override
public boolean sellItem(Player player, Item item) {
if (item.getId() != 995) {
int price = sellValue(item.getId()) * item.getAmount();
player.getInventory().add(995, price);
return true;
}
return false;
}
@Override
public int sellValue(int item) {
return new Item(item).getDefinition().getValue() / 2;
}
}
Player.java
Code:
import org.apollo.game.model.inter.store.Shop;
/**
* The shop that is currently open.
*/
private Shop shop;
/**
* Gets the shop.
* @return the shop.
*/
public Shop getShop() {
return shop;
}
/**
* Sets the shop.
* @param shop The shop.
*/
public void setShop(Shop shop) {
this.shop = shop;
}
World.java
Code:
import org.apollo.game.model.inter.store.WorldStore;
/**
* The world shops.
*/
private static final WorldStore worldStore = new WorldStore();
/**
* Gets the world stores.
* @return The world stores.
*/
public WorldStore getStores() {
return worldStore;
}
And how you add shops, quite simple. You can also add your own payment type, which shouldnt be too hard to figure out, especially if your coding in the apollo base.
Code:
# This class holds the world shop adding.
# Specify the payment type by executing shop.set_payment(payment)
# while; payment extends org.apollo.game.model.inter.store.ShopPaymentType
require 'java'
java_import 'org.apollo.game.model.inter.store.Shop'
java_import 'org.apollo.game.model.Item'
java_import 'org.apollo.game.model.World'
class Shops
attr_reader :id, :name, :items, :type
def initialize(id, name, items, type)
@id = id
@name = name
@items = items
@type = type
end
end
def appendShop(shop)
worldshop = Shop.new(shop.name, shop.type)
World.get_world.get_stores.addShop(shop.id, worldshop)
shop.items.each do |item, amount|
worldshop.add_item Item.new(item, amount)
end
return worldshop
end
Code:
# The general store.
require 'java'
java_import 'org.apollo.game.model.inter.store.Shop'
# Define our values.
id = 1
name = "General Store"
items = {
# item id => item amount,
2437 => 100, 2441 => 100, 2443 => 100, 2435 => 100, 2445 => 100, 3041 => 100, 15273 => 100, 386 => 100, 1079 => 100, 1093 => 100, 1113 => 100, 1128 => 100, 3749 => 100, 3753 => 100, 3755 => 100, 7461 => 100, 7462 => 100, 4151 => 100, 8850 => 100, 20072 => 100, 4587 => 100, 11732 => 100
}
type = Shop::ShopType::BUY_AND_SELL
# Ship out the shop to the world.
shop = appendShop(Shops.new(id, name, items, type))