Skip to content

Commit

Permalink
Another trinket fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
McJty committed Apr 14, 2023
1 parent 71ac181 commit 516b84a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/mcjty/fancytrinkets/api/ITrinketItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mcjty.fancytrinkets.datapack.TrinketDescription;
import com.mcjty.fancytrinkets.modules.effects.IEffect;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

Expand All @@ -15,7 +14,7 @@ public interface ITrinketItem {
// Get the id of the trinket that this item represents
ResourceLocation getTrinketId(ItemStack stack);

void registerTrinketInstance(ServerLevel level, ResourceLocation id, TrinketDescription description);
void registerTrinketInstance(Level level, ResourceLocation id, TrinketDescription description);

void forAllEffects(Level level, ItemStack stack, BiConsumer<IEffect, Integer> consumer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import mcjty.lib.varia.Tools;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -36,7 +36,7 @@ public static record EffectRef(ResourceLocation effectId, boolean hidden) {
Codec.list(EffectRef.EFFECTREF_CODEC).fieldOf("effects").forGetter(l -> l.effects)
).apply(instance, TrinketDescription::new));

public TrinketInstance build(ResourceLocation id, ServerLevel level) {
public TrinketInstance build(ResourceLocation id, Level level) {
List<EffectInstance> effectInstances = new ArrayList<>();
for (EffectRef effectRef : effects) {
EffectDescription effectDescription = Tools.getRegistryAccess(level).registryOrThrow(CustomRegistries.EFFECT_REGISTRY_KEY).get(effectRef.effectId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.Tags;
import net.minecraftforge.common.capabilities.Capability;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void onServerStarting(ServerStartingEvent event) {
registerTrinkets(event.getServer().overworld());
}

private void registerTrinkets(ServerLevel level) {
public static void registerTrinkets(Level level) {
Registry<TrinketDescription> registry = Tools.getRegistryAccess(level).registryOrThrow(CustomRegistries.TRINKET_REGISTRY_KEY);
for (ResourceLocation trinket : Tools.getRegistryAccess(level).registryOrThrow(CustomRegistries.TRINKET_SET_REGISTRY_KEY).get(new ResourceLocation(MODID, "standard")).trinkets()) {
TrinketDescription description = registry.get(trinket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mcjty.fancytrinkets.modules.effects.EffectInstance;
import com.mcjty.fancytrinkets.modules.effects.IEffect;
import com.mcjty.fancytrinkets.modules.trinkets.TrinketInstance;
import com.mcjty.fancytrinkets.modules.trinkets.TrinketsModule;
import com.mcjty.fancytrinkets.setup.Config;
import com.mcjty.fancytrinkets.setup.Registration;
import mcjty.lib.items.BaseItem;
Expand All @@ -26,7 +27,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
Expand Down Expand Up @@ -118,7 +118,7 @@ private static void addEffect(float targetQuality, List<ResourceLocation> effect
@Override
public List<ItemStack> getItemsForTab() {
List<ItemStack> list = new ArrayList<>();
for (TrinketInstance trinket : trinkets.values()) {
for (TrinketInstance trinket : getTrinkets(SafeClientTools.getClientWorld()).values()) {
ItemStack stack = new ItemStack(this);
toNBT(stack, trinket);
list.add(stack);
Expand Down Expand Up @@ -181,16 +181,24 @@ public ResourceLocation getTrinketId(ItemStack stack) {
}

@Override
public void registerTrinketInstance(ServerLevel level, ResourceLocation id, TrinketDescription description) {
public void registerTrinketInstance(Level level, ResourceLocation id, TrinketDescription description) {
trinkets.put(id, description.build(id, level));
}

// Get and create trinket map
private Map<ResourceLocation, TrinketInstance> getTrinkets(Level level) {
if (trinkets.isEmpty()) {
TrinketsModule.registerTrinkets(level);
}
return trinkets;
}

@Override
public void forAllEffects(Level level, ItemStack stack, BiConsumer<IEffect, Integer> consumer) {
ResourceLocation trinketId = getTrinketId(stack);
if (trinketId != null) {
AtomicInteger idx = new AtomicInteger(0);
TrinketInstance instance = trinkets.get(trinketId);
TrinketInstance instance = getTrinkets(level).get(trinketId);
if (instance != null) {
for (EffectInstance effect : instance.effects()) {
consumer.accept(effect.effect(), idx.incrementAndGet());
Expand All @@ -210,7 +218,7 @@ public void forAllEffects(Level level, ItemStack stack, BiConsumer<IEffect, Inte
public void appendHoverText(ItemStack stack, Level world, List<Component> list, TooltipFlag flags) {
ResourceLocation trinketId = getTrinketId(stack);
if (trinketId != null) {
TrinketInstance instance = trinkets.get(trinketId);
TrinketInstance instance = getTrinkets(world).get(trinketId);
if (instance != null) {
MutableComponent name = ComponentFactory.translatable(instance.nameKey()).withStyle(ChatFormatting.AQUA);
if (list.isEmpty()) {
Expand Down

0 comments on commit 516b84a

Please sign in to comment.