generated from FabricMC/fabric-example-mod
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Utils.java
89 lines (76 loc) · 3.67 KB
/
Utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package de.rubixdev.enchantedshulkers;
import de.rubixdev.enchantedshulkers.config.WorldConfig;
import de.rubixdev.enchantedshulkers.interfaces.EnchantableBlockEntity;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.collection.DefaultedList;
public class Utils {
public static boolean canEnchant(Item item) {
return canEnchant(item.getDefaultStack());
}
public static boolean canEnchant(ItemStack stack) {
return stack.isIn(Mod.PORTABLE_CONTAINER_TAG);
}
public static List<ItemStack> getContainers(ServerPlayerEntity player, Enchantment enchantment) {
visitedEnderChest = false;
List<ItemStack> playerInventory = new ArrayList<>();
for (int i = 0; i <= player.getInventory().size(); i++) {
playerInventory.add(player.getInventory().getStack(i));
}
return getContainers(playerInventory, player, enchantment, 0);
}
// TODO: does this break with multiple players?
private static boolean visitedEnderChest;
private static List<ItemStack> getContainers(
List<ItemStack> inventory, ServerPlayerEntity player, Enchantment enchantment, int recursionDepth) {
List<ItemStack> out = new ArrayList<>();
for (ItemStack stack : inventory) {
if (canEnchant(stack)
&& EnchantmentHelper.getLevel(enchantment, stack) > 0
&& !(visitedEnderChest && stack.isOf(Items.ENDER_CHEST))) {
out.add(stack);
if (stack.isOf(Items.ENDER_CHEST)) visitedEnderChest = true;
if (recursionDepth < (WorldConfig.nestedContainers() ? 255 : 0)) {
out.addAll(getContainers(
getContainerInventory(stack, player), player, enchantment, recursionDepth + 1));
}
}
}
return out;
}
public static DefaultedList<ItemStack> getContainerInventory(ItemStack container, ServerPlayerEntity player) {
DefaultedList<ItemStack> inventory = DefaultedList.ofSize(27, ItemStack.EMPTY);
if (container.isOf(Items.ENDER_CHEST)) {
return player.getEnderChestInventory().stacks;
}
NbtCompound nbt = container.getSubNbt("BlockEntityTag");
if (nbt != null && nbt.contains("Items", NbtElement.LIST_TYPE)) Inventories.readNbt(nbt, inventory);
return inventory;
}
public static void setContainerInventory(ItemStack container, DefaultedList<ItemStack> inventory) {
// No need to write any NBT on ender chests
if (container.isOf(Items.ENDER_CHEST)) return;
NbtCompound nbt = container.getOrCreateSubNbt("BlockEntityTag");
Inventories.writeNbt(nbt, inventory);
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static <T extends BlockEntity> boolean shouldGlint(T blockEntity) {
return blockEntity instanceof EnchantableBlockEntity enchantableBlockEntity
&& !enchantableBlockEntity.enchantedShulkers$getEnchantments().isEmpty();
}
public static MutableText translatableText(String trKey, Object... args) {
return Text.translatableWithFallback(trKey, Mod.EN_US_TRANSLATIONS.get(trKey), args);
}
}