2015-08-29 14:33:25 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file ("ItemUtil.java") is part of the Actually Additions Mod for Minecraft.
|
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
|
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
|
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
|
*
|
|
|
|
|
* <EFBFBD> 2015 Ellpeck
|
|
|
|
|
*/
|
|
|
|
|
|
2015-03-29 15:29:05 +02:00
|
|
|
|
package ellpeck.actuallyadditions.util;
|
|
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.registry.GameRegistry;
|
|
|
|
|
import ellpeck.actuallyadditions.creative.CreativeTab;
|
2015-09-23 21:34:12 +02:00
|
|
|
|
import net.minecraft.enchantment.Enchantment;
|
2015-03-29 15:29:05 +02:00
|
|
|
|
import net.minecraft.item.Item;
|
2015-09-22 23:32:19 +02:00
|
|
|
|
import net.minecraft.item.ItemStack;
|
2015-09-23 21:34:12 +02:00
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2015-04-04 05:20:19 +02:00
|
|
|
|
|
2015-10-03 21:56:22 +02:00
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2015-10-03 22:09:53 +02:00
|
|
|
|
|
2015-03-29 15:29:05 +02:00
|
|
|
|
public class ItemUtil{
|
|
|
|
|
|
2015-07-12 22:05:34 +02:00
|
|
|
|
public static Item getItemFromName(String name){
|
|
|
|
|
if(Item.itemRegistry.containsKey(name)){
|
|
|
|
|
return (Item)Item.itemRegistry.getObject(name);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 15:29:05 +02:00
|
|
|
|
public static void registerItems(Item[] items){
|
|
|
|
|
for(Item item : items){
|
|
|
|
|
register(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void register(Item item){
|
2015-10-25 14:30:59 +01:00
|
|
|
|
item.setCreativeTab(CreativeTab.instance);
|
2015-04-04 05:20:19 +02:00
|
|
|
|
item.setUnlocalizedName(createUnlocalizedName(item));
|
2015-10-01 23:20:31 +02:00
|
|
|
|
GameRegistry.registerItem(item, ((IActAddItemOrBlock)item).getName());
|
2015-03-29 15:29:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String createUnlocalizedName(Item item){
|
2015-10-01 23:20:31 +02:00
|
|
|
|
return ModUtil.MOD_ID_LOWER+"."+((IActAddItemOrBlock)item).getName();
|
2015-03-29 15:29:05 +02:00
|
|
|
|
}
|
2015-06-18 13:14:57 +02:00
|
|
|
|
|
2015-09-22 23:32:19 +02:00
|
|
|
|
/**
|
|
|
|
|
* Returns true if stacksOne contains every ItemStack in stackTwo
|
|
|
|
|
*/
|
2015-09-28 23:39:34 +02:00
|
|
|
|
public static boolean containsAll(ItemStack[] stacksOne, ItemStack[] stacksTwo, boolean checkWildcard){
|
2015-09-22 23:32:19 +02:00
|
|
|
|
for(ItemStack stackTwo : stacksTwo){
|
2015-09-28 23:39:34 +02:00
|
|
|
|
if(!contains(stacksOne, stackTwo, checkWildcard)){
|
2015-09-22 23:32:19 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if array contains stack or if both contain null
|
|
|
|
|
*/
|
2015-09-28 23:39:34 +02:00
|
|
|
|
public static boolean contains(ItemStack[] array, ItemStack stack, boolean checkWildcard){
|
|
|
|
|
return getPlaceAt(array, stack, checkWildcard) != -1;
|
2015-09-22 23:48:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the place of stack in array, -1 if not present
|
|
|
|
|
*/
|
2015-09-28 23:39:34 +02:00
|
|
|
|
public static int getPlaceAt(ItemStack[] array, ItemStack stack, boolean checkWildcard){
|
|
|
|
|
if(array != null && array.length > 0){
|
|
|
|
|
for(int i = 0; i < array.length; i++){
|
|
|
|
|
if((stack == null && array[i] == null) || areItemsEqual(stack, array[i], checkWildcard)){
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2015-09-22 23:32:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-22 23:48:27 +02:00
|
|
|
|
return -1;
|
2015-09-22 23:32:19 +02:00
|
|
|
|
}
|
2015-09-23 18:36:53 +02:00
|
|
|
|
|
|
|
|
|
public static boolean areItemsEqual(ItemStack stack1, ItemStack stack2, boolean checkWildcard){
|
|
|
|
|
return stack1 != null && stack2 != null && (stack1.isItemEqual(stack2) || (checkWildcard && stack1.getItem() == stack2.getItem() && (stack1.getItemDamage() == Util.WILDCARD || stack2.getItemDamage() == Util.WILDCARD)));
|
|
|
|
|
}
|
2015-09-23 21:34:12 +02:00
|
|
|
|
|
2015-10-03 22:13:57 +02:00
|
|
|
|
/**
|
|
|
|
|
* Returns true if list contains stack or if both contain null
|
|
|
|
|
*/
|
|
|
|
|
public static boolean contains(List<ItemStack> list, ItemStack stack, boolean checkWildcard){
|
|
|
|
|
return !(list == null || list.isEmpty()) && getPlaceAt(list.toArray(new ItemStack[list.size()]), stack, checkWildcard) != -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 21:00:53 +02:00
|
|
|
|
public static void addEnchantment(ItemStack stack, Enchantment e, int level){
|
|
|
|
|
if(!hasEnchantment(stack, e)){
|
|
|
|
|
stack.addEnchantment(e, level);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 10:19:40 +02:00
|
|
|
|
public static boolean hasEnchantment(ItemStack stack, Enchantment e){
|
2015-09-23 21:34:12 +02:00
|
|
|
|
NBTTagList ench = stack.getEnchantmentTagList();
|
|
|
|
|
if(ench != null){
|
|
|
|
|
for(int i = 0; i < ench.tagCount(); i++){
|
|
|
|
|
short id = ench.getCompoundTagAt(i).getShort("id");
|
|
|
|
|
if(id == e.effectId){
|
2015-10-03 10:19:40 +02:00
|
|
|
|
return true;
|
2015-09-23 21:34:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-03 10:19:40 +02:00
|
|
|
|
return false;
|
2015-09-23 21:34:12 +02:00
|
|
|
|
}
|
2015-09-24 20:53:07 +02:00
|
|
|
|
|
2015-10-03 10:19:40 +02:00
|
|
|
|
public static void removeEnchantment(ItemStack stack, Enchantment e){
|
2015-09-24 20:53:07 +02:00
|
|
|
|
NBTTagList ench = stack.getEnchantmentTagList();
|
|
|
|
|
if(ench != null){
|
|
|
|
|
for(int i = 0; i < ench.tagCount(); i++){
|
|
|
|
|
short id = ench.getCompoundTagAt(i).getShort("id");
|
|
|
|
|
if(id == e.effectId){
|
2015-10-03 10:19:40 +02:00
|
|
|
|
ench.removeTag(i);
|
2015-09-24 20:53:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-29 15:29:05 +02:00
|
|
|
|
}
|