Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Starexify committed Oct 31, 2024
1 parent edb92ec commit bdddea2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 48 deletions.
24 changes: 16 additions & 8 deletions src/main/java/net/nova/nmt/client/renderer/NMTItemProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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<Float> 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();
});
}
}
47 changes: 15 additions & 32 deletions src/main/java/net/nova/nmt/data/NMTItemModelProvider.java
Original file line number Diff line number Diff line change
@@ -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<String, Float> POTION_PREDICATES = Map.of(
"lava", 1.0f,
"awfully", 2.0f
);

public NMTItemModelProvider(PackOutput output, ExistingFileHelper existingFileHelper) {
super(output, MODID, existingFileHelper);
}
Expand All @@ -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";
Expand All @@ -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<DeferredHolder<Potion, ? extends Potion>> 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<Potion> 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) {
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/net/nova/nmt/init/CreativeTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public class CreativeTab {
);

private static void generatePotionEffectTypes(CreativeModeTab.Output output, Item item) {
List<Holder<Potion>> 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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/nova/nmt/init/NMTPotions.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class NMTPotions {
public static final DeferredRegister<Potion> POTIONS = DeferredRegister.create(Registries.POTION, MODID);

public static final Holder<Potion> AWFULLY = POTIONS.register("awfully", () -> new Potion() {
public static final Holder<Potion> LAVA = POTIONS.register("lava", () -> new Potion(new MobEffectInstance(NMTEffects.BURN, 900)) {
@Override
public boolean isEnabled(FeatureFlagSet enabledFeatures) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
Expand All @@ -24,7 +24,7 @@ public boolean isEnabled(FeatureFlagSet enabledFeatures) {
return false;
}
});
public static final Holder<Potion> LAVA = POTIONS.register("lava", () -> new Potion(new MobEffectInstance(NMTEffects.BURN, 900)) {
public static final Holder<Potion> AWFULLY = POTIONS.register("awfully", () -> new Potion() {
@Override
public boolean isEnabled(FeatureFlagSet enabledFeatures) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
Expand Down

0 comments on commit bdddea2

Please sign in to comment.