package me.km.snuviscript;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import me.hammerle.snuviscript.code.Script;
import me.hammerle.snuviscript.code.SnuviUtils;
import me.hammerle.snuviscript.inputprovider.Variable;
import me.kcm.BlockHarvest;
import me.kcm.Craft;
import me.km.utils.Utils;
import me.km.entities.EntityHuman;
import me.km.entities.EntityItemProjectile;
import me.km.events.CommandEvent;
import me.km.inventory.ModInventory;
import me.km.utils.ExplosionUtils;
import me.km.utils.Location;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.command.ICommandSource;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.CraftResultInventory;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.network.play.server.SSetSlotPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootParameters;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.*;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.*;
import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent;
import net.minecraftforge.event.entity.player.*;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.ExplosionEvent;
import net.minecraftforge.eventbus.api.*;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.Event.Result;

public class ScriptEvents implements BlockHarvest, Craft {
    private static void setLivingEntity(Script sc, LivingEntity ent) {
        sc.setVar("living_entity", ent);
    }

    private static void setItem(Script sc, ItemStack stack) {
        sc.setVar("item", stack);
    }

    private static void setPlayer(Script sc, PlayerEntity p) {
        sc.setVar("player", p);
    }

    private final Scripts scripts;
    private final MinecraftServer server;

    public ScriptEvents(Scripts scripts, MinecraftServer server) {
        this.scripts = scripts;
        this.server = server;
    }

    private void handleEvent(Event e, String event, Consumer<Script> before, Consumer<Script> after) {
        if(e.isCancelable()) {
            scripts.getScriptManager().callEvent(event, sc -> {
                if(before != null) {
                    before.accept(sc);
                }
                sc.setVar("cancel", e.isCanceled());
            }, sc -> {
                if(after != null) {
                    after.accept(sc);
                }
                handleVar(sc, event, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
            });
        } else {
            scripts.getScriptManager().callEvent(event, before, after);
        }
    }

    private void handleEvent(PlayerEntity p, String event, Consumer<Script> before, Consumer<Script> after) {
        scripts.getScriptManager().callEvent(event, (sc) -> {
            ScriptVars.setPlayerVars(sc, p);
            before.accept(sc);
        }, after);

        if(p != null) {
            Script data = scripts.getScript(p);
            if(data != null) {
                scripts.getScriptManager().callEvent(event, data, (sc) -> {
                    ScriptVars.setPlayerVars(sc, p);
                    before.accept(sc);
                }, after);
            }
        }
    }

    @Deprecated
    private void handlePlayerEvent(PlayerEntity p, String event, Consumer<Script> before, Consumer<Script> after) {
        if(p != null) {
            Script data = scripts.getScript(p);
            if(data != null) {
                scripts.getScriptManager().callEvent(event, data, (sc) -> {
                    ScriptVars.setPlayerVars(sc, p);
                    before.accept(sc);
                }, after);
            }
        }
    }

    private void handleEvent(PlayerEntity p, String event, Consumer<Script> before) {
        handleEvent(p, event, before, null);
    }

    private void handleVar(Script sc, String event, String name, Consumer<Variable> c) {
        try {
            ifVarNotNull(sc, name, c);
        } catch(Exception ex) {
            scripts.getLogger().print(String.format("invalid var in '%s' event", event), ex, null, sc.getName(), sc, sc.getStackTrace());
        }
    }

    private void ifVarNotNull(Script sc, String name, Consumer<Variable> c) {
        Variable v = sc.getVar(name);
        if(v != null) {
            c.accept(v);
        }
    }

    private void simpleCancel(Script sc, Event e, String name) {
        try {
            ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
        } catch(Exception ex) {
            scripts.getLogger().print(String.format("invalid var in '%s' event", name), ex, null, sc.getName(), sc, sc.getStackTrace());
        }
    }

    public void onPlayerDataTick(PlayerEntity p, String var) {
        handleEvent(p, "player_data_tick", sc -> {
            sc.setVar("var", var);
        });
    }

    public void onPlayerMove(PlayerEntity p, int id) {
        handleEvent(p, "player_move", sc -> {
            sc.setVar("id", (double) id);
        });
    }

    public boolean onInventoryClick(Script script, ITextComponent text, ModInventory inv, int slot, ClickType click, PlayerEntity p) {
        scripts.getScriptManager().callEvent("inv_click", script, sc -> {
            ScriptVars.setPlayerVars(sc, p);
            sc.setVar("inv", inv);
            sc.setVar("inv_id", (double) inv.getModId());
            sc.setVar("inv_name", text.getFormattedText());
            sc.setVar("inv_slot", (double) slot);
            setItem(sc, inv.getStackInSlot(slot));
            sc.setVar("cancel", false);
        }, null);
        Variable v = script.getVar("cancel");
        return v != null && v.getBoolean(script);
    }

    public void onInventoryClose(Script script, ITextComponent text, ModInventory inv, PlayerEntity p) {
        scripts.getScriptManager().callEvent("inv_close", script, sc -> {
            ScriptVars.setPlayerVars(sc, p);
            sc.setVar("inv", inv);
            sc.setVar("inv_id", (double) inv.getModId());
            sc.setVar("inv_name", text.getFormattedText());
        }, null);
    }

    public void onHumanHurt(Entity attacker, EntityHuman h) {
        handleEvent(null, "human_hurt", sc -> {
            ScriptVars.setEntityVars(sc, attacker);
            sc.setVar("human", h);
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onPlayerPostRespawn(PlayerEvent.PlayerRespawnEvent e) {
        handleEvent(e.getPlayer(), "player_post_respawn", sc -> {
        });
    }

    public void onPlayerPreRespawn(PlayerEntity p) {
        handleEvent(p, "player_pre_respawn", sc -> {
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onOldPlayerDamage(LivingHurtEvent e) {
        if(!(e.getEntityLiving() instanceof ServerPlayerEntity)) {
            return;
        }
        PlayerEntity p = (PlayerEntity) e.getEntityLiving();
        handleEvent(p, "player_hurt", (sc) -> {
            sc.setVar("player_killed", p.getHealth() <= e.getAmount());
            sc.setVar("player_damage", (double) e.getAmount());
            sc.setVar("player_damage_cause", e.getSource());
            PlayerEntity ent = Utils.getDamager(e.getSource());
            if(ent != null) {
                sc.setVar("player_involved", true);
                ScriptVars.setSecPlayer(sc, ent);
            } else {
                sc.setVar("player_involved", false);
            }
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            try {
                ifVarNotNull(sc, "player_damage", v -> e.setAmount(v.getFloat(sc)));
                ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
            } catch(Exception ex) {
                scripts.getLogger().print("invalid var in 'player_hurt' event", ex, null, sc.getName(), sc, sc.getStackTrace());
            }
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onLivingHurt(LivingHurtEvent e) {
        handleEvent(e, "living_hurt", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            sc.setVar("damage_source", e.getSource());
            sc.setVar("damage_amount", (double) e.getAmount());
        }, (sc) -> {
            handleVar(sc, "living_hurt", "damage_amount", v -> e.setAmount(v.getFloat(sc)));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onLivingAttacked(LivingAttackEvent e) {
        LivingEntity liv = e.getEntityLiving();
        if(liv instanceof PlayerEntity) {
            PlayerEntity p = (PlayerEntity) liv;
            // deprecated
            handleEvent(p, "player_is_attacked", (sc) -> {
                sc.setVar("player_killed", p.getHealth() <= e.getAmount());
                sc.setVar("player_damage_cause", e.getSource());
                PlayerEntity ent = Utils.getDamager(e.getSource());
                if(ent != null) {
                    sc.setVar("player_involved", true);
                    ScriptVars.setSecPlayer(sc, ent);
                } else {
                    sc.setVar("player_involved", false);
                }
                sc.setVar("cancel", e.isCanceled());
            }, (sc) -> {
                simpleCancel(sc, e, "player_is_attacked");
            });
        }
        handleEvent(e, "living_is_attacked", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            sc.setVar("damage_source", e.getSource());
            sc.setVar("damage_amount", (double) e.getAmount());
        }, null);
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onLivingHeal(LivingHealEvent e) {
        handleEvent(e, "living_heal", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            sc.setVar("heal_amount", (double) e.getAmount());
        }, (sc) -> {
            handleVar(sc, "living_heal", "heal_amount", v -> e.setAmount(v.getFloat(sc)));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onLivingDeath(LivingDeathEvent e) {
        handleEvent(e, "living_death", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            sc.setVar("damage_source", e.getSource());
        }, null);
        if(!(e.getEntityLiving() instanceof PlayerEntity)) {
            return;
        }
        // deprecated
        PlayerEntity p = (PlayerEntity) e.getEntity();
        handleEvent(p, "player_death", (sc) -> {
            sc.setVar("clear", false);
            sc.setVar("player_damage_cause", e.getSource());
            PlayerEntity ent = Utils.getDamager(e.getSource());
            if(ent != null) {
                ScriptVars.setSecPlayer(sc, ent);
            }
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            try {
                Variable clear = sc.getVar("clear");
                if(clear != null && clear.getBoolean(sc)) {
                    p.inventory.clear();
                }
                ifVarNotNull(sc, "cancel", v -> e.setCanceled(v.getBoolean(sc)));
            } catch(Exception ex) {
                scripts.getLogger().print("invalid var in 'player_death' event", ex, null, sc.getName(), sc, sc.getStackTrace());
            }
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onLivingDrop(LivingDropsEvent e) {
        handleEvent(e, "living_drop", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            sc.setVar("drops", e.getDrops());
            sc.setVar("damage_source", e.getSource());
            sc.setVar("looting", (double) e.getLootingLevel());
        }, null);
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onLivingExperienceDrop(LivingExperienceDropEvent e) {
        handleEvent(e, "living_experience_drop", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            sc.setVar("experience", (double) e.getDroppedExperience());
        }, (sc) -> {
            handleVar(sc, "living_experience_drop", "experience", v -> e.setDroppedExperience(v.getInt(sc)));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onProjectileHit(ProjectileImpactEvent e) {
        final Entity hitEntity;
        final Location loc;

        RayTraceResult ray = e.getRayTraceResult();
        switch(ray.getType()) {
            case ENTITY:
                hitEntity = ((EntityRayTraceResult) e.getRayTraceResult()).getEntity();
                loc = null;
                break;
            case BLOCK:
                loc = new Location(e.getEntity().world, ((BlockRayTraceResult) e.getRayTraceResult()).getPos());
                hitEntity = null;
                break;
            default:
                return;
        }

        handleEvent(e, "projectile_hit", (sc) -> {
            sc.setVar("projectile", e.getEntity());
            sc.setVar("entity_hit", hitEntity);
            sc.setVar("loc_hit", loc);
            sc.setVar("shooter", Utils.getEntityFromProjectile(e.getEntity()));
        }, null);
    }

    public void onEntityItemProjectileHit(EntityItemProjectile ent, LivingEntity liv, ItemStack stack, Entity hitEntity, BlockPos pos) {
        Location loc = (pos == null) ? null : new Location(ent.world, pos);
        handleEvent(null, "item_hit", (sc) -> {
            sc.setVar("projectile", ent);
            setItem(sc, stack);
            sc.setVar("entity_hit", hitEntity);
            sc.setVar("loc_hit", loc);
            sc.setVar("shooter", liv);
        });
        // deprecated
        if(liv instanceof PlayerEntity) {
            handlePlayerEvent((PlayerEntity) liv, "item_hit", (sc) -> {
                sc.setVar("projectile", ent);
                setItem(sc, stack);
                sc.setVar("entity_hit", hitEntity);
                sc.setVar("loc_hit", loc);
                sc.setVar("shooter", liv);
            }, null);
        }
    }

    @Override
    public List<ItemStack> onBlockHarvest(BlockState state, ServerWorld w, BlockPos pos, TileEntity tileEnt, Entity ent, ItemStack stack) {
        LootContext.Builder loot = new LootContext.Builder(w)
                .withRandom(w.getRandom())
                .withParameter(LootParameters.POSITION, pos)
                .withParameter(LootParameters.TOOL, stack == null ? ItemStack.EMPTY : stack)
                .withNullableParameter(LootParameters.THIS_ENTITY, ent)
                .withNullableParameter(LootParameters.BLOCK_ENTITY, tileEnt);
        List<ItemStack> list = state.getDrops(loot);
        try {
            final Block b = state.getBlock();
            final String name = b.getRegistryName().toString();
            scripts.getScriptManager().callEvent("block_drop", sc -> {
                sc.setVar("drops", list);
                sc.setVar("block_type", name);
                sc.setVar("block", b);
                sc.setVar("location", new Location(w, pos));
                ScriptVars.setEntityVars(sc, ent);
                setItem(sc, stack);
            }, sc -> {
            });
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onBlockHarvest(BlockEvent.HarvestDropsEvent e) {
        handleEvent(e, "block_harvest", (sc) -> {
            setPlayer(sc, e.getHarvester());
            sc.setVar("location", new Location(e.getWorld(), e.getPos()));
            sc.setVar("drop_chance", (double) e.getDropChance());
            sc.setVar("drops", e.getDrops());
            sc.setVar("fortune", (double) e.getFortuneLevel());
            sc.setVar("silktouch", e.isSilkTouching());
        }, (sc) -> {
            handleVar(sc, "block_harvest", "drop_chance", v -> e.setDropChance(v.getFloat(sc)));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onBlockBreak(BlockEvent.BreakEvent e) {
        handleEvent(e.getPlayer(), "block_break", (sc) -> {
            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos(), e.getState());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "block_break");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onBlockPlace(BlockEvent.EntityPlaceEvent e) {
        if(!(e.getEntity() instanceof PlayerEntity)) {
            return;
        }
        handleEvent((PlayerEntity) e.getEntity(), "block_place", (sc) -> {
            sc.setVar("block_type_after", e.getPlacedBlock().getBlock().getRegistryName());
            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos(), e.getState());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "block_place");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent e) {
        PlayerEntity p = e.getPlayer();
        if(p == null) {
            return;
        }
        handleEvent(p, "player_login", (sc) -> {
            PlayerList list = server.getPlayerList();
            sc.setVar("is_banned", list.getBannedPlayers().isBanned(p.getGameProfile()));
            sc.setVar("is_whitelisted", list.getWhitelistedPlayers().isWhitelisted(p.getGameProfile()));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onPlayerLogout(PlayerEvent.PlayerLoggedOutEvent e) {
        PlayerEntity p = e.getPlayer();
        if(p == null || e.getPlayer().ticksExisted < 20) {
            return;
        }
        handleEvent(p, "player_logout", sc -> {
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onBucketFill(FillBucketEvent e) {
        handleEvent(e.getPlayer(), "bucket_use", (sc) -> {
            RayTraceResult ray = e.getTarget();
            if(ray != null && ray.hitInfo != null && ray.hitInfo instanceof BlockPos) {
                BlockPos pos = (BlockPos) ray.hitInfo;
                sc.setVar("has_block", true);
                ScriptVars.setBlockVars(sc, e.getWorld(), pos);
            } else {
                sc.setVar("has_block", false);
            }
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "bucket_use");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onRightClickBlock(PlayerInteractEvent.RightClickBlock e) {
        handleEvent(e.getPlayer(), "block_click", (sc) -> {
            sc.setVar("action", "right");
            sc.setVar("hand", e.getHand().name());
            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "block_click");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onLeftClickBlock(PlayerInteractEvent.LeftClickBlock e) {
        handleEvent(e.getPlayer(), "block_click", (sc) -> {
            sc.setVar("action", "left");
            sc.setVar("hand", e.getHand().name());
            ScriptVars.setBlockVars(sc, e.getWorld(), e.getPos());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "block_click");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onEntityClick(PlayerInteractEvent.EntityInteract e) {
        handleEvent(e.getPlayer(), "entity_click", (sc) -> {
            sc.setVar("hand", e.getHand().name());
            ScriptVars.setEntityVars(sc, e.getTarget());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "entity_click");
        });
    }

    public void onEmptyLeftClick(PlayerEntity p) {
        handleEvent(p, "left_click_air", (sc) -> {
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onItemClick(PlayerInteractEvent.RightClickItem e) {
        handleEvent(e.getPlayer(), "item_air_click", (sc) -> {
            setItem(sc, e.getItemStack());
            sc.setVar("hand", e.getHand().toString());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "item_air_click");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onItemUseStart(LivingEntityUseItemEvent.Start e) {
        handleEvent(e, "item_use_start", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            sc.setVar("duration", (double) e.getDuration());
            setItem(sc, e.getItem());
        }, (sc) -> {
            handleVar(sc, "item_use_start", "duration", v -> e.setDuration(v.getInt(sc)));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onConsuming(LivingEntityUseItemEvent.Finish e) {
        handleEvent(e, "item_use_finish", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
            setItem(sc, e.getItem());
            sc.setVar("result", e.getResultStack());
        }, (sc) -> {
            handleVar(sc, "item_use_finish", "result", v -> {
                ItemStack stack = (ItemStack) v.get(sc);
                if(stack == null) {
                    e.setResultStack(ItemStack.EMPTY);
                } else {
                    e.setResultStack(stack);
                }
            });
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onFishing(ItemFishedEvent e) {
        handleEvent(e.getPlayer(), "fishing", (sc) -> {
            sc.setVar("drops", e.getDrops());
            sc.setVar("hook", e.getHookEntity());
            sc.setVar("rod_damage", (double) e.getRodDamage());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "fishing");
            handleVar(sc, "fishing", "rod_damage", v -> e.damageRodBy(v.getInt(sc)));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onCrafting(PlayerEvent.ItemCraftedEvent e) {
        handleEvent(e.getPlayer(), "craft", (sc) -> {
            setItem(sc, e.getCrafting());
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onItemDrop(ItemTossEvent e) {
        handleEvent(e.getPlayer(), "player_toss", (sc) -> {
            setItem(sc, e.getEntityItem().getItem());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "player_toss");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onItemPickup(EntityItemPickupEvent e) {
        handleEvent(e.getPlayer(), "player_pickup", (sc) -> {
            ScriptVars.setEntityVars(sc, e.getItem());
            setItem(sc, e.getItem().getItem());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            simpleCancel(sc, e, "player_pickup");
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onEntityMount(EntityMountEvent e) {
        handleEvent(e, "entity_mount", (sc) -> {
            sc.setVar("mounting", e.isMounting());
            ScriptVars.setEntityVars(sc, e.getEntityBeingMounted());
            sc.setVar("rider", e.getEntityMounting());
        }, null);
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onPlayerUsePortal(PlayerEvent.PlayerChangedDimensionEvent e) {
        handleEvent(e.getPlayer(), "portal", (sc) -> {
            sc.setVar("from", e.getFrom().getRegistryName().getPath());
            sc.setVar("to", e.getTo().getRegistryName().getPath());
        });
    }

    public void onCommand(CommandEvent e) {
        handleEvent(e.getPlayer(), "command", (sc) -> {
            sc.setVar("command", e.getName());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            handleVar(sc, "command", "cancel", v -> e.setCanceled(v.getBoolean(sc)));
        });
    }

    public void onCustomCommand(PlayerEntity p, String command, String[] args) {
        handleEvent(p, "custom_command", (sc) -> {
            sc.setVar("command", command);
            if(args.length == 0) {
                sc.setVar("args", new ArrayList<>());
            } else {
                sc.setVar("args", Arrays.stream(args).map(s -> SnuviUtils.convert(s)).collect(Collectors.toList()));
            }
        });
    }

    public void onFunctionKey(ServerPlayerEntity p, int key) {
        handleEvent(p, "function_key", (sc) -> {
            sc.setVar("key", (double) key);
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onChatEvent(ServerChatEvent e) {
        handleEvent(e.getPlayer(), "chat", (sc) -> {
            sc.setVar("message", e.getMessage());
            sc.setVar("cancel", e.isCanceled());
        }, (sc) -> {
            handleVar(sc, "chat", "message", v -> e.setComponent(new StringTextComponent(v.getString(sc))));
            handleVar(sc, "chat", "cancel", v -> e.setCanceled(v.getBoolean(sc)));
        });
    }

    @SubscribeEvent(receiveCanceled = true, priority = EventPriority.HIGHEST)
    public void onPreExplosion(ExplosionEvent.Start e) {
        e.setCanceled(true);
        handleEvent(e, "pre_explosion", sc -> {
            sc.setVar("damage_source", e.getExplosion().getDamageSource());
            sc.setVar("location", new Location(e.getWorld(), e.getExplosion().getPosition()));
        }, null);
    }

    @SubscribeEvent
    public void onExplosion(ExplosionEvent.Detonate e) {
        ExplosionUtils.explosion(e.getExplosion(), e.getWorld());
        handleEvent(e, "explosion", sc -> {
            sc.setVar("affected_blocks", e.getAffectedBlocks());
            sc.setVar("affected_entities", e.getAffectedEntities());
            sc.setVar("damage_source", e.getExplosion().getDamageSource());
            sc.setVar("location", new Location(e.getWorld(), e.getExplosion().getPosition()));
        }, null);
    }

    private static String getName(ICommandSource cs) {
        if(cs instanceof PlayerEntity) {
            return ((PlayerEntity) cs).getName().getFormattedText();
        } else if(cs instanceof MinecraftServer) {
            return "Server";
        }
        return null;
    }

    public void onMissingCommand(ICommandSource cs, String command) {
        PlayerEntity p = null;
        if(cs instanceof PlayerEntity) {
            p = (PlayerEntity) cs;
        }

        handleEvent(p, "missing_command", (sc) -> {
            sc.setVar("command_name", command);
            sc.setVar("sender_name", getName(cs));
        });
    }

    public void onMissingPermission(ICommandSource cs, String command) {
        PlayerEntity p = null;
        if(cs instanceof PlayerEntity) {
            p = (PlayerEntity) cs;
        }

        handleEvent(p, "missing_perm", (sc) -> {
            sc.setVar("command_name", command);
            sc.setVar("sender_name", getName(cs));
        });
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onEntityJoinWorld(EntityJoinWorldEvent e) {
        Entity ent = e.getEntity();
        if(!e.getEntity().isPassenger() && !scripts.getEntityLimits().isAllowedToSpawn(ent.getType())) {
            e.getEntity().getPassengers().forEach(rider -> {
                if(rider == null || rider instanceof PlayerEntity) {
                    return;
                }
                rider.remove();
            });
            e.getEntity().removePassengers();
            e.setCanceled(true);
            return;
        }
        handleEvent(e, "entity_join", (sc) -> {
            ScriptVars.setEntityVars(sc, ent);
        }, null);
    }

    @SubscribeEvent
    public void onServerTick(TickEvent.ServerTickEvent e) {
        if(e.phase == TickEvent.Phase.END) {
            scripts.getEntityLimits().tick(server.getWorlds());
        }
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onAnimalTame(AnimalTameEvent e) {
        handleEvent(e, "animal_tame", (sc) -> {
            sc.setVar("animal", e.getAnimal());
            sc.setVar("tamer", e.getTamer());
        }, null);
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onJump(LivingJumpEvent e) {
        handleEvent(e, "living_jump", (sc) -> {
            setLivingEntity(sc, e.getEntityLiving());
        }, null);
    }

    public void onSneak(PlayerEntity p, boolean sneak) {
        handleEvent(p, "player_sneak", (sc) -> {
            sc.setVar("sneak", sneak);
        }, null);
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onSneak(EntityMobGriefingEvent e) {
        handleEvent(e, "mob_griefing", (sc) -> {
            ScriptVars.setEntityVars(sc, e.getEntity());
            sc.setVar("cancel", !e.getEntity().world.getGameRules().getBoolean(GameRules.MOB_GRIEFING));
        }, (sc) -> {
            handleVar(sc, "mob_griefing", "cancel", (v) -> {
                e.setResult(v.getBoolean(sc) ? Result.DENY : Result.ALLOW);
            });
        });
    }

    @Override
    public void onCraft(int id, World w, PlayerEntity p, CraftingInventory inv, CraftResultInventory result) {
        if(w.isRemote) {
            return;
        }
        ServerPlayerEntity serverplayerentity = (ServerPlayerEntity) p;
        Wrapper<ItemStack> wrapper = new Wrapper(ItemStack.EMPTY);
        Optional<ICraftingRecipe> optional = w.getServer().getRecipeManager().getRecipe(IRecipeType.CRAFTING, inv, w);
        if(optional.isPresent()) {
            ICraftingRecipe icraftingrecipe = optional.get();
            if(result.canUseRecipe(w, serverplayerentity, icraftingrecipe)) {
                wrapper.set(icraftingrecipe.getCraftingResult(inv));
                scripts.getScriptManager().callEvent("craft", (sc) -> {
                    ScriptVars.setPlayerVars(sc, p);
                    sc.setVar("result", wrapper.get());
                }, (sc) -> {
                    handleVar(sc, "craft", "result", (v) -> {
                        ItemStack stack = (ItemStack) v.get(sc);
                        if(stack == null) {
                            wrapper.set(ItemStack.EMPTY);
                        } else {
                            wrapper.set(stack);
                        }
                    });
                });
            }
        }
        result.setInventorySlotContents(0, wrapper.get());
        serverplayerentity.connection.sendPacket(new SSetSlotPacket(id, 0, wrapper.get()));
    }
}
