ScriptEvents.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. package me.km.snuviscript;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import java.util.function.Consumer;
  8. import java.util.stream.Collectors;
  9. import me.hammerle.code.Code;
  10. import me.hammerle.code.Script;
  11. import me.hammerle.code.ScriptUtils;
  12. import me.hammerle.code.SnuviParser;
  13. import me.hammerle.math.Fraction;
  14. import me.km.KajetansMod;
  15. import me.km.api.Location;
  16. import me.km.api.ModuleListener;
  17. import me.km.api.Module;
  18. import me.km.api.Utils;
  19. import me.km.dimensions.ModDimensions;
  20. import me.km.effects.PlayerUsesEffectEvent;
  21. import me.km.entities.EntityItemProjectile;
  22. import me.km.events.PlayerHurtEvent;
  23. import me.km.events.PlayerJoinMessageEvent;
  24. import me.km.events.PlayerLeaveMessageEvent;
  25. import me.km.events.PlayerMoveEvent;
  26. import me.km.events.PlayerRespawnAtEvent;
  27. import net.minecraft.entity.Entity;
  28. import net.minecraft.entity.EntityLivingBase;
  29. import net.minecraft.entity.passive.EntitySheep;
  30. import net.minecraft.entity.player.EntityPlayer;
  31. import net.minecraft.entity.player.EntityPlayerMP;
  32. import net.minecraft.init.Items;
  33. import net.minecraft.inventory.ClickType;
  34. import net.minecraft.item.ItemStack;
  35. import net.minecraft.server.management.PlayerList;
  36. import net.minecraft.util.EnumHand;
  37. import net.minecraft.util.math.BlockPos;
  38. import net.minecraft.util.math.RayTraceResult;
  39. import net.minecraft.world.WorldServer;
  40. import net.minecraftforge.event.CommandEvent;
  41. import net.minecraftforge.event.entity.EntityMountEvent;
  42. import net.minecraftforge.event.entity.ThrowableImpactEvent;
  43. import net.minecraftforge.event.entity.item.ItemTossEvent;
  44. import net.minecraftforge.event.entity.living.*;
  45. import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
  46. import net.minecraftforge.event.entity.player.FillBucketEvent;
  47. import net.minecraftforge.event.entity.player.ItemFishedEvent;
  48. import net.minecraftforge.event.entity.player.PlayerDropsEvent;
  49. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  50. import net.minecraftforge.event.world.BlockEvent;
  51. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  52. import net.minecraftforge.fml.common.gameevent.PlayerEvent;
  53. public class ScriptEvents extends ModuleListener
  54. {
  55. private final HashMap<UUID, UUID> questJoin;
  56. private final SnuviParser parser;
  57. public ScriptEvents(Module m)
  58. {
  59. super(m);
  60. questJoin = new HashMap<>();
  61. parser = KajetansMod.scripts.getSnuviParser();
  62. }
  63. // -------------------------------------------------------------------------
  64. // Basics
  65. // -------------------------------------------------------------------------
  66. private void handleEvent(EntityPlayer p, String event, Consumer<Script> before, Consumer<Script> after)
  67. {
  68. parser.callEvent(event, (sc) ->
  69. {
  70. if(p != null)
  71. {
  72. ScriptVars.setPlayerVars(sc, p);
  73. }
  74. before.accept(sc);
  75. }, after);
  76. if(p != null)
  77. {
  78. Script data = KajetansMod.scripts.getScript(p);
  79. if(data != null)
  80. {
  81. parser.callEvent(event, data, (sc) ->
  82. {
  83. ScriptVars.setPlayerVars(sc, p);
  84. before.accept(sc);
  85. }, after);
  86. }
  87. }
  88. }
  89. private void handleEvent(EntityPlayer p, String event, Consumer<Script> before)
  90. {
  91. handleEvent(p, event, before, null);
  92. }
  93. // -------------------------------------------------------------------------
  94. // Questevents
  95. // -------------------------------------------------------------------------
  96. @SubscribeEvent
  97. public void joinOtherPlayersQuests(PlayerInteractEvent.EntityInteract e)
  98. {
  99. if(e.getHand() == EnumHand.OFF_HAND || !(e.getTarget() instanceof EntityPlayer))
  100. {
  101. return;
  102. }
  103. EntityPlayer p = e.getEntityPlayer();
  104. EntityPlayer affectedPlayer = (EntityPlayer) e.getTarget();
  105. if(KajetansMod.scripts.hasScript(p) && !KajetansMod.scripts.hasScript(affectedPlayer))
  106. {
  107. if(questJoin.get(affectedPlayer.getUniqueID()) != null)
  108. {
  109. if(questJoin.get(affectedPlayer.getUniqueID()).equals(p.getUniqueID()))
  110. {
  111. return;
  112. }
  113. }
  114. questJoin.put(affectedPlayer.getUniqueID(), p.getUniqueID());
  115. this.getModule().send(affectedPlayer, p.getName() + " fragt dich, ob du seiner Quest beitreten willst.");
  116. this.getModule().send(p, affectedPlayer.getName() + " wurde eine Anfrage gesendet.");
  117. return;
  118. }
  119. if(!KajetansMod.scripts.hasScript(p) && questJoin.get(p.getUniqueID()) != null && KajetansMod.scripts.hasScript(affectedPlayer))
  120. {
  121. if(questJoin.get(p.getUniqueID()).equals(affectedPlayer.getUniqueID()))
  122. {
  123. questJoin.remove(p.getUniqueID());
  124. KajetansMod.scripts.addPlayerToPlayer(p, affectedPlayer);
  125. }
  126. }
  127. }
  128. @SubscribeEvent
  129. public void onPlayerMove(PlayerMoveEvent e)
  130. {
  131. EntityPlayer p = e.getEntityPlayer();
  132. Location l = new Location(p);
  133. l.round();
  134. parser.callEvent("player_move", (sc) -> ScriptVars.setPlayerVars(sc, p), null,
  135. sc -> ((MinecraftScript) sc).removeLocation(l));
  136. PlayerScript data = KajetansMod.scripts.getScript(p);
  137. if(data != null)
  138. {
  139. parser.callEvent("player_move", data, (sc) ->
  140. {
  141. ScriptVars.setPlayerVars(sc, p);
  142. }, null, data.removeLocation(l));
  143. }
  144. }
  145. public boolean QuestClickInventory(Script qd, SnuviInventory inv, int slot, ClickType click, EntityPlayer p)
  146. {
  147. parser.callEvent("inv_click", qd, sc ->
  148. {
  149. ScriptVars.setPlayerVars(qd, p);
  150. qd.setEventVar("inv_id", new Fraction(inv.getId()));
  151. qd.setEventVar("inv_name", inv.getName());
  152. qd.setEventVar("inv_slot", new Fraction(slot));
  153. ScriptVars.setItemVars(qd, inv.getStackInSlot(slot));
  154. qd.setVar("cancel", false);
  155. }, null);
  156. return qd.getBooleanVar("cancel");
  157. }
  158. public void QuestCloseInventory(Script qd, SnuviInventory inv, EntityPlayer p)
  159. {
  160. parser.callEvent("inv_close", qd, sc ->
  161. {
  162. ScriptVars.setPlayerVars(qd, p);
  163. qd.setEventVar("inv_id", new Fraction(inv.getId()));
  164. qd.setEventVar("inv_name", inv.getName());
  165. }, null);
  166. }
  167. @SubscribeEvent
  168. public void QuestOnPlayerRespawn(PlayerRespawnAtEvent e)
  169. {
  170. handleEvent(e.getEntityPlayer(), "player_respawn", (qd) ->
  171. {
  172. qd.setEventVar("keep_inventory", e.isInventoryKeepingForced());
  173. qd.setEventVar("respawn_loc", new Location(e.getWorld(), e.getRespawnLoc()));
  174. }, (qd) ->
  175. {
  176. try
  177. {
  178. e.setForcedInventoryKeeping(qd.getBooleanVar("keep_inventory"));
  179. Location l = (Location) qd.getVar("respawn_loc");
  180. e.setRespawnLoc(l.getPos());
  181. e.setWorld((WorldServer) l.getWorld());
  182. }
  183. catch(ClassCastException | NullPointerException ex)
  184. {
  185. KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
  186. }
  187. });
  188. }
  189. @SubscribeEvent
  190. public void QuestPlayerDamage(PlayerHurtEvent e)
  191. {
  192. EntityPlayer p = e.getEntityPlayer();
  193. handleEvent(p, "player_hurt", (qd) ->
  194. {
  195. if(p.getHealth() <= e.getAmount())
  196. {
  197. qd.setEventVar("player_killed", true);
  198. }
  199. else
  200. {
  201. qd.setEventVar("player_killed", false);
  202. }
  203. qd.setEventVar("player_damage", Fraction.fromDouble(e.getAmount()));
  204. qd.setEventVar("player_damage_cause", e.getSource().getDamageType());
  205. EntityPlayer ent = Utils.getDamager(e.getSource());
  206. if(ent != null)
  207. {
  208. qd.setEventVar("player_involved", true);
  209. ScriptVars.setSecPlayer(qd, ent);
  210. }
  211. else
  212. {
  213. qd.setEventVar("player_involved", false);
  214. }
  215. qd.setVar("cancel", e.isCanceled());
  216. }, (qd) ->
  217. {
  218. e.setCanceled(qd.getBooleanVar("cancel"));
  219. });
  220. }
  221. @SubscribeEvent
  222. public void QuestDeath(LivingDeathEvent e)
  223. {
  224. if(!(e.getEntityLiving() instanceof EntityPlayer))
  225. {
  226. handleEvent(null, "entity_death", (qd) ->
  227. {
  228. EntityLivingBase ent = e.getEntityLiving();
  229. ScriptVars.setEntityVars(qd, ent);
  230. EntityPlayer p = Utils.getDamager(e.getSource());
  231. if(p != null)
  232. {
  233. qd.setEventVar("player_involved", true);
  234. ScriptVars.setPlayerVars(qd, p);
  235. }
  236. else
  237. {
  238. qd.setEventVar("player_involved", false);
  239. }
  240. qd.setVar("cancel", e.isCanceled());
  241. }, (qd) ->
  242. {
  243. e.setCanceled(qd.getBooleanVar("cancel"));
  244. });
  245. return;
  246. }
  247. EntityPlayer p = (EntityPlayer) e.getEntity();
  248. handleEvent(p, "player_death", (qd) ->
  249. {
  250. qd.setEventVar("clear", false);
  251. EntityPlayer ent = Utils.getDamager(e.getSource());
  252. if(ent != null)
  253. {
  254. ScriptVars.setSecPlayer(qd, ent);
  255. }
  256. qd.setVar("cancel", e.isCanceled());
  257. }, (qd) ->
  258. {
  259. if(qd.getBooleanVar("clear"))
  260. {
  261. p.inventory.clear();
  262. }
  263. e.setCanceled(qd.getBooleanVar("cancel"));
  264. });
  265. }
  266. @SubscribeEvent
  267. public void QuestEntityDamage(LivingHurtEvent e)
  268. {
  269. EntityPlayer p = Utils.getDamager(e.getSource());
  270. if(p == null)
  271. {
  272. return;
  273. }
  274. handleEvent(p, "entity_hurt", (qd) ->
  275. {
  276. qd.setEventVar("entity_killed", e.getEntityLiving().getHealth() <= e.getAmount());
  277. ScriptVars.setEntityVars(qd, e.getEntity());
  278. qd.setEventVar("entity_damage", Fraction.fromDouble(e.getAmount()));
  279. qd.setEventVar("entity_damage_cause", e.getSource().getDamageType());
  280. qd.setVar("cancel", e.isCanceled());
  281. }, (qd) ->
  282. {
  283. e.setCanceled(qd.getBooleanVar("cancel"));
  284. });
  285. }
  286. @SubscribeEvent
  287. public void QuestEntityDrop(LivingDropsEvent e)
  288. {
  289. try
  290. {
  291. handleEvent(null, "entity_drop", (qd) ->
  292. {
  293. qd.setEventVar("drops", e.getDrops());
  294. ScriptVars.setEntityVars(qd, e.getEntityLiving());
  295. EntityPlayer p = Utils.getDamager(e.getSource());
  296. if(p != null)
  297. {
  298. qd.setEventVar("player_involved", true);
  299. ScriptVars.setPlayerVars(qd, p);
  300. }
  301. else
  302. {
  303. qd.setEventVar("player_involved", false);
  304. }
  305. qd.setVar("cancel", e.isCanceled());
  306. }, (qd) ->
  307. {
  308. e.setCanceled(qd.getBooleanVar("cancel"));
  309. });
  310. }
  311. catch(NullPointerException ex)
  312. {
  313. KajetansMod.scripts.sendWarningToConsole(ex.toString() + " " + ex.getMessage());
  314. }
  315. }
  316. @SubscribeEvent
  317. public void QuestPlayerDrop(PlayerDropsEvent e)
  318. {
  319. handleEvent(e.getEntityPlayer(), "player_drop", (qd) ->
  320. {
  321. qd.setVar("cancel", e.isCanceled());
  322. }, (qd) ->
  323. {
  324. e.setCanceled(qd.getBooleanVar("cancel"));
  325. });
  326. }
  327. @SubscribeEvent
  328. public void QuestProjectileHit(ThrowableImpactEvent e)
  329. {
  330. EntityPlayer p = Utils.getPlayerFromProjectile(e.getEntityThrowable());
  331. if(p != null)
  332. {
  333. handleEvent(p, "throw_hit", (qd) ->
  334. {
  335. if(e.getRayTraceResult().entityHit != null)
  336. {
  337. qd.setEventVar("is_entity_hit", true);
  338. qd.setEventVar("entity_hit", e.getRayTraceResult().entityHit);
  339. }
  340. else
  341. {
  342. qd.setEventVar("is_entity_hit", false);
  343. }
  344. ScriptVars.setEntityVars(qd, e.getEntityThrowable());
  345. });
  346. }
  347. }
  348. public void onEntityItemProjectileHit(EntityItemProjectile ent, EntityPlayer p, ItemStack stack, List<Entity> ents)
  349. {
  350. handleEvent(p, "item_hit", (qd) ->
  351. {
  352. ScriptVars.setEntityVars(qd, ent);
  353. ScriptVars.setItemVars(qd, stack);
  354. qd.setEventVar("entities", ents);
  355. });
  356. }
  357. @SubscribeEvent
  358. public void QuestEntityShear(PlayerInteractEvent.EntityInteract e)
  359. {
  360. if(e.getHand() == EnumHand.OFF_HAND || !(e.getTarget() instanceof EntitySheep))
  361. {
  362. return;
  363. }
  364. EntityPlayer p = e.getEntityPlayer();
  365. if(p.getHeldItemMainhand().getItem() != Items.SHEARS)
  366. {
  367. return;
  368. }
  369. EntitySheep sheep = (EntitySheep) e.getTarget();
  370. handleEvent(p, "entity_shear", (qd) ->
  371. {
  372. ScriptVars.setEntityVars(qd, sheep);
  373. qd.setEventVar("entity_sheared", sheep.getSheared());
  374. qd.setEventVar("entity_color", sheep.getFleeceColor().toString());
  375. qd.setVar("cancel", e.isCanceled());
  376. }, (qd) ->
  377. {
  378. e.setCanceled(qd.getBooleanVar("cancel"));
  379. sheep.setSheared(qd.getBooleanVar("entity_sheared"));
  380. });
  381. }
  382. @SubscribeEvent
  383. public void QuestBlockBreak(BlockEvent.BreakEvent e)
  384. {
  385. handleEvent(e.getPlayer(), "block_break", (qd) ->
  386. {
  387. ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
  388. qd.setVar("cancel", e.isCanceled());
  389. }, (qd) ->
  390. {
  391. e.setCanceled(qd.getBooleanVar("cancel"));
  392. });
  393. }
  394. @SubscribeEvent
  395. public void QuestPlayerLogin(PlayerEvent.PlayerLoggedInEvent e)
  396. {
  397. EntityPlayer p = e.player;
  398. if(p == null)
  399. {
  400. return;
  401. }
  402. handleEvent(p, "player_login", (qd) ->
  403. {
  404. qd.setEventVar("first_join", Utils.hasPlayedBefore(p));
  405. PlayerList list = KajetansMod.server.getPlayerList();
  406. qd.setEventVar("is_banned", list.getBannedPlayers().isBanned(p.getGameProfile()));
  407. qd.setEventVar("is_whitelisted", list.getWhitelistedPlayers().isWhitelisted(p.getGameProfile()));
  408. });
  409. }
  410. @SubscribeEvent
  411. public void QuestPlayerJoin(PlayerJoinMessageEvent e)
  412. {
  413. EntityPlayer p = e.getEntityPlayer();
  414. handleEvent(p, "player_join_server", (qd) ->
  415. {
  416. qd.setEventVar("message", e.getMessage());
  417. qd.setEventVar("changed_name", e.hasNameChanged());
  418. qd.setVar("cancel", e.isCanceled());
  419. }, (qd) ->
  420. {
  421. e.setMessage(String.valueOf(qd.getVar("message")));
  422. e.setCanceled(qd.getBooleanVar("cancel"));
  423. });
  424. }
  425. @SubscribeEvent
  426. public void onPlayerLeave(PlayerLeaveMessageEvent e)
  427. {
  428. EntityPlayer p = e.getEntityPlayer();
  429. handleEvent(p, "player_leave", (qd) ->
  430. {
  431. qd.setEventVar("message", e.getMessage());
  432. qd.setVar("cancel", e.isCanceled());
  433. }, (qd) ->
  434. {
  435. e.setMessage(String.valueOf(qd.getVar("message")));
  436. e.setCanceled(qd.getBooleanVar("cancel"));
  437. });
  438. }
  439. @SubscribeEvent
  440. public void QuestBucketFill(FillBucketEvent e)
  441. {
  442. handleEvent(e.getEntityPlayer(), "bucket_fill", (qd) ->
  443. {
  444. RayTraceResult ray = e.getTarget();
  445. if(ray != null)
  446. {
  447. BlockPos pos = ray.getBlockPos();
  448. if(pos != null)
  449. {
  450. qd.setEventVar("has_block", true);
  451. ScriptVars.setBlockVars(qd, e.getWorld(), pos);
  452. }
  453. else
  454. {
  455. qd.setEventVar("has_block", false);
  456. }
  457. }
  458. else
  459. {
  460. qd.setEventVar("has_block", false);
  461. }
  462. qd.setVar("cancel", e.isCanceled());
  463. }, (qd) ->
  464. {
  465. e.setCanceled(qd.getBooleanVar("cancel"));
  466. });
  467. }
  468. @SubscribeEvent
  469. public void QuestBlockPlace(BlockEvent.PlaceEvent e)
  470. {
  471. handleEvent(e.getPlayer(), "block_place", (qd) ->
  472. {
  473. qd.setEventVar("block_type_after", e.getPlacedBlock().getBlock().getRegistryName());
  474. ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
  475. qd.setVar("cancel", e.isCanceled());
  476. }, (qd) ->
  477. {
  478. e.setCanceled(qd.getBooleanVar("cancel"));
  479. });
  480. }
  481. @SubscribeEvent
  482. public void QuestClickBlockRight(PlayerInteractEvent.RightClickBlock e)
  483. {
  484. if(e.getHand() == EnumHand.OFF_HAND)
  485. {
  486. return;
  487. }
  488. handleEvent(e.getEntityPlayer(), "block_click", (qd) ->
  489. {
  490. qd.setEventVar("action", "right");
  491. ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
  492. qd.setVar("cancel", e.isCanceled());
  493. }, (qd) ->
  494. {
  495. e.setCanceled(qd.getBooleanVar("cancel"));
  496. });
  497. }
  498. @SubscribeEvent
  499. public void QuestClickBlockLeft(PlayerInteractEvent.LeftClickBlock e)
  500. {
  501. if(e.getHand() == EnumHand.OFF_HAND)
  502. {
  503. return;
  504. }
  505. handleEvent(e.getEntityPlayer(), "block_click", (qd) ->
  506. {
  507. qd.setEventVar("action", "left");
  508. ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
  509. qd.setVar("cancel", e.isCanceled());
  510. }, (qd) ->
  511. {
  512. e.setCanceled(qd.getBooleanVar("cancel"));
  513. });
  514. }
  515. @SubscribeEvent
  516. public void QuestClickEntity(PlayerInteractEvent.EntityInteract e)
  517. {
  518. if(e.getHand() != EnumHand.OFF_HAND)
  519. {
  520. handleEvent(e.getEntityPlayer(), "entity_click", (qd) ->
  521. {
  522. ScriptVars.setEntityVars(qd, e.getTarget());
  523. qd.setVar("cancel", e.isCanceled());
  524. }, (qd) ->
  525. {
  526. e.setCanceled(qd.getBooleanVar("cancel"));
  527. });
  528. }
  529. }
  530. @SubscribeEvent
  531. public void QuestFishing(ItemFishedEvent e)
  532. {
  533. handleEvent(e.getEntityPlayer(), "fishing", (qd) ->
  534. {
  535. qd.setEventVar("drops", e.getDrops());
  536. qd.setVar("cancel", e.isCanceled());
  537. }, (qd) ->
  538. {
  539. e.setCanceled(qd.getBooleanVar("cancel"));
  540. });
  541. }
  542. @SubscribeEvent
  543. public void onItemClick(PlayerInteractEvent.RightClickItem e)
  544. {
  545. handleEvent(e.getEntityPlayer(), "item_air_click", (qd) ->
  546. {
  547. ScriptVars.setItemVars(qd, e.getItemStack());
  548. qd.setEventVar("hand", e.getHand().toString());
  549. qd.setVar("cancel", e.isCanceled());
  550. }, (qd) ->
  551. {
  552. e.setCanceled(qd.getBooleanVar("cancel"));
  553. });
  554. }
  555. public void onArmSwing(EntityPlayer p, EnumHand hand)
  556. {
  557. handleEvent(p, "arm_swing", (qd) ->
  558. {
  559. qd.setEventVar("hand", hand);
  560. });
  561. }
  562. @SubscribeEvent
  563. public void onItemUseStart(LivingEntityUseItemEvent.Start e)
  564. {
  565. if(!(e.getEntityLiving() instanceof EntityPlayer))
  566. {
  567. return;
  568. }
  569. handleEvent((EntityPlayer) e.getEntityLiving(), "item_use_start", (qd) ->
  570. {
  571. qd.setEventVar("duration", new Fraction(e.getDuration()));
  572. qd.setVar("cancel", e.isCanceled());
  573. }, (qd) ->
  574. {
  575. e.setCanceled(qd.getBooleanVar("cancel"));
  576. try
  577. {
  578. e.setDuration(ScriptUtils.getInt(qd.getVar("duration")));
  579. }
  580. catch(Exception ex)
  581. {
  582. KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
  583. }
  584. });
  585. }
  586. @SubscribeEvent
  587. public void QuestConsuming(LivingEntityUseItemEvent.Finish e)
  588. {
  589. if(!(e.getEntityLiving() instanceof EntityPlayer))
  590. {
  591. return;
  592. }
  593. handleEvent((EntityPlayer) e.getEntityLiving(), "item_use_finish", (qd) ->
  594. {
  595. qd.setEventVar("result_stack", e.getResultStack());
  596. });
  597. }
  598. @SubscribeEvent
  599. public void QuestCrafting(PlayerEvent.ItemCraftedEvent e)
  600. {
  601. handleEvent(e.player, "craft", (qd) ->
  602. {
  603. ScriptVars.setItemVars(qd, e.crafting);
  604. });
  605. }
  606. @SubscribeEvent
  607. public void QuestDropItem(ItemTossEvent e)
  608. {
  609. handleEvent(e.getPlayer(), "player_toss", (qd) ->
  610. {
  611. ScriptVars.setItemVars(qd, e.getEntityItem().getItem());
  612. qd.setVar("cancel", e.isCanceled());
  613. }, (qd) ->
  614. {
  615. e.setCanceled(qd.getBooleanVar("cancel"));
  616. });
  617. }
  618. @SubscribeEvent
  619. public void QuestPickupItem(EntityItemPickupEvent e)
  620. {
  621. handleEvent(e.getEntityPlayer(), "player_pickup", (qd) ->
  622. {
  623. ScriptVars.setItemVars(qd, e.getItem().getItem());
  624. qd.setVar("cancel", e.isCanceled());
  625. }, (qd) ->
  626. {
  627. e.setCanceled(qd.getBooleanVar("cancel"));
  628. });
  629. }
  630. @SubscribeEvent
  631. public void VehicleEnterEvent(EntityMountEvent e)
  632. {
  633. if(!(e.getEntityMounting() instanceof EntityPlayer))
  634. {
  635. return;
  636. }
  637. EntityPlayer p = (EntityPlayer) e.getEntityMounting();
  638. handleEvent(p, "entity_mount", (qd) ->
  639. {
  640. qd.setEventVar("mounting", e.isMounting());
  641. ScriptVars.setEntityVars(qd, e.getEntityBeingMounted());
  642. qd.setVar("cancel", e.isCanceled());
  643. }, (qd) ->
  644. {
  645. e.setCanceled(qd.getBooleanVar("cancel"));
  646. });
  647. }
  648. @SubscribeEvent
  649. public void PlayerPortalEvent(PlayerEvent.PlayerChangedDimensionEvent e)
  650. {
  651. handleEvent(e.player, "portal", (qd) ->
  652. {
  653. qd.setEventVar("from", ModDimensions.getWorldName(e.fromDim));
  654. qd.setEventVar("to", ModDimensions.getWorldName(e.toDim));
  655. });
  656. }
  657. @SubscribeEvent
  658. public void onCommand(CommandEvent e)
  659. {
  660. if(e.getSender() instanceof EntityPlayer)
  661. {
  662. handleEvent((EntityPlayer) e.getSender(), "command", (qd) ->
  663. {
  664. qd.setEventVar("command", e.getCommand().getName());
  665. qd.setEventVar("args", Arrays.stream(e.getParameters()).map(s -> Code.convertInput(null, s, false)).collect(Collectors.toList()));
  666. qd.setVar("cancel", e.isCanceled());
  667. }, (qd) ->
  668. {
  669. e.setCanceled(qd.getBooleanVar("cancel"));
  670. });
  671. }
  672. }
  673. public void onCustomCommand(EntityPlayer p, String command, String args)
  674. {
  675. handleEvent(p, "custom_command", (qd) ->
  676. {
  677. qd.setEventVar("command", command);
  678. if(args.length() == 0)
  679. {
  680. qd.setEventVar("args", new ArrayList<>());
  681. return;
  682. }
  683. qd.setEventVar("args", Arrays.stream(args.trim().split(" ")).map(s -> Code.convertInput(null, s, false)).collect(Collectors.toList()));
  684. });
  685. }
  686. public void useEffectEvent(PlayerUsesEffectEvent e)
  687. {
  688. handleEvent(e.getPlayer(), "player_use_effect", (qd) ->
  689. {
  690. qd.setEventVar("power", new Fraction(e.getPower()));
  691. qd.setEventVar("mana_cost", new Fraction(e.getMana()));
  692. qd.setEventVar("cause", e.getCause().toString());
  693. qd.setEventVar("effect", e.getEffect());
  694. qd.setVar("cancel", e.isCanceled());
  695. }, (qd) ->
  696. {
  697. try
  698. {
  699. int power = ScriptUtils.getInt(qd.getVar("power"));
  700. if(power < 1 || power > 20)
  701. {
  702. throw new IllegalArgumentException();
  703. }
  704. e.setPower(power);
  705. }
  706. catch(Exception ex)
  707. {
  708. KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
  709. }
  710. try
  711. {
  712. int mana = ScriptUtils.getInt(qd.getVar("mana_cost"));
  713. if(mana < 0)
  714. {
  715. throw new IllegalArgumentException();
  716. }
  717. e.setMana(mana);
  718. }
  719. catch(Exception ex)
  720. {
  721. KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
  722. }
  723. e.setCanceled(qd.getBooleanVar("cancel"));
  724. });
  725. }
  726. public void onFunctionKey(EntityPlayerMP p, int key)
  727. {
  728. handleEvent(p, "function_key", (qd) ->
  729. {
  730. qd.setEventVar("key", new Fraction(key));
  731. });
  732. }
  733. /*@SubscribeEvent
  734. public void QuestVillagerPickUpItem(ItemTossEvent e)
  735. {
  736. EntityPlayer p = e.getPlayer();
  737. PlayerScript qd = KajetansMod.scripts.getScript(p);
  738. if(qd == null || !qd.isLoadedEvent("villager_give"))
  739. {
  740. return;
  741. }
  742. final EntityItem itemEnt = e.getEntityItem();
  743. KajetansMod.scheduler.scheduleTask(() ->
  744. {
  745. if(itemEnt.isDead)
  746. {
  747. return;
  748. }
  749. EntityVillager v = Utils.getNearestEntity(p.world, itemEnt.getPositionVector(), 2, EntityVillager.class);
  750. if(v == null)
  751. {
  752. return;
  753. }
  754. Script data = KajetansMod.scripts.getScript(p);
  755. if(data == null || !data.isLoadedEvent("villager_give"))
  756. {
  757. return;
  758. }
  759. data.setVar("event", "villager_give");
  760. ScriptVars.setPlayerVars(data, p);
  761. ScriptVars.setItemVars(data, itemEnt.getItem());
  762. data.setEventVar("villager_loc", new Location(v));
  763. data.setEventVar("villager_prof", v.getProfessionForge().getRegistryName());
  764. data.setVar("cancel", e.isCanceled());
  765. data.runCode();
  766. if(data.getBooleanVar("cancel"))
  767. {
  768. return;
  769. }
  770. itemEnt.setDead();
  771. }, 40);
  772. }*/
  773. }