<commit_before>package romelo333.notenoughwands.Items;


import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import romelo333.notenoughwands.Config;
import romelo333.notenoughwands.ProtectedBlocks;
import romelo333.notenoughwands.varia.Tools;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SwappingWand extends GenericWand {

    public static final int MODE_FIRST = 0;
    public static final int MODE_3X3 = 0;
    public static final int MODE_5X5 = 1;
    public static final int MODE_7X7 = 2;
    public static final int MODE_SINGLE = 3;
    public static final int MODE_LAST = MODE_SINGLE;

    private float hardnessDistance = 35.0f;

    public static final String[] descriptions = new String[] {
        "3x3", "5x5", "7x7", "single"
    };

    public SwappingWand() {
        setup("swapping_wand").xpUsage(1).availability(AVAILABILITY_NORMAL).loot(5);
    }

    @Override
    public void initConfig(Configuration cfg) {
        super.initConfig(cfg, 2000, 100000, 500, 200000, 200, 500000);
        hardnessDistance = (float) cfg.get(Config.CATEGORY_WANDS, getConfigPrefix() + "_hardnessDistance", hardnessDistance, "How far away the hardness can be to allow swapping (100 means basically everything allowed)").getDouble();
    }

    @Override
    public void toggleMode(EntityPlayer player, ItemStack stack) {
        int mode = getMode(stack);
        mode++;
        if (mode > MODE_LAST) {
            mode = MODE_FIRST;
        }
        Tools.notify(player, "Switched to " + descriptions[mode] + " mode");
        Tools.getTagCompound(stack).setInteger("mode", mode);
    }

    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean b) {
        super.addInformation(stack, player, list, b);
        NBTTagCompound compound = stack.getTagCompound();
        if (compound == null) {
            list.add(TextFormatting.RED + "No selected block");
        } else {
            int id = compound.getInteger("block");
            Block block = Block.REGISTRY.getObjectById(id);
            int meta = compound.getInteger("meta");
            String name = Tools.getBlockName(block, meta);
            list.add(TextFormatting.GREEN + "Selected block: " + name);
            list.add(TextFormatting.GREEN + "Mode: " + descriptions[compound.getInteger("mode")]);
        }
        list.add("Sneak right click to select a block.");
        list.add("Right click on block to replace.");
        showModeKeyDescription(list, "switch mode");
    }

    @Override
    protected EnumActionResult clOnItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
        ItemStack stack = player.getHeldItem(hand);
        if (!world.isRemote) {
            if (player.isSneaking()) {
                selectBlock(stack, player, world, pos);
            } else {
                placeBlock(stack, player, world, pos, side);
            }
        }
        return EnumActionResult.SUCCESS;
    }

    private void placeBlock(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side) {
        if (!checkUsage(stack, player, 1.0f)) {
            return;
        }

        NBTTagCompound tagCompound = stack.getTagCompound();
        if (tagCompound == null) {
            Tools.error(player, "First select a block by sneaking");
            return;
        }
        int id = tagCompound.getInteger("block");
        Block block = Block.REGISTRY.getObjectById(id);
        int meta = tagCompound.getInteger("meta");
        float hardness = tagCompound.getFloat("hardness");

        IBlockState oldState = world.getBlockState(pos);
        Block oldblock = oldState.getBlock();

        double cost = BlackListSettings.getBlacklistCost(oldblock);
        if (cost <= 0.001f) {
            Tools.error(player, "It is illegal to swap this block");
            return;
        }

        int oldmeta = oldblock.getMetaFromState(oldState);
        float blockHardness = oldblock.getBlockHardness(oldState, world, pos);

        if (block == oldblock && meta == oldmeta) {
            // The same, nothing happens.
            return;
        }

        if (blockHardness < -0.1f) {
            Tools.error(player, "This block cannot be swapped!");<commit_msg>New mode for the swapping wand to swap with the block in off hand<commit_after>package romelo333.notenoughwands.Items;


import mcjty.lib.tools.ItemStackTools;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import romelo333.notenoughwands.Config;
import romelo333.notenoughwands.ProtectedBlocks;
import romelo333.notenoughwands.varia.Tools;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SwappingWand extends GenericWand {

    public static final int MODE_FIRST = 0;
    public static final int MODE_3X3 = 0;
    public static final int MODE_5X5 = 1;
    public static final int MODE_7X7 = 2;
    public static final int MODE_SINGLE = 3;
    public static final int MODE_LAST = MODE_SINGLE;

    private float hardnessDistance = 35.0f;

    public static final String[] descriptions = new String[] {
        "3x3", "5x5", "7x7", "single"
    };

    public SwappingWand() {
        setup("swapping_wand").xpUsage(1).availability(AVAILABILITY_NORMAL).loot(5);
    }

    @Override
    public void initConfig(Configuration cfg) {
        super.initConfig(cfg, 2000, 100000, 500, 200000, 200, 500000);
        hardnessDistance = (float) cfg.get(Config.CATEGORY_WANDS, getConfigPrefix() + "_hardnessDistance", hardnessDistance, "How far away the hardness can be to allow swapping (100 means basically everything allowed)").getDouble();
    }

    @Override
    public void toggleMode(EntityPlayer player, ItemStack stack) {
        int mode = getMode(stack);
        mode++;
        if (mode > MODE_LAST) {
            mode = MODE_FIRST;
        }
        Tools.notify(player, "Switched to " + descriptions[mode] + " mode");
        Tools.getTagCompound(stack).setInteger("mode", mode);
    }

    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean b) {
        super.addInformation(stack, player, list, b);
        NBTTagCompound compound = stack.getTagCompound();
        if (compound == null) {
            list.add(TextFormatting.RED + "No selected block");
        } else {
            if (isSwappingWithOffHand(stack)) {
                list.add(TextFormatting.GREEN + "Will swap with block in offhand");
            } else {
                int id = compound.getInteger("block");
                Block block = Block.REGISTRY.getObjectById(id);
                if (block != Blocks.AIR) {
                    int meta = compound.getInteger("meta");
                    String name = Tools.getBlockName(block, meta);
                    list.add(TextFormatting.GREEN + "Selected block: " + name);
                    list.add(TextFormatting.GREEN + "Mode: " + descriptions[compound.getInteger("mode")]);
                }
            }
        }
        list.add("Sneak right click to select a block.");
        list.add("Right click in empty air to select 'offhand' mode.");
        list.add("Right click on block to replace.");
        showModeKeyDescription(list, "switch mode");
    }

    private static boolean isSwappingWithOffHand(ItemStack stack) {
        NBTTagCompound compound = stack.getTagCompound();
        if (compound == null) {
            return false;
        }
        return compound.hasKey("offhand");
    }

    private static void enableSwappingWithOffHand(ItemStack stack) {
        NBTTagCompound compound = stack.getTagCompound();
        if (compound == null) {
            compound = new NBTTagCompound();
            stack.setTagCompound(compound);
        }
        compound.setBoolean("offhand", true);
    }

    private static void disableSwappingWithOffHand(ItemStack stack) {
        NBTTagCompound compound = stack.getTagCompound();
        if (compound == null) {
            return;
        }
        compound.removeTag("offhand");
    }

    @Override
    protected ActionResult<ItemStack> clOnItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand hand) {
        ItemStack heldItem = playerIn.getHeldItem(hand);
        if (ItemStackTools.isValid(heldItem)) {
            if (isSwappingWithOffHand(heldItem)) {
                disableSwappingWithOffHand(heldItem);
                if (worldIn.isRemote) {
                    Tools.notify(playerIn, "Switched to swapping with selected block");
                }
            } else {
                enableSwappingWithOffHand(heldItem);
                if (worldIn.isRemote) {
                    Tools.notify(playerIn, "Switched to swapping with block in offhand");
                }
            }
        }
        return super.clOnItemRightClick(worldIn, playerIn, hand);
    }

    @Override
    protected EnumActionResult clOnItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
        ItemStack stack = player.getHeldItem(hand);
        if (!world.isRemote) {
            if (player.isSneaking()) {
                selectBlock(stack, player, world, pos);
            } else {
                placeBlock(stack, player, world, pos, side);
            }
        }
        return EnumActionResult.SUCCESS;
    }

    private void placeBlock(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side) {
        if (!checkUsage(stack, player, 1.0f)) {
            return;
        }

        NBTTagCompound tagCompound = stack.getTagCompound();
        if (tagCompound == null) {
            Tools.error(player, "First select a block by sneaking");
            return;
        }

        Block block;
        int meta;
        float hardness;

        if (isSwappingWithOffHand(stack)) {
            ItemStack off = player.getHeldItemOffhand();
            if (ItemStackTools.isEmpty(off)) {
                Tools.error(player, "You need to hold a block in your offhand!");
                return;
            }
            if (!(off.getItem() instanceof ItemBlock)) {
                Tools.error(player, "The item in your offhand cannot be placed!");
                return;
            }
            ItemBlock itemBlock = (ItemBlock) off.getItem();
            block = itemBlock.getBlock();
            meta = itemBlock.getDamage(off);
            IBlockState s = block.getStateFromMeta(meta);
            hardness = s.getBlockHardness(world, pos);
        } else {
            int id = tagCompound.getInteger("block");
            block = Block.REGISTRY.getObjectById(id);
            meta = tagCompound.getInteger("meta");
            hardness = tagCompound.getFloat("hardness");
        }

        IBlockState oldState = world.getBlockState(pos);
        Block oldblock = oldState.getBlock();

        double cost = BlackListSettings.getBlacklistCost(oldblock);
        if (cost <= 0.001f) {
            Tools.error(player, "It is illegal to swap this block");
            return;
        }

        int oldmeta = oldblock.getMetaFromState(oldState);
        float blockHardness = oldblock.getBlockHardness(oldState, world, pos);

        if (block == oldblock && meta == oldmeta) {
            // The same, nothing happens.
            return;
        }

        if (blockHardness < -0.1f) {
            Tools.error(player, "This block cannot be swapped!");
{'fraction_non_alphanumeric': 0.10202103065105526, 'fraction_numerical': 0.01088820941158923, 'mean_word_length': 2.6891334250343877, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, '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': '980299', 'n_tokens_mistral': 3933, 'n_tokens_neox': 3794, 'n_words': 908}