<commit_before>package com.lothrazar.cyclic.item.storagebag;

import com.lothrazar.cyclic.ModCyclic;
import com.lothrazar.cyclic.base.ItemBase;
import com.lothrazar.cyclic.registry.ContainerScreenRegistry;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.DyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;

public class StorageBagItem extends ItemBase {

  private static final String NBT_COLOUR = "COLOUR";
  public static final int REFILL_TICKS = 4;
  public int timer = 0;
  private int slots;

  public enum PickupMode implements IStringSerializable {

    NOTHING, EVERYTHING, FILTER;

    public static final String NBT = "pickup_mode";

    @Override
    public String getString() {
      return this.name().toLowerCase();
    }
  }

  public enum DepositMode implements IStringSerializable {

    NOTHING, DUMP, MERGE;

    public static final String NBT = "deposit_mode";

    @Override
    public String getString() {
      return this.name().toLowerCase();
    }
  }

  public enum RefillMode implements IStringSerializable {

    NOTHING, HOTBAR;

    public static final String NBT = "refill_mode";

    @Override
    public String getString() {
      return this.name().toLowerCase();
    }
  }

  public StorageBagItem(Properties properties) {
    super(properties);
  }

  public StorageBagItem(Properties properties, int slots) {
    this(properties);
    this.slots = slots;
  }

  @Override
  public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
    if (!worldIn.isRemote && !playerIn.isCrouching()) {
      NetworkHooks.openGui((ServerPlayerEntity) playerIn, new StorageBagContainerProvider(), playerIn.getPosition());
    }
    return super.onItemRightClick(worldIn, playerIn, handIn);
  }

  public static void setColour(ItemStack stack, DyeColor col) {
    CompoundNBT tags = stack.getOrCreateTag();
    tags.putInt(NBT_COLOUR, col.getColorValue());
  }

  public static int getColour(ItemStack stack) {
    CompoundNBT tags = stack.getOrCreateTag();
    if (tags.contains(NBT_COLOUR) == false) {
      return DyeColor.BROWN.getColorValue(); //BROWN as default for normal look
    }
    return tags.getInt(NBT_COLOUR);
  }

  @Override
  public ActionResultType onItemUse(ItemUseContext context) {
    BlockPos pos = context.getPos();
    Direction face = context.getFace();
    World world = context.getWorld();
    TileEntity te = world.getTileEntity(pos);
    ItemStack bag = context.getItem();
    DepositMode mode = getDepositMode(bag);
    ItemStackHandler handler = getInventory(bag);
    if (handler != null && te != null && te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face).isPresent()) {
      IItemHandler teHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face).orElse(null);
      Set<Item> itemsInTargetInventory = new HashSet<>();
      if (teHandler != null) {
        for (int j = 0; j < teHandler.getSlots(); j++) {
          itemsInTargetInventory.add(teHandler.getStackInSlot(j).getItem());
        }
      }
      for (int i = 0; i < handler.getSlots(); i++) {
        ItemStack stack = handler.getStackInSlot(i);
        ItemStack remaining = ItemHandlerHelper.copyStackWithSize(stack, stack.getCount());
        if (!stack.isEmpty()) {
          if (mode == DepositMode.DUMP || (mode == DepositMode.MERGE && itemsInTargetInventory.contains(stack.getItem()))) {
            remaining = ItemHandlerHelper.insertItem(teHandler, stack, false);
            handler.setStackInSlot(i, remaining);
          }
        }
      }
      return ActionResultType.SUCCESS;
    }
    return ActionResultType.PASS;
  }

  @Override
  public void addInformation(ItemStack stack, World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
    super.addInformation(stack, worldIn, tooltip, flagIn);
    CompoundNBT nbt = stack.getOrCreateTag();
    String pickupMode = nbt.getString(PickupMode.NBT);
    String depositMode = nbt.getString("deposit_mode");
    String refillMode = nbt.getString("refill_mode");
    if (!pickupMode.equals("")) {
      tooltip.add(new TranslationTextComponent("item.cyclic.storage_bag.tooltip.pickup",
          new TranslationTextComponent(String.format(
              pickupMode.equals("nothing") ? "item.cyclic.storage_bag.disabled" : "item.cyclic.storage_bag.pickup.%s", pickupMode)))
                  .mergeStyle(TextFormatting.GREEN));
    }
    if (!depositMode.equals("")) {
      tooltip.add(new TranslationTextComponent("item.cyclic.storage_bag.tooltip.deposit",
          new TranslationTextComponent(String.format(
              depositMode.equals("nothing") ? "item.cyclic.storage_bag.disabled" : "item.cyclic.storage_bag.deposit.%s", depositMode)))
                  .mergeStyle(TextFormatting.BLUE));
    }
    if (!refillMode.equals("")) {
      tooltip.add(new TranslationTextComponent("item.cyclic.storage_bag.tooltip.refill",
          new TranslationTextComponent(String.format(
              refillMode.equals("nothing") ? "item.cyclic.storage_bag.disabled" : "item.cyclic.storage_bag.refill.%s", refillMode)))
                  .mergeStyle(TextFormatting.RED));
    }
  }

  @Override
  public void registerClient() {
    ScreenManager.registerFactory(ContainerScreenRegistry.storage_bag, StorageBagScreen::new);
  }

  @Override
  public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) {
    return new StorageBagCapabilityProvider(stack, slots);
  }

  @Override
  public CompoundNBT getShareTag(ItemStack stack) {
    CompoundNBT tag = super.getShareTag(stack);
    ItemStackHandler handler = getInventory(stack);
    if (handler != null) {
      tag.merge(handler.serializeNBT());
      ModCyclic.LOGGER.info("getShareTag capability merge" + tag);
    }
    return tag;
  }

  @Override
  public void readShareTag(ItemStack stack, CompoundNBT nbt) {
    super.readShareTag(stack, nbt);
  }

  @Override
  public void inventoryTick(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
    timer++;
    if (timer < REFILL_TICKS) {
      return;
    }
    timer = 0;
    if (!world.isRemote && entity instanceof PlayerEntity) {
      if (getRefillMode(stack) == RefillMode.HOTBAR) {
        tryRefillHotbar(stack, (PlayerEntity) entity);
      }
    }
  }

  private void tryRefillHotbar(ItemStack bag, PlayerEntity player) {
    ItemStackHandler handler = getInventory(bag);
    if (handler == null) {
      return;
    }
    for (int i = 0; i < Math.min(9, player.inventory.getSizeInventory()); i++) { //hotbar is 0-8, but do a Math.min in case some mod reduces inventory size? Idk, why not.
      ItemStack stack = player.inventory.getStackInSlot(i);
      if (!stack.isEmpty()) {
        //we found a non
        int slot = getLastSlotWithStack(bag, stack);<commit_msg>#1704 revert shareTag compatibility on bag to fix server-client null pointer<commit_after>package com.lothrazar.cyclic.item.storagebag;

import com.lothrazar.cyclic.base.ItemBase;
import com.lothrazar.cyclic.registry.ContainerScreenRegistry;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.DyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;

public class StorageBagItem extends ItemBase {

  private static final String NBT_COLOUR = "COLOUR";
  public static final int REFILL_TICKS = 4;
  public int timer = 0;
  private int slots;

  public enum PickupMode implements IStringSerializable {

    NOTHING, EVERYTHING, FILTER;

    public static final String NBT = "pickup_mode";

    @Override
    public String getString() {
      return this.name().toLowerCase();
    }
  }

  public enum DepositMode implements IStringSerializable {

    NOTHING, DUMP, MERGE;

    public static final String NBT = "deposit_mode";

    @Override
    public String getString() {
      return this.name().toLowerCase();
    }
  }

  public enum RefillMode implements IStringSerializable {

    NOTHING, HOTBAR;

    public static final String NBT = "refill_mode";

    @Override
    public String getString() {
      return this.name().toLowerCase();
    }
  }

  public StorageBagItem(Properties properties) {
    super(properties);
  }

  public StorageBagItem(Properties properties, int slots) {
    this(properties);
    this.slots = slots;
  }

  @Override
  public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
    if (!worldIn.isRemote && !playerIn.isCrouching()) {
      NetworkHooks.openGui((ServerPlayerEntity) playerIn, new StorageBagContainerProvider(), playerIn.getPosition());
    }
    return super.onItemRightClick(worldIn, playerIn, handIn);
  }

  public static void setColour(ItemStack stack, DyeColor col) {
    CompoundNBT tags = stack.getOrCreateTag();
    tags.putInt(NBT_COLOUR, col.getColorValue());
  }

  public static int getColour(ItemStack stack) {
    CompoundNBT tags = stack.getOrCreateTag();
    if (tags.contains(NBT_COLOUR) == false) {
      return DyeColor.BROWN.getColorValue(); //BROWN as default for normal look
    }
    return tags.getInt(NBT_COLOUR);
  }

  @Override
  public ActionResultType onItemUse(ItemUseContext context) {
    BlockPos pos = context.getPos();
    Direction face = context.getFace();
    World world = context.getWorld();
    TileEntity te = world.getTileEntity(pos);
    ItemStack bag = context.getItem();
    DepositMode mode = getDepositMode(bag);
    ItemStackHandler handler = getInventory(bag);
    if (handler != null && te != null && te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face).isPresent()) {
      IItemHandler teHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face).orElse(null);
      Set<Item> itemsInTargetInventory = new HashSet<>();
      if (teHandler != null) {
        for (int j = 0; j < teHandler.getSlots(); j++) {
          itemsInTargetInventory.add(teHandler.getStackInSlot(j).getItem());
        }
      }
      for (int i = 0; i < handler.getSlots(); i++) {
        ItemStack stack = handler.getStackInSlot(i);
        ItemStack remaining = ItemHandlerHelper.copyStackWithSize(stack, stack.getCount());
        if (!stack.isEmpty()) {
          if (mode == DepositMode.DUMP || (mode == DepositMode.MERGE && itemsInTargetInventory.contains(stack.getItem()))) {
            remaining = ItemHandlerHelper.insertItem(teHandler, stack, false);
            handler.setStackInSlot(i, remaining);
          }
        }
      }
      return ActionResultType.SUCCESS;
    }
    return ActionResultType.PASS;
  }

  @Override
  public void addInformation(ItemStack stack, World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
    super.addInformation(stack, worldIn, tooltip, flagIn);
    CompoundNBT nbt = stack.getOrCreateTag();
    String pickupMode = nbt.getString(PickupMode.NBT);
    String depositMode = nbt.getString("deposit_mode");
    String refillMode = nbt.getString("refill_mode");
    if (!pickupMode.equals("")) {
      tooltip.add(new TranslationTextComponent("item.cyclic.storage_bag.tooltip.pickup",
          new TranslationTextComponent(String.format(
              pickupMode.equals("nothing") ? "item.cyclic.storage_bag.disabled" : "item.cyclic.storage_bag.pickup.%s", pickupMode)))
                  .mergeStyle(TextFormatting.GREEN));
    }
    if (!depositMode.equals("")) {
      tooltip.add(new TranslationTextComponent("item.cyclic.storage_bag.tooltip.deposit",
          new TranslationTextComponent(String.format(
              depositMode.equals("nothing") ? "item.cyclic.storage_bag.disabled" : "item.cyclic.storage_bag.deposit.%s", depositMode)))
                  .mergeStyle(TextFormatting.BLUE));
    }
    if (!refillMode.equals("")) {
      tooltip.add(new TranslationTextComponent("item.cyclic.storage_bag.tooltip.refill",
          new TranslationTextComponent(String.format(
              refillMode.equals("nothing") ? "item.cyclic.storage_bag.disabled" : "item.cyclic.storage_bag.refill.%s", refillMode)))
                  .mergeStyle(TextFormatting.RED));
    }
  }

  @Override
  public void registerClient() {
    ScreenManager.registerFactory(ContainerScreenRegistry.storage_bag, StorageBagScreen::new);
  }

  @Override
  public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) {
    return new StorageBagCapabilityProvider(stack, slots);
  }

  @Override
  public void inventoryTick(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
    timer++;
    if (timer < REFILL_TICKS) {
      return;
    }
    timer = 0;
    if (!world.isRemote && entity instanceof PlayerEntity) {
      if (getRefillMode(stack) == RefillMode.HOTBAR) {
        tryRefillHotbar(stack, (PlayerEntity) entity);
      }
    }
  }

  private void tryRefillHotbar(ItemStack bag, PlayerEntity player) {
    ItemStackHandler handler = getInventory(bag);
    if (handler == null) {
      return;
    }
    for (int i = 0; i < Math.min(9, player.inventory.getSizeInventory()); i++) { //hotbar is 0-8, but do a Math.min in case some mod reduces inventory size? Idk, why not.
      ItemStack stack = player.inventory.getStackInSlot(i);
      if (!stack.isEmpty()) {
        //we found a non
        int slot = getLastSlotWithStack(bag, stack);
{'fraction_non_alphanumeric': 0.10911497730711044, 'fraction_numerical': 0.0013867876954109935, 'mean_word_length': 3.9439077594266125, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, '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': '1891743', 'n_tokens_mistral': 4838, 'n_tokens_neox': 4712, 'n_words': 969}