chestshops.txt 12 KB

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