From 9e6ffe71f39156563974d89576f07b90361abc63 Mon Sep 17 00:00:00 2001 From: Fej1Dev Date: Mon, 21 Oct 2024 14:21:29 +0300 Subject: [PATCH 1/2] Fix crash when effect gets saved satisfyu/Let-s-Do-Hub#282 --- .../platform/EffectRegister.java | 12 ++++++ .../registry/MobEffectRegistry.java | 42 ++++++++++--------- .../platform/fabric/EffectRegisterImpl.java | 15 +++++++ .../neoforge/FarmAndCharmNeoForge.java | 2 + .../platform/neoforge/EffectRegisterImpl.java | 17 ++++++++ 5 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 common/src/main/java/net/satisfy/farm_and_charm/platform/EffectRegister.java create mode 100644 fabric/src/main/java/net/satisfy/farm_and_charm/platform/fabric/EffectRegisterImpl.java create mode 100644 forge/src/main/java/net/satisfy/farm_and_charm/platform/neoforge/EffectRegisterImpl.java diff --git a/common/src/main/java/net/satisfy/farm_and_charm/platform/EffectRegister.java b/common/src/main/java/net/satisfy/farm_and_charm/platform/EffectRegister.java new file mode 100644 index 00000000..95c5a749 --- /dev/null +++ b/common/src/main/java/net/satisfy/farm_and_charm/platform/EffectRegister.java @@ -0,0 +1,12 @@ +package net.satisfy.farm_and_charm.platform; + +import dev.architectury.injectables.annotations.ExpectPlatform; +import net.minecraft.core.Holder; +import net.minecraft.world.effect.MobEffect; + +import java.util.function.Supplier; + +public class EffectRegister { + @ExpectPlatform + public static Holder registerEffect(String name, Supplier effect) {throw new AssertionError();} +} diff --git a/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java b/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java index c824ab1f..aeb3fd33 100644 --- a/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java +++ b/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java @@ -4,10 +4,12 @@ import dev.architectury.registry.registries.DeferredRegister; import dev.architectury.registry.registries.Registrar; import dev.architectury.registry.registries.RegistrySupplier; +import net.minecraft.core.Holder; import net.minecraft.core.registries.Registries; import net.minecraft.world.effect.MobEffect; import net.satisfy.farm_and_charm.FarmAndCharm; import net.satisfy.farm_and_charm.effect.*; +import net.satisfy.farm_and_charm.platform.EffectRegister; import net.satisfy.farm_and_charm.util.FarmAndCharmIdentifier; import java.util.function.Supplier; @@ -16,16 +18,16 @@ public class MobEffectRegistry { private static final DeferredRegister MOB_EFFECTS = DeferredRegister.create(FarmAndCharm.MOD_ID, Registries.MOB_EFFECT); private static final Registrar MOB_EFFECTS_REGISTRAR = MOB_EFFECTS.getRegistrar(); - public static final RegistrySupplier SWEETS; - public static final RegistrySupplier HORSE_FODDER; - public static final RegistrySupplier DOG_FOOD; - public static final RegistrySupplier CLUCK; - public static final RegistrySupplier GRANDMAS_BLESSING; - public static final RegistrySupplier RESTED; - public static final RegistrySupplier FARMERS_BLESSING; - public static final RegistrySupplier SUSTENANCE; - public static final RegistrySupplier SATIATION; - public static final RegistrySupplier FEAST; + public static final Holder SWEETS; + public static final Holder HORSE_FODDER; + public static final Holder DOG_FOOD; + public static final Holder CLUCK; + public static final Holder GRANDMAS_BLESSING; + public static final Holder RESTED; + public static final Holder FARMERS_BLESSING; + public static final Holder SUSTENANCE; + public static final Holder SATIATION; + public static final Holder FEAST; private static RegistrySupplier registerEffect(String name, Supplier effect) { if (Platform.isNeoForge()) { @@ -40,15 +42,15 @@ public static void init() { } static { - SWEETS = registerEffect("sweets", SweetsEffect::new); - HORSE_FODDER = registerEffect("horse_fodder", HorseFodderEffect::new); - DOG_FOOD = registerEffect("dog_food", DogFoodEffect::new); - CLUCK = registerEffect("cluck", ChickenEffect::new); - GRANDMAS_BLESSING = registerEffect("grandmas_blessing", GrandmasBlessingEffect::new); - RESTED = registerEffect("rested", RestedEffect::new); - FARMERS_BLESSING = registerEffect("farmers_blessing", FarmersBlessingEffect::new); - SUSTENANCE = registerEffect("sustenance", SustenanceEffect::new); - SATIATION = registerEffect("satiation", SatiationEffect::new); - FEAST = registerEffect("feast", FeastEffect::new); + SWEETS = EffectRegister.registerEffect("sweets", SweetsEffect::new); + HORSE_FODDER = EffectRegister.registerEffect("horse_fodder", HorseFodderEffect::new); + DOG_FOOD = EffectRegister.registerEffect("dog_food", DogFoodEffect::new); + CLUCK = EffectRegister.registerEffect("cluck", ChickenEffect::new); + GRANDMAS_BLESSING = EffectRegister.registerEffect("grandmas_blessing", GrandmasBlessingEffect::new); + RESTED = EffectRegister.registerEffect("rested", RestedEffect::new); + FARMERS_BLESSING = EffectRegister.registerEffect("farmers_blessing", FarmersBlessingEffect::new); + SUSTENANCE = EffectRegister.registerEffect("sustenance", SustenanceEffect::new); + SATIATION = EffectRegister.registerEffect("satiation", SatiationEffect::new); + FEAST = EffectRegister.registerEffect("feast", FeastEffect::new); } } diff --git a/fabric/src/main/java/net/satisfy/farm_and_charm/platform/fabric/EffectRegisterImpl.java b/fabric/src/main/java/net/satisfy/farm_and_charm/platform/fabric/EffectRegisterImpl.java new file mode 100644 index 00000000..218cbc7b --- /dev/null +++ b/fabric/src/main/java/net/satisfy/farm_and_charm/platform/fabric/EffectRegisterImpl.java @@ -0,0 +1,15 @@ +package net.satisfy.farm_and_charm.platform.fabric; + +import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.world.effect.MobEffect; +import net.satisfy.farm_and_charm.util.FarmAndCharmIdentifier; + +import java.util.function.Supplier; + +public class EffectRegisterImpl { + public static Holder registerEffect(String name, Supplier effect) { + return Registry.registerForHolder(BuiltInRegistries.MOB_EFFECT, FarmAndCharmIdentifier.of(name), effect.get()); + } +} diff --git a/forge/src/main/java/net/satisfy/farm_and_charm/neoforge/FarmAndCharmNeoForge.java b/forge/src/main/java/net/satisfy/farm_and_charm/neoforge/FarmAndCharmNeoForge.java index 0d2d376e..3c43be8f 100644 --- a/forge/src/main/java/net/satisfy/farm_and_charm/neoforge/FarmAndCharmNeoForge.java +++ b/forge/src/main/java/net/satisfy/farm_and_charm/neoforge/FarmAndCharmNeoForge.java @@ -4,6 +4,7 @@ import net.neoforged.fml.common.Mod; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.satisfy.farm_and_charm.FarmAndCharm; +import net.satisfy.farm_and_charm.platform.neoforge.EffectRegisterImpl; import net.satisfy.farm_and_charm.registry.CompostableRegistry; @Mod(FarmAndCharm.MOD_ID) @@ -11,6 +12,7 @@ public class FarmAndCharmNeoForge { public FarmAndCharmNeoForge(IEventBus modBus) { FarmAndCharm.init(); + EffectRegisterImpl.MOB_EFFECTS.register(modBus); modBus.addListener(this::commonSetup); } diff --git a/forge/src/main/java/net/satisfy/farm_and_charm/platform/neoforge/EffectRegisterImpl.java b/forge/src/main/java/net/satisfy/farm_and_charm/platform/neoforge/EffectRegisterImpl.java new file mode 100644 index 00000000..89e1ba48 --- /dev/null +++ b/forge/src/main/java/net/satisfy/farm_and_charm/platform/neoforge/EffectRegisterImpl.java @@ -0,0 +1,17 @@ +package net.satisfy.farm_and_charm.platform.neoforge; + +import net.minecraft.core.Holder; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.world.effect.MobEffect; +import net.neoforged.neoforge.registries.DeferredRegister; +import net.satisfy.farm_and_charm.FarmAndCharm; + +import java.util.function.Supplier; + +public class EffectRegisterImpl { + public static final DeferredRegister MOB_EFFECTS = DeferredRegister.create(BuiltInRegistries.MOB_EFFECT, FarmAndCharm.MOD_ID); + + public static Holder registerEffect(String name, Supplier effect) { + return MOB_EFFECTS.register(name, effect); + } +} From aea97af6bb125dd4b9783c633df8c0e089bd3ef5 Mon Sep 17 00:00:00 2001 From: Fej1Dev Date: Mon, 21 Oct 2024 15:04:41 +0300 Subject: [PATCH 2/2] remove unnecessary effect code --- .../net/satisfy/farm_and_charm/FarmAndCharm.java | 1 - .../farm_and_charm/registry/MobEffectRegistry.java | 14 -------------- 2 files changed, 15 deletions(-) diff --git a/common/src/main/java/net/satisfy/farm_and_charm/FarmAndCharm.java b/common/src/main/java/net/satisfy/farm_and_charm/FarmAndCharm.java index f68c3767..a16d17f5 100644 --- a/common/src/main/java/net/satisfy/farm_and_charm/FarmAndCharm.java +++ b/common/src/main/java/net/satisfy/farm_and_charm/FarmAndCharm.java @@ -17,7 +17,6 @@ public static void init() { MANAGER = Suppliers.memoize(() -> RegistrarManager.get(MOD_ID)); ObjectRegistry.init(); EntityTypeRegistry.init(); - MobEffectRegistry.init(); TabRegistry.init(); ScreenhandlerTypeRegistry.init(); RecipeTypeRegistry.init(); diff --git a/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java b/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java index aeb3fd33..d7824e54 100644 --- a/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java +++ b/common/src/main/java/net/satisfy/farm_and_charm/registry/MobEffectRegistry.java @@ -15,8 +15,6 @@ import java.util.function.Supplier; public class MobEffectRegistry { - private static final DeferredRegister MOB_EFFECTS = DeferredRegister.create(FarmAndCharm.MOD_ID, Registries.MOB_EFFECT); - private static final Registrar MOB_EFFECTS_REGISTRAR = MOB_EFFECTS.getRegistrar(); public static final Holder SWEETS; public static final Holder HORSE_FODDER; @@ -29,18 +27,6 @@ public class MobEffectRegistry { public static final Holder SATIATION; public static final Holder FEAST; - private static RegistrySupplier registerEffect(String name, Supplier effect) { - if (Platform.isNeoForge()) { - return MOB_EFFECTS.register(name, effect); - } - return MOB_EFFECTS_REGISTRAR.register(FarmAndCharmIdentifier.of(name), effect); - } - - public static void init() { - FarmAndCharm.LOGGER.debug("Registering MobEffects for " + FarmAndCharm.MOD_ID); - MOB_EFFECTS.register(); - } - static { SWEETS = EffectRegister.registerEffect("sweets", SweetsEffect::new); HORSE_FODDER = EffectRegister.registerEffect("horse_fodder", HorseFodderEffect::new);