Thread: [Asteria 2.0] Better Shop Opening

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 [Asteria 2.0] Shop Opening through JSON 
    NirvanaRS

    Join Date
    Mar 2013
    Posts
    97
    Thanks given
    26
    Thanks received
    21
    Rep Power
    20
    Instead of handling shop opening in the separate cases in the NpcClickAction packet, you can just store them in the shop's JSON file.

    Spoiler for What you're changing:
    Before:
    Code:
    [
        {
           "id": 1,
           "name": "Pk Point Store",
           "items": [
            {
              "id": 1931,
              "amount": 15
            },
            {
              "id": 1935,
              "amount": 15
            },
            {
              "id": 1735,
              "amount": 15
            },
            {
              "id": 7156,
              "amount": 15
            }
           ],
           "restock": true,
           "can-sell-items": false,
           "currency": "PK_POINTS"
        }
    ]
    Then in NpcClickAction:
    Code:
    case 1303:
    case 548:
    Shop.getShop(1).openShop(player);
    break;

    After:
    Code:
    [
        {
           "id": 1,
           "name": "Pk Point Store",
           "npcs": [
               1303, 548
           ],
           "items": [
            {
              "id": 1931,
              "amount": 15
            },
            {
              "id": 1935,
              "amount": 15
            },
            {
              "id": 1735,
              "amount": 15
            },
            {
              "id": 7156,
              "amount": 15
            }
           ],
           "restock": true,
           "can-sell-items": false,
           "currency": "PK_POINTS"
        }
    ]



    In Shop.java add these:
    Code:
    	/**
    	 * Gets the shop based on an npc id.
    	 * 
    	 * @param npcId
    	 * @return
    	 */
    	public static boolean openShopForNpc(Player player, Npc npc) {
    		if (shopNpcs.containsKey(npc.getNpcId())) {
    			getShop(shopNpcs.get(npc.getNpcId())).openShop(player);
    			return true;
    		}
    		return false;
    	}
    Code:
    	/** A map of npcs and their respective shop. */
    	private static Map<Integer, Integer> shopNpcs = new HashMap<Integer, Integer>();
    Change your constructor parameters to:
    Code:
    public Shop(int index, String name, int[] npcs, Item[] items,
    			boolean restockItems, boolean sellItems, Currency currency)
    Add this in the constructor:
    Code:
    		for (int npcIds : npcs)
    			shopNpcs.put(npcIds, index);
    Change your load method in the parseShops() to:
    Code:
    			@Override
    			public void load(JsonObject reader, Gson builder) {
    				Shop shop = new Shop(reader.get("id").getAsInt(), reader.get(
    						"name").getAsString(), builder.fromJson(
    						reader.get("npcs").getAsJsonArray(), int[].class),
    						builder.fromJson(reader.get("items").getAsJsonArray(),
    								Item[].class), reader.get("restock")
    								.getAsBoolean(), reader.get("can-sell-items")
    								.getAsBoolean(), Currency.valueOf(reader.get(
    								"currency").getAsString()));
    
    				for (int id : NO_SHOP_ITEMS) {
    					if (shop.getShopContainer().contains(id)) {
    						throw new IllegalStateException(
    								"Item not allowed to be sold in shops: "
    										+ ItemDefinition.getDefinitions()[id]
    												.getItemName());
    					}
    				}
    
    				Shop.getShops()[shop.getIndex()] = shop;
    			}
    Finally, in DecodeNpcClickAction add:
    Code:
    if (Shop.openShopForNpc(player, interact))
    							return;
    All done


    Looking for a more stable base? Try Asteria 2.0
    Reply With Quote  
     

  2. #2  
    NirvanaRS

    Join Date
    Mar 2013
    Posts
    97
    Thanks given
    26
    Thanks received
    21
    Rep Power
    20
    Seems like more of a snippet, to be honest. Move if perceived the same.


    Looking for a more stable base? Try Asteria 2.0
    Reply With Quote  
     

  3. #3  
    Banned
    Join Date
    Jul 2014
    Posts
    437
    Thanks given
    200
    Thanks received
    76
    Rep Power
    0
    Good job.
    Reply With Quote  
     

  4. Thankful user:


  5. #4  
    NirvanaRS

    Join Date
    Mar 2013
    Posts
    97
    Thanks given
    26
    Thanks received
    21
    Rep Power
    20
    Appreciated ^


    Looking for a more stable base? Try Asteria 2.0
    Reply With Quote  
     

  6. #5  
    Registered Member
    CTucker's Avatar
    Join Date
    Oct 2008
    Posts
    2,422
    Thanks given
    263
    Thanks received
    281
    Rep Power
    343
    Not sure how this is better, you didn't improve the system at all. All you did was remove the switch statement and stuff the data into the JSON file.

    Either way, gj i guess.
    Reply With Quote  
     

  7. #6  
    Registered Member
    Join Date
    Dec 2010
    Posts
    354
    Thanks given
    17
    Thanks received
    61
    Rep Power
    47
    Quote Originally Posted by Jellal View Post
    Not sure how this is better, you didn't improve the system at all. All you did was remove the switch statement and stuff the data into the JSON file.

    Either way, gj i guess.
    What? How is it not better instead of filling his first click NPC packet he can just put the NPC ids in the Json file.
    Reply With Quote  
     

  8. #7  
    Registered Member
    CTucker's Avatar
    Join Date
    Oct 2008
    Posts
    2,422
    Thanks given
    263
    Thanks received
    281
    Rep Power
    343
    Quote Originally Posted by dan0194 View Post
    What? How is it not better instead of filling his first click NPC packet he can just put the NPC ids in the Json file.
    Did you not read my post?

    All you did was remove the switch statement and stuff the data into the JSON file.
    This isn't an improvement to the system itself, it's not "better", it's not more efficient(for the application), it's just simplified(for the user).
    Reply With Quote  
     

  9. #8  
    Banned [Asteria 2.0] Better Shop Opening Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    Quote Originally Posted by dan0194 View Post
    What? How is it not better instead of filling his first click NPC packet he can just put the NPC ids in the Json file.
    not very dynamic, for example if you only want NPCs to only be able to open a shop if you've completed a quest. that's why I never did it like that in the first place
    Reply With Quote  
     

  10. Thankful user:


  11. #9  
    NirvanaRS

    Join Date
    Mar 2013
    Posts
    97
    Thanks given
    26
    Thanks received
    21
    Rep Power
    20
    Quote Originally Posted by lare96 View Post
    not very dynamic, for example if you only want NPCs to only be able to open a shop if you've completed a quest. that's why I never did it like that in the first place
    Easily dynamic. Just modify to something like such:
    Code:
    	/**
    	 * Gets the shop based on an npc id.
    	 * 
    	 * @param npcId
    	 * @return
    	 */
    	public static boolean openShopForNpc(Player player, Npc npc) {
    		if (shopNpcs.containsKey(npc.getNpcId())) {
                                  //special cases
                                   switch(npc.getNpcId()) {
                                          case xx:
                                                if(questrequirementnotmet)
                                                          return false;
                                   }
    			getShop(shopNpcs.get(npc.getNpcId())).openShop(player);
    			return true;
    		}
    		return false;
    	}
    And I'd consider it more dynamic in the long run because it's a lot easier to reload a new shop in while the server is running by parsing a JSON file.


    Looking for a more stable base? Try Asteria 2.0
    Reply With Quote  
     

  12. #10  
    Banned [Asteria 2.0] Better Shop Opening Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    Quote Originally Posted by Gurkha View Post
    Easily dynamic. Just modify to something like such:
    Code:
    	/**
    	 * Gets the shop based on an npc id.
    	 * 
    	 * @param npcId
    	 * @return
    	 */
    	public static boolean openShopForNpc(Player player, Npc npc) {
    		if (shopNpcs.containsKey(npc.getNpcId())) {
                                  //special cases
                                   switch(npc.getNpcId()) {
                                          case xx:
                                                if(questrequirementnotmet)
                                                          return false;
                                   }
    			getShop(shopNpcs.get(npc.getNpcId())).openShop(player);
    			return true;
    		}
    		return false;
    	}
    And I'd consider it more dynamic in the long run because it's a lot easier to reload a new shop in while the server is running by parsing a JSON file.
    i just see it as more personal preference rather than an actual improvement
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

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


User Tag List

Similar Threads

  1. Replies: 6
    Last Post: 01-30-2012, 03:30 AM
  2. Making Shop open for Donor+ only---Requested
    By DatguyJay in forum Tutorials
    Replies: 8
    Last Post: 01-08-2012, 10:46 PM
  3. Replies: 2
    Last Post: 05-18-2011, 09:49 PM
  4. Replies: 0
    Last Post: 01-12-2010, 12:23 AM
  5. Connection lost when shop opens?
    By Enjoi in forum Help
    Replies: 4
    Last Post: 04-26-2009, 04:31 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
  •