Jump to content

The RuneLocus forum has been archived and does not accept new registrations.
Click here for more information, and click here to join the community on Discord.

562 New ShopHandler


Steve
 Share

Recommended Posts

Since i did a lot of stuff client sided, i thought i'd try something server sided... So i re-did the shophandler because i see too many help threads for problems with shops. Oh and I'm not completely sure about mine. It works, but idk really lol. Like instead of having the object for options in each shop instance, i could have put them in shophandler.java and called from there. And some other things that you'll have to let me know about. I guess you could point out what i did BAD. Note, I didn't test this as much as i should have, so if ur server is an economy be careful adding this.

Some Features:

-Shouldn't glitch.

-Updates the stock for everyone viewing it

-EASY general store support lol

-Maybe i'll add a currency system :\

Replace shophandler.java with this,

package com.rs2hd.content;


import java.util.HashMap;


import com.rs2hd.GameEngine;

import com.rs2hd.model.Player;

import com.rs2hd.util.log.Logger;


/**

 * Handles the shops, loads them, and opens them

 * 

 * @author Stephen Moyer <[email protected]>

 * 

 */

public class ShopHandler {


    private HashMap<Integer, Shop> shops = new HashMap<Integer, Shop>();


    /**

     * Load up the shops

     */

    public void loadShops() {

        shops.put(1, new Shop(1, true, new int[] { 4151, 10551 }, new int[] {

                1000, 1000 }));

        shops.put(2, new Shop(2, false, GameEngine.shops.Armor,

                GameEngine.shops.ArmorN));

        Logger.getInstance().info("Loaded Shops");


    }


    /**

     * Open a shop for the player, by the specified id

     * 

     * @param player

     *            The player to open for

     * @param id

     *            The id of the shop

     */

    public void openShop(Player player, int id) {

        player.shopId = id;

        shops.get(id).openForPlayer(player);

        shops.get(id).addPlayer(player);

    }


    /**

     * Return the shop instance of the specified id

     * 

     * @param id

     *            The shop's id

     * @return The shop instance

     */

    public Shop getShop(int id) {

        return shops.get(id);

    }


    /**

     * For people that are lazy to convert, and use proper conventions.

     * 

     * @param p

     *            The player that opened the shop

     * @param id

     *            The shops id

     */

    public void openshop(Player p, int id) {

        openShop(p, id);

    }


}

To make new shops do this, Under loadShops() { add like shops.put(1, new Shop(1, true, new int[] { 4151, 10551 }, new int[] { 1000, 1000 })); I'll go through what it does, .put(1, is the shop id, (like openshop(p, 2);) new Shop(1, The 1 is the shop id, true, Set to true or false if you want it to be a general store new int[] { 4151, 10551 }, new int[] { 1000, 1000 }, These are the items and the amounts. 1st one (4151, 10551) is the items, the next one is amounts. To use like your old system you can do stuff like GameEngine.shops.Armor, GameEngine.shops.ArmorN. Then create a new Class in com.rs2hd.content and name it Shop.java Then add this in it,
package com.rs2hd.content;


import java.text.NumberFormat;

import java.util.ArrayList;


import com.rs2hd.GameEngine;

import com.rs2hd.event.Event;

import com.rs2hd.model.Container;

import com.rs2hd.model.Item;

import com.rs2hd.model.ItemDefinition;

import com.rs2hd.model.Player;

import com.rs2hd.model.World;


/**

 * Creates a new shop.

 * 

 * @author Stephen Moyer <[email protected]>

 * 

 */

public class Shop {


    private int restockTime = 0;

    private int STORE_SIZE = 40;

    private Container<Item> itemsInShop = new Container<Item>(STORE_SIZE, true);

    private ArrayList<Player> playersViewing = new ArrayList<Player>(); // Not

                                                                        // sure.

    private boolean generalStore = false;

    Object[] sellOptions = new Object[] { "Sell 50", "Sell 10", "Sell 1",

            "Value", -1, 1, 7, 4, 93, 40697856 };

    Object[] invParams = new Object[] { "", "", "", "", "Sell 50", "Sell 10",

            "Sell 5", "Sell 1", "Value", -1, 0, 7, 4, 93, 40697856 };

    Object[] shopParams = new Object[] { "", "", "", "", "Buy 50", "Buy 10",

            "Buy 5", "Buy 1", "Value", -1, 0, 4, 10, 556, 40632345 };

    int[] unsellableItems = new int[] { 995 };

    /**

     * Forgot what i was going to use this for

     */

    @SuppressWarnings("unused")

    private int shopId = 0;


    /**

     * Create a new shop and add the items to it.

     * 

     * @param id

     * 

     * @param generalStore

     *            Whether you want it to be a general store or not.

     */

    public Shop(int id, boolean isGeneralStore, int[] items, int[] amounts) {

        this.shopId = id;

        this.generalStore = isGeneralStore;

        for (int itemSlot = 0; itemSlot < items.length; itemSlot++) {

            itemsInShop.set(itemsInShop.getFreeSlot(), new Item(

                    items[itemSlot], amounts[itemSlot]));

        }

        // startRestocking();

    }


    /**

     * Little thing to restock every x amount of seconds. I wasn't sure about it

     * so i didn't use it.

     */

    @SuppressWarnings("unused")

    private void startRestocking() {

        World.getInstance().registerEvent(new Event(restockTime) {

            public void execute() {

                for (int i = 0; i < itemsInShop.getSize(); i++) {

                    if (itemsInShop.get(i) != null) {

                        itemsInShop

                                .add(new Item(itemsInShop.get(i).getId(), 1));

                    }

                }

                update();

            }

        });


    }


    /**

     * Open a shop for the specified player, then this shop will add the players

     * to it's list of currently viewing players

     * 

     * @param p

     *            The player to open for

     */

    public void openForPlayer(Player p) {

        p.getActionSender().sendConfig(199, -1);

        p.getActionSender().sendConfig2(118, 17);

        p.getActionSender().sendInterface(620, true);

        p.getActionSender().sendInventoryInterface(621);

        p.getActionSender().sendTab(146, 449);

        p.getActionSender().sendRunScript(149, sellOptions, "IviiiIsssss");

        p.getActionSender().sendRunScript(150, invParams, "IviiiIsssssssss");

        p.getActionSender().sendRunScript(150, shopParams, "IviiiIsssssssss");

        p.getActionSender().sendAccessMask(1278, 621, 0, 0, 28);

        p.getActionSender().sendAccessMask(1278, 620, 25, 0, 40);

        p.getActionSender().sendAccessMask(1278, 620, 26, 0, 40);

        p.getActionSender().sendItems(-1, 64209, 93,

                p.getInventory().getContainer());

        p.getActionSender().sendItems(-1, 63746, 556, itemsInShop);

    }


    /**

     * Add a player to the list of currently viewing players

     * 

     * @param player

     *            The player to add

     */

    public void addPlayer(Player player) {

        playersViewing.add(player);

    }


    /**

     * Remove a player from the list of players viewing

     * 

     * @param player

     *            The player to remove

     */

    public void removePlayer(Player player) {

        playersViewing.remove(player);

    }


    /**

     * Handle options sent from the client

     * 

     * @param p

     *            The player that did the option

     * @param interfaceId

     *            The interface they're on

     * @param buttonId

     *            The button they clicked

     * @param buttonId2

     *            Could be another button they clicked

     * @param packetId

     *            The packet sent to us.

     */

    public void handleOption(Player p, int interfaceId, int buttonId,

            int buttonId2, int packetId) {

        switch (interfaceId) {

        case 620:

            switch (buttonId) {

            case 18:

                removePlayer(p);

                p.shopId = -1;

                p.getActionSender().sendCloseInterface();

                p.getActionSender().sendCloseInventoryInterface();

                break;

            default:

                switch (packetId) {

                case 216:

                    p.sm(itemsInShop.get(buttonId2).getDefinition().getName()

                            + " costs "

                            + formatPrice(GameEngine.prices

                                    .getMaximumPrice(itemsInShop.get(buttonId2)

                                            .getDefinition().getId()))

                            + " coins.");

                    break;

                case 19:

                    buyItem(p, itemsInShop.get(buttonId2).getId(), 1);

                    break;

                case 193:

                    buyItem(p, itemsInShop.get(buttonId2).getId(), 5);

                    break;

                case 76:

                    buyItem(p, itemsInShop.get(buttonId2).getId(), 10);

                    break;

                case 173:

                    buyItem(p, itemsInShop.get(buttonId2).getId(), 50);

                    break;

                case 3:

                    p.sm(itemsInShop.get(buttonId2).getDefinition()

                            .getExamine());

                    break;

                }

            }

            break;

        case 621:

            if (buttonId == 0) {

                Item definition = p.getInventory().getContainer()

                        .get(buttonId2);

                int itemId = definition.getId();

                switch (packetId) {

                case 216:

                    p.sm(definition.getDefinition().getName()

                            + " costs "

                            + formatPrice(GameEngine.prices

                                    .getMinimumPrice(itemId)) + " coins.");

                    break;

                case 19:

                    sellItem(p, itemId, 1);

                    break;

                case 193:

                    sellItem(p, itemId, 5);

                    break;

                case 76:

                    sellItem(p, itemId, 10);

                    break;

                case 173:

                    sellItem(p, itemId, 50);

                    break;

                case 3:

                    p.sm(definition.getDefinition().getExamine());

                    break;

                }

            }

            break;

        }

    }


    /**

     * Sells an item to the store

     * 

     * @param p

     *            the player who sold

     * @param itemId

     *            The item they're selling

     * @param amount

     *            The amount of the item they're selling

     */

    private void sellItem(Player p, int itemId, int amount) {

        int price = GameEngine.prices.getMinimumPrice(itemId) * amount;

        if (!p.getInventory().contains(itemId)) {

            return;

        }

        if (!p.getInventory().hasRoomFor(995, price)) {

            p.sm("Not enough space in your inventory");

            return;

        }

        // One more calculation for if you have notes of the item and regular

        // items needs to be here.

        if (p.getInventory().getContainer().getNumberOff(itemId) < amount) {

            if (ItemDefinition.forId(itemId).isNoted()

                    || ItemDefinition.forId(itemId).isStackable()) {

                amount = p.getInventory().lookup(itemId).getAmount();

            } else {

                amount = p.getInventory().getContainer().getNumberOff(itemId);

            }

            price = GameEngine.prices.getMinimumPrice(itemId) * amount;

            p.sm("Your inventory does not contain that many.");

        }

        if (itemsInShop.freeSlots() <= 0) {

            p.sm("The shop is full");

        }

        for (int i = 0; i < unsellableItems.length; i++) {

            if (unsellableItems[i] == itemId) {

                p.sm("You cannot sell that item to this shop");

                return;

            }

        }

        if (!itemsInShop.contains(new Item(itemId, 1)) && !generalStore) {

            p.sm("You cannot sell that item to this shop");

            return;

        }

        itemsInShop.add(new Item(itemId, amount));

        p.getInventory().deleteItem(itemId, amount);

        p.getInventory().addItem(995, price);

        update();


    }


    /**

     * Update the items in the shop for every player viewing this shop

     */

    private void update() {

        for (Player p : playersViewing) {

            openForPlayer(p); // Send items wouldn't work alone lol.

        }

    }


    /**

     * Buys an item from the store

     * 

     * @param p

     *            The player trying to buy

     * @param id

     *            The id of the item they're trying to buy

     * @param amount

     *            The amount of the item they're trying to buy

     */

    private void buyItem(Player p, int id, int amount) {

        Item item = new Item(id, 1);

        int price = GameEngine.prices.getMaximumPrice(id) * amount;

        if (p.getInventory().getFreeSlots() < amount

                && !(item.getDefinition().isNoted() || item.getDefinition()

                        .isStackable())) {

            p.sm("Not enough space in your inventory for that many.");

            if (p.getInventory().getFreeSlots() == 0) {

                return;

            }

            amount = p.getInventory().getFreeSlots();

            price = GameEngine.prices.getMaximumPrice(id) * amount;

        }

        if (itemsInShop.getNumberOff(id) < amount) {

            p.sm("The shop has run out of stock of that item.");

            amount = itemsInShop.getNumberOff(id);

            price = GameEngine.prices.getMaximumPrice(id) * amount;

        }

        if (p.getInventory().getContainer().getNumberOff(995) < price) {

            p.sm("You do not have enough coins for that many.");

            price = price / p.getInventory().getContainer().getNumberOff(995);

            if (price == 0) {

                return;

            }

        }

        p.getInventory().deleteItem(995, price);

        p.getInventory().addItem(id, amount);

        itemsInShop.remove(new Item(id, amount));

        if (!generalStore) {

            if (itemsInShop.getNumberOff(id) == 0) {

                itemsInShop.add(new Item(id, 1));

            }

        }

        update();

    }


    /**

     * Format a number into a string with commas

     * 

     * @param price

     *            The original number

     * @return The formatted string

     */

    public String formatPrice(int price) {

        return NumberFormat.getInstance().format(price);

    }


}

Ok go into GameEngine.java and replace, well actually im not sure what the old one looked like. but if it looks different add this,
public static ShopHandler shopHandler = new ShopHandler(); 

Then below this
        logger.info("Loading item definitions...");

        ItemDefinition.load();

        logger.info("Loading npc definitions...");

        NPCDefinition.load();

add this,
        shopHandler.loadShops();
Oh and add this to walkpackethandler.java
        if (player.shopId != -1) {

            GameEngine.shopHandler.getShop(player.shopId).removePlayer(player);

            player.shopId = -1;

        }

Somewhere in here
        if(player.frozen > 0) {

player.getActionSender().sendMessage("You are frozen!");

        player.getWalkingQueue().reset();

            return;

        }

        if(player.resting == true) {

            player.resting = false;

            player.animate(5748);

        }

        if (player.cantWalk == true)

            return;

        if(player.takingitem > 0) {

            return;

        }

        if(player.isBusy()) {

            return;

        }

AND change this in ActionButtonPacketHandler.java (something that looks similar to this, i replaced my old one)
           case 620:

           case 621:

                GameEngine.shopHandler.handleoption(player,interfaceId,buttonId,buttonId2,packet.getId());

                break;

To this
           case 620:

           case 621:

                GameEngine.shopHandler.getShop(player.shopId).handleOption(player,interfaceId,buttonId,buttonId2,packet.getId());

                break;

If it's bad, oh well, it's better than the old one, and maybe the help threads will die down :D

Edited by Steve
Link to comment
Share on other sites

  • Replies 78
  • Created
  • Last Reply

Top Posters In This Topic

i just dont understand where to add this

.put(1, is the shop id, (like openshop(p, 2)

new Shop(1, The 1 is the shop id,

true, Set to true or false if you want it to be a general store

new int[] { 4151, 10551 }, new int[] {

1000, 1000 }, These are the items and the amounts. 1st one (4151, 10551) is the items, the next one is amounts. To use like your old system you can do stuff like GameEngine.shops.Armor, GameEngine.shops.ArmorN.

Then create a new Class in com.rs2hd.content and name it Shop.java

Then add this in it,

"Then create a new Class in com.rs2hd.content and name it Shop.java"

i just dont understand this he means the normal shop.java in content?

Link to comment
Share on other sites

Yes im pretty sure i did. I pasted this

        if (player.shopId != -1) {

            GameEngine.shopHandler.getShop(player.shopId).removePlayer(player);

            player.shopId = -1;

        }
Right in here.
        if(player.frozen > 0) {

player.getActionSender().sendMessage("You are frozen!");

        player.getWalkingQueue().reset();

            return;

        }

        if(player.resting == true) {

            player.resting = false;

            player.animate(5748);

        }

        if (player.cantWalk == true)

            return;

        if(player.takingitem > 0) {

            return;

        }

        if(player.isBusy()) {

            return;

        }
So it looks like this.
if(player.frozen > 0) {

player.getActionSender().sendMessage("You are frozen!");

		player.getWalkingQueue().reset();

			return;

		}

		if(player.resting == true) {

			player.resting = false;

			player.animate(5748);

		}

        if (player.shopId != -1) {

            GameEngine.shopHandler.getShop(player.shopId).removePlayer(player);

            player.shopId = -1;

        }

		if (player.cantWalk == true)

			return;

		if(player.takingitem > 0) {

			return;

		}

       		if (player.shopId != -1) {

            	GameEngine.shopHandler.getShop(player.shopId).removePlayer(player);

            	player.shopId = -1;

        	}

		if(player.isBusy()) {

			return;

		}

But, yet i still cant walk?

Edited by owner jacob
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

×
×
  • Create New...