Thread: [Apollo] Shops

Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42
  1. #1 [Apollo] Shops 
    Konklex Dev

    Buroa's Avatar
    Join Date
    Oct 2010
    Posts
    752
    Thanks
    676
    Thanked 177 Times in 108 Posts
    Rep Power
    266
    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))
    Intel i7 3770K 4.7GHz Processor
    Asus P8Z77-I DELUXE/WD Motherboard
    G.Skill Ripjaws X Series 16GB DDR3-1866 Memory
    Samsung 840 Pro Series 256GB Solid State Drive
    EVGA GeForce GTX 670 4GB Graphics Card
    Liquid Cooled
    http://pcpartpicker.com/user/Buroa/saved/1ymR
    Reply With Quote  
     


  2. #2  
    Banned

    Join Date
    Sep 2009
    Age
    18
    Posts
    4,123
    Thanks
    717
    Thanked 604 Times in 391 Posts
    Rep Power
    0
    Nice man :0
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Jan 2010
    Posts
    466
    Thanks
    264
    Thanked 77 Times in 46 Posts
    Rep Power
    43
    :O Thank you. So by using or modifying this

    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();
    		int left = player.getMoneyPouch().buy(price, 1);
    		if (left == 0) {
    			return true;
    		} else
    			if (player.getInventory().contains(995, left)) {
    				player.getInventory().remove(995, left);
    				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;
    	}
    }
    You should be able to add in for example tokkul, etc? Would be the same way for point values as well, right?
    Reply With Quote  
     

  4. #4  
    Konklex Dev

    Buroa's Avatar
    Join Date
    Oct 2010
    Posts
    752
    Thanks
    676
    Thanked 177 Times in 108 Posts
    Rep Power
    266
    Quote Originally Posted by Onlyme View Post
    :O Thank you. So by using or modifying this

    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;
    	}
    }
    You should be able to add in for example tokkul, etc? Would be the same way for point values as well, right?
    Correct, or you could make the class extend the payment type in a plugin, which is why apollo was build that way. But yeah, both will work.
    Intel i7 3770K 4.7GHz Processor
    Asus P8Z77-I DELUXE/WD Motherboard
    G.Skill Ripjaws X Series 16GB DDR3-1866 Memory
    Samsung 840 Pro Series 256GB Solid State Drive
    EVGA GeForce GTX 670 4GB Graphics Card
    Liquid Cooled
    http://pcpartpicker.com/user/Buroa/saved/1ymR
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    Registered Member

    Join Date
    Aug 2011
    Posts
    2,779
    Thanks
    298
    Thanked 542 Times in 393 Posts
    Rep Power
    1555
    That must have been alot of work, good job.
    Quote Originally Posted by Aj View Post
    This is not even a tutorial. It's fail for rep. It's fail for life.
    Reply With Quote  
     

  7. #6  
    Poser Hater Degree

    Valour's Avatar
    Join Date
    Nov 2010
    Posts
    1,007
    Thanks
    138
    Thanked 124 Times in 97 Posts
    Rep Power
    86
    Nice work xD Sadly not to many people use apollo


    Walk with pride. Fight with honor. Die in glory.

    Epicurus "Is God willing to prevent evil, but not able? Then He is not omnipotent. Is He able, but not willing? Then He is malevolent. Is He both able and willing? Then whence cometh evil? Is He neither able nor willing? Then why call Him God?"
    Seneca the Younger "Religion is regarded by the common people as true, by the wise as false, and by the rulers as useful."
    Reply With Quote  
     

  8. #7  
    Konklex Dev

    Buroa's Avatar
    Join Date
    Oct 2010
    Posts
    752
    Thanks
    676
    Thanked 177 Times in 108 Posts
    Rep Power
    266
    Quote Originally Posted by 'Spike View Post
    That must have been alot of work, good job.
    It was a decent amount . Not too hard though.
    Intel i7 3770K 4.7GHz Processor
    Asus P8Z77-I DELUXE/WD Motherboard
    G.Skill Ripjaws X Series 16GB DDR3-1866 Memory
    Samsung 840 Pro Series 256GB Solid State Drive
    EVGA GeForce GTX 670 4GB Graphics Card
    Liquid Cooled
    http://pcpartpicker.com/user/Buroa/saved/1ymR
    Reply With Quote  
     

  9. #8  
    Konklex Dev

    Buroa's Avatar
    Join Date
    Oct 2010
    Posts
    752
    Thanks
    676
    Thanked 177 Times in 108 Posts
    Rep Power
    266
    Quote Originally Posted by Artesia View Post
    Nice work xD Sadly not to many people use apollo
    We'll see by this summer.
    Intel i7 3770K 4.7GHz Processor
    Asus P8Z77-I DELUXE/WD Motherboard
    G.Skill Ripjaws X Series 16GB DDR3-1866 Memory
    Samsung 840 Pro Series 256GB Solid State Drive
    EVGA GeForce GTX 670 4GB Graphics Card
    Liquid Cooled
    http://pcpartpicker.com/user/Buroa/saved/1ymR
    Reply With Quote  
     

  10. #9  
    Registered Member

    Join Date
    Feb 2012
    Age
    20
    Posts
    862
    Thanks
    83
    Thanked 472 Times in 248 Posts
    Rep Power
    578
    First view looks good, nice work.

    Great to see you release something again, hope I helped motivating you a bit.

    Continuing the concept of Apollo
    - - -- --- -- - -
    “The only limit to our realization of tomorrow will be our doubts of today.”
    ― Franklin D. Roosevelt
    Reply With Quote  
     

  11. Thankful user:


  12. #10  
    Konklex Dev

    Buroa's Avatar
    Join Date
    Oct 2010
    Posts
    752
    Thanks
    676
    Thanked 177 Times in 108 Posts
    Rep Power
    266
    Quote Originally Posted by Chris Fletcher View Post
    First view looks good, nice work.

    Great to see you release something again, hope I helped motivating you a bit.
    I felt like I didn't release something for awhile, so I came up with this. I'm really slacking on my combat base hahah.
    Intel i7 3770K 4.7GHz Processor
    Asus P8Z77-I DELUXE/WD Motherboard
    G.Skill Ripjaws X Series 16GB DDR3-1866 Memory
    Samsung 840 Pro Series 256GB Solid State Drive
    EVGA GeForce GTX 670 4GB Graphics Card
    Liquid Cooled
    http://pcpartpicker.com/user/Buroa/saved/1ymR
    Reply With Quote  
     


Page 1 of 5 123 ... LastLast
Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Apollo]NPC Updating[Apollo]
    By The Wanderer in forum Tutorials
    Replies: 40
    Last Post: 10-15-2012, 01:18 AM
  2. Replies: 5
    Last Post: 03-28-2012, 05:08 PM
  3. Replies: 1
    Last Post: 12-13-2009, 03:02 AM
  4. [SHOPS] - RichScape - [SHOPS]
    By Cent lol in forum Help
    Replies: 0
    Last Post: 05-22-2009, 04:45 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •