chestshops.txt 14 KB

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