Skip to content

Commit

Permalink
Compartmentalize client references
Browse files Browse the repository at this point in the history
fixes #227
  • Loading branch information
iron431 committed Nov 28, 2023
1 parent 4de4736 commit 14e49a5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/io/redspace/ironsspellbooks/item/Scroll.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Component> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
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;
import net.minecraft.network.chat.Component;
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;
Expand All @@ -25,26 +24,23 @@ public AffinityRing(Properties properties) {
super(properties);
}

@OnlyIn(Dist.CLIENT)
@Override
public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> 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<IClientItemExtensions> consumer) {
consumer.accept(new IClientItemExtensions() {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/io/redspace/ironsspellbooks/setup/ClientSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@
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;
import io.redspace.ironsspellbooks.registries.ItemRegistry;
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;
Expand All @@ -78,15 +79,14 @@
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;
import net.minecraftforge.client.event.RegisterParticleProvidersEvent;
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;
Expand Down Expand Up @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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();}
}
17 changes: 6 additions & 11 deletions src/main/java/io/redspace/ironsspellbooks/util/TooltipsUtils.java
Original file line number Diff line number Diff line change
@@ -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 {


Expand Down Expand Up @@ -128,11 +122,12 @@ public static MutableComponent getManaCostComponent(CastType castType, int manaC
}

public static List<FormattedCharSequence> 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<FormattedCharSequence>();
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;
Expand Down

0 comments on commit 14e49a5

Please sign in to comment.