diff --git a/.gitignore b/.gitignore index 3c37caf..2174bae 100644 --- a/.gitignore +++ b/.gitignore @@ -113,6 +113,8 @@ gradle-app.setting # Common working directory run/ +run-data/ +src/generated/ # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) !gradle-wrapper.jar diff --git a/src/main/java/com/gensokyo/nucleardelight/NuclearDelight.java b/src/main/java/com/gensokyo/nucleardelight/NuclearDelight.java index 2d8f043..ef83094 100644 --- a/src/main/java/com/gensokyo/nucleardelight/NuclearDelight.java +++ b/src/main/java/com/gensokyo/nucleardelight/NuclearDelight.java @@ -1,9 +1,12 @@ package com.gensokyo.nucleardelight; +import com.gensokyo.nucleardelight.register.AutoRegistryObject; +import com.gensokyo.nucleardelight.world.effect.MobEffects; import com.gensokyo.nucleardelight.world.food.Foods; import com.mojang.logging.LogUtils; import net.minecraft.client.Minecraft; import net.minecraft.core.registries.Registries; +import net.minecraft.world.effect.MobEffect; import net.minecraft.world.food.FoodProperties; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.CreativeModeTab; @@ -30,7 +33,8 @@ import net.minecraftforge.registries.RegistryObject; import org.slf4j.Logger; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import java.util.stream.Collectors; // The value here should match an entry in the META-INF/mods.toml file @@ -45,6 +49,7 @@ public class NuclearDelight { public static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); // Create a Deferred Register to hold Items which will all be registered under the "nucleardelight" namespace public static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); + public static final DeferredRegister MOB_EFFECTS = DeferredRegister.create(ForgeRegistries.MOB_EFFECTS, MODID); // Create a Deferred Register to hold CreativeModeTabs which will all be registered under the "examplemod" namespace public static final DeferredRegister CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID); @@ -65,23 +70,31 @@ public class NuclearDelight { output.accept(EXAMPLE_ITEM.get()); // Add the example item to the tab. For your own tabs, this method is preferred over the event }).build()); + public static List> RegisteredItems = new ArrayList<>(); + public NuclearDelight() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); // Register the commonSetup method for modloading modEventBus.addListener(this::commonSetup); - Utils.registryObjects(ITEMS, + List> registeredFoods = Utils.registryObjects(ITEMS, Utils.getStaticFinalFieldsNameAndValue(Foods.class, FoodProperties.class).entrySet().stream() .collect(Collectors.toMap( entry -> entry.getKey().toLowerCase(), entry -> () -> new Item(new Item.Properties().food(entry.getValue())) ))); + Utils.getStaticFinalFieldsNameAndValue(MobEffects.class, AutoRegistryObject.class) + .forEach((key, value) -> value.register(MOB_EFFECTS, key.toLowerCase())); + + RegisteredItems.addAll(registeredFoods); + // Register the Deferred Register to the mod event bus so blocks get registered BLOCKS.register(modEventBus); // Register the Deferred Register to the mod event bus so items get registered ITEMS.register(modEventBus); + MOB_EFFECTS.register(modEventBus); // Register the Deferred Register to the mod event bus so tabs get registered CREATIVE_MODE_TABS.register(modEventBus); diff --git a/src/main/java/com/gensokyo/nucleardelight/datagenerator/DataGenerator.java b/src/main/java/com/gensokyo/nucleardelight/datagenerator/DataGenerator.java index 9328ab6..2471578 100644 --- a/src/main/java/com/gensokyo/nucleardelight/datagenerator/DataGenerator.java +++ b/src/main/java/com/gensokyo/nucleardelight/datagenerator/DataGenerator.java @@ -1,13 +1,24 @@ package com.gensokyo.nucleardelight.datagenerator; +import com.gensokyo.nucleardelight.NuclearDelight; +import net.minecraft.data.PackOutput; +import net.minecraft.world.item.Item; +import net.minecraftforge.common.data.ExistingFileHelper; import net.minecraftforge.data.event.GatherDataEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.registries.RegistryObject; + +import java.util.List; @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public class DataGenerator { @SubscribeEvent public static void dataGen(GatherDataEvent event) { -// event.getGenerator().addProvider(); + PackOutput packOutput = event.getGenerator().getPackOutput(); + ExistingFileHelper existingFileHelper = event.getExistingFileHelper(); + + List items = NuclearDelight.RegisteredItems.stream().map(RegistryObject::get).toList(); + event.getGenerator().addProvider(true, new SingleTextureItemModelGenerator(items, packOutput, existingFileHelper)); } } diff --git a/src/main/java/com/gensokyo/nucleardelight/datagenerator/SingleTextureItemModelGenerator.java b/src/main/java/com/gensokyo/nucleardelight/datagenerator/SingleTextureItemModelGenerator.java index 73da78b..33768ca 100644 --- a/src/main/java/com/gensokyo/nucleardelight/datagenerator/SingleTextureItemModelGenerator.java +++ b/src/main/java/com/gensokyo/nucleardelight/datagenerator/SingleTextureItemModelGenerator.java @@ -2,6 +2,7 @@ import com.gensokyo.nucleardelight.NuclearDelight; import net.minecraft.data.PackOutput; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraftforge.client.model.generators.ItemModelProvider; import net.minecraftforge.common.data.ExistingFileHelper; @@ -19,7 +20,16 @@ public SingleTextureItemModelGenerator(Collection items, PackOutput output @Override protected void registerModels() { - items.forEach(item -> - singleTexture(item.getDescriptionId(), mcLoc("item/handheld"), "layer0", modLoc("item/" + item.getDescriptionId()))); + items.stream().map(SingleTextureItemModelGenerator::getNameFromItem).forEach(itemName -> + singleTexture(itemName, new ResourceLocation("item/generated"), "layer0", modLoc("item/" + itemName))); + } + + private static String getNameFromItem(Item item) { + return getNameFromDescriptionId(item.getDescriptionId()); + } + + private static String getNameFromDescriptionId(String descriptionId) { + String[] strings = descriptionId.split("\\."); + return strings[strings.length - 1]; } } diff --git a/src/main/java/com/gensokyo/nucleardelight/register/AutoRegistryObject.java b/src/main/java/com/gensokyo/nucleardelight/register/AutoRegistryObject.java new file mode 100644 index 0000000..0ec3ccc --- /dev/null +++ b/src/main/java/com/gensokyo/nucleardelight/register/AutoRegistryObject.java @@ -0,0 +1,23 @@ +package com.gensokyo.nucleardelight.register; + +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.RegistryObject; + +import java.util.function.Supplier; + +public class AutoRegistryObject { + private final Supplier supplier; + private RegistryObject object; + + public AutoRegistryObject(Supplier supplier) { + this.supplier = supplier; + } + + public void register(DeferredRegister register, String name) { + object = register.register(name, supplier); + } + + public T get() { + return object.get(); + } +} diff --git a/src/main/java/com/gensokyo/nucleardelight/world/effect/MobEffects.java b/src/main/java/com/gensokyo/nucleardelight/world/effect/MobEffects.java new file mode 100644 index 0000000..1477bc7 --- /dev/null +++ b/src/main/java/com/gensokyo/nucleardelight/world/effect/MobEffects.java @@ -0,0 +1,8 @@ +package com.gensokyo.nucleardelight.world.effect; + +import com.gensokyo.nucleardelight.register.AutoRegistryObject; +import net.minecraft.world.effect.MobEffect; + +public class MobEffects { + public static final AutoRegistryObject VOMITING = new AutoRegistryObject<>(VomitingMobEffect::new); +} diff --git a/src/main/java/com/gensokyo/nucleardelight/world/effect/VomitingMobEffect.java b/src/main/java/com/gensokyo/nucleardelight/world/effect/VomitingMobEffect.java new file mode 100644 index 0000000..a97f910 --- /dev/null +++ b/src/main/java/com/gensokyo/nucleardelight/world/effect/VomitingMobEffect.java @@ -0,0 +1,28 @@ +package com.gensokyo.nucleardelight.world.effect; + +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectCategory; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.effect.MobEffects; +import net.minecraft.world.entity.LivingEntity; +import org.jetbrains.annotations.NotNull; + +public class VomitingMobEffect extends MobEffect { + + protected VomitingMobEffect() { + super(MobEffectCategory.HARMFUL, 0xA6A03A); + } + + @Override + public void applyEffectTick(@NotNull LivingEntity entity, int level) { + if (!entity.hasEffect(MobEffects.CONFUSION)) { + entity.addEffect(new MobEffectInstance(MobEffects.CONFUSION, + entity.getEffect(com.gensokyo.nucleardelight.world.effect.MobEffects.VOMITING.get()).getDuration(), 1)); + } + } + + @Override + public boolean isDurationEffectTick(int p_19455_, int p_19456_) { + return true; + } +} diff --git a/src/main/java/com/gensokyo/nucleardelight/world/food/Foods.java b/src/main/java/com/gensokyo/nucleardelight/world/food/Foods.java index 5893dc5..b35adf5 100644 --- a/src/main/java/com/gensokyo/nucleardelight/world/food/Foods.java +++ b/src/main/java/com/gensokyo/nucleardelight/world/food/Foods.java @@ -1,41 +1,16 @@ package com.gensokyo.nucleardelight.world.food; -import com.gensokyo.nucleardelight.NuclearDelight; -import com.gensokyo.nucleardelight.Utils; +import com.gensokyo.nucleardelight.world.effect.MobEffects; import com.mojang.logging.LogUtils; import net.minecraft.world.effect.MobEffectInstance; -import net.minecraft.world.effect.MobEffects; import net.minecraft.world.food.FoodProperties; -import net.minecraft.world.item.Item; import org.slf4j.Logger; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Supplier; - public class Foods { private static final Logger LOGGER = LogUtils.getLogger(); -// public static List FOODS = List.of(); -// -// public static List getFoods() { -// return FOODS; -// } - - public static final FoodProperties EXAMPLE_FOOD = new FoodProperties.Builder().alwaysEat() - .effect(() -> new MobEffectInstance(MobEffects.REGENERATION, 100, 1), 1.0F) + public static final FoodProperties APPLE_JAM_JAR = new FoodProperties.Builder().alwaysEat() + .effect(() -> new MobEffectInstance(MobEffects.VOMITING.get(), 1000, 1), 1.0F) .nutrition(1) .saturationMod(2f).build(); - - public static void Initializing() { - LOGGER.debug("Initializing Foods"); - Map staticFinalFieldsNameAndValue = Utils.getStaticFinalFieldsNameAndValue(Foods.class, FoodProperties.class); - staticFinalFieldsNameAndValue.forEach((name, value) -> { - String id = name.toLowerCase(); - Supplier itemSupplier = () -> new Item(new Item.Properties().food(value)); - NuclearDelight.ITEMS.register(id, itemSupplier); - }); - } }