From f8f91c8a242fe9ad534575fe715c57217a0324be Mon Sep 17 00:00:00 2001 From: Anthony Hilyard Date: Tue, 12 Dec 2023 17:32:09 -0800 Subject: Fixed item NBT changing unexpectedly. Fixed tooltip decorations being misaligned sometimes. --- .../java/com/anthonyhilyard/iceberg/IcebergClient.java | 7 ++++++- .../iceberg/events/CriterionCallback.java | 8 ++++---- .../iceberg/mixin/PlayerAdvancementsMixin.java | 6 +++--- .../iceberg/renderer/CustomItemRenderer.java | 17 +++++++++-------- .../anthonyhilyard/iceberg/util/EntityCollector.java | 4 ++-- .../java/com/anthonyhilyard/iceberg/util/ItemColor.java | 14 ++++++++++++-- .../java/com/anthonyhilyard/iceberg/util/ItemUtil.java | 12 ++++++++++++ .../java/com/anthonyhilyard/iceberg/util/Selectors.java | 2 +- 8 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/anthonyhilyard/iceberg/util/ItemUtil.java (limited to 'src/main/java/com/anthonyhilyard') diff --git a/src/main/java/com/anthonyhilyard/iceberg/IcebergClient.java b/src/main/java/com/anthonyhilyard/iceberg/IcebergClient.java index 8e3c31b..82e89e7 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/IcebergClient.java +++ b/src/main/java/com/anthonyhilyard/iceberg/IcebergClient.java @@ -8,18 +8,23 @@ import com.anthonyhilyard.iceberg.util.Tooltips.TitleBreakComponent; import com.mojang.datafixers.util.Either; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.event.Event; import net.minecraft.network.chat.FormattedText; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionResult; import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; public class IcebergClient implements ClientModInitializer { + private static ResourceLocation earlyEventPhase = new ResourceLocation("iceberg", "early"); + @Override public void onInitializeClient() { TitleBreakComponent.registerFactory(); - RenderTooltipEvents.GATHER.register(IcebergClient::onGatherComponentsEventEnd); + RenderTooltipEvents.GATHER.addPhaseOrdering(earlyEventPhase, Event.DEFAULT_PHASE); + RenderTooltipEvents.GATHER.register(earlyEventPhase, IcebergClient::onGatherComponentsEventEnd); // Event testing. // CriterionCallback.EVENT.register((player, advancement, criterion) -> { Loader.LOGGER.info("CriterionCallback: {}, {}, {}", player.getName().getString(), advancement.getId().toString(), criterion); }); diff --git a/src/main/java/com/anthonyhilyard/iceberg/events/CriterionCallback.java b/src/main/java/com/anthonyhilyard/iceberg/events/CriterionCallback.java index 0486304..7c63a03 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/events/CriterionCallback.java +++ b/src/main/java/com/anthonyhilyard/iceberg/events/CriterionCallback.java @@ -1,6 +1,6 @@ package com.anthonyhilyard.iceberg.events; -import net.minecraft.advancements.Advancement; +import net.minecraft.advancements.AdvancementHolder; import net.minecraft.world.entity.player.Player; /** @@ -9,13 +9,13 @@ import net.minecraft.world.entity.player.Player; public interface CriterionCallback { ToggleableEvent EVENT = ToggleableEvent.create(CriterionCallback.class, - (listeners) -> (player, advancement, criterionKey) -> { + (listeners) -> (player, advancementHolder, criterionKey) -> { for (CriterionCallback listener : listeners) { - listener.awardCriterion(player, advancement, criterionKey); + listener.awardCriterion(player, advancementHolder, criterionKey); } } ); - public void awardCriterion(Player player, Advancement advancement, String criterionKey); + public void awardCriterion(Player player, AdvancementHolder advancementHolder, String criterionKey); } diff --git a/src/main/java/com/anthonyhilyard/iceberg/mixin/PlayerAdvancementsMixin.java b/src/main/java/com/anthonyhilyard/iceberg/mixin/PlayerAdvancementsMixin.java index 568036e..559192b 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/mixin/PlayerAdvancementsMixin.java +++ b/src/main/java/com/anthonyhilyard/iceberg/mixin/PlayerAdvancementsMixin.java @@ -9,7 +9,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; -import net.minecraft.advancements.Advancement; +import net.minecraft.advancements.AdvancementHolder; import net.minecraft.server.PlayerAdvancements; import net.minecraft.server.level.ServerPlayer; @@ -20,11 +20,11 @@ public class PlayerAdvancementsMixin private ServerPlayer player; @Inject(method = "award", at = @At(value = "TAIL"), locals = LocalCapture.CAPTURE_FAILEXCEPTION) - public void onAward(Advancement advancement, String criterionKey, CallbackInfoReturnable callbackInfo, boolean success) + public void onAward(AdvancementHolder advancementHolder, String criterionKey, CallbackInfoReturnable callbackInfo, boolean success) { if (success) { - CriterionCallback.EVENT.invoker().awardCriterion(player, advancement, criterionKey); + CriterionCallback.EVENT.invoker().awardCriterion(player, advancementHolder, criterionKey); } } } diff --git a/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java b/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java index 90d1278..fccd4f4 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java +++ b/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java @@ -9,6 +9,7 @@ import java.util.function.Predicate; import com.anthonyhilyard.iceberg.util.EntityCollector; import com.anthonyhilyard.iceberg.util.GuiHelper; +import com.anthonyhilyard.iceberg.util.ItemUtil; import com.google.common.collect.Maps; import org.joml.Matrix4f; @@ -421,9 +422,9 @@ public class CustomItemRenderer extends ItemRenderer BlockEntity blockEntity = entityBlock.newBlockEntity(BlockPos.ZERO, blockState); if (blockEntity != null) { - if (itemStack.getTag() != null) + if (itemStack.hasTag()) { - blockEntity.load(itemStack.getTag()); + blockEntity.load(ItemUtil.getItemNBT(itemStack)); } BlockEntityRenderer renderer = minecraft.getBlockEntityRenderDispatcher().getRenderer(blockEntity); @@ -498,7 +499,7 @@ public class CustomItemRenderer extends ItemRenderer } // If the item has changed, then we need to update the armor stand. - if (cachedArmorStandItem != Pair.of(itemStack.getItem(), itemStack.getTag())) + if (cachedArmorStandItem != Pair.of(itemStack.getItem(), ItemUtil.getItemNBT(itemStack))) { // Clear the armor stand. for (EquipmentSlot slot : EquipmentSlot.values()) @@ -509,7 +510,7 @@ public class CustomItemRenderer extends ItemRenderer // Equip the armor stand with the armor. armorStand.setItemSlot(equipmentSlot, itemStack); - cachedArmorStandItem = Pair.of(itemStack.getItem(), itemStack.getTag()); + cachedArmorStandItem = Pair.of(itemStack.getItem(), ItemUtil.getItemNBT(itemStack)); } return true; } @@ -530,7 +531,7 @@ public class CustomItemRenderer extends ItemRenderer private boolean updateEntity(ItemStack itemStack) { - Pair entityItem = Pair.of(itemStack.getItem(), itemStack.getTag()); + Pair entityItem = Pair.of(itemStack.getItem(), ItemUtil.getItemNBT(itemStack)); if (entity == null || cachedEntityItem != entityItem) { entity = getEntityFromItem(itemStack); @@ -563,12 +564,12 @@ public class CustomItemRenderer extends ItemRenderer } // If the item has changed, then we need to update the horse. - if (cachedHorseArmorItem != Pair.of(horseArmorItem.getItem(), horseArmorItem.getTag())) + if (cachedHorseArmorItem != Pair.of(horseArmorItem.getItem(), ItemUtil.getItemNBT(horseArmorItem))) { // Equip the horse with the armor. horse.setItemSlot(EquipmentSlot.CHEST, horseArmorItem); - cachedHorseArmorItem = Pair.of(horseArmorItem.getItem(), horseArmorItem.getTag()); + cachedHorseArmorItem = Pair.of(horseArmorItem.getItem(), ItemUtil.getItemNBT(horseArmorItem)); } return true; } @@ -610,7 +611,7 @@ public class CustomItemRenderer extends ItemRenderer private ModelBounds getModelBounds(ItemStack itemStack, ItemDisplayContext displayContext, boolean leftHanded, PoseStack poseStack, Quaternionf rotation, MultiBufferSource bufferSource, int packedLight, int packedOverlay, BakedModel bakedModel) { - Pair key = Pair.of(itemStack.getItem(), itemStack.getTag()); + Pair key = Pair.of(itemStack.getItem(), ItemUtil.getItemNBT(itemStack)); if (!modelBoundsCache.containsKey(key)) { VertexCollector vertexCollector = VertexCollector.create(); diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java b/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java index 90acfe4..fb51ff6 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java @@ -104,7 +104,7 @@ public class EntityCollector extends Level List entities = Lists.newArrayList(); Item item = itemStack.getItem(); ItemStack dummyStack = new ItemStack(item, itemStack.getCount()); - dummyStack.setTag(itemStack.getTag()); + dummyStack.setTag(ItemUtil.getItemNBT(itemStack)); try { @@ -188,7 +188,7 @@ public class EntityCollector extends Level public static boolean itemCreatesEntity(ItemStack itemStack, Class targetClass) { - ItemClassPair key = new ItemClassPair(itemStack.getItem(), itemStack.getTag(), targetClass); + ItemClassPair key = new ItemClassPair(itemStack.getItem(), ItemUtil.getItemNBT(itemStack), targetClass); boolean result = false; if (!itemCreatesEntityResultCache.containsKey(key)) { diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java b/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java index 8831bad..c69ab3f 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java @@ -108,8 +108,18 @@ public class ItemColor if (result == null || result.equals(item.getDisplayName().getStyle().getColor())) { Minecraft mc = Minecraft.getInstance(); - List lines = item.getTooltipLines(mc.player, TooltipFlag.Default.ADVANCED); - if (!lines.isEmpty()) + List lines = null; + + try + { + lines = item.getTooltipLines(mc.player, TooltipFlag.Default.ADVANCED); + } + catch (Exception e) + { + // An item must have misbehaved. + } + + if (lines != null && !lines.isEmpty()) { result = lines.get(0).getStyle().getColor(); } diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/ItemUtil.java b/src/main/java/com/anthonyhilyard/iceberg/util/ItemUtil.java new file mode 100644 index 0000000..4887e45 --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/util/ItemUtil.java @@ -0,0 +1,12 @@ +package com.anthonyhilyard.iceberg.util; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.item.ItemStack; + +public class ItemUtil +{ + public static CompoundTag getItemNBT(final ItemStack item) + { + return item.hasTag() ? item.getTag().copy() : null; + } +} diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java index 3e9c429..0aea56d 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java @@ -293,7 +293,7 @@ public class Selectors } // Look for a tag matching the given name and value. - return findMatchingSubtag(item.getTag(), tagName, tagValue, valueChecker); + return findMatchingSubtag(ItemUtil.getItemNBT(item), tagName, tagValue, valueChecker); } return false; -- cgit