Advertisement
Guest User

EMC /shops

a guest
Sep 10th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.72 KB | None | 0 0
  1. //There are more imports. These are just the ones that are not part of the core bukkit API.
  2. import EMC.getRes //Is not called this. Explained later.
  3. import com.Acrobot.Breeze.Utils.MaterialUtil
  4. import com.Acrobot.Breeze.Utils.InventoryUtil
  5. import com.Acrobot.ChestShop.Events.ShopCreatedEvent
  6.  
  7. /*
  8. assume file named shops.db
  9. contains array of all blocks and items. Let's call this items
  10. The index of each entry corresponds to the item that entry represents.
  11. items[1] contains all shops selling stone,
  12. items[13] contains all shops selling gravel,
  13. items[262] contains all shops selling arrows.
  14.  
  15. items[1] is another array, containing all of the shops selling that item (not sorted in any order)
  16. items[1][0] is the first shop added (programmers count starting with 0)
  17. items[1][1] is the second shop added
  18. and so on.
  19.  
  20. each of those are a third array, containing all of the data associated with that shop.
  21. items[1][0][price] contains the cost of dirt from the 1st dirt shop.
  22. items[1][0][chestloc] contains the location of the chest
  23. */
  24.  
  25. on event=ShopCreatedEvent():
  26. //This runs whenever ShopCreatedEvent() happens.
  27. //The event gets stored in the event variable.
  28.  
  29.     array tosave[] //contains stuff we will write to shop DB
  30.    
  31.     signtext=event.getSignLines() //Gets text on shop sign
  32.     qty=signtext[2]              //gets number of items sold at a time
  33.     price=signtext[3]           //and how much they sell for.
  34.     item=signtext[4]           //and the item being sold.
  35.     itemid=MaterialUtil.getMaterial(item)//gets item id from item name    
  36.    
  37.     tosave.append(qty)
  38.     tosave.append(price)    //Add stuff to save
  39.    
  40.     chest=event.getChest()             //Gets chest attached to shop
  41.     chestblock=chest.getBlock()       //Gets block occupied by chest
  42.     chestloc=chestblock.getLocation()//Gets the location of the chest
  43.        
  44.     tosave.append(chestloc) //Add stuff to save
  45.    
  46.     /*
  47.     There is a function that EMC uses to determine what res a block is in.
  48.     This is used to determine if a player has permissions to destroy that block.
  49.     I don't know what it is called, so I will call it EMC.getRes().
  50.     We will feed it the chest's location, and it will tell us what res it's in.
  51.     If this is part of a larger function, create a separate function for it.
  52.     */
  53.    
  54.     chestres=EMC.getRes(chestloc) //get res id from chest location
  55.    
  56.     tosave.append(chestres) //Add stuff to save
  57.    
  58.     /*
  59.     we are done generating stuff. Now let's save it!
  60.     NOTE: This step currently reads and writes the ENTIRE database file everytime a shop is created.
  61.     This should be changed so it only reads and writes what it needs to.
  62.     This step should also probably be run in a separate thread.
  63.     */
  64.    
  65.     items=load(shops.db)        //load shops database from shops.db
  66.     items[itemid].append(tosave)//Add shop to database
  67.     write(shops.db,items)       //write shops database to shops.db
  68.    
  69. def getContents(chest,itemid):
  70.     contents=chest.getBlockInventory()//Gets contents of chest
  71.     amount=getAmount(contents, ItemStack(itemid))//Gets amount in stock
  72.     return amount //returns amount of items in chest
  73.    
  74. def getChestFromLoc(location):
  75.     block=location.getBlock() //get block at location
  76.     if (block.getType() == Material.CHEST): //makes sure it is a chest. If we skip this, it might have changed, and the plugin will crash!
  77.         return (Chest) block.getState()    //returns chest item at location.
  78.  
  79. on event=onCommand(/shops):
  80.     //runs when user types /shops.
  81.     //event is stored in event.
  82.     args=onCommand.args //gets requested item
  83.     if args.length=1: //if user typed in [item], assume they want to buy it.
  84.         listShopsSelling(sender,args[0]) //list all shops selling item
  85.     else if args.length=2: //this means they typed [buy|sell] [item]
  86.         if args[0]="buy": //they want to buy item
  87.             return listShopsSelling(sender,args[1]) //list all shops selling item, return true if success
  88.         else if args[0]="sell": //they want to sell item
  89.             return listShopsBuying(sender,args[1])  //list all shops buying item, return true if success
  90.         else: //Problem! They typed something other than buy or sell!
  91.             return false //Display help message
  92.     else: //They typed 0 arguments, or more than 2
  93.         sender.sendMessage("Wrong number of arguments!")
  94.         return false //Display help message
  95.     return false //We will only get here if something went wrong. Display help message.
  96.    
  97. def listShopsSelling(sender,item) //prints list of shops selling item
  98.     itemid=MaterialUtil.getMaterial(item)//gets item id from item name  
  99.     items=load(shops.db)        //load shops database from shops.db
  100.     sender.sendMessage("Stores selling "+item+":") //Print info
  101.     sender.sendMessage("§nRes #     |Qty.      |Price     |# in stock") //print table headers
  102.     for shop in items[itemid]:
  103.     //run this once for every shop selling the item
  104.         loc=shop[chestloc]         //reads location of chest
  105.         chest=getChestFromLoc(loc)//gets chest at location
  106.         inv=getContents(chest,itemid)   //gets amount of item in chest
  107.         res=shop[res]
  108.         resstr=formatstr(str(res)) //converts res to string and properly formats it.
  109.         qty=shop[qty]
  110.         qtystr=formatstr(str(qty)) //converts qty to string and properly formats it.
  111.         price=shop[price]
  112.         pricestr=formatstr(str(price)) //converts price to string and properly formats it.
  113.         invstr=formatstr(str(inv)) //converts inv to string and properly formats it.
  114.         if inv>=qty //if there are enough items to sell,
  115.         sender.sendMessage(resstr+"|"+qtystr+"|"+pricestr+"|"+invstr) //Print table row
  116.  
  117. def listShopsBuying(sender,item): //prints list of shops buying item
  118.     //not implemented yet.
  119.  
  120. def formatstr(str): //properly formats string
  121.     trimmed=str.substring(0, Math.min(str.length(), 9)) //trims the string down to 10 characters
  122.     padded=String.format("%10s", trimmed) //pads the string to 10 characters
  123.     return padded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement