chestshops.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. event.load("custom_command");
  2. event.load("inv_click");
  3. event.load("inv_close");
  4. event.load("block_click");
  5. präfix = "§vShop";
  6. wall_signs_tag = block.getTag("minecraft:wall_signs");
  7. invid_to_sign_loc = map.new();
  8. msg("dev", "§bChestShops §rloaded.");
  9. @wait
  10. wait();
  11. ignoreGoto(event);
  12. goto("wait");
  13. @inv_click
  14. cancel = true;
  15. if(inv_slot == 10) {
  16. map.remove(invid_to_sign_loc, inv_id);
  17. inv.close(player);
  18. goto("wait");
  19. }
  20. sign_loc = map.get(invid_to_sign_loc, inv_id);
  21. if(!isAWallSign(sign_loc)) {
  22. map.remove(invid_to_sign_loc, inv_id);
  23. inv.close(player);
  24. speakPrefix(player, präfix, "No longer a shop.");
  25. goto("wait");
  26. }
  27. adminshop = isAdminShop(sign_loc);
  28. buy_price = item.getAmount(inv.getItem(inv, 0)) + item.getAmount(inv.getItem(inv, 1)) + item.getAmount(inv.getItem(inv, 2));
  29. sell_price = item.getAmount(inv.getItem(inv, 7)) + item.getAmount(inv.getItem(inv, 8)) + item.getAmount(inv.getItem(inv, 9));
  30. itemstack = inv.getItem(inv, 4);
  31. chestloc1 = loc.mod(sign_loc, 0, -1, 0);
  32. chestloc2 = block.getSecChest(chestloc1);
  33. if(!isAChest(chestloc1)) {
  34. map.remove(invid_to_sign_loc, inv_id);
  35. inv.close(player);
  36. speakPrefix(player, präfix, "No longer a shop.");
  37. goto("wait");
  38. }
  39. if(inv_slot == 3) { //Buy
  40. //Check players money
  41. if(!hasEnoughMoney(player, buy_price)) {
  42. speakPrefix(player, präfix, "You've not enough money.");
  43. goto("wait");
  44. }
  45. if(!adminshop) {
  46. itemamount2 = 0;
  47. if(chestloc2 != null) {
  48. itemamount2 = block.getItemAmount(chestloc2, false, itemstack);
  49. }
  50. itemamount1 = block.getItemAmount(chestloc1, false, itemstack);
  51. if(itemamount1 + itemamount2 < item.getAmount(itemstack)) {
  52. speakPrefix(player, präfix, "Not enough items in chest.");
  53. goto("wait");
  54. }
  55. }
  56. //Items ins Inventar geben oder droppen
  57. notgiven = player.giveItem(player, itemstack);
  58. if(notgiven != null) {
  59. item.drop(entity.getLocation(player), notgiven);
  60. }
  61. //In einem AdminShop werden keine Items abgelegt
  62. if(!adminshop) {
  63. itemstack2 = item.clone(itemstack);
  64. notremoved = block.subItem(chestloc1, itemstack2);
  65. if(chestloc2 != null) {
  66. block.subItem(chestloc2, notremoved);
  67. }
  68. owner_player_id = read.number(block.getSign(sign_loc, 3));
  69. addMoney(owner_player_id, buy_price);
  70. }
  71. subMoney(player, buy_price);
  72. inv.update(player);
  73. } elseif(inv_slot == 6) { //Sell
  74. if(!adminshop) {
  75. owner_player_id = read.number(block.getSign(sign_loc, 3));
  76. if(!hasEnoughMoney(owner_player_id, sell_price)) {
  77. speakPrefix(player, präfix, "Shop owner has not enough money.");
  78. goto("wait");
  79. }
  80. }
  81. //Items des Spielers checken
  82. if(player.getItemAmount(player, false, itemstack) < item.getAmount(itemstack)) {
  83. speakPrefix(player, präfix, "You've not enough items.");
  84. goto("wait");
  85. }
  86. //Platz in der Kiste checken
  87. if(!adminshop) {
  88. //Versuche den ganzen ItemStack in der 1.Kiste unterzubringen
  89. itemstack2 = item.clone(itemstack);
  90. notinchest1 = block.addItem(chestloc1, itemstack2);
  91. if(item.getType(notinchest1) != "minecraft:air") {
  92. //Versuche den Rest des ItemStack in der 2.Kiste unterzubringen
  93. notinchest2 = block.addItem(chestloc2, notinchest1);
  94. if(item.getType(notinchest1) != "minecraft:air") {
  95. //Entferne die ItemStacks wieder
  96. speakPrefix(player, präfix, "Not enough space in chest.");
  97. block.subItem(chestloc1, itemstack2);
  98. block.addItem(chestloc1, notinchest2);
  99. goto("wait");
  100. }
  101. }
  102. subMoney(owner_player_id, sell_price);
  103. }
  104. addMoney(player, sell_price);
  105. player.removeItem(player, itemstack);
  106. inv.update(player);
  107. }
  108. goto("wait");
  109. @inv_close
  110. map.remove(invid_to_sign_loc, inv_id);
  111. goto("wait");
  112. @custom_command
  113. if(player == null) {
  114. goto("wait");
  115. }
  116. if(command == "shop") {
  117. size = list.getSize(args);
  118. if(size == 0) {
  119. @syntax
  120. player.speak(player, präfix, "/shop ...");
  121. msg(player, "§v - create §rCreates a shop");
  122. msg(player, "§v - buy <amount> <price> §rSet buy settings");
  123. msg(player, "§v - sell <amount> <price> §rSet sell settings");
  124. msg(player, "§v - remove <buy/sell> §rRemove settings");
  125. if(perm.has(player, "adminshop")) {
  126. msg(player, "§v - admin §rSet to adminshop");
  127. }
  128. goto("wait");
  129. }
  130. arg0 = text.toLowerCase(list.getIndex(args, 0));
  131. if(arg0 == "create") {
  132. if(size != 1) {
  133. speakPrefix(player, präfix, "§v/shop create");
  134. goto("wait");
  135. }
  136. sign_loc = player.getTarget(player, 5, false, false);
  137. if(!isAWallSign(sign_loc)) {
  138. speakPrefix(player, präfix, "Look at a sign.");
  139. goto("wait");
  140. }
  141. chest_loc = loc.mod(sign_loc, 0, -1, 0);
  142. if(!isAChest(chest_loc)) {
  143. speakPrefix(player, präfix, "No chest under sign.");
  144. goto("wait");
  145. }
  146. if(!plot.check(sign_loc, player, 0, false)) {
  147. speakPrefix(player, präfix, "Not your plot.");
  148. goto("wait");
  149. }
  150. block.setSign(sign_loc, 0, "[Shop]");
  151. block.setSign(sign_loc, 3, text.number(player.getId(player)));
  152. speakPrefix(player, präfix, "§rCreated Shop.");
  153. goto("wait");
  154. }
  155. if(arg0 == "admin") {
  156. if(!perm.has(player, "adminshop")) {
  157. speakPrefix(player, präfix, "§vNo Permission.");
  158. goto("wait");
  159. }
  160. if(size != 1) {
  161. speakPrefix(player, präfix, "§v/shop admin");
  162. goto("wait");
  163. }
  164. sign_loc = player.getTarget(player, 5, false, false);
  165. if(!isAWallSign(sign_loc)) {
  166. speakPrefix(player, präfix, "Look at a sign.");
  167. goto("wait");
  168. }
  169. chest_loc = loc.mod(sign_loc, 0, -1, 0);
  170. if(!isAChest(chest_loc)) {
  171. speakPrefix(player, präfix, "No chest under sign.");
  172. goto("wait");
  173. }
  174. block.setSign(sign_loc, 0, "[Admin Shop]");
  175. block.setSign(sign_loc, 3, "");
  176. speakPrefix(player, präfix, "§rCreated Admin Shop.");
  177. goto("wait");
  178. }
  179. if(arg0 == "buy" || arg0 == "sell") {
  180. if(size != 3) {
  181. speakPrefix(player, präfix, concat("§v/shop ", arg0, " <amount> <price>"));
  182. goto("wait");
  183. }
  184. amount = list.getIndex(args, 1);
  185. price = list.getIndex(args, 2);
  186. if(!isDouble(amount) || !isDouble(amount)) {
  187. speakPrefix(player, präfix, "§rNumber expected.");
  188. goto("wait");
  189. }
  190. if(price < 0) {
  191. speakPrefix(player, präfix, "§rPositive number expected.");
  192. goto("wait");
  193. }
  194. sign_loc = player.getTarget(player, 5, false, false);
  195. if(!isAWallSign(sign_loc)) {
  196. speakPrefix(player, präfix, "Look at a sign.");
  197. goto("wait");
  198. }
  199. chest_loc = loc.mod(sign_loc, 0, -1, 0);
  200. if(!isAChest(chest_loc)) {
  201. speakPrefix(player, präfix, "No chest under sign.");
  202. goto("wait");
  203. }
  204. if(!shop.isOwner(player, sign_loc)) {
  205. speakPrefix(player, präfix, "You are not the owner.");
  206. goto("wait");
  207. }
  208. if(arg0 == "buy") {
  209. block.setSign(sign_loc, 1, concat("Buy ", text.number(amount), " for ", text.number(price)));
  210. } else {
  211. block.setSign(sign_loc, 2, concat("Sell ", text.number(amount), " for ", text.number(price)));
  212. }
  213. speakPrefix(player, präfix, "§rPrice set.");
  214. goto("wait");
  215. }
  216. if(arg0 == "remove") {
  217. if(size != 2) {
  218. speakPrefix(player, präfix, concat("§v/shop remove <buy/sell>"));
  219. goto("wait");
  220. }
  221. sign_loc = player.getTarget(player, 5, false, false);
  222. if(!isAWallSign(sign_loc)) {
  223. speakPrefix(player, präfix, "Look at a sign.");
  224. goto("wait");
  225. }
  226. chest_loc = loc.mod(sign_loc, 0, -1, 0);
  227. if(!isAChest(chest_loc)) {
  228. speakPrefix(player, präfix, "No chest under sign.");
  229. goto("wait");
  230. }
  231. if(!shop.isOwner(player, sign_loc)) {
  232. speakPrefix(player, präfix, "You are not the owner.");
  233. goto("wait");
  234. }
  235. arg1 = text.toLowerCase(list.getIndex(args, 1));
  236. if(arg1 == "buy") {
  237. block.setSign(sign_loc, 1, "");
  238. } elseif(arg1 == "sell") {
  239. block.setSign(sign_loc, 2, "");
  240. } else {
  241. speakPrefix(player, präfix, concat("§v/shop remove <buy/sell>"));
  242. goto("wait");
  243. }
  244. speakPrefix(player, präfix, "§rPrice removed.");
  245. goto("wait");
  246. }
  247. goto("syntax");
  248. }
  249. goto("wait");
  250. @block_click
  251. if(isAChest(block_loc)) {
  252. chest_loc = block_loc;
  253. sign_loc = loc.mod(chest_loc, 0, 1, 0);
  254. if(!isAWallSign(sign_loc)) {
  255. chest_loc = block.getSecChest(chest_loc);
  256. if(chest_loc == null) {
  257. goto("wait");
  258. }
  259. sign_loc = loc.mod(chest_loc, 0, 1, 0);
  260. if(!isAWallSign(sign_loc)) {
  261. goto("wait");
  262. }
  263. }
  264. line0 = block.getSign(sign_loc, 0);
  265. if(line0 != "[Shop]" && line0 != "[Admin Shop]") {
  266. goto("wait");
  267. }
  268. if(shop.isOwner(player, sign_loc)) {
  269. goto("wait");
  270. }
  271. cancel = true;
  272. goto("wait");
  273. }
  274. if(isAWallSign(block_loc)) {
  275. sign_loc = block_loc;
  276. chest_loc = loc.mod(sign_loc, 0, -1, 0);
  277. if(!isAChest(chest_loc)) {
  278. goto("wait");
  279. }
  280. line0 = block.getSign(sign_loc, 0);
  281. if(line0 != "[Shop]" && line0 != "[Admin Shop]") {
  282. goto("wait");
  283. }
  284. buy_line = block.getSign(sign_loc, 1);
  285. sell_line = block.getSign(sign_loc, 2);
  286. owner_id = block.getSign(sign_loc, 3);
  287. if(owner_id == "") {
  288. inv_title = "Admin Shop";
  289. } else {
  290. inv_title = concat("§8Shop von ", player.getNameFromId(read.number(owner_id)));
  291. }
  292. item_stack = shop.getItem(chest_loc);
  293. if(item_stack == null) {
  294. buy_line = "";
  295. sell_line = "";
  296. }
  297. inv = inv.new("222320000002322203");
  298. if(buy_line != "") {
  299. split_list = text.split(" ", buy_line);
  300. buy_amount = read.number(list.getIndex(split_list, 1));
  301. buy_price = read.number(list.getIndex(split_list, 3));
  302. buy_stack = item.clone(item_stack);
  303. item.setAmount(buy_stack, buy_amount);
  304. a = money.split(buy_price);
  305. gold = a[0];
  306. silver = a[1];
  307. bronze = a[2];
  308. index = 2;
  309. if(bronze != 0) {
  310. inv.setItem(inv, index--, read.item("km:coin_copper", bronze));
  311. }
  312. if(silver != 0) {
  313. inv.setItem(inv, index--, read.item("km:coin_silver", silver));
  314. }
  315. if(gold != 0) {
  316. inv.setItem(inv, index, read.item("km:coin_gold", gold));
  317. }
  318. inv.setItem(inv, 3, read.item("km:arrow_right", 1, "§rBuy"));
  319. inv.setItem(inv, 4, buy_stack);
  320. }
  321. if(sell_line != "") {
  322. split_list = text.split(" ", sell_line);
  323. sell_amount = read.number(list.getIndex(split_list, 1));
  324. sell_price = read.number(list.getIndex(split_list, 3));
  325. sell_stack = item.clone(item_stack);
  326. item.setAmount(sell_stack, sell_amount);
  327. a = money.split(sell_price);
  328. gold = a[0];
  329. silver = a[1];
  330. bronze = a[2];
  331. inv.setItem(inv, 5, sell_stack);
  332. inv.setItem(inv, 6, read.item("km:arrow_right", 1, "§rSell"));
  333. index = 7;
  334. if(bronze != 0) {
  335. inv.setItem(inv, index++, read.item("km:coin_copper", bronze));
  336. }
  337. if(silver != 0) {
  338. inv.setItem(inv, index++, read.item("km:coin_silver", silver));
  339. }
  340. if(gold != 0) {
  341. inv.setItem(inv, index, read.item("km:coin_gold", gold));
  342. }
  343. }
  344. inv.setItem(inv, 10, read.item("km:cross_red", 1, "§rCancel"));
  345. inv.open(inv, player, inv_title);
  346. map.add(invid_to_sign_loc, inv.getId(inv), sign_loc);
  347. }
  348. goto("wait");
  349. function shop.getItem(chest_loc) {
  350. inv = block.getInv(chest_loc);
  351. for(i = 0; i < inv.getSize(inv); i++) {
  352. item_stack = item.clone(inv.getItem(inv, i));
  353. if(item.getType(item_stack) != "minecraft:air") {
  354. return item_stack;
  355. }
  356. }
  357. return null;
  358. }
  359. function isAChest(location) {
  360. return block.getType(location) == "minecraft:chest";
  361. }
  362. function isAWallSign(location) {
  363. return block.hasTag($wall_signs_tag, block.get(location));
  364. }
  365. function shop.isOwner(player, sign_loc) {
  366. owner_id = block.getSign(sign_loc, 3);
  367. if(owner_id == "") {
  368. return perm.has(player, "adminshop");
  369. } else {
  370. return read.number(owner_id) == player.getId(player);
  371. }
  372. }
  373. function isAdminShop(sign_loc) {
  374. return block.getSign(sign_loc, 0) == "[Admin Shop]";
  375. }