2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.util;
|
2015-03-29 15:29:05 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-03-19 15:10:20 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2017-06-29 13:00:11 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.RegistryHandler;
|
2020-09-09 09:58:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockItemBase;
|
2016-03-18 18:38:39 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.creative.CreativeTab;
|
2017-02-07 18:25:45 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.IMCHandler;
|
2016-03-18 18:38:39 +01:00
|
|
|
import net.minecraft.block.Block;
|
2015-09-23 21:34:12 +02:00
|
|
|
import net.minecraft.enchantment.Enchantment;
|
2016-11-27 11:56:22 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
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;
|
2016-11-27 11:56:22 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-09-23 21:34:12 +02:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2016-11-27 11:56:22 +01:00
|
|
|
import net.minecraft.util.EnumHand;
|
2016-01-07 23:42:42 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2017-11-02 22:49:53 +01:00
|
|
|
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
2015-04-04 05:20:19 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public final class ItemUtil {
|
2015-03-29 15:29:05 +02:00
|
|
|
|
2019-05-02 09:10:29 +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
|
|
|
}
|
|
|
|
|
2020-09-09 09:58:55 +02:00
|
|
|
@Deprecated // canitzp: should be removed
|
|
|
|
public static void registerBlock(Block block, BlockItemBase itemBlock, String name, boolean addTab) {
|
2019-05-02 09:10:29 +02:00
|
|
|
block.setTranslationKey(ActuallyAdditions.MODID + "." + name);
|
2016-03-18 18:38:39 +01:00
|
|
|
|
2018-05-10 11:38:58 +02:00
|
|
|
block.setRegistryName(ActuallyAdditions.MODID, name);
|
2017-06-29 13:00:11 +02:00
|
|
|
RegistryHandler.BLOCKS_TO_REGISTER.add(block);
|
2016-04-20 21:39:03 +02:00
|
|
|
|
|
|
|
itemBlock.setRegistryName(block.getRegistryName());
|
2017-06-29 13:00:11 +02:00
|
|
|
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
|
|
|
|
2017-02-07 18:25:45 +01:00
|
|
|
IMCHandler.doBlockIMC(block);
|
|
|
|
|
2019-05-02 09:10:29 +02: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
|
|
|
}
|
|
|
|
|
2020-09-09 09:58:55 +02:00
|
|
|
@Deprecated // canitzp: should be removed
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void registerItem(Item item, String name, boolean addTab) {
|
|
|
|
item.setTranslationKey(ActuallyAdditions.MODID + "." + name);
|
2016-03-18 18:38:39 +01:00
|
|
|
|
2018-05-10 11:38:58 +02:00
|
|
|
item.setRegistryName(ActuallyAdditions.MODID, name);
|
2017-06-29 13:00:11 +02:00
|
|
|
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
|
|
|
|
2017-02-07 18:25:45 +01:00
|
|
|
IMCHandler.doItemIMC(item);
|
|
|
|
|
2019-05-02 09:10:29 +02: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
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean contains(ItemStack[] array, ItemStack stack, boolean checkWildcard) {
|
2015-09-28 23:39:34 +02:00
|
|
|
return getPlaceAt(array, stack, checkWildcard) != -1;
|
2015-09-22 23:48:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static int getPlaceAt(ItemStack[] array, ItemStack stack, boolean checkWildcard) {
|
2016-08-03 04:01:47 +02:00
|
|
|
return getPlaceAt(Arrays.asList(array), stack, checkWildcard);
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
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; }
|
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
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean areItemsEqual(ItemStack stack1, ItemStack stack2, boolean checkWildcard) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return StackUtil.isValid(stack1) && StackUtil.isValid(stack2) && (stack1.isItemEqual(stack2) || checkWildcard && stack1.getItem() == stack2.getItem() && (stack1.getItemDamage() == Util.WILDCARD || stack2.getItemDamage() == Util.WILDCARD));
|
2015-09-23 18:36:53 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean contains(List<ItemStack> list, ItemStack stack, boolean checkWildcard) {
|
2016-08-03 04:01:47 +02:00
|
|
|
return !(list == null || list.isEmpty()) && getPlaceAt(list, stack, checkWildcard) != -1;
|
2015-10-03 22:13:57 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void addEnchantment(ItemStack stack, Enchantment e, int level) {
|
|
|
|
if (!hasEnchantment(stack, e)) {
|
2015-09-24 21:00:53 +02:00
|
|
|
stack.addEnchantment(e, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean hasEnchantment(ItemStack stack, Enchantment e) {
|
2015-09-23 21:34:12 +02:00
|
|
|
NBTTagList ench = stack.getEnchantmentTagList();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (ench != null) {
|
|
|
|
for (int i = 0; i < ench.tagCount(); i++) {
|
2015-09-23 21:34:12 +02:00
|
|
|
short id = ench.getCompoundTagAt(i).getShort("id");
|
2019-05-02 09:10:29 +02:00
|
|
|
if (id == Enchantment.getEnchantmentID(e)) { 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
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void removeEnchantment(ItemStack stack, Enchantment e) {
|
2015-09-24 20:53:07 +02:00
|
|
|
NBTTagList ench = stack.getEnchantmentTagList();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (ench != null) {
|
|
|
|
for (int i = 0; i < ench.tagCount(); i++) {
|
2015-09-24 20:53:07 +02:00
|
|
|
short id = ench.getCompoundTagAt(i).getShort("id");
|
2019-05-02 09:10:29 +02:00
|
|
|
if (id == Enchantment.getEnchantmentID(e)) {
|
2015-10-03 10:19:40 +02:00
|
|
|
ench.removeTag(i);
|
2015-09-24 20:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
if (ench.isEmpty() && stack.hasTagCompound()) {
|
2016-07-20 10:42:41 +02:00
|
|
|
stack.getTagCompound().removeTag("ench");
|
|
|
|
}
|
2015-09-24 20:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean canBeStacked(ItemStack stack1, ItemStack stack2) {
|
2016-08-02 16:32:13 +02:00
|
|
|
return ItemStack.areItemsEqual(stack1, stack2) && ItemStack.areItemStackTagsEqual(stack1, stack2);
|
|
|
|
}
|
2016-11-27 11:56:22 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean isEnabled(ItemStack stack) {
|
2016-11-27 11:56:22 +01:00
|
|
|
return stack.hasTagCompound() && stack.getTagCompound().getBoolean("IsEnabled");
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void changeEnabled(EntityPlayer player, EnumHand hand) {
|
2016-12-18 17:28:29 +01:00
|
|
|
changeEnabled(player.getHeldItem(hand));
|
|
|
|
}
|
2016-11-27 11:56:22 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void changeEnabled(ItemStack stack) {
|
|
|
|
if (!stack.hasTagCompound()) {
|
2016-11-27 11:56:22 +01:00
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isEnabled = isEnabled(stack);
|
|
|
|
stack.getTagCompound().setBoolean("IsEnabled", !isEnabled);
|
|
|
|
}
|
2015-03-29 15:29:05 +02:00
|
|
|
}
|