<commit_before>package notjoe.pointersmod.api.actions;

import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import notjoe.pointersmod.PointersMod;
import notjoe.pointersmod.api.BlockInWorld;
import notjoe.pointersmod.api.PointerAction;
import notjoe.pointersmod.api.helpers.NbtHelper;
import notjoe.pointersmod.common.Config;

import java.util.Collections;
import java.util.List;

public class PointerActionInventory extends PointerAction {
    private boolean ignoreFacing;

    public PointerActionInventory(boolean ignoreFacing) {
        this.ignoreFacing = ignoreFacing;
    }

    @Override
    public boolean setPointerTarget(ItemStack stack, BlockInWorld blockInWorld, World world) {
        NbtHelper.initNbtTagForStack(stack);
        if (blockInWorld.isTileEntity(world)) {
            if (blockInWorld.getTileEntity(world)
                .hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
                    ignoreFacing ? null : blockInWorld.facing)) {
                blockInWorld.serializeNbt(stack.getTagCompound());
                String localizedTargetName =
                    world.getBlockState(blockInWorld.pos).getBlock().getLocalizedName();
                stack.getTagCompound().setString("target_name", localizedTargetName);
                return true;
            }
        }
        return false;
    }

    @Override public boolean pointerActivated(ItemStack stack, World world, EntityPlayer player) {
        if (hasTarget(stack) && isTargetAccessible(stack, world, player) && !world.isRemote) {
            BlockInWorld blockInWorld = getPointerTarget(stack);
            IItemHandler handler = blockInWorld.getTileEntity(world)
                .getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
                    ignoreFacing ? null : blockInWorld.facing);
            if (handler != null) {
                player.openGui(PointersMod.INSTANCE, 0, world, 0, 0, 0);
            }
            return true;
        }
        return false;
    }

    @Override public List<String> getExtraInfo(ItemStack stack) {
        NbtHelper.initNbtTagForStack(stack);
        if (stack.getTagCompound().hasKey("target_name"))
            return Collections.singletonList(I18n.format("pointers.targetname",
                stack.getTagCompound().getString("target_name")));
        return null;<commit_msg>Secondary function for Inventory Pointers<commit_after>package notjoe.pointersmod.api.actions;

import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import notjoe.pointersmod.PointersMod;
import notjoe.pointersmod.api.BlockInWorld;
import notjoe.pointersmod.api.PointerAction;
import notjoe.pointersmod.api.helpers.NbtHelper;
import notjoe.pointersmod.common.Config;

import java.util.Collections;
import java.util.List;

public class PointerActionInventory extends PointerAction {
    private boolean ignoreFacing;

    public PointerActionInventory(boolean ignoreFacing) {
        this.ignoreFacing = ignoreFacing;
    }

    @Override
    public boolean setPointerTarget(ItemStack stack, BlockInWorld blockInWorld, World world) {
        NbtHelper.initNbtTagForStack(stack);
        if (blockInWorld.isTileEntity(world)) {
            if (blockInWorld.getTileEntity(world)
                .hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
                    ignoreFacing ? null : blockInWorld.facing)) {
                blockInWorld.serializeNbt(stack.getTagCompound());
                String localizedTargetName =
                    world.getBlockState(blockInWorld.pos).getBlock().getLocalizedName();
                stack.getTagCompound().setString("target_name", localizedTargetName);
                return true;
            }
        }
        return false;
    }

    @Override public boolean pointerActivated(ItemStack stack, World world, EntityPlayer player) {
        if (hasTarget(stack) && isTargetAccessible(stack, world, player) && !world.isRemote) {
            BlockInWorld blockInWorld = getPointerTarget(stack);
            IItemHandler handler = blockInWorld.getTileEntity(world)
                .getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
                    ignoreFacing ? null : blockInWorld.facing);
            if (handler != null) {
                player.openGui(PointersMod.INSTANCE, 0, world, 0, 0, 0);
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean pointerActivatedSecondary(ItemStack stack, World world, EntityPlayer player) {
        if(hasTarget(stack) && isTargetAccessible(stack, world, player)) {
            IItemHandler targetHandler = getStackHandler(stack, world, player);
            for(int i = 0; i < player.inventory.mainInventory.length; i++) {
                if(player.inventory.mainInventory[i] != null && player.inventory.mainInventory[i] != stack) {
                    player.inventory.mainInventory[i] = moveStackToHandler(player.inventory.mainInventory[i], targetHandler);
                }
            }
            player.inventory.markDirty();

            return true;
        }

        return false;
    }

    @Override public List<String> getExtraInfo(ItemStack stack) {
        NbtHelper.initNbtTagForStack(stack);
        if (stack.getTagCompound().hasKey("target_name"))
            return Collections.singletonList(I18n.format("pointers.targetname",
                stack.getTagCompound().getString("target_name")));
        return null;
    }

    private ItemStack moveStackToHandler(ItemStack stack, IItemHandler handler) {
        // First pass: Try to put stacks with others
        for(int i = 0; i < handler.getSlots(); i++) {
            if(handler.getStackInSlot(i) != null && handler.getStackInSlot(i).isItemEqual(stack)) {
                stack = handler.insertItem(i, stack, false);
            }
        }
        // Second pass: Try to put stack in an empty slot
        if(stack != null) {
            for(int i = 0; i < handler.getSlots(); i++) {
                if(handler.getStackInSlot(i) == null) {
                    stack = handler.insertItem(i, stack, false);
                    break;
                }
            }
        }

        return stack;
    }

    @Override public boolean isTargetAccessible(ItemStack stack, World world, EntityPlayer player) {
        BlockInWorld target = new BlockInWorld(stack.getTagCompound());
        return super.isTargetAccessible(stack, world, player) &&
            target.isTileEntity(world) && target.getTileEntity(world).hasCapability(
            CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, target.facing);

{'fraction_non_alphanumeric': 0.09356308743947593, 'fraction_numerical': 0.0027057818285388777, 'mean_word_length': 2.771750805585392, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, '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': '831242', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1904, 'n_words': 354}