Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //There are more imports. These are just the ones that are not part of the core bukkit API.
- import EMC.getRes //Is not called this. Explained later.
- import com.Acrobot.Breeze.Utils.MaterialUtil
- import com.Acrobot.Breeze.Utils.InventoryUtil
- import com.Acrobot.ChestShop.Events.ShopCreatedEvent
- /*
- assume file named shops.db
- contains array of all blocks and items. Let's call this items
- The index of each entry corresponds to the item that entry represents.
- items[1] contains all shops selling stone,
- items[13] contains all shops selling gravel,
- items[262] contains all shops selling arrows.
- items[1] is another array, containing all of the shops selling that item (not sorted in any order)
- items[1][0] is the first shop added (programmers count starting with 0)
- items[1][1] is the second shop added
- and so on.
- each of those are a third array, containing all of the data associated with that shop.
- items[1][0][price] contains the cost of dirt from the 1st dirt shop.
- items[1][0][chestloc] contains the location of the chest
- */
- on event=ShopCreatedEvent():
- //This runs whenever ShopCreatedEvent() happens.
- //The event gets stored in the event variable.
- array tosave[] //contains stuff we will write to shop DB
- signtext=event.getSignLines() //Gets text on shop sign
- qty=signtext[2] //gets number of items sold at a time
- price=signtext[3] //and how much they sell for.
- item=signtext[4] //and the item being sold.
- itemid=MaterialUtil.getMaterial(item)//gets item id from item name
- tosave.append(qty)
- tosave.append(price) //Add stuff to save
- chest=event.getChest() //Gets chest attached to shop
- chestblock=chest.getBlock() //Gets block occupied by chest
- chestloc=chestblock.getLocation()//Gets the location of the chest
- tosave.append(chestloc) //Add stuff to save
- /*
- There is a function that EMC uses to determine what res a block is in.
- This is used to determine if a player has permissions to destroy that block.
- I don't know what it is called, so I will call it EMC.getRes().
- We will feed it the chest's location, and it will tell us what res it's in.
- If this is part of a larger function, create a separate function for it.
- */
- chestres=EMC.getRes(chestloc) //get res id from chest location
- tosave.append(chestres) //Add stuff to save
- /*
- we are done generating stuff. Now let's save it!
- NOTE: This step currently reads and writes the ENTIRE database file everytime a shop is created.
- This should be changed so it only reads and writes what it needs to.
- This step should also probably be run in a separate thread.
- */
- items=load(shops.db) //load shops database from shops.db
- items[itemid].append(tosave)//Add shop to database
- write(shops.db,items) //write shops database to shops.db
- def getContents(chest,itemid):
- contents=chest.getBlockInventory()//Gets contents of chest
- amount=getAmount(contents, ItemStack(itemid))//Gets amount in stock
- return amount //returns amount of items in chest
- def getChestFromLoc(location):
- block=location.getBlock() //get block at location
- if (block.getType() == Material.CHEST): //makes sure it is a chest. If we skip this, it might have changed, and the plugin will crash!
- return (Chest) block.getState() //returns chest item at location.
- on event=onCommand(/shops):
- //runs when user types /shops.
- //event is stored in event.
- args=onCommand.args //gets requested item
- if args.length=1: //if user typed in [item], assume they want to buy it.
- listShopsSelling(sender,args[0]) //list all shops selling item
- else if args.length=2: //this means they typed [buy|sell] [item]
- if args[0]="buy": //they want to buy item
- return listShopsSelling(sender,args[1]) //list all shops selling item, return true if success
- else if args[0]="sell": //they want to sell item
- return listShopsBuying(sender,args[1]) //list all shops buying item, return true if success
- else: //Problem! They typed something other than buy or sell!
- return false //Display help message
- else: //They typed 0 arguments, or more than 2
- sender.sendMessage("Wrong number of arguments!")
- return false //Display help message
- return false //We will only get here if something went wrong. Display help message.
- def listShopsSelling(sender,item) //prints list of shops selling item
- itemid=MaterialUtil.getMaterial(item)//gets item id from item name
- items=load(shops.db) //load shops database from shops.db
- sender.sendMessage("Stores selling "+item+":") //Print info
- sender.sendMessage("§nRes # |Qty. |Price |# in stock") //print table headers
- for shop in items[itemid]:
- //run this once for every shop selling the item
- loc=shop[chestloc] //reads location of chest
- chest=getChestFromLoc(loc)//gets chest at location
- inv=getContents(chest,itemid) //gets amount of item in chest
- res=shop[res]
- resstr=formatstr(str(res)) //converts res to string and properly formats it.
- qty=shop[qty]
- qtystr=formatstr(str(qty)) //converts qty to string and properly formats it.
- price=shop[price]
- pricestr=formatstr(str(price)) //converts price to string and properly formats it.
- invstr=formatstr(str(inv)) //converts inv to string and properly formats it.
- if inv>=qty //if there are enough items to sell,
- sender.sendMessage(resstr+"|"+qtystr+"|"+pricestr+"|"+invstr) //Print table row
- def listShopsBuying(sender,item): //prints list of shops buying item
- //not implemented yet.
- def formatstr(str): //properly formats string
- trimmed=str.substring(0, Math.min(str.length(), 9)) //trims the string down to 10 characters
- padded=String.format("%10s", trimmed) //pads the string to 10 characters
- return padded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement