<commit_before>package com.lothrazar.command;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;   

import com.lothrazar.util.SamsUtilities;

import net.minecraftforge.fml.common.FMLCommonHandler; 
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

public class CommandSearchItem  implements ICommand
{
	private ArrayList<String> aliases = new ArrayList<String>();
	private static int RADIUS = 32;
	public static boolean showCoords = true;   
 
	public static boolean REQUIRES_OP; 
	
	public CommandSearchItem()
	{
		aliases.add("is");
	}
	 
	@Override
	public boolean canCommandSenderUse(ICommandSender ic)
	{
		return (REQUIRES_OP) ? ic.canUseCommand(2, this.getName()) : true; 
	}
	  
	@Override
	public List getAliases() 
	{  
		return aliases;
	}

	@Override
	public String getName() 
	{ 
		return "searchitem";
	}

	@Override
	public String getCommandUsage(ICommandSender arg0) 
	{ 
		return "/" + getName()+" <itemname>";
	}

	@Override
	public boolean isUsernameIndex(String[] arg0, int arg1) 
	{ 
		return false;
	}
  
	@Override
	public void execute(ICommandSender sender, String[] args) 
	{ 
		if (!(sender instanceof EntityPlayerMP)) {return;}
			
		EntityPlayerMP player = (EntityPlayerMP) sender;
		if (args.length < 1)
		{
			player.addChatMessage(new ChatComponentTranslation(getCommandUsage(sender))); 
			return;
		}
		
		String searchQuery = args[0].trim().toLowerCase(); // args[0] is the command name or alias used such as "is"
		ArrayList<IInventory> tilesToSearch = new ArrayList<IInventory>();
		HashMap<IInventory,BlockPos> dictionary = new HashMap<IInventory,BlockPos> ();
		
		ArrayList<BlockPos> foundChests = SamsUtilities.findBlocks(player, Blocks.chest, RADIUS); 
		for (BlockPos pos : foundChests)
		{  	
			TileEntity tile = player.worldObj.getTileEntity(pos); 
			if(tile == null || !(tile instanceof IInventory) ) {continue;}  
			tilesToSearch.add((IInventory) tile); 
			dictionary.put((IInventory) tile,pos);
		}
		ArrayList<BlockPos> foundTrapChests = SamsUtilities.findBlocks(player, Blocks.trapped_chest, RADIUS); 
		for (BlockPos pos : foundTrapChests)
		{  	
			TileEntity tile = player.worldObj.getTileEntity(pos); 
			if(tile == null || !(tile instanceof IInventory) ) {continue;}  
			tilesToSearch.add((IInventory) tile); 
			dictionary.put((IInventory) tile,pos);
		}
		ArrayList<BlockPos> foundDisp = SamsUtilities.findBlocks(player, Blocks.dispenser, RADIUS); 
		for (BlockPos pos : foundDisp)
		{  	
			TileEntity tile = player.worldObj.getTileEntity(pos); 
			if(tile == null || !(tile instanceof IInventory) ) {continue;}  
			tilesToSearch.add((IInventory) tile); 
			dictionary.put((IInventory) tile,pos);
		}
		
		int foundQtyTotal;
		ArrayList<String> foundMessages = new ArrayList<String>();
	 
		for(IInventory inventory : tilesToSearch)
		{ 
			foundQtyTotal = 0;
			foundQtyTotal = searchTileInventory(searchQuery, inventory);
			
			if(foundQtyTotal > 0)
			{
				//something was found in this box?
				//
				foundMessages.add(itemLocDisplay(player,dictionary.get(inventory),foundQtyTotal));
			}  
		}
 
		//LOOP on foundItems and chat out so each line of chat will be an zyx of a chest that contains 
		int ifound = foundMessages.size();
		
		if(ifound == 0)
		{ 
			player.addChatMessage(new ChatComponentTranslation("No items found within "+RADIUS+" blocks of you."));
		}
		else
		{ 
			for (int i = 0; i < ifound; i++) 
			{  
				player.addChatMessage(new ChatComponentTranslation(foundMessages.get(i)));
		    }
		}
	}

	private int searchTileInventory(String search, IInventory inventory) {
		int foundQty;
		foundQty = 0;
		//foundStacks = 0;
		
		for (int slot = 0; slot < inventory.getSizeInventory(); slot++)//a break; will cancel this loop
		{
			ItemStack invItem = inventory.getStackInSlot(slot);
			if(invItem == null){continue;} //empty slot in chest (or container)
			 
			String invItemName = invItem.getDisplayName().toLowerCase(); 
			
			//find any overlap: so if x ==y , or if x substring of y, or y substring of x 
			if(search.equals(invItemName) 
				|| search.contains(invItemName)
				|| invItemName.contains(search))
			{  
				//foundStacks++;
				foundQty += invItem.stackSize; 
			} 
		} //end loop on current tile entity
		return foundQty;
	}
	
	private static String itemLocDisplay(	EntityPlayerMP player, BlockPos pos ,int foundQty )
	{   
		int xLoop = pos.getX();
		int yLoop = pos.getY();
		int zLoop = pos.getZ();
		
		String totalsStr = foundQty + " found";
		
		if(showCoords )
		{ 
			return "(" + xLoop + ", " + yLoop + ", " + zLoop + ")" + " : "+ totalsStr;
		}
		//else do this other thing with directions
		
		int xDist,yDist,zDist;
		
		xDist = (int) player.posX - xLoop;
		yDist = (int) player.posY - yLoop;
		zDist = (int) player.posZ - zLoop;
		
		//in terms of directon copmass:
		//North is -z;  south is +z		
		//east is +x, west is -x
		
		//so for Distances: 
		
		boolean isNorth = (zDist > 0);
		boolean isSouth = (zDist < 0);
		
		boolean isWest = (xDist > 0);<commit_msg>searchItem: some tiny refactors, remove some unneeded checks<commit_after>package com.lothrazar.command;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;   

import com.lothrazar.util.SamsUtilities;

import net.minecraftforge.fml.common.FMLCommonHandler; 
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

public class CommandSearchItem  implements ICommand
{
	private ArrayList<String> aliases = new ArrayList<String>();
	private static int RADIUS = 32;
	public static boolean showCoords = true;   
 
	public static boolean REQUIRES_OP; 
	
	public CommandSearchItem()
	{
		aliases.add("is");
	}
	 
	@Override
	public boolean canCommandSenderUse(ICommandSender ic)
	{
		return (REQUIRES_OP) ? ic.canUseCommand(2, this.getName()) : true; 
	}
	  
	@Override
	public List getAliases() 
	{  
		return aliases;
	}

	@Override
	public String getName() 
	{ 
		return "searchitem";
	}

	@Override
	public String getCommandUsage(ICommandSender arg0) 
	{ 
		return "/" + getName()+" <itemname>";
	}

	@Override
	public boolean isUsernameIndex(String[] arg0, int arg1) 
	{ 
		return false;
	}
  
	@Override
	public void execute(ICommandSender sender, String[] args) 
	{ 
		if (!(sender instanceof EntityPlayerMP)) {return;}
			
		EntityPlayerMP player = (EntityPlayerMP) sender;
		if (args.length < 1)
		{
			player.addChatMessage(new ChatComponentTranslation(getCommandUsage(sender))); 
			return;
		}
		
		String searchQuery = args[0].trim().toLowerCase(); // args[0] is the command name or alias used such as "is"
		ArrayList<IInventory> tilesToSearch = new ArrayList<IInventory>();
		HashMap<IInventory,BlockPos> dictionary = new HashMap<IInventory,BlockPos> ();
		IInventory tile;
		
		ArrayList<BlockPos> foundChests = SamsUtilities.findBlocks(player, Blocks.chest, RADIUS); 
		for (BlockPos pos : foundChests)
		{  	
			tile = (IInventory)player.worldObj.getTileEntity(pos);  
			tilesToSearch.add(tile); 
			dictionary.put(tile, pos);
		}
		ArrayList<BlockPos> foundTrapChests = SamsUtilities.findBlocks(player, Blocks.trapped_chest, RADIUS); 
		for (BlockPos pos : foundTrapChests)
		{  	
			tile = (IInventory)player.worldObj.getTileEntity(pos);  
			tilesToSearch.add(tile); 
			dictionary.put(tile, pos);
		}
		ArrayList<BlockPos> foundDisp = SamsUtilities.findBlocks(player, Blocks.dispenser, RADIUS); 
		for (BlockPos pos : foundDisp)
		{  	
			tile = (IInventory)player.worldObj.getTileEntity(pos);  
			tilesToSearch.add(tile); 
			dictionary.put(tile, pos);
		}
		
		int foundQtyTotal;
		ArrayList<String> foundMessages = new ArrayList<String>();
	 
		for(IInventory inventory : tilesToSearch)
		{ 
			foundQtyTotal = 0;
			foundQtyTotal = searchTileInventory(searchQuery, inventory);
			
			if(foundQtyTotal > 0)
			{ 
				foundMessages.add(itemLocDisplay(player,dictionary.get(inventory),foundQtyTotal));
			}  
		}
  
		int ifound = foundMessages.size();
		
		if(ifound == 0) //TODO: put that in .lang file
		{ 
			player.addChatMessage(new ChatComponentTranslation("No items found within " + RADIUS + " blocks of you."));
		}
		else
		{ 
			for (int i = 0; i < ifound; i++) 
			{  
				player.addChatMessage(new ChatComponentTranslation(foundMessages.get(i)));
		    }
		}
	}

	private int searchTileInventory(String search, IInventory inventory) 
	{
		int foundQty;
		foundQty = 0; 
		
		for (int slot = 0; slot < inventory.getSizeInventory(); slot++)//a break; will cancel this loop
		{
			ItemStack invItem = inventory.getStackInSlot(slot);
			if(invItem == null){continue;} //empty slot in chest (or container)
			 
			String invItemName = invItem.getDisplayName().toLowerCase(); 
			
			//find any overlap: so if x ==y , or if x substring of y, or y substring of x 
			if(search.equals(invItemName) 
				|| search.contains(invItemName)
				|| invItemName.contains(search))
			{  
				//foundStacks++;
				foundQty += invItem.stackSize; 
			} 
		} //end loop on current tile entity
		return foundQty;
	}
	
	private static String itemLocDisplay(	EntityPlayerMP player, BlockPos pos ,int foundQty )
	{   
		int xLoop = pos.getX();
		int yLoop = pos.getY();
		int zLoop = pos.getZ();
		
		String totalsStr = foundQty + " found"; //TODO: put that in .lang file
		
		if(showCoords )
		{ 
			return "(" + xLoop + ", " + yLoop + ", " + zLoop + ")" + " : "+ totalsStr;
		}
		//else do this other thing with directions
		
		int xDist,yDist,zDist;
		
		xDist = (int) player.posX - xLoop;
		yDist = (int) player.posY - yLoop;
		zDist = (int) player.posZ - zLoop;
		
		//in terms of directon copmass:
		//North is -z;  south is +z		
		//east is +x, west is -x
		
		//so for Distances: 
		
		boolean isNorth = (zDist > 0);
		boolean isSouth = (zDist < 0);
		
		boolean isWest = (xDist > 0);
{'fraction_non_alphanumeric': 0.11711796121053125, 'fraction_numerical': 0.003466691651831725, 'mean_word_length': 3.7844016136261764, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 33, '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': '5933890', 'n_tokens_mistral': 4002, 'n_tokens_neox': 3548, 'n_words': 830}