From bdddea258a700c61197eee9c1aaf7e548aa4bdba Mon Sep 17 00:00:00 2001 From: Starexify Date: Thu, 31 Oct 2024 11:05:24 +0100 Subject: [PATCH] 1.0.0 --- .../client/renderer/NMTItemProperties.java | 24 ++++++---- .../nova/nmt/data/NMTItemModelProvider.java | 47 ++++++------------- .../java/net/nova/nmt/init/CreativeTab.java | 7 +-- .../java/net/nova/nmt/init/NMTPotions.java | 4 +- 4 files changed, 34 insertions(+), 48 deletions(-) diff --git a/src/main/java/net/nova/nmt/client/renderer/NMTItemProperties.java b/src/main/java/net/nova/nmt/client/renderer/NMTItemProperties.java index 32c00bf..cc650a8 100644 --- a/src/main/java/net/nova/nmt/client/renderer/NMTItemProperties.java +++ b/src/main/java/net/nova/nmt/client/renderer/NMTItemProperties.java @@ -9,6 +9,9 @@ import net.nova.nmt.init.NMTItems; import net.nova.nmt.init.NMTPotions; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + public class NMTItemProperties { public static ResourceLocation potionTypePredicate = NoMoreThings.rl("potion_type"); @@ -18,17 +21,22 @@ public static void addCustomItemProperties() { makePotion(NMTItems.LINGERING_OBSIDIAN_POTION.get()); } - private static void makePotion(Item item) { + public static void makePotion(Item item) { ItemProperties.register(item, potionTypePredicate, (stack, level, entity, seed) -> { PotionContents potionContents = stack.get(DataComponents.POTION_CONTENTS); - if (potionContents != null) { - if (potionContents.is(NMTPotions.LAVA)) { - return 1.0F; - } else if (potionContents.is(NMTPotions.AWFULLY)) { - return 2.0F; + if (potionContents == null) return 0.0F; + + AtomicInteger counter = new AtomicInteger(0); + AtomicReference result = new AtomicReference<>(0.0F); + + NMTPotions.POTIONS.getEntries().forEach(potion -> { + int currentId = counter.incrementAndGet(); + if (potionContents.is(potion)) { + result.set((float) currentId); } - } - return 0.0F; + }); + + return result.get(); }); } } diff --git a/src/main/java/net/nova/nmt/data/NMTItemModelProvider.java b/src/main/java/net/nova/nmt/data/NMTItemModelProvider.java index d1c6c5b..be7854a 100644 --- a/src/main/java/net/nova/nmt/data/NMTItemModelProvider.java +++ b/src/main/java/net/nova/nmt/data/NMTItemModelProvider.java @@ -1,30 +1,21 @@ package net.nova.nmt.data; -import net.minecraft.core.Holder; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.data.PackOutput; import net.minecraft.world.item.Item; -import net.minecraft.world.item.alchemy.Potion; import net.neoforged.neoforge.client.model.generators.ItemModelProvider; import net.neoforged.neoforge.client.model.generators.ModelFile; import net.neoforged.neoforge.common.data.ExistingFileHelper; -import net.neoforged.neoforge.registries.DeferredHolder; import net.nova.nmt.NoMoreThings; import net.nova.nmt.client.renderer.NMTItemProperties; import net.nova.nmt.init.NMTItems; import net.nova.nmt.init.NMTPotions; -import java.util.List; -import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import static net.nova.nmt.NoMoreThings.MODID; public class NMTItemModelProvider extends ItemModelProvider { - public static final Map POTION_PREDICATES = Map.of( - "lava", 1.0f, - "awfully", 2.0f - ); - public NMTItemModelProvider(PackOutput output, ExistingFileHelper existingFileHelper) { super(output, MODID, existingFileHelper); } @@ -40,12 +31,15 @@ protected void registerModels() { // Models public void potionItem(Item item) { String itemName = getItemName(item); + float[] potionId = {0.0f}; - String prefix = ""; + String prefix; if (itemName.startsWith("splash_")) { prefix = "splash_"; } else if (itemName.startsWith("lingering_")) { prefix = "lingering_"; + } else { + prefix = ""; } String baseTexture = prefix + "lava_bottle"; @@ -54,35 +48,24 @@ public void potionItem(Item item) { .parent(new ModelFile.UncheckedModelFile("item/generated")) .texture("layer0", NoMoreThings.rl("item/" + baseTexture)); - // Sort potions to ensure lava (1.0) comes before awfully (2.0) - List> sortedPotions = NMTPotions.POTIONS.getEntries().stream() - .sorted((a, b) -> { - float predA = POTION_PREDICATES.getOrDefault(a.getKey().location().getPath(), 0.0f); - float predB = POTION_PREDICATES.getOrDefault(b.getKey().location().getPath(), 0.0f); - return Float.compare(predA, predB); // Normal order to get 1.0 before 2.0 - }) - .toList(); - - for (Holder potionHolder : sortedPotions) { + NMTPotions.POTIONS.getEntries().forEach(potionHolder -> { String potion = potionHolder.getKey().location().getPath(); String potionName = switch (potion) { case "lava" -> prefix + potion + "_bottle"; default -> prefix + potion + "_potion"; }; - float predicate = POTION_PREDICATES.getOrDefault(potion, 0.0f); + getBuilder(itemName).override() + .predicate(NMTItemProperties.potionTypePredicate, potionId[0] + 1.0f) + .model(new ModelFile.UncheckedModelFile(NoMoreThings.rl("item/" + potionName))) + .end(); - if (predicate > 0) { // Only generate override for potions with predicates - getBuilder(itemName).override() - .predicate(NMTItemProperties.potionTypePredicate, predicate) - .model(new ModelFile.UncheckedModelFile(NoMoreThings.rl("item/" + potionName))) - .end(); + getBuilder("item/" + potionName) + .parent(new ModelFile.UncheckedModelFile("item/generated")) + .texture("layer0", NoMoreThings.rl("item/" + potionName)); - getBuilder("item/" + potionName) - .parent(new ModelFile.UncheckedModelFile("item/generated")) - .texture("layer0", NoMoreThings.rl("item/" + potionName)); - } - } + potionId[0] += 1.0f; + }); } public String getItemName(Item item) { diff --git a/src/main/java/net/nova/nmt/init/CreativeTab.java b/src/main/java/net/nova/nmt/init/CreativeTab.java index e63e182..56d42c7 100644 --- a/src/main/java/net/nova/nmt/init/CreativeTab.java +++ b/src/main/java/net/nova/nmt/init/CreativeTab.java @@ -48,12 +48,7 @@ public class CreativeTab { ); private static void generatePotionEffectTypes(CreativeModeTab.Output output, Item item) { - List> nmtPotions = Arrays.asList( - NMTPotions.LAVA, - NMTPotions.AWFULLY - ); - - nmtPotions.stream() + NMTPotions.POTIONS.getEntries().stream() .map(potion -> PotionContents.createItemStack(item, potion)) .forEach(itemStack -> output.accept(itemStack, CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS)); } diff --git a/src/main/java/net/nova/nmt/init/NMTPotions.java b/src/main/java/net/nova/nmt/init/NMTPotions.java index bd8e478..f0a0814 100644 --- a/src/main/java/net/nova/nmt/init/NMTPotions.java +++ b/src/main/java/net/nova/nmt/init/NMTPotions.java @@ -12,7 +12,7 @@ public class NMTPotions { public static final DeferredRegister POTIONS = DeferredRegister.create(Registries.POTION, MODID); - public static final Holder AWFULLY = POTIONS.register("awfully", () -> new Potion() { + public static final Holder LAVA = POTIONS.register("lava", () -> new Potion(new MobEffectInstance(NMTEffects.BURN, 900)) { @Override public boolean isEnabled(FeatureFlagSet enabledFeatures) { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); @@ -24,7 +24,7 @@ public boolean isEnabled(FeatureFlagSet enabledFeatures) { return false; } }); - public static final Holder LAVA = POTIONS.register("lava", () -> new Potion(new MobEffectInstance(NMTEffects.BURN, 900)) { + public static final Holder AWFULLY = POTIONS.register("awfully", () -> new Potion() { @Override public boolean isEnabled(FeatureFlagSet enabledFeatures) { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();