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
|
|
|
|
2015-09-23 21:34:12 +02:00
|
|
|
import net.minecraft.enchantment.Enchantment;
|
2021-03-01 17:25:30 +01:00
|
|
|
import net.minecraft.enchantment.EnchantmentHelper;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
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;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.util.Hand;
|
2016-01-07 23:42:42 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2021-03-01 17:25:30 +01:00
|
|
|
import net.minecraftforge.registries.ForgeRegistries;
|
2015-04-04 05:20:19 +02:00
|
|
|
|
2021-03-01 17:25:30 +01:00
|
|
|
import java.util.Map;
|
2021-02-26 22:15:48 +01: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
|
|
|
}
|
|
|
|
|
2021-03-01 17:25:30 +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;
|
|
|
|
// }
|
|
|
|
|
|
|
|
@Deprecated
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean areItemsEqual(ItemStack stack1, ItemStack stack2, boolean checkWildcard) {
|
2021-08-22 17:09:06 +02:00
|
|
|
return stack1.sameItem(stack2);
|
2021-03-01 17:25:30 +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
|
|
|
|
2021-03-01 17:25:30 +01: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
|
|
|
|
2021-03-01 17:25:30 +01:00
|
|
|
@Deprecated
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void addEnchantment(ItemStack stack, Enchantment e, int level) {
|
2021-03-01 17:25:30 +01:00
|
|
|
if (!EnchantmentHelper.getEnchantments(stack).containsKey(e)) {
|
2021-08-22 17:09:06 +02:00
|
|
|
stack.enchant(e, level);
|
2015-09-24 21:00:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 17:25:30 +01:00
|
|
|
// TODO: [port] ensure this still works :D
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void removeEnchantment(ItemStack stack, Enchantment e) {
|
2021-03-01 17:25:30 +01:00
|
|
|
Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(stack);
|
|
|
|
enchantments.remove(e);
|
|
|
|
|
|
|
|
EnchantmentHelper.setEnchantments(enchantments, stack);
|
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) {
|
2021-08-22 17:09:06 +02:00
|
|
|
return ItemStack.isSame(stack1, stack2) && ItemStack.tagMatches(stack1, stack2);
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
2016-11-27 11:56:22 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static boolean isEnabled(ItemStack stack) {
|
2021-03-01 17:25:30 +01:00
|
|
|
return stack.getOrCreateTag().getBoolean("IsEnabled");
|
2016-11-27 11:56:22 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public static void changeEnabled(PlayerEntity player, Hand hand) {
|
2021-08-22 17:09:06 +02:00
|
|
|
changeEnabled(player.getItemInHand(hand));
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
2016-11-27 11:56:22 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static void changeEnabled(ItemStack stack) {
|
2016-11-27 11:56:22 +01:00
|
|
|
boolean isEnabled = isEnabled(stack);
|
2021-03-01 17:25:30 +01:00
|
|
|
stack.getOrCreateTag().putBoolean("IsEnabled", !isEnabled);
|
2016-11-27 11:56:22 +01:00
|
|
|
}
|
2015-03-29 15:29:05 +02:00
|
|
|
}
|