Advertisement
BobMe

Minecraft Code Connection - Currency

Aug 8th, 2021 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // MAKE SURE TO SAVE YOUR CURRENCY BY OVERWRITING THE CURRENCY VARIABLE BELOW THIS LINE BEFORE YOU EXIT MINECRAFT
  2. let currency = 0
  3. let selloff = 0.5 // selling an item is worth this value times the buy value of the item
  4. let boom = 100 // price boom, all prices and value of currency will be mutiplied by this number
  5. let gamble = true // set to true to enable to ability to gamble your earnings (INCREDIBLY UNBALANCED)
  6.  
  7. player.onChat(";bal", function() {
  8.     chat("§aYou have: §l$"+currency.toString(),false)
  9. })
  10.  
  11. let marketplace_items = [
  12.     [IRON_INGOT,"iron",1,"iron_ingot"],
  13.     [DIAMOND,"diamond",13,"diamond"],
  14.     [GOLD_INGOT,"gold",2,"gold_ingot"],
  15.     [LOG_OAK,"log",0.05,"log"],
  16.     [STICK,"stick",0.0065,"stick"],
  17.     [PLANKS_OAK,"planks",0.015,"planks"],
  18.     [LAPIS_LAZULI_BLOCK,"lapisblock",0.5*9,"lapis_block"],
  19.     [DIRT,"dirt",0.01,"dirt"],
  20.     [GRASS,"grass",0.025,"grass"],
  21.     [COBBLESTONE,"cobblestone",0.015,"cobblestone"],
  22.     [STONE,"stone",0.03,"stone"],
  23.     [STONE_BRICKS,"stonebricks",0.035,"stone_bricks"],
  24.     [STRING,"string",0.35,"string"],
  25.     [GUNPOWDER,"gunpowder",0.45,"gunpowder"],
  26.     [LEATHER,"leather",0.15,"leather"],
  27.     [LEAD,"lead",0.9,"lead"],
  28.     [BONE,"bone",0.25,"bone"],
  29.     [ROTTEN_FLESH,"rottenflesh",0.015,"rotten_flesh"],
  30.     [INK_SAC,"inksac",0.06,"ink_sac"],
  31.     [BONE,"bone",0.25,"bone"],
  32.     [GOLDEN_APPLE,"goldenapple",17,"golden_apple"],
  33.     [SPIDER_EYE,"spidereye",0.03,"spider_eye"],
  34.     [GRAVEL,"gravel",0.02,"gravel"],
  35.     [FLINT,"flint",0.05,"flint"],
  36.     [IRON_NUGGET,"ironnugget",0.11,"iron_nugget"],
  37.     [GOLD_NUGGET,"goldnugget",0.22,"gold_nugget"],
  38.     [EMERALD,"emerald",6,"emerald"],
  39.     [SHULKER_SHELL,"shulkershell",7,"shulker_shell"],
  40.     [ICE,"ice",0.05,"ice"],
  41.     [PACKED_ICE,"packedice",0.50,"packed_ice"],
  42.     [BLUE_ICE,"blueice",5,"BLUE_ICE"], // may need to rebalance this
  43.     [OAK_SAPLING,"sapling",0.07,"sapling"],
  44.     [OBSIDIAN,"obsidian",1.5,"obsidian"],
  45.     [COAL,"coal",0.25,"coal"],
  46.     [SAND,"sand",0.025,"sand"],
  47.     [SLIMEBALL,"slimeball",0.4,"slimeball"],
  48.     [REDSTONE,"restone",0.25,"redstone"],
  49.     [REDSTONE_BLOCK,"restoneblock",0.27*9,"redstone_block"],
  50.     [WOOL,"wool",0.16,"wool"],
  51.     [FEATHER,"feather",0.15,"feather"],
  52.     [ARROW,"arrow",0.21,"arrow"],
  53. ]
  54.  
  55. let marketplace_mobs = [
  56.     [CHICKEN,0.05,"chicken"],
  57.     [PIG,0.08,"pig"],
  58.     [COW,0.08,"cow"],
  59.     [ZOMBIE,0.15,"zombie"],
  60.     [SKELETON,0.2,"skeleton"],
  61.     [CREEPER,0.15,"creeper"],
  62.     [DONKEY,0.1,"donkey"],
  63.     [HORSE,0.11,"horse"],
  64.     [SHEEP,0.08,"sheep"],
  65.     [DROWNED,0.15,"drowned"],
  66.     [BAT,0.15,"bat"],
  67.     [RABBIT,0.05,"rabbit"],
  68.     [SPIDER,0.1,"spider"],
  69.     [ZOMBIE_VILLAGER,0.2,"zombie villager"],
  70.     [ENDERMAN,0.3,"enderman"],
  71.     [SILVERFISH,0.2,"silverfish"],
  72.     [COD,0.02,"cod fish"],
  73.     [SALMON,0.02,"salmon"],
  74.     [TROPICAL_FISH,0.02,"tropical fish"],
  75.     [PUFFERFISH,0.1,"puffer fish"],
  76.     [SQUID,0.05,"squid"],
  77.     [STRAY,0.25,"stray"],
  78.     [HUSK,0.15,"husk"],
  79.     [BEE,-0.3,"bee"],
  80.     [LLAMA,0.11,"llama"],
  81.     [WANDERING_TRADER,-1,"wandering trader"],
  82.     [PANDA,-0.5,"panda"],
  83.     [PARROT,-0.5,"parrot"],
  84.     [WITHER_SKELETON,0.5,"wither skeleton"],
  85.     [GHAST,0.5,"ghast"],
  86.     [BLAZE,0.6,"blaze"],
  87.     [SLIME,0.3,"slime"],
  88.     [LAVA_SLIME,0.5,"magma cube"],
  89.     [WITHER,-2.5,"iron golem"],
  90.     [WOLF,-0.25,"wolf"],
  91.     [CAT,-0.5,"cat"],
  92.     [OCELOT,-0.5,"ocelot"],
  93.     [FOX,-0.5,"fox"],
  94.     [] // extra space needed for resolving weirdness
  95. ]
  96. function chat(str:string,self:boolean) {
  97.     if (self) {
  98.         player.execute('tellraw @s {"rawtext":[{"text":"'+str+'"}]}')
  99.     } else {
  100.         player.execute('tellraw @a {"rawtext":[{"text":"'+str+'"}]}')
  101.     }
  102. }
  103.  
  104. function doDing() {
  105.     loops.runInBackground(function() {
  106.         player.execute("playsound note.pling @s ~ ~ ~ 1 2")
  107.         player.execute("playsound note.pling @s ~ ~ ~ 1 2.5")
  108.     })
  109. }
  110.  
  111. for (let i = 0; i < marketplace_mobs.length; i++) {
  112.     if (i != marketplace_mobs.length-1) {
  113.         let li = i
  114.         loops.runInBackground(function() {
  115.             loops.forever(function() { // because we're only allowed to use let and const, and not var, we have to reset variables using while loops (var would of been really nice to use here )
  116.                 let tig = false        // we also could just reuse a mob event, but for some reason they fire twice, once upon mob death, and another after the code inside the event has finished running.
  117.                 let an:any = marketplace_mobs[li][0]
  118.                 let an1:any = marketplace_mobs[li][1]
  119.                 let an2:any = marketplace_mobs[li][2]
  120.                 mobs.onMobKilled(mobs.monster(an), function() {
  121.                     if (tig == false) {
  122.                         tig = true
  123.                         let worth = an1*boom
  124.                         if (worth > 0) {
  125.                             chat("§a§l+$"+worth.toString()+" §r§afor killing a §g§l"+an2,true)
  126.                             currency+=worth
  127.                         } else {
  128.                             worth = worth *-1
  129.                             chat("§4You were fined §l$"+worth.toString()+"§r§4 for killing a §g§l"+an2,true)
  130.                             currency-=worth
  131.                         }
  132.                     }
  133.                 })  
  134.                 while (tig == false) {
  135.                     loops.pause(100)
  136.                 }  
  137.             })
  138.        })
  139.     } else {
  140.         break
  141.     }
  142. }
  143.  
  144. function isItem(num:number) {
  145.     for (let i = 0; i < marketplace_items.length; i++) {
  146.         if (num == marketplace_items[i][0]) {
  147.             let re:number = i
  148.             return i
  149.         }
  150.     }
  151.     return null
  152. }
  153.  
  154. function findItem(str:string) {
  155.     for (let i = 0; i < marketplace_items.length; i++) {
  156.         if (str.toLowerCase() == marketplace_items[i][1]) {
  157.             return i
  158.         }
  159.     }
  160.     return null
  161. }
  162.  
  163. player.onChat(";tp", function() {
  164.     chat("§gAgent has been teleported to your position",false)
  165.     agent.teleportToPlayer()
  166. })
  167.  
  168. if (gamble) {
  169.     player.onChat(";gamble", function(num:number) {
  170.         let arg = player.getChatArg(0)
  171.         let pars = parseFloat(arg)
  172.         if (isNaN(pars) == true || pars < 0) {
  173.             chat("§4Value must be a valid number.",false)
  174.         } else {
  175.             if (pars > currency) {
  176.                 chat("§4You do not have §l$"+pars.toString()+"§r§4. You have: §l$"+currency.toString(),false)
  177.             } else {
  178.                 let r = randint(0,1)
  179.                 if (r == 0) {
  180.                     let botroll = randint(1, 12)
  181.                     let yourroll = randint(1, 12)
  182.                     if (botroll == yourroll || botroll > yourroll) {
  183.                         currency -= pars
  184.                         chat("§4§l------------------------\n§r§cYou lost: §l$"+pars.toString()+"\n§r§cYour roll: §l"+yourroll.toString()+"§r§c | Bot roll: §l"+botroll.toString()+"§r§4§l\n------------------------",false)
  185.                     } else {
  186.                         let winpercentage = randint(50, 150)
  187.                         let w = winpercentage/100
  188.                         pars = pars*w
  189.                         currency += pars
  190.                         chat("§2§l------------------------\n§r§aYou won: §l$"+pars.toString()+"\n§r§aYour roll: §l"+yourroll.toString()+"§r§a | Bot roll: §l"+botroll.toString()+"\n§r§aYou won §l"+winpercentage.toString()+"%§r§a of your bet"+"§r§2§l\n------------------------",false)
  191.                     }
  192.                 } else if(r == 1) {
  193.                     let pos1 = positions.groundPosition(pos(1, 100, 2).toWorld())
  194.                     let pos2 = positions.groundPosition(pos(1, 100, 0).toWorld())
  195.                     let pos3 = positions.groundPosition(pos(1, 100, -2).toWorld())
  196.                     let bet = randint(1, 3)
  197.                     let tochoose:number = 0
  198.                     let mult = randint(2, 4)
  199.                     chat("§gIn front of you are 3 dirt piles.  Within one of them contains your bet. If you choose correctly you will earn §l"+mult.toString()+"x§r§g your bet.", false)
  200.                     blocks.place(COARSE_DIRT, pos1.add(pos(0,-1,0)))
  201.                     player.execute("summon splash_potion "+pos1.add(pos(0,1,0)))
  202.                     blocks.place(COARSE_DIRT, pos2.add(pos(0,-1,0)))
  203.                     player.execute("summon splash_potion "+pos2.add(pos(0,1,0)))
  204.                     blocks.place(COARSE_DIRT, pos3.add(pos(0,-1,0)))
  205.                     player.execute("summon splash_potion "+pos3.add(pos(0,1,0)))
  206.                     loops.runInBackground(function() {
  207.                         while (blocks.testForBlock(COARSE_DIRT, pos1.add(pos(0,-1,0))) && tochoose == 0) {
  208.                             loops.pause(100)
  209.                         }
  210.                         if (tochoose == 0) {
  211.                             tochoose = 1
  212.                         }
  213.                     })
  214.                     loops.runInBackground(function() {
  215.                         while (blocks.testForBlock(COARSE_DIRT, pos2.add(pos(0,-1,0))) && tochoose == 0) {
  216.                             loops.pause(100)
  217.                         }
  218.                         if (tochoose == 0) {
  219.                             tochoose = 2
  220.                         }
  221.                     })
  222.                     loops.runInBackground(function() {
  223.                         while (blocks.testForBlock(COARSE_DIRT, pos3.add(pos(0,-1,0))) && tochoose == 0) {
  224.                             loops.pause(100)
  225.                         }
  226.                         if (tochoose == 0) {
  227.                             tochoose = 3
  228.                         }
  229.                     })
  230.                     while (tochoose == 0) {
  231.                         loops.pause(500)
  232.                     }
  233.                     blocks.place(GRASS, pos1.add(pos(0,-1,0)))
  234.                     blocks.place(GRASS, pos2.add(pos(0,-1,0)))
  235.                     blocks.place(GRASS, pos3.add(pos(0,-1,0)))
  236.                     if (tochoose == bet) {
  237.                         doDing()
  238.                         let earnings = pars*mult
  239.                         currency += earnings
  240.                         chat("§a§lYou got it! §r§aYou have won: §l$"+earnings.toString(), false)
  241.                     } else {
  242.                         player.execute("playsound ambient.weather.lightning.impact @s ~ ~ ~ 5 0.4")
  243.                         player.execute("playsound block.bell.hit @s ~ ~ ~ 5 0.2")
  244.                         currency -= pars
  245.                         chat("§4§lOops!§r §4Wrong one, you have lost: §l$"+pars.toString(),false)
  246.                     }
  247.                 }
  248.             }
  249.         }
  250.     })
  251. }
  252. player.onChat(";sell", function() {
  253.     if (agent.getItemCount(1) > 0) {
  254.         let tab = []
  255.         let i = 1
  256.         let ongo = true
  257.         while (ongo) {
  258.             if (i != 27) {
  259.                 let itemcount = agent.getItemCount(i)
  260.                 if (itemcount == 0) {
  261.                     ongo = false
  262.                     break
  263.                 } else {
  264.                     let itemtype = agent.getItemDetail(i)
  265.                     tab.push([itemtype,itemcount,i])
  266.                     i += 1
  267.                 }
  268.             } else {
  269.                 let itemcount = agent.getItemCount(i)
  270.                 if (itemcount != 0) {
  271.                     let itemtype = agent.getItemDetail(i)
  272.                     tab.push([itemtype,itemcount,i])
  273.                     i += 1
  274.                     ongo = false
  275.                     break
  276.                 } else {
  277.                     ongo = false
  278.                     break
  279.                 }
  280.             }
  281.         }
  282.         let selltab = []
  283.         let amount = 0
  284.         for (let i = 0; i < tab.length; i++) {
  285.             let h:number = isItem(tab[i][0])
  286.             let hh:(string | number)[] = marketplace_items[h]
  287.             if (h != null) {
  288.                 let d:any = (marketplace_items[h][2])
  289.                 d = parseFloat(d)
  290.                 let num = d
  291.                 currency += ((num*boom)*tab[i][1])*selloff
  292.                 amount += ((num*boom)*tab[i][1])*selloff
  293.                 selltab.push([hh[1],tab[i][1].toString()])
  294.                 agent.setItem(AIR, 0, tab[i][2])
  295.             }
  296.         }
  297.         if (amount == 0) {
  298.             chat("§4§lNothing was sold.",false)
  299.         } else {
  300.             let str = "§aYou've sold: "
  301.             for (let i = 0; i < selltab.length; i++) {
  302.                 str = "§l"+str+selltab[i][0]+"§r§lx"+selltab[i][1]+"§a, "
  303.             }
  304.             str = str.substr(0,str.length-2)
  305.             chat(str,false)
  306.             chat("§a§l+$"+amount.toString(),false)
  307.         }
  308.     } else {
  309.         chat("§4Please put in items into the agent starting from the first slot. Say ;tp to bring your agent to you.",false)
  310.     }
  311. })
  312.  
  313. player.onChat(";buy", function(n:number,n2:number) {
  314.     let item = player.getChatArg(0)
  315.     let num = findItem(item)
  316.     if (num != null) {
  317.         let it = marketplace_items[num]
  318.         let price:any = it[2]
  319.         let itemid = it[3]
  320.         let amount = parseFloat(player.getChatArg(1))
  321.         price = parseFloat(price) * boom
  322.         let setprice:number = price*amount
  323.         if (item != null && amount != null) {
  324.             if (setprice > currency) {
  325.                 chat("§4Insufficient funds; Funds needed: §l$"+setprice.toString()+"§r§4, You have: §l"+currency.toString()+"",false)
  326.             } else {
  327.                 if (amount > 0) {
  328.                     currency -= setprice
  329.                     player.execute("give @s "+itemid+" "+amount)
  330.                     chat("§aYou have bought: §l§g"+item+"§fx"+amount.toString()+"§r§a for §l$"+setprice.toString(),false)
  331.                 } else {
  332.                     chat("§4You cannot buy a negative amount of an item.",false)
  333.                 }
  334.             }
  335.         } else {
  336.             chat("§4Arguments are incorrect; Correct usage: ;buy string integer. For example: ;buy iron 5",false)
  337.         }
  338.     } else {
  339.         chat("§4No item named '"+item+"'. Please check your spelling.",false)
  340.     }
  341. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement