<commit_before>package mcjty.rftools;

import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import mcjty.rftools.blocks.blockprotector.BlockProtectorTileEntity;
import mcjty.rftools.blocks.blockprotector.BlockProtectors;
import mcjty.rftools.blocks.dimlets.DimletConfiguration;
import mcjty.rftools.blocks.environmental.PeacefulAreaManager;
import mcjty.rftools.dimension.DimensionInformation;
import mcjty.rftools.dimension.DimensionStorage;
import mcjty.rftools.dimension.RfToolsDimensionManager;
import mcjty.rftools.dimension.world.types.EffectType;
import mcjty.rftools.dimension.world.types.FeatureType;
import mcjty.rftools.playerprops.PlayerExtendedProperties;
import mcjty.varia.Coordinate;
import mcjty.varia.GlobalCoordinate;
import mcjty.varia.Logging;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBed;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.terraingen.ChunkProviderEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.ExplosionEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ForgeEventHandlers {


//    @SubscribeEvent
//    public void onGrassColorRequest(BiomeEvent.GetGrassColor event) {
//        event.
//        event.newColor = BiomeMutator.PINK;
//    }

    @SubscribeEvent
    public void onBlockBreakEvent(BlockEvent.BreakEvent event) {
        BlockProtectors blockProtectors = BlockProtectors.getProtectors(event.world);
        if (blockProtectors == null) {
            return;
        }

        int id = event.world.provider.dimensionId;
        Collection<GlobalCoordinate> protectors = blockProtectors.findProtectors(event.x, event.y, event.z, id, 2);

        for (GlobalCoordinate protector : protectors) {
            int cx = protector.getCoordinate().getX();
            int cy = protector.getCoordinate().getY();
            int cz = protector.getCoordinate().getZ();
            TileEntity te = event.world.getTileEntity(cx, cy, cz);
            if (te instanceof BlockProtectorTileEntity) {
                BlockProtectorTileEntity blockProtectorTileEntity = (BlockProtectorTileEntity) te;
                Coordinate relative = blockProtectorTileEntity.absoluteToRelative(event.x, event.y, event.z);
                boolean b = blockProtectorTileEntity.isProtected(relative);
                if (b) {
                    if (blockProtectorTileEntity.attemptHarvestProtection()) {
                        event.setCanceled(true);
                    } else {
                        blockProtectorTileEntity.removeProtection(relative);
                    }
                    return;
                }
            }
        }
    }


    @SubscribeEvent
    public void onDetonate(ExplosionEvent.Detonate event) {
        BlockProtectors blockProtectors = BlockProtectors.getProtectors(event.world);
        if (blockProtectors == null) {
            return;
        }

        int id = event.world.provider.dimensionId;
        Explosion explosion = event.explosion;
        Collection<GlobalCoordinate> protectors = blockProtectors.findProtectors((int) explosion.explosionX, (int) explosion.explosionY, (int) explosion.explosionZ, id, (int) explosion.explosionSize);

        if (protectors.isEmpty()) {
            return;
        }

        List<ChunkPosition> affectedBlocks = event.getAffectedBlocks();
        List<ChunkPosition> toremove = new ArrayList<ChunkPosition>();

        Vec3 explosionVector = Vec3.createVectorHelper(explosion.explosionX, explosion.explosionY, explosion.explosionZ);

        int rf = 0;
        for (GlobalCoordinate protector : protectors) {
            int cx = protector.getCoordinate().getX();
            int cy = protector.getCoordinate().getY();
            int cz = protector.getCoordinate().getZ();
            TileEntity te = event.world.getTileEntity(cx, cy, cz);
            if (te instanceof BlockProtectorTileEntity) {
                BlockProtectorTileEntity blockProtectorTileEntity = (BlockProtectorTileEntity) te;
                for (ChunkPosition block : affectedBlocks) {
                    Coordinate relative = blockProtectorTileEntity.absoluteToRelative(block.chunkPosX, block.chunkPosY, block.chunkPosZ);
                    boolean b = blockProtectorTileEntity.isProtected(relative);
                    if (b) {
                        Vec3 blockVector = Vec3.createVectorHelper(block.chunkPosX, block.chunkPosY, block.chunkPosZ);<commit_msg>The block protector can now also protect blocks from being sneak-wrenched<commit_after>package mcjty.rftools;

import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import mcjty.rftools.blocks.blockprotector.BlockProtectorTileEntity;
import mcjty.rftools.blocks.blockprotector.BlockProtectors;
import mcjty.rftools.blocks.dimlets.DimletConfiguration;
import mcjty.rftools.blocks.environmental.PeacefulAreaManager;
import mcjty.rftools.dimension.DimensionInformation;
import mcjty.rftools.dimension.DimensionStorage;
import mcjty.rftools.dimension.RfToolsDimensionManager;
import mcjty.rftools.dimension.world.types.EffectType;
import mcjty.rftools.dimension.world.types.FeatureType;
import mcjty.rftools.playerprops.PlayerExtendedProperties;
import mcjty.varia.Coordinate;
import mcjty.varia.GlobalCoordinate;
import mcjty.varia.Logging;
import mcjty.varia.WrenchChecker;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBed;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.terraingen.ChunkProviderEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.ExplosionEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ForgeEventHandlers {

    private Collection<GlobalCoordinate> getProtectors(World world, int x, int y, int z) {
        Collection<GlobalCoordinate> protectors;
        BlockProtectors blockProtectors = BlockProtectors.getProtectors(world);
        if (blockProtectors == null) {
            protectors = Collections.emptyList();
        } else {
            int id = world.provider.dimensionId;
            protectors = blockProtectors.findProtectors(x, y, z, id, 2);
        }
        return protectors;
    }

    @SubscribeEvent
    public void onPlayerInteractEvent(PlayerInteractEvent event) {
        ItemStack heldItem = event.entityPlayer.getHeldItem();
        if (heldItem == null || heldItem.getItem() == null) {
            return;
        }
        if (event.entityPlayer.isSneaking() && WrenchChecker.isAWrench(heldItem.getItem())) {
            // If the block is protected we prevent sneak-wrenching it.
            World world = event.world;
            int x = event.x;
            int y = event.y;
            int z = event.z;
            Collection<GlobalCoordinate> protectors = getProtectors(world, x, y, z);
            checkHarvestProtection(event, x, y, z, world, protectors);
        }

    }

    @SubscribeEvent
    public void onBlockBreakEvent(BlockEvent.BreakEvent event) {
        int x = event.x;
        int y = event.y;
        int z = event.z;
        World world = event.world;
        Collection<GlobalCoordinate> protectors = getProtectors(world, x, y, z);
        checkHarvestProtection(event, x, y, z, world, protectors);
    }

    private void checkHarvestProtection(Event event, int x, int y, int z, World world, Collection<GlobalCoordinate> protectors) {
        for (GlobalCoordinate protector : protectors) {
            int cx = protector.getCoordinate().getX();
            int cy = protector.getCoordinate().getY();
            int cz = protector.getCoordinate().getZ();
            TileEntity te = world.getTileEntity(cx, cy, cz);
            if (te instanceof BlockProtectorTileEntity) {
                BlockProtectorTileEntity blockProtectorTileEntity = (BlockProtectorTileEntity) te;
                Coordinate relative = blockProtectorTileEntity.absoluteToRelative(x, y, z);
                boolean b = blockProtectorTileEntity.isProtected(relative);
                if (b) {
                    if (blockProtectorTileEntity.attemptHarvestProtection()) {
                        event.setCanceled(true);
                    } else {
                        blockProtectorTileEntity.removeProtection(relative);
                    }
                    return;
                }
            }
        }
    }


    @SubscribeEvent
    public void onDetonate(ExplosionEvent.Detonate event) {
        Explosion explosion = event.explosion;
        Collection<GlobalCoordinate> protectors = getProtectors(event.world, (int) explosion.explosionX, (int) explosion.explosionY, (int) explosion.explosionZ);

        if (protectors.isEmpty()) {
            return;
        }

        List<ChunkPosition> affectedBlocks = event.getAffectedBlocks();
        List<ChunkPosition> toremove = new ArrayList<ChunkPosition>();

        Vec3 explosionVector = Vec3.createVectorHelper(explosion.explosionX, explosion.explosionY, explosion.explosionZ);

        int rf = 0;
        for (GlobalCoordinate protector : protectors) {
            int cx = protector.getCoordinate().getX();
            int cy = protector.getCoordinate().getY();
            int cz = protector.getCoordinate().getZ();
            TileEntity te = event.world.getTileEntity(cx, cy, cz);
            if (te instanceof BlockProtectorTileEntity) {
                BlockProtectorTileEntity blockProtectorTileEntity = (BlockProtectorTileEntity) te;
                for (ChunkPosition block : affectedBlocks) {
                    Coordinate relative = blockProtectorTileEntity.absoluteToRelative(block.chunkPosX, block.chunkPosY, block.chunkPosZ);
                    boolean b = blockProtectorTileEntity.isProtected(relative);
                    if (b) {
                        Vec3 blockVector = Vec3.createVectorHelper(block.chunkPosX, block.chunkPosY, block.chunkPosZ);
{'fraction_non_alphanumeric': 0.09119577260717635, 'fraction_numerical': 0.0011932157163555782, 'mean_word_length': 3.2315182113234764, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3828466', 'n_tokens_mistral': 3365, 'n_tokens_neox': 3244, 'n_words': 596}