<commit_before>package com.whammich.sstow.util;

import com.whammich.sstow.ConfigHandler;
import com.whammich.sstow.RegistrarSoulShards;
import com.whammich.sstow.SoulShardsTOW;
import com.whammich.sstow.api.*;
import com.whammich.sstow.api.event.CageSpawnEvent;
import com.whammich.sstow.api.event.ShardTierChangeEvent;
import com.whammich.sstow.compat.CompatibilityType;
import com.whammich.sstow.item.ItemSoulShard;
import com.whammich.sstow.tile.TileEntityCage;
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingExperienceDropEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.items.ItemHandlerHelper;
import org.apache.commons.lang3.tuple.Pair;

@Mod.EventBusSubscriber
public class EventHandler {

    @SubscribeEvent
    public static void onEntityKill(LivingDeathEvent event) {
        World world = event.getEntity().getEntityWorld();

        if (world.isRemote || !(event.getEntity() instanceof EntityLiving) || !(event.getSource().getTrueSource() instanceof EntityPlayer) || event.getSource().getTrueSource() instanceof FakePlayer)
            return;

        EntityLiving dead = (EntityLiving) event.getEntity();
        EntityPlayer player = (EntityPlayer) event.getSource().getTrueSource();
        ResourceLocation entName = EntityList.getKey(dead);

        if (entName == null) {
            SoulShardsTOW.LOGGER.error("Player killed an entity with no mapped name: {}", dead);
            return;
        }

        ItemStack shard = Utils.getShardFromInv(player, entName);

        if (!ConfigHandler.entityList.contains(entName) || SoulShardsAPI.isEntityBlacklisted(dead))
            return;

        if (!EntityMapper.isEntityValid(entName))
            return;

        if (!ConfigHandler.countCageBornForShard && Utils.isCageBorn(dead))
            return;

        if (!ShardHelper.isBound(shard))
            ShardHelper.setBoundEntity(shard, entName);

        int soulStealer = EnchantmentHelper.getEnchantmentLevel(RegistrarSoulShards.SOUL_STEALER, player.getHeldItemMainhand());
        soulStealer *= ConfigHandler.soulStealerBonus;

        if (!player.getHeldItemMainhand().isEmpty() && player.getHeldItemMainhand().getItem() instanceof ISoulWeapon)
            soulStealer += ((ISoulWeapon) player.getHeldItemMainhand().getItem()).getBonusSouls(player.getHeldItemMainhand());

        Utils.increaseShardKillCount(shard, 1 + soulStealer);
    }

    @SubscribeEvent
    public static void checkExperienceDrop(LivingExperienceDropEvent event) {
        if (!ConfigHandler.enableExperienceDrop && Utils.isCageBorn(event.getEntityLiving()))
            event.setDroppedExperience(0);
    }

    @SubscribeEvent
    public static void tryHandleStructure(PlayerInteractEvent.RightClickBlock event) {
        if (ItemSoulShard.multiblock.isEmpty())
            ItemSoulShard.buildMultiblock();

        IBlockState worldState = event.getWorld().getBlockState(event.getPos());

        if (!event.getItemStack().isEmpty() && ItemStack.areItemsEqual(event.getItemStack(), ConfigHandler.catalystItem) && worldState == ItemSoulShard.originBlock) {
            for (Pair<BlockPos, IBlockState> multiblockPair : ItemSoulShard.multiblock) {
                IBlockState offState = event.getWorld().getBlockState(event.getPos().add(multiblockPair.getLeft()));
                if (!multiblockPair.getRight().equals(offState))
                    return;
            }

            for (Pair<BlockPos, IBlockState> multiblockPair : ItemSoulShard.multiblock)
                event.getWorld().destroyBlock(event.getPos().add(multiblockPair.getLeft()), false);

            if (!event.getWorld().isRemote)
                InventoryHelper.spawnItemStack(event.getWorld(), event.getEntityPlayer().posX, event.getEntityPlayer().posY + 0.25, event.getEntityPlayer().posZ, new ItemStack(RegistrarSoulShards.SHARD));

            if (!event.getEntityPlayer().capabilities.isCreativeMode)
                event.getItemStack().shrink(1);

            event.getEntityPlayer().swingArm(event.getHand());
            event.setCanceled(true);
        }
    }

    @SubscribeEvent
    public static void tryHandleCageInteract(PlayerInteractEvent.RightClickBlock event) {
        TileEntity tile = event.getWorld().getTileEntity(event.getPos());
        EntityPlayer player = event.getEntityPlayer();
        ItemStack heldItem = event.getItemStack();

        if (tile != null && tile instanceof ISoulCage) {
            TileEntityCage cage = (TileEntityCage) tile;
            if (!heldItem.isEmpty() && cage.getStackHandler().getStackInSlot(0).isEmpty() && ShardHelper.isBound(heldItem) && !player.isSneaking()) {
                cage.getStackHandler().setStackInSlot(0, heldItem.copy());
                cage.setTier(ShardHelper.getTierFromShard(heldItem));
                cage.setEntName(ShardHelper.getBoundEntity(heldItem));
                if (!event.getWorld().isRemote)
                    cage.setOwner(player.getGameProfile().getId().toString());
                heldItem.shrink(1);
                player.swingArm(event.getHand());
            } else if (!cage.getStackHandler().getStackInSlot(0).isEmpty() && player.getHeldItemMainhand().isEmpty() && player.isSneaking()) {
                ItemHandlerHelper.giveItemToPlayer(player, cage.getStackHandler().getStackInSlot(0));
                cage.reset();
                player.swingArm(event.getHand());
            }
        }
    }

    @SubscribeEvent
    public static void onAnvil(AnvilUpdateEvent event) {
        if (!ConfigHandler.allowShardCombination)
            return;

        if (!event.getLeft().isEmpty() && event.getLeft().getItem() instanceof ISoulShard && !event.getRight().isEmpty() && event.getRight().getItem() instanceof ISoulShard) {
            if (ShardHelper.isBound(event.getLeft()) && ShardHelper.getKillsFromShard(event.getRight()) > 0) {
                if (Utils.hasMaxedKills(event.getLeft()))
                    return;

                ItemStack output = event.getLeft().copy();
                Utils.increaseShardKillCount(output, ShardHelper.getKillsFromShard(event.getRight()));
                event.setOutput(output);
                event.setCost(ShardHelper.getTierFromShard(output) * 6);
            }
        }
    }<commit_msg>Only shards of the same type should be combinable (#47)<commit_after>package com.whammich.sstow.util;

import com.whammich.sstow.ConfigHandler;
import com.whammich.sstow.RegistrarSoulShards;
import com.whammich.sstow.SoulShardsTOW;
import com.whammich.sstow.api.*;
import com.whammich.sstow.api.event.CageSpawnEvent;
import com.whammich.sstow.api.event.ShardTierChangeEvent;
import com.whammich.sstow.compat.CompatibilityType;
import com.whammich.sstow.item.ItemSoulShard;
import com.whammich.sstow.tile.TileEntityCage;
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingExperienceDropEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.items.ItemHandlerHelper;
import org.apache.commons.lang3.tuple.Pair;

@Mod.EventBusSubscriber
public class EventHandler {

    @SubscribeEvent
    public static void onEntityKill(LivingDeathEvent event) {
        World world = event.getEntity().getEntityWorld();

        if (world.isRemote || !(event.getEntity() instanceof EntityLiving) || !(event.getSource().getTrueSource() instanceof EntityPlayer) || event.getSource().getTrueSource() instanceof FakePlayer)
            return;

        EntityLiving dead = (EntityLiving) event.getEntity();
        EntityPlayer player = (EntityPlayer) event.getSource().getTrueSource();
        ResourceLocation entName = EntityList.getKey(dead);

        if (entName == null) {
            SoulShardsTOW.LOGGER.error("Player killed an entity with no mapped name: {}", dead);
            return;
        }

        ItemStack shard = Utils.getShardFromInv(player, entName);

        if (!ConfigHandler.entityList.contains(entName) || SoulShardsAPI.isEntityBlacklisted(dead))
            return;

        if (!EntityMapper.isEntityValid(entName))
            return;

        if (!ConfigHandler.countCageBornForShard && Utils.isCageBorn(dead))
            return;

        if (!ShardHelper.isBound(shard))
            ShardHelper.setBoundEntity(shard, entName);

        int soulStealer = EnchantmentHelper.getEnchantmentLevel(RegistrarSoulShards.SOUL_STEALER, player.getHeldItemMainhand());
        soulStealer *= ConfigHandler.soulStealerBonus;

        if (!player.getHeldItemMainhand().isEmpty() && player.getHeldItemMainhand().getItem() instanceof ISoulWeapon)
            soulStealer += ((ISoulWeapon) player.getHeldItemMainhand().getItem()).getBonusSouls(player.getHeldItemMainhand());

        Utils.increaseShardKillCount(shard, 1 + soulStealer);
    }

    @SubscribeEvent
    public static void checkExperienceDrop(LivingExperienceDropEvent event) {
        if (!ConfigHandler.enableExperienceDrop && Utils.isCageBorn(event.getEntityLiving()))
            event.setDroppedExperience(0);
    }

    @SubscribeEvent
    public static void tryHandleStructure(PlayerInteractEvent.RightClickBlock event) {
        if (ItemSoulShard.multiblock.isEmpty())
            ItemSoulShard.buildMultiblock();

        IBlockState worldState = event.getWorld().getBlockState(event.getPos());

        if (!event.getItemStack().isEmpty() && ItemStack.areItemsEqual(event.getItemStack(), ConfigHandler.catalystItem) && worldState == ItemSoulShard.originBlock) {
            for (Pair<BlockPos, IBlockState> multiblockPair : ItemSoulShard.multiblock) {
                IBlockState offState = event.getWorld().getBlockState(event.getPos().add(multiblockPair.getLeft()));
                if (!multiblockPair.getRight().equals(offState))
                    return;
            }

            for (Pair<BlockPos, IBlockState> multiblockPair : ItemSoulShard.multiblock)
                event.getWorld().destroyBlock(event.getPos().add(multiblockPair.getLeft()), false);

            if (!event.getWorld().isRemote)
                InventoryHelper.spawnItemStack(event.getWorld(), event.getEntityPlayer().posX, event.getEntityPlayer().posY + 0.25, event.getEntityPlayer().posZ, new ItemStack(RegistrarSoulShards.SHARD));

            if (!event.getEntityPlayer().capabilities.isCreativeMode)
                event.getItemStack().shrink(1);

            event.getEntityPlayer().swingArm(event.getHand());
            event.setCanceled(true);
        }
    }

    @SubscribeEvent
    public static void tryHandleCageInteract(PlayerInteractEvent.RightClickBlock event) {
        TileEntity tile = event.getWorld().getTileEntity(event.getPos());
        EntityPlayer player = event.getEntityPlayer();
        ItemStack heldItem = event.getItemStack();

        if (tile != null && tile instanceof ISoulCage) {
            TileEntityCage cage = (TileEntityCage) tile;
            if (!heldItem.isEmpty() && cage.getStackHandler().getStackInSlot(0).isEmpty() && ShardHelper.isBound(heldItem) && !player.isSneaking()) {
                cage.getStackHandler().setStackInSlot(0, heldItem.copy());
                cage.setTier(ShardHelper.getTierFromShard(heldItem));
                cage.setEntName(ShardHelper.getBoundEntity(heldItem));
                if (!event.getWorld().isRemote)
                    cage.setOwner(player.getGameProfile().getId().toString());
                heldItem.shrink(1);
                player.swingArm(event.getHand());
            } else if (!cage.getStackHandler().getStackInSlot(0).isEmpty() && player.getHeldItemMainhand().isEmpty() && player.isSneaking()) {
                ItemHandlerHelper.giveItemToPlayer(player, cage.getStackHandler().getStackInSlot(0));
                cage.reset();
                player.swingArm(event.getHand());
            }
        }
    }

    @SubscribeEvent
    public static void onAnvil(AnvilUpdateEvent event) {
        if (!ConfigHandler.allowShardCombination)
            return;

        if (!event.getLeft().isEmpty() && event.getLeft().getItem() instanceof ISoulShard && !event.getRight().isEmpty() && event.getRight().getItem() instanceof ISoulShard) {
            if (ShardHelper.isBound(event.getLeft()) && ShardHelper.getKillsFromShard(event.getRight()) > 0) {
                if (Utils.hasMaxedKills(event.getLeft()))
                    return;

                if (!ShardHelper.getBoundEntity(event.getLeft()).equals(ShardHelper.getBoundEntity(event.getRight())))
                    return;

                ItemStack output = event.getLeft().copy();
                Utils.increaseShardKillCount(output, ShardHelper.getKillsFromShard(event.getRight()));
                event.setOutput(output);
                event.setCost(ShardHelper.getTierFromShard(output) * 6);
            }
        }
    }
{'fraction_non_alphanumeric': 0.11541666666666667, 'fraction_numerical': 0.0020833333333333333, 'mean_word_length': 3.4269904703350753, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5932814', 'n_tokens_mistral': 4267, 'n_tokens_neox': 4155, 'n_words': 588}