From 14e49a597f3c38a672094a93f358b375509090f3 Mon Sep 17 00:00:00 2001 From: iron431 <34083081+iron431@users.noreply.github.com> Date: Tue, 28 Nov 2023 09:09:50 -0600 Subject: [PATCH] Compartmentalize client references fixes #227 --- .../capabilities/spell/SpellData.java | 11 ++++------- .../redspace/ironsspellbooks/item/Scroll.java | 16 ++++++++-------- .../item/curios/AffinityRing.java | 8 ++------ .../ironsspellbooks/setup/ClientSetup.java | 15 +++++++++++---- .../util/IMinecraftInstanceHelper.java | 10 ++++++++++ .../util/MinecraftInstanceHelper.java | 19 +++++++++++++++++++ .../ironsspellbooks/util/TooltipsUtils.java | 17 ++++++----------- 7 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 src/main/java/io/redspace/ironsspellbooks/util/IMinecraftInstanceHelper.java create mode 100644 src/main/java/io/redspace/ironsspellbooks/util/MinecraftInstanceHelper.java diff --git a/src/main/java/io/redspace/ironsspellbooks/capabilities/spell/SpellData.java b/src/main/java/io/redspace/ironsspellbooks/capabilities/spell/SpellData.java index 627fc4413..10ac85700 100644 --- a/src/main/java/io/redspace/ironsspellbooks/capabilities/spell/SpellData.java +++ b/src/main/java/io/redspace/ironsspellbooks/capabilities/spell/SpellData.java @@ -1,18 +1,16 @@ package io.redspace.ironsspellbooks.capabilities.spell; import io.redspace.ironsspellbooks.api.item.weapons.MagicSwordItem; +import io.redspace.ironsspellbooks.api.registry.SpellRegistry; +import io.redspace.ironsspellbooks.api.spells.AbstractSpell; import io.redspace.ironsspellbooks.api.spells.SpellRarity; import io.redspace.ironsspellbooks.registries.ItemRegistry; -import io.redspace.ironsspellbooks.api.spells.AbstractSpell; -import io.redspace.ironsspellbooks.api.registry.SpellRegistry; -import net.minecraft.client.Minecraft; +import io.redspace.ironsspellbooks.util.MinecraftInstanceHelper; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import java.util.Objects; @@ -88,10 +86,9 @@ public SpellRarity getRarity() { return getSpell().getRarity(getLevel()); } - @OnlyIn(Dist.CLIENT) public Component getDisplayName() { if (displayName == null) { - displayName = getSpell().getDisplayName(Minecraft.getInstance().player).append(" ").append(Component.translatable(ItemRegistry.SCROLL.get().getDescriptionId())); + displayName = getSpell().getDisplayName(MinecraftInstanceHelper.instance.player()).append(" ").append(Component.translatable(ItemRegistry.SCROLL.get().getDescriptionId())); } return displayName; } diff --git a/src/main/java/io/redspace/ironsspellbooks/item/Scroll.java b/src/main/java/io/redspace/ironsspellbooks/item/Scroll.java index ce5102b95..5d2e6edbc 100644 --- a/src/main/java/io/redspace/ironsspellbooks/item/Scroll.java +++ b/src/main/java/io/redspace/ironsspellbooks/item/Scroll.java @@ -2,17 +2,18 @@ import io.redspace.ironsspellbooks.api.item.IScroll; import io.redspace.ironsspellbooks.api.magic.MagicData; -import io.redspace.ironsspellbooks.api.spells.AbstractSpell; import io.redspace.ironsspellbooks.api.registry.SpellRegistry; +import io.redspace.ironsspellbooks.api.spells.AbstractSpell; +import io.redspace.ironsspellbooks.api.spells.CastSource; +import io.redspace.ironsspellbooks.api.spells.CastType; +import io.redspace.ironsspellbooks.api.util.Utils; import io.redspace.ironsspellbooks.capabilities.spell.SpellData; import io.redspace.ironsspellbooks.player.ClientMagicData; import io.redspace.ironsspellbooks.registries.ItemRegistry; -import io.redspace.ironsspellbooks.api.spells.CastSource; -import io.redspace.ironsspellbooks.api.spells.CastType; +import io.redspace.ironsspellbooks.util.MinecraftInstanceHelper; import io.redspace.ironsspellbooks.util.SpellbookModCreativeTabs; import io.redspace.ironsspellbooks.util.TooltipsUtils; -import io.redspace.ironsspellbooks.api.util.Utils; -import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.NonNullList; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; @@ -120,9 +121,8 @@ public void releaseUsing(@NotNull ItemStack itemStack, @NotNull Level level, Liv @Override public void appendHoverText(@NotNull ItemStack itemStack, @Nullable Level level, List lines, @NotNull TooltipFlag flag) { - var player = Minecraft.getInstance().player; - if (player != null) - lines.addAll(TooltipsUtils.formatScrollTooltip(itemStack, player)); + if (MinecraftInstanceHelper.instance.player() instanceof LocalPlayer localPlayer) + lines.addAll(TooltipsUtils.formatScrollTooltip(itemStack, localPlayer)); super.appendHoverText(itemStack, level, lines, flag); } } diff --git a/src/main/java/io/redspace/ironsspellbooks/item/curios/AffinityRing.java b/src/main/java/io/redspace/ironsspellbooks/item/curios/AffinityRing.java index 35732a6cb..ab3123bb9 100644 --- a/src/main/java/io/redspace/ironsspellbooks/item/curios/AffinityRing.java +++ b/src/main/java/io/redspace/ironsspellbooks/item/curios/AffinityRing.java @@ -3,6 +3,7 @@ import io.redspace.ironsspellbooks.api.item.curios.RingData; import io.redspace.ironsspellbooks.api.registry.SpellRegistry; import io.redspace.ironsspellbooks.render.AffinityRingRenderer; +import io.redspace.ironsspellbooks.util.MinecraftInstanceHelper; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; @@ -10,8 +11,6 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.Level; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.extensions.common.IClientItemExtensions; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -25,26 +24,23 @@ public AffinityRing(Properties properties) { super(properties); } - @OnlyIn(Dist.CLIENT) @Override public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List tooltip, TooltipFlag pIsAdvanced) { var spell = RingData.getRingData(pStack).getSpell(); if (!spell.equals(SpellRegistry.none())) { tooltip.add(Component.empty()); tooltip.add(Component.translatable("curios.modifiers.ring").withStyle(ChatFormatting.GOLD)); - tooltip.add(Component.translatable("tooltip.irons_spellbooks.enhance_spell_level", spell.getDisplayName(Minecraft.getInstance().player).withStyle(spell.getSchoolType().getDisplayName().getStyle())).withStyle(ChatFormatting.YELLOW)); + tooltip.add(Component.translatable("tooltip.irons_spellbooks.enhance_spell_level", spell.getDisplayName(MinecraftInstanceHelper.instance.player()).withStyle(spell.getSchoolType().getDisplayName().getStyle())).withStyle(ChatFormatting.YELLOW)); } else { tooltip.add(Component.translatable("tooltip.irons_spellbooks.empty_affinity_ring").withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC)); } } - @OnlyIn(Dist.CLIENT) @Override public Component getName(ItemStack pStack) { return Component.translatable(this.getDescriptionId(pStack), RingData.getRingData(pStack).getNameForItem()); } - @OnlyIn(Dist.CLIENT) @Override public void initializeClient(@NotNull Consumer consumer) { consumer.accept(new IClientItemExtensions() { diff --git a/src/main/java/io/redspace/ironsspellbooks/setup/ClientSetup.java b/src/main/java/io/redspace/ironsspellbooks/setup/ClientSetup.java index 255eaf1aa..c3091a3fa 100644 --- a/src/main/java/io/redspace/ironsspellbooks/setup/ClientSetup.java +++ b/src/main/java/io/redspace/ironsspellbooks/setup/ClientSetup.java @@ -58,7 +58,6 @@ import io.redspace.ironsspellbooks.entity.spells.wisp.WispRenderer; import io.redspace.ironsspellbooks.item.WaywardCompass; import io.redspace.ironsspellbooks.item.armor.*; -import io.redspace.ironsspellbooks.item.weapons.AutoloaderCrossbow; import io.redspace.ironsspellbooks.particle.*; import io.redspace.ironsspellbooks.registries.BlockRegistry; import io.redspace.ironsspellbooks.registries.EntityRegistry; @@ -66,6 +65,8 @@ import io.redspace.ironsspellbooks.registries.ParticleRegistry; import io.redspace.ironsspellbooks.render.*; import io.redspace.ironsspellbooks.util.AbstractClientPlayerMixinHelper; +import io.redspace.ironsspellbooks.util.IMinecraftInstanceHelper; +import io.redspace.ironsspellbooks.util.MinecraftInstanceHelper; import net.minecraft.client.Minecraft; import net.minecraft.client.model.HumanoidModel; import net.minecraft.client.model.geom.LayerDefinitions; @@ -78,8 +79,6 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.CrossbowItem; -import net.minecraft.world.item.Items; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.client.event.EntityRenderersEvent; import net.minecraftforge.client.event.ModelEvent; @@ -87,6 +86,7 @@ import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import org.jetbrains.annotations.Nullable; import software.bernie.geckolib3.renderers.geo.GeoArmorRenderer; import java.util.Map; @@ -287,7 +287,14 @@ public static void clientSetup(final FMLClientSetupEvent e) { e.enqueueWork(() -> { ItemProperties.register(ItemRegistry.WAYWARD_COMPASS.get(), new ResourceLocation("angle"), new CompassItemPropertyFunction((level, itemStack, entity) -> WaywardCompass.getCatacombsLocation(entity, itemStack.getOrCreateTag()))); -// ItemProperties.register(ItemRegistry.AUTOLOADER_CROSSBOW.get(), new ResourceLocation("pull"), (itemStack, clientLevel, livingEntity, i) -> { + MinecraftInstanceHelper.instance = new IMinecraftInstanceHelper() { + @Nullable + @Override + public Player player() { + return Minecraft.getInstance().player; + } + }; + // ItemProperties.register(ItemRegistry.AUTOLOADER_CROSSBOW.get(), new ResourceLocation("pull"), (itemStack, clientLevel, livingEntity, i) -> { // return CrossbowItem.isCharged(itemStack) ? 0.0F : AutoloaderCrossbow.getLoadingTicks(itemStack) / (float) AutoloaderCrossbow.getChargeDuration(itemStack); // }); // ItemProperties.register(ItemRegistry.AUTOLOADER_CROSSBOW.get(), new ResourceLocation("pulling"), (itemStack, clientLevel, livingEntity, i) -> { diff --git a/src/main/java/io/redspace/ironsspellbooks/util/IMinecraftInstanceHelper.java b/src/main/java/io/redspace/ironsspellbooks/util/IMinecraftInstanceHelper.java new file mode 100644 index 000000000..263799af6 --- /dev/null +++ b/src/main/java/io/redspace/ironsspellbooks/util/IMinecraftInstanceHelper.java @@ -0,0 +1,10 @@ +package io.redspace.ironsspellbooks.util; + +import net.minecraft.world.entity.player.Player; + +import javax.annotation.Nullable; + +public interface IMinecraftInstanceHelper { + @Nullable + Player player(); +} diff --git a/src/main/java/io/redspace/ironsspellbooks/util/MinecraftInstanceHelper.java b/src/main/java/io/redspace/ironsspellbooks/util/MinecraftInstanceHelper.java new file mode 100644 index 000000000..4f7cc031d --- /dev/null +++ b/src/main/java/io/redspace/ironsspellbooks/util/MinecraftInstanceHelper.java @@ -0,0 +1,19 @@ +package io.redspace.ironsspellbooks.util; + +import net.minecraft.world.entity.player.Player; +import org.jetbrains.annotations.Nullable; + +public class MinecraftInstanceHelper implements IMinecraftInstanceHelper { + /** + * If we are on the client, this is replaced with an implementation that returns the client host player + */ + public static IMinecraftInstanceHelper instance = () -> null; + + @Nullable + @Override + public Player player() { + return instance.player(); + } + + public static Player getPlayer(){return instance.player();} +} diff --git a/src/main/java/io/redspace/ironsspellbooks/util/TooltipsUtils.java b/src/main/java/io/redspace/ironsspellbooks/util/TooltipsUtils.java index 2ef159bde..9c2ef9ac3 100644 --- a/src/main/java/io/redspace/ironsspellbooks/util/TooltipsUtils.java +++ b/src/main/java/io/redspace/ironsspellbooks/util/TooltipsUtils.java @@ -1,36 +1,30 @@ package io.redspace.ironsspellbooks.util; +import io.redspace.ironsspellbooks.api.spells.AbstractSpell; +import io.redspace.ironsspellbooks.api.spells.CastSource; +import io.redspace.ironsspellbooks.api.spells.CastType; import io.redspace.ironsspellbooks.api.util.Utils; import io.redspace.ironsspellbooks.capabilities.magic.MagicManager; import io.redspace.ironsspellbooks.capabilities.spell.SpellData; import io.redspace.ironsspellbooks.capabilities.spellbook.SpellBookData; import io.redspace.ironsspellbooks.config.ServerConfigs; import io.redspace.ironsspellbooks.item.SpellBook; -import io.redspace.ironsspellbooks.api.spells.AbstractSpell; -import io.redspace.ironsspellbooks.api.spells.CastSource; -import io.redspace.ironsspellbooks.api.spells.CastType; import io.redspace.ironsspellbooks.spells.eldritch.AbstractEldritchSpell; import net.minecraft.ChatFormatting; -import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.player.LocalPlayer; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.Style; -import net.minecraft.network.chat.contents.TranslatableContents; -import net.minecraft.resources.ResourceLocation; import net.minecraft.util.FormattedCharSequence; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import org.spongepowered.asm.mixin.Mutable; import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -import static io.redspace.ironsspellbooks.gui.scroll_forge.ScrollForgeScreen.RUNIC_FONT; - public class TooltipsUtils { @@ -128,11 +122,12 @@ public static MutableComponent getManaCostComponent(CastType castType, int manaC } public static List createSpellDescriptionTooltip(AbstractSpell spell, Font font) { - var name = spell.getDisplayName(Minecraft.getInstance().player); + Player player = MinecraftInstanceHelper.instance.player(); + var name = spell.getDisplayName(player); var description = font.split(Component.translatable(String.format("%s.guide", spell.getComponentId())).withStyle(ChatFormatting.GRAY), 180); var hoverText = new ArrayList(); hoverText.add(FormattedCharSequence.forward(name.getString(), name.getStyle().withUnderlined(true))); - if (!spell.obfuscateStats(Minecraft.getInstance().player)) { + if (!spell.obfuscateStats(player)) { hoverText.addAll(description); } return hoverText;