|
| 1 | +package com.ldtteam.blockui.controls; |
| 2 | + |
| 3 | +import com.ldtteam.blockui.BOGuiGraphics; |
| 4 | +import com.ldtteam.blockui.PaneParams; |
| 5 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 6 | +import net.minecraft.client.renderer.item.ItemProperties; |
| 7 | +import net.minecraft.client.renderer.item.ItemPropertyFunction; |
| 8 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 9 | +import net.minecraft.nbt.CompoundTag; |
| 10 | +import net.minecraft.nbt.Tag; |
| 11 | +import net.minecraft.nbt.TagParser; |
| 12 | +import net.minecraft.resources.ResourceLocation; |
| 13 | +import net.minecraft.world.item.Item; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +/** |
| 20 | + * Useful for overriding things like clock/compass textures. In xml defined through {@value #PARAM_PROPERTIES} key using nbt: |
| 21 | + * {<item registry key>:{<property name>:<float value>, ...}, ...}. |
| 22 | + * <p> |
| 23 | + * Special keys: {@value #NBT_CURRENT_ITEM} - refers to xml item (resolved during parsing not dynamic), |
| 24 | + * {@value #NBT_GENERIC_KEY} - generic properties |
| 25 | + * |
| 26 | + * @see ItemProperties |
| 27 | + */ |
| 28 | +@SuppressWarnings("deprecation") |
| 29 | +public class ItemIconWithProperties extends ItemIcon |
| 30 | +{ |
| 31 | + private static final String NBT_GENERIC_KEY = "_generic"; |
| 32 | + private static final String NBT_CURRENT_ITEM = "_item"; |
| 33 | + |
| 34 | + public static final String PARAM_PROPERTIES = "properties"; |
| 35 | + |
| 36 | + protected final Map<ResourceLocation, ItemPropertyFunction> genericPropertyOverrides = new HashMap<>(); |
| 37 | + protected final Map<Item, Map<ResourceLocation, ItemPropertyFunction>> itemPropertyOverrides = new HashMap<>(); |
| 38 | + private Map<ResourceLocation, ItemPropertyFunction> currentItemOverrides = Collections.emptyMap(); |
| 39 | + |
| 40 | + public ItemIconWithProperties() |
| 41 | + { |
| 42 | + super(); |
| 43 | + } |
| 44 | + |
| 45 | + public ItemIconWithProperties(final PaneParams paneParams) |
| 46 | + { |
| 47 | + super(paneParams); |
| 48 | + |
| 49 | + final String data = paneParams.getString(PARAM_PROPERTIES); |
| 50 | + if (data != null && itemStack != null) |
| 51 | + { |
| 52 | + final CompoundTag tag; |
| 53 | + try |
| 54 | + { |
| 55 | + tag = TagParser.parseTag(data); |
| 56 | + } |
| 57 | + catch (CommandSyntaxException e) |
| 58 | + { |
| 59 | + throw new RuntimeException(data, null); |
| 60 | + } |
| 61 | + tag.getAllKeys().forEach(itemKey -> { |
| 62 | + if (tag.contains(itemKey, Tag.TAG_COMPOUND)) |
| 63 | + { |
| 64 | + final CompoundTag child = tag.getCompound(itemKey); |
| 65 | + final var itemOverrides = NBT_GENERIC_KEY.equals(itemKey) ? genericPropertyOverrides : |
| 66 | + itemPropertyOverrides.computeIfAbsent(NBT_CURRENT_ITEM.equals(itemKey) ? itemStack.getItem() : |
| 67 | + BuiltInRegistries.ITEM.get(new ResourceLocation(itemKey)), i -> new HashMap<>()); |
| 68 | + |
| 69 | + child.getAllKeys().forEach(key -> { |
| 70 | + if (child.contains(key, Tag.TAG_ANY_NUMERIC)) |
| 71 | + { |
| 72 | + final float value = child.getFloat(key); // intentionally ouf of lambda |
| 73 | + itemOverrides.put(new ResourceLocation(key), (itemStack, level, entity, seee) -> value); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + onItemUpdate(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Short call for adding itemProperty to current item |
| 85 | + */ |
| 86 | + public void addPropertyForCurrentItem(final ResourceLocation propertyKey, final ItemPropertyFunction property) |
| 87 | + { |
| 88 | + itemPropertyOverrides |
| 89 | + .computeIfAbsent(Objects.requireNonNull(itemStack, "Call #setItem before this method").getItem(), item -> new HashMap<>()) |
| 90 | + .put(propertyKey, property); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return modifiable all item-based overrides |
| 95 | + */ |
| 96 | + public Map<Item, Map<ResourceLocation, ItemPropertyFunction>> getItemPropertyOverrides() |
| 97 | + { |
| 98 | + return itemPropertyOverrides; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return modifiable generic overrides |
| 103 | + */ |
| 104 | + public Map<ResourceLocation, ItemPropertyFunction> getGenericPropertyOverrides() |
| 105 | + { |
| 106 | + return genericPropertyOverrides; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void drawSelf(final BOGuiGraphics target, final double mx, final double my) |
| 111 | + { |
| 112 | + if (isDataEmpty()) |
| 113 | + { |
| 114 | + return; |
| 115 | + } |
| 116 | + if (currentItemOverrides.isEmpty() && genericPropertyOverrides.isEmpty()) |
| 117 | + { |
| 118 | + super.drawSelf(target, mx, my); |
| 119 | + return; |
| 120 | + } |
| 121 | + final Item item = itemStack.getItem(); |
| 122 | + |
| 123 | + // generic |
| 124 | + final Map<ResourceLocation, ItemPropertyFunction> oldGenericVals = |
| 125 | + genericPropertyOverrides.isEmpty() ? Collections.emptyMap() : new HashMap<>(); |
| 126 | + genericPropertyOverrides.forEach((key, val) -> { |
| 127 | + oldGenericVals.put(key, ItemProperties.getProperty(item, key)); |
| 128 | + ItemProperties.registerGeneric(key, val); |
| 129 | + }); |
| 130 | + |
| 131 | + // item |
| 132 | + final Map<ResourceLocation, ItemPropertyFunction> oldItemVals = |
| 133 | + currentItemOverrides.isEmpty() ? Collections.emptyMap() : new HashMap<>(); |
| 134 | + currentItemOverrides.forEach((key, val) -> { |
| 135 | + oldItemVals.put(key, ItemProperties.getProperty(item, key)); |
| 136 | + ItemProperties.register(item, key, val); |
| 137 | + }); |
| 138 | + |
| 139 | + super.drawSelf(target, mx, my); |
| 140 | + |
| 141 | + oldItemVals.forEach((key, val) -> ItemProperties.register(item, key, val)); |
| 142 | + oldGenericVals.forEach((key, val) -> ItemProperties.registerGeneric(key, val)); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + protected void onItemUpdate() |
| 147 | + { |
| 148 | + super.onItemUpdate(); |
| 149 | + if (itemPropertyOverrides != null) // ctor race condition |
| 150 | + { |
| 151 | + currentItemOverrides = itemPropertyOverrides.getOrDefault(itemStack.getItem(), Collections.emptyMap()); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments