ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemUtil.java

156 lines
5.7 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ItemUtil.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.util;
2015-03-29 15:29:05 +02:00
2016-03-19 15:10:20 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.RegistryHandler;
2016-03-18 18:38:39 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.base.ItemBlockBase;
import de.ellpeck.actuallyadditions.mod.creative.CreativeTab;
import de.ellpeck.actuallyadditions.mod.util.compat.IMCHandler;
2016-03-18 18:38:39 +01:00
import net.minecraft.block.Block;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.player.EntityPlayer;
2015-03-29 15:29:05 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
2017-11-02 22:49:53 +01:00
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import java.util.Arrays;
import java.util.List;
2016-06-17 23:50:38 +02:00
public final class ItemUtil{
2015-03-29 15:29:05 +02:00
2015-07-12 22:05:34 +02:00
public static Item getItemFromName(String name){
2017-11-02 22:49:53 +01:00
return ForgeRegistries.ITEMS.getValue(new ResourceLocation(name));
2015-07-12 22:05:34 +02:00
}
2016-04-20 21:39:03 +02:00
public static void registerBlock(Block block, ItemBlockBase itemBlock, String name, boolean addTab){
2018-05-10 11:38:58 +02:00
block.setUnlocalizedName(ActuallyAdditions.MODID+"."+name);
2016-03-18 18:38:39 +01:00
2018-05-10 11:38:58 +02:00
block.setRegistryName(ActuallyAdditions.MODID, name);
RegistryHandler.BLOCKS_TO_REGISTER.add(block);
2016-04-20 21:39:03 +02:00
itemBlock.setRegistryName(block.getRegistryName());
RegistryHandler.ITEMS_TO_REGISTER.add(itemBlock);
2016-03-18 18:38:39 +01:00
2016-06-17 23:50:38 +02:00
block.setCreativeTab(addTab ? CreativeTab.INSTANCE : null);
2016-11-19 21:54:43 +01:00
IMCHandler.doBlockIMC(block);
2017-02-13 23:17:08 +01:00
if(block instanceof IColorProvidingBlock){
2018-05-10 11:38:58 +02:00
ActuallyAdditions.PROXY.addColoredBlock(block);
2017-02-13 23:17:08 +01:00
}
2016-03-18 18:38:39 +01:00
}
public static void registerItem(Item item, String name, boolean addTab){
2018-05-10 11:38:58 +02:00
item.setUnlocalizedName(ActuallyAdditions.MODID+"."+name);
2016-03-18 18:38:39 +01:00
2018-05-10 11:38:58 +02:00
item.setRegistryName(ActuallyAdditions.MODID, name);
RegistryHandler.ITEMS_TO_REGISTER.add(item);
2016-03-18 18:38:39 +01:00
2016-06-17 23:50:38 +02:00
item.setCreativeTab(addTab ? CreativeTab.INSTANCE : null);
2016-03-19 15:10:20 +01:00
IMCHandler.doItemIMC(item);
2016-03-19 15:10:20 +01:00
if(item instanceof IColorProvidingItem){
2018-05-10 11:38:58 +02:00
ActuallyAdditions.PROXY.addColoredItem(item);
2016-03-19 15:10:20 +01:00
}
2016-03-18 18:38:39 +01:00
}
public static boolean contains(ItemStack[] array, ItemStack stack, boolean checkWildcard){
return getPlaceAt(array, stack, checkWildcard) != -1;
}
public static int getPlaceAt(ItemStack[] array, ItemStack stack, boolean checkWildcard){
return getPlaceAt(Arrays.asList(array), stack, checkWildcard);
}
public static int getPlaceAt(List<ItemStack> list, ItemStack stack, boolean checkWildcard){
if(list != null && list.size() > 0){
for(int i = 0; i < list.size(); i++){
if((!StackUtil.isValid(stack) && !StackUtil.isValid(list.get(i))) || areItemsEqual(stack, list.get(i), checkWildcard)){
return i;
}
}
}
return -1;
}
public static boolean areItemsEqual(ItemStack stack1, ItemStack stack2, boolean checkWildcard){
return StackUtil.isValid(stack1) && StackUtil.isValid(stack2) && (stack1.isItemEqual(stack2) || (checkWildcard && stack1.getItem() == stack2.getItem() && (stack1.getItemDamage() == Util.WILDCARD || stack2.getItemDamage() == Util.WILDCARD)));
}
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, stack, checkWildcard) != -1;
2015-10-03 22:13:57 +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){
NBTTagList ench = stack.getEnchantmentTagList();
if(ench != null){
for(int i = 0; i < ench.tagCount(); i++){
short id = ench.getCompoundTagAt(i).getShort("id");
2016-03-18 23:47:22 +01:00
if(id == Enchantment.getEnchantmentID(e)){
2015-10-03 10:19:40 +02:00
return true;
}
}
}
2015-10-03 10:19:40 +02:00
return false;
}
2015-10-03 10:19:40 +02:00
public static void removeEnchantment(ItemStack stack, Enchantment e){
NBTTagList ench = stack.getEnchantmentTagList();
if(ench != null){
for(int i = 0; i < ench.tagCount(); i++){
short id = ench.getCompoundTagAt(i).getShort("id");
2016-03-18 23:47:22 +01:00
if(id == Enchantment.getEnchantmentID(e)){
2015-10-03 10:19:40 +02:00
ench.removeTag(i);
}
}
if(ench.hasNoTags() && stack.hasTagCompound()){
stack.getTagCompound().removeTag("ench");
}
}
}
public static boolean canBeStacked(ItemStack stack1, ItemStack stack2){
return ItemStack.areItemsEqual(stack1, stack2) && ItemStack.areItemStackTagsEqual(stack1, stack2);
}
public static boolean isEnabled(ItemStack stack){
return stack.hasTagCompound() && stack.getTagCompound().getBoolean("IsEnabled");
}
public static void changeEnabled(EntityPlayer player, EnumHand hand){
changeEnabled(player.getHeldItem(hand));
}
public static void changeEnabled(ItemStack stack){
if(!stack.hasTagCompound()){
stack.setTagCompound(new NBTTagCompound());
}
boolean isEnabled = isEnabled(stack);
stack.getTagCompound().setBoolean("IsEnabled", !isEnabled);
}
2015-03-29 15:29:05 +02:00
}