123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- package me.km.snuviscript;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.UUID;
- import java.util.function.Consumer;
- import java.util.stream.Collectors;
- import me.hammerle.code.Code;
- import me.hammerle.code.Script;
- import me.hammerle.code.ScriptUtils;
- import me.hammerle.code.SnuviParser;
- import me.hammerle.math.Fraction;
- import me.km.KajetansMod;
- import me.km.api.Location;
- import me.km.api.ModuleListener;
- import me.km.api.Module;
- import me.km.api.Utils;
- import me.km.dimensions.ModDimensions;
- import me.km.effects.PlayerUsesEffectEvent;
- import me.km.entities.EntityItemProjectile;
- import me.km.events.PlayerHurtEvent;
- import me.km.events.PlayerJoinMessageEvent;
- import me.km.events.PlayerLeaveMessageEvent;
- import me.km.events.PlayerMoveEvent;
- import me.km.events.PlayerRespawnAtEvent;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.passive.EntitySheep;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.ClickType;
- import net.minecraft.item.ItemStack;
- import net.minecraft.server.management.PlayerList;
- import net.minecraft.util.EnumHand;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.RayTraceResult;
- import net.minecraft.world.WorldServer;
- import net.minecraftforge.event.CommandEvent;
- import net.minecraftforge.event.entity.EntityMountEvent;
- import net.minecraftforge.event.entity.ThrowableImpactEvent;
- import net.minecraftforge.event.entity.item.ItemTossEvent;
- import net.minecraftforge.event.entity.living.*;
- import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
- import net.minecraftforge.event.entity.player.FillBucketEvent;
- import net.minecraftforge.event.entity.player.ItemFishedEvent;
- import net.minecraftforge.event.entity.player.PlayerDropsEvent;
- import net.minecraftforge.event.entity.player.PlayerInteractEvent;
- import net.minecraftforge.event.world.BlockEvent;
- import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
- import net.minecraftforge.fml.common.gameevent.PlayerEvent;
- public class ScriptEvents extends ModuleListener
- {
- private final HashMap<UUID, UUID> questJoin;
- private final SnuviParser parser;
-
- public ScriptEvents(Module m)
- {
- super(m);
- questJoin = new HashMap<>();
- parser = KajetansMod.scripts.getSnuviParser();
- }
-
- // -------------------------------------------------------------------------
- // Basics
- // -------------------------------------------------------------------------
-
- private void handleEvent(EntityPlayer p, String event, Consumer<Script> before, Consumer<Script> after)
- {
- parser.callEvent(event, (sc) ->
- {
- if(p != null)
- {
- ScriptVars.setPlayerVars(sc, p);
- }
- before.accept(sc);
- }, after);
-
- if(p != null)
- {
- Script data = KajetansMod.scripts.getScript(p);
- if(data != null)
- {
- parser.callEvent(event, data, (sc) ->
- {
- ScriptVars.setPlayerVars(sc, p);
- before.accept(sc);
- }, after);
- }
- }
- }
- private void handleEvent(EntityPlayer p, String event, Consumer<Script> before)
- {
- handleEvent(p, event, before, null);
- }
-
- // -------------------------------------------------------------------------
- // Questevents
- // -------------------------------------------------------------------------
-
- @SubscribeEvent
- public void joinOtherPlayersQuests(PlayerInteractEvent.EntityInteract e)
- {
- if(e.getHand() == EnumHand.OFF_HAND || !(e.getTarget() instanceof EntityPlayer))
- {
- return;
- }
- EntityPlayer p = e.getEntityPlayer();
- EntityPlayer affectedPlayer = (EntityPlayer) e.getTarget();
- if(KajetansMod.scripts.hasScript(p) && !KajetansMod.scripts.hasScript(affectedPlayer))
- {
- if(questJoin.get(affectedPlayer.getUniqueID()) != null)
- {
- if(questJoin.get(affectedPlayer.getUniqueID()).equals(p.getUniqueID()))
- {
- return;
- }
- }
- questJoin.put(affectedPlayer.getUniqueID(), p.getUniqueID());
- this.getModule().send(affectedPlayer, p.getName() + " fragt dich, ob du seiner Quest beitreten willst.");
- this.getModule().send(p, affectedPlayer.getName() + " wurde eine Anfrage gesendet.");
- return;
- }
- if(!KajetansMod.scripts.hasScript(p) && questJoin.get(p.getUniqueID()) != null && KajetansMod.scripts.hasScript(affectedPlayer))
- {
- if(questJoin.get(p.getUniqueID()).equals(affectedPlayer.getUniqueID()))
- {
- questJoin.remove(p.getUniqueID());
- KajetansMod.scripts.addPlayerToPlayer(p, affectedPlayer);
- }
- }
- }
- @SubscribeEvent
- public void onPlayerMove(PlayerMoveEvent e)
- {
- EntityPlayer p = e.getEntityPlayer();
- Location l = new Location(p);
- l.round();
- parser.callEvent("player_move", (sc) -> ScriptVars.setPlayerVars(sc, p), null,
- sc -> ((MinecraftScript) sc).removeLocation(l));
-
- PlayerScript data = KajetansMod.scripts.getScript(p);
- if(data != null)
- {
- parser.callEvent("player_move", data, (sc) ->
- {
- ScriptVars.setPlayerVars(sc, p);
- }, null, data.removeLocation(l));
- }
- }
-
- public boolean QuestClickInventory(Script qd, SnuviInventory inv, int slot, ClickType click, EntityPlayer p)
- {
- parser.callEvent("inv_click", qd, sc ->
- {
- ScriptVars.setPlayerVars(qd, p);
- qd.setEventVar("inv_id", new Fraction(inv.getId()));
- qd.setEventVar("inv_name", inv.getName());
- qd.setEventVar("inv_slot", new Fraction(slot));
- ScriptVars.setItemVars(qd, inv.getStackInSlot(slot));
- qd.setVar("cancel", false);
- }, null);
- return qd.getBooleanVar("cancel");
- }
- public void QuestCloseInventory(Script qd, SnuviInventory inv, EntityPlayer p)
- {
- parser.callEvent("inv_close", qd, sc ->
- {
- ScriptVars.setPlayerVars(qd, p);
- qd.setEventVar("inv_id", new Fraction(inv.getId()));
- qd.setEventVar("inv_name", inv.getName());
- }, null);
- }
-
- @SubscribeEvent
- public void QuestOnPlayerRespawn(PlayerRespawnAtEvent e)
- {
- handleEvent(e.getEntityPlayer(), "player_respawn", (qd) ->
- {
- qd.setEventVar("keep_inventory", e.isInventoryKeepingForced());
- qd.setEventVar("respawn_loc", new Location(e.getWorld(), e.getRespawnLoc()));
- }, (qd) ->
- {
- try
- {
- e.setForcedInventoryKeeping(qd.getBooleanVar("keep_inventory"));
- Location l = (Location) qd.getVar("respawn_loc");
- e.setRespawnLoc(l.getPos());
- e.setWorld((WorldServer) l.getWorld());
- }
- catch(ClassCastException | NullPointerException ex)
- {
- KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
- }
- });
- }
-
- @SubscribeEvent
- public void QuestPlayerDamage(PlayerHurtEvent e)
- {
- EntityPlayer p = e.getEntityPlayer();
- handleEvent(p, "player_hurt", (qd) ->
- {
- if(p.getHealth() <= e.getAmount())
- {
- qd.setEventVar("player_killed", true);
- }
- else
- {
- qd.setEventVar("player_killed", false);
- }
- qd.setEventVar("player_damage", Fraction.fromDouble(e.getAmount()));
- qd.setEventVar("player_damage_cause", e.getSource().getDamageType());
- EntityPlayer ent = Utils.getDamager(e.getSource());
- if(ent != null)
- {
- qd.setEventVar("player_involved", true);
- ScriptVars.setSecPlayer(qd, ent);
- }
- else
- {
- qd.setEventVar("player_involved", false);
- }
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestDeath(LivingDeathEvent e)
- {
- if(!(e.getEntityLiving() instanceof EntityPlayer))
- {
- handleEvent(null, "entity_death", (qd) ->
- {
- EntityLivingBase ent = e.getEntityLiving();
- ScriptVars.setEntityVars(qd, ent);
- EntityPlayer p = Utils.getDamager(e.getSource());
- if(p != null)
- {
- qd.setEventVar("player_involved", true);
- ScriptVars.setPlayerVars(qd, p);
- }
- else
- {
- qd.setEventVar("player_involved", false);
- }
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- return;
- }
- EntityPlayer p = (EntityPlayer) e.getEntity();
- handleEvent(p, "player_death", (qd) ->
- {
- qd.setEventVar("clear", false);
- EntityPlayer ent = Utils.getDamager(e.getSource());
- if(ent != null)
- {
- ScriptVars.setSecPlayer(qd, ent);
- }
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- if(qd.getBooleanVar("clear"))
- {
- p.inventory.clear();
- }
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestEntityDamage(LivingHurtEvent e)
- {
- EntityPlayer p = Utils.getDamager(e.getSource());
- if(p == null)
- {
- return;
- }
- handleEvent(p, "entity_hurt", (qd) ->
- {
- qd.setEventVar("entity_killed", e.getEntityLiving().getHealth() <= e.getAmount());
- ScriptVars.setEntityVars(qd, e.getEntity());
- qd.setEventVar("entity_damage", Fraction.fromDouble(e.getAmount()));
- qd.setEventVar("entity_damage_cause", e.getSource().getDamageType());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestEntityDrop(LivingDropsEvent e)
- {
- try
- {
- handleEvent(null, "entity_drop", (qd) ->
- {
- qd.setEventVar("drops", e.getDrops());
- ScriptVars.setEntityVars(qd, e.getEntityLiving());
- EntityPlayer p = Utils.getDamager(e.getSource());
- if(p != null)
- {
- qd.setEventVar("player_involved", true);
- ScriptVars.setPlayerVars(qd, p);
- }
- else
- {
- qd.setEventVar("player_involved", false);
- }
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
- catch(NullPointerException ex)
- {
- KajetansMod.scripts.sendWarningToConsole(ex.toString() + " " + ex.getMessage());
- }
- }
-
- @SubscribeEvent
- public void QuestPlayerDrop(PlayerDropsEvent e)
- {
- handleEvent(e.getEntityPlayer(), "player_drop", (qd) ->
- {
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestProjectileHit(ThrowableImpactEvent e)
- {
- EntityPlayer p = Utils.getPlayerFromProjectile(e.getEntityThrowable());
- if(p != null)
- {
- handleEvent(p, "throw_hit", (qd) ->
- {
- if(e.getRayTraceResult().entityHit != null)
- {
- qd.setEventVar("is_entity_hit", true);
- qd.setEventVar("entity_hit", e.getRayTraceResult().entityHit);
- }
- else
- {
- qd.setEventVar("is_entity_hit", false);
- }
- ScriptVars.setEntityVars(qd, e.getEntityThrowable());
- });
- }
- }
-
- public void onEntityItemProjectileHit(EntityItemProjectile ent, EntityPlayer p, ItemStack stack, List<Entity> ents)
- {
- handleEvent(p, "item_hit", (qd) ->
- {
- ScriptVars.setEntityVars(qd, ent);
- ScriptVars.setItemVars(qd, stack);
- qd.setEventVar("entities", ents);
- });
- }
-
- @SubscribeEvent
- public void QuestEntityShear(PlayerInteractEvent.EntityInteract e)
- {
- if(e.getHand() == EnumHand.OFF_HAND || !(e.getTarget() instanceof EntitySheep))
- {
- return;
- }
- EntityPlayer p = e.getEntityPlayer();
- if(p.getHeldItemMainhand().getItem() != Items.SHEARS)
- {
- return;
- }
- EntitySheep sheep = (EntitySheep) e.getTarget();
- handleEvent(p, "entity_shear", (qd) ->
- {
- ScriptVars.setEntityVars(qd, sheep);
- qd.setEventVar("entity_sheared", sheep.getSheared());
- qd.setEventVar("entity_color", sheep.getFleeceColor().toString());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- sheep.setSheared(qd.getBooleanVar("entity_sheared"));
- });
- }
-
- @SubscribeEvent
- public void QuestBlockBreak(BlockEvent.BreakEvent e)
- {
- handleEvent(e.getPlayer(), "block_break", (qd) ->
- {
- ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestPlayerLogin(PlayerEvent.PlayerLoggedInEvent e)
- {
- EntityPlayer p = e.player;
- if(p == null)
- {
- return;
- }
- handleEvent(p, "player_login", (qd) ->
- {
- qd.setEventVar("first_join", Utils.hasPlayedBefore(p));
- PlayerList list = KajetansMod.server.getPlayerList();
- qd.setEventVar("is_banned", list.getBannedPlayers().isBanned(p.getGameProfile()));
- qd.setEventVar("is_whitelisted", list.getWhitelistedPlayers().isWhitelisted(p.getGameProfile()));
- });
- }
-
- @SubscribeEvent
- public void QuestPlayerJoin(PlayerJoinMessageEvent e)
- {
- EntityPlayer p = e.getEntityPlayer();
- handleEvent(p, "player_join_server", (qd) ->
- {
- qd.setEventVar("message", e.getMessage());
- qd.setEventVar("changed_name", e.hasNameChanged());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setMessage(String.valueOf(qd.getVar("message")));
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void onPlayerLeave(PlayerLeaveMessageEvent e)
- {
- EntityPlayer p = e.getEntityPlayer();
- handleEvent(p, "player_leave", (qd) ->
- {
- qd.setEventVar("message", e.getMessage());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setMessage(String.valueOf(qd.getVar("message")));
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestBucketFill(FillBucketEvent e)
- {
- handleEvent(e.getEntityPlayer(), "bucket_fill", (qd) ->
- {
- RayTraceResult ray = e.getTarget();
- if(ray != null)
- {
- BlockPos pos = ray.getBlockPos();
- if(pos != null)
- {
- qd.setEventVar("has_block", true);
- ScriptVars.setBlockVars(qd, e.getWorld(), pos);
- }
- else
- {
- qd.setEventVar("has_block", false);
- }
- }
- else
- {
- qd.setEventVar("has_block", false);
- }
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestBlockPlace(BlockEvent.PlaceEvent e)
- {
- handleEvent(e.getPlayer(), "block_place", (qd) ->
- {
- qd.setEventVar("block_type_after", e.getPlacedBlock().getBlock().getRegistryName());
- ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestClickBlockRight(PlayerInteractEvent.RightClickBlock e)
- {
- if(e.getHand() == EnumHand.OFF_HAND)
- {
- return;
- }
- handleEvent(e.getEntityPlayer(), "block_click", (qd) ->
- {
- qd.setEventVar("action", "right");
- ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestClickBlockLeft(PlayerInteractEvent.LeftClickBlock e)
- {
- if(e.getHand() == EnumHand.OFF_HAND)
- {
- return;
- }
- handleEvent(e.getEntityPlayer(), "block_click", (qd) ->
- {
- qd.setEventVar("action", "left");
- ScriptVars.setBlockVars(qd, e.getWorld(), e.getPos());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestClickEntity(PlayerInteractEvent.EntityInteract e)
- {
- if(e.getHand() != EnumHand.OFF_HAND)
- {
- handleEvent(e.getEntityPlayer(), "entity_click", (qd) ->
- {
- ScriptVars.setEntityVars(qd, e.getTarget());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
- }
-
- @SubscribeEvent
- public void QuestFishing(ItemFishedEvent e)
- {
- handleEvent(e.getEntityPlayer(), "fishing", (qd) ->
- {
- qd.setEventVar("drops", e.getDrops());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void onItemClick(PlayerInteractEvent.RightClickItem e)
- {
- handleEvent(e.getEntityPlayer(), "item_air_click", (qd) ->
- {
- ScriptVars.setItemVars(qd, e.getItemStack());
- qd.setEventVar("hand", e.getHand().toString());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- public void onArmSwing(EntityPlayer p, EnumHand hand)
- {
- handleEvent(p, "arm_swing", (qd) ->
- {
- qd.setEventVar("hand", hand);
- });
- }
-
- @SubscribeEvent
- public void onItemUseStart(LivingEntityUseItemEvent.Start e)
- {
- if(!(e.getEntityLiving() instanceof EntityPlayer))
- {
- return;
- }
- handleEvent((EntityPlayer) e.getEntityLiving(), "item_use_start", (qd) ->
- {
- qd.setEventVar("duration", new Fraction(e.getDuration()));
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- try
- {
- e.setDuration(ScriptUtils.getInt(qd.getVar("duration")));
- }
- catch(Exception ex)
- {
- KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
- }
- });
- }
-
- @SubscribeEvent
- public void QuestConsuming(LivingEntityUseItemEvent.Finish e)
- {
- if(!(e.getEntityLiving() instanceof EntityPlayer))
- {
- return;
- }
- handleEvent((EntityPlayer) e.getEntityLiving(), "item_use_finish", (qd) ->
- {
- qd.setEventVar("result_stack", e.getResultStack());
- });
- }
-
- @SubscribeEvent
- public void QuestCrafting(PlayerEvent.ItemCraftedEvent e)
- {
- handleEvent(e.player, "craft", (qd) ->
- {
- ScriptVars.setItemVars(qd, e.crafting);
- });
- }
-
- @SubscribeEvent
- public void QuestDropItem(ItemTossEvent e)
- {
- handleEvent(e.getPlayer(), "player_toss", (qd) ->
- {
- ScriptVars.setItemVars(qd, e.getEntityItem().getItem());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void QuestPickupItem(EntityItemPickupEvent e)
- {
- handleEvent(e.getEntityPlayer(), "player_pickup", (qd) ->
- {
- ScriptVars.setItemVars(qd, e.getItem().getItem());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void VehicleEnterEvent(EntityMountEvent e)
- {
- if(!(e.getEntityMounting() instanceof EntityPlayer))
- {
- return;
- }
- EntityPlayer p = (EntityPlayer) e.getEntityMounting();
- handleEvent(p, "entity_mount", (qd) ->
- {
- qd.setEventVar("mounting", e.isMounting());
- ScriptVars.setEntityVars(qd, e.getEntityBeingMounted());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- @SubscribeEvent
- public void PlayerPortalEvent(PlayerEvent.PlayerChangedDimensionEvent e)
- {
- handleEvent(e.player, "portal", (qd) ->
- {
- qd.setEventVar("from", ModDimensions.getWorldName(e.fromDim));
- qd.setEventVar("to", ModDimensions.getWorldName(e.toDim));
- });
- }
-
- @SubscribeEvent
- public void onCommand(CommandEvent e)
- {
- if(e.getSender() instanceof EntityPlayer)
- {
- handleEvent((EntityPlayer) e.getSender(), "command", (qd) ->
- {
- qd.setEventVar("command", e.getCommand().getName());
- qd.setEventVar("args", Arrays.stream(e.getParameters()).map(s -> Code.convertInput(null, s, false)).collect(Collectors.toList()));
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
- }
-
- public void onCustomCommand(EntityPlayer p, String command, String args)
- {
- handleEvent(p, "custom_command", (qd) ->
- {
- qd.setEventVar("command", command);
- if(args.length() == 0)
- {
- qd.setEventVar("args", new ArrayList<>());
- return;
- }
- qd.setEventVar("args", Arrays.stream(args.trim().split(" ")).map(s -> Code.convertInput(null, s, false)).collect(Collectors.toList()));
- });
- }
-
- public void useEffectEvent(PlayerUsesEffectEvent e)
- {
- handleEvent(e.getPlayer(), "player_use_effect", (qd) ->
- {
- qd.setEventVar("power", new Fraction(e.getPower()));
- qd.setEventVar("mana_cost", new Fraction(e.getMana()));
- qd.setEventVar("cause", e.getCause().toString());
- qd.setEventVar("effect", e.getEffect());
- qd.setVar("cancel", e.isCanceled());
- }, (qd) ->
- {
- try
- {
- int power = ScriptUtils.getInt(qd.getVar("power"));
- if(power < 1 || power > 20)
- {
- throw new IllegalArgumentException();
- }
- e.setPower(power);
- }
- catch(Exception ex)
- {
- KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
- }
- try
- {
- int mana = ScriptUtils.getInt(qd.getVar("mana_cost"));
- if(mana < 0)
- {
- throw new IllegalArgumentException();
- }
- e.setMana(mana);
- }
- catch(Exception ex)
- {
- KajetansMod.scripts.logger.printException(ex, qd, qd.getActiveRealCodeLine());
- }
- e.setCanceled(qd.getBooleanVar("cancel"));
- });
- }
-
- public void onFunctionKey(EntityPlayerMP p, int key)
- {
- handleEvent(p, "function_key", (qd) ->
- {
- qd.setEventVar("key", new Fraction(key));
- });
- }
-
- /*@SubscribeEvent
- public void QuestVillagerPickUpItem(ItemTossEvent e)
- {
- EntityPlayer p = e.getPlayer();
- PlayerScript qd = KajetansMod.scripts.getScript(p);
- if(qd == null || !qd.isLoadedEvent("villager_give"))
- {
- return;
- }
- final EntityItem itemEnt = e.getEntityItem();
- KajetansMod.scheduler.scheduleTask(() ->
- {
- if(itemEnt.isDead)
- {
- return;
- }
- EntityVillager v = Utils.getNearestEntity(p.world, itemEnt.getPositionVector(), 2, EntityVillager.class);
- if(v == null)
- {
- return;
- }
- Script data = KajetansMod.scripts.getScript(p);
- if(data == null || !data.isLoadedEvent("villager_give"))
- {
- return;
- }
- data.setVar("event", "villager_give");
- ScriptVars.setPlayerVars(data, p);
- ScriptVars.setItemVars(data, itemEnt.getItem());
- data.setEventVar("villager_loc", new Location(v));
- data.setEventVar("villager_prof", v.getProfessionForge().getRegistryName());
- data.setVar("cancel", e.isCanceled());
- data.runCode();
- if(data.getBooleanVar("cancel"))
- {
- return;
- }
- itemEnt.setDead();
- }, 40);
- }*/
- }
|