diff --git a/build.gradle b/build.gradle index 114fbbc..8071ca8 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,10 @@ base { archivesName = project.archives_base_name } +loom { + accessWidenerPath = file("src/main/resources/fallingleaves.accesswidener") +} + repositories { maven { url 'https://maven.shedaniel.me/' } maven { url 'https://maven.terraformersmc.com/' } diff --git a/src/main/java/randommcsomethin/fallingleaves/init/Leaves.java b/src/main/java/randommcsomethin/fallingleaves/init/Leaves.java index f96b5fa..3885602 100644 --- a/src/main/java/randommcsomethin/fallingleaves/init/Leaves.java +++ b/src/main/java/randommcsomethin/fallingleaves/init/Leaves.java @@ -1,10 +1,11 @@ package randommcsomethin.fallingleaves.init; -import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry; import net.fabricmc.fabric.api.event.player.AttackBlockCallback; +import net.fabricmc.fabric.api.particle.v1.FabricParticleTypes; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; import net.minecraft.block.BlockState; +import net.minecraft.client.particle.ParticleFactory; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.particle.BlockStateParticleEffect; import net.minecraft.particle.ParticleType; @@ -17,34 +18,33 @@ import net.minecraft.util.math.Direction; import net.minecraft.world.World; import randommcsomethin.fallingleaves.config.LeafSettingsEntry; -import randommcsomethin.fallingleaves.particle.FallingLeafParticle; import randommcsomethin.fallingleaves.util.LeafUtil; -import randommcsomethin.fallingleaves.util.RegistryUtil; import randommcsomethin.fallingleaves.util.TextureCache; -import static randommcsomethin.fallingleaves.FallingLeavesClient.LOGGER; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static randommcsomethin.fallingleaves.init.Config.CONFIG; import static randommcsomethin.fallingleaves.util.LeafUtil.getLeafSettingsEntry; import static randommcsomethin.fallingleaves.util.RegistryUtil.makeId; public class Leaves { - public static ParticleType FALLING_LEAF; - public static ParticleType FALLING_CONIFER_LEAF; - public static ParticleType FALLING_SNOW; + public static ParticleType FALLING_LEAF = FabricParticleTypes.complex(true, BlockStateParticleEffect.PARAMETERS_FACTORY); + public static ParticleType FALLING_CONIFER_LEAF = FabricParticleTypes.complex(true, BlockStateParticleEffect.PARAMETERS_FACTORY); + public static ParticleType FALLING_SNOW = FabricParticleTypes.complex(true, BlockStateParticleEffect.PARAMETERS_FACTORY); + + public static final List> TYPES = List.of(FALLING_LEAF, FALLING_CONIFER_LEAF, FALLING_SNOW); + public static final Map, Identifier> IDS = Map.of( + FALLING_LEAF, makeId("falling_leaf"), + FALLING_CONIFER_LEAF, makeId("falling_leaf_conifer"), + FALLING_SNOW, makeId("falling_snow") + ); + public static final Map, ParticleFactory> FACTORIES = new HashMap<>(); private static boolean preLoadedRegisteredLeafBlocks = false; public static void init() { - LOGGER.debug("Registering leaf particles."); - - FALLING_LEAF = RegistryUtil.registerNewLeafParticle("falling_leaf"); - FALLING_CONIFER_LEAF = RegistryUtil.registerNewLeafParticle("falling_leaf_conifer"); - FALLING_SNOW = RegistryUtil.registerNewLeafParticle("falling_snow"); - - ParticleFactoryRegistry.getInstance().register(FALLING_LEAF, FallingLeafParticle.BlockStateFactory::new); - ParticleFactoryRegistry.getInstance().register(FALLING_CONIFER_LEAF, FallingLeafParticle.BlockStateFactory::new); - ParticleFactoryRegistry.getInstance().register(FALLING_SNOW, FallingLeafParticle.BlockStateFactory::new); - registerReloadListener(); registerAttackBlockLeaves(); } diff --git a/src/main/java/randommcsomethin/fallingleaves/mixin/ParticleManagerMixin.java b/src/main/java/randommcsomethin/fallingleaves/mixin/ParticleManagerMixin.java index 1f1bde0..3a79cb8 100644 --- a/src/main/java/randommcsomethin/fallingleaves/mixin/ParticleManagerMixin.java +++ b/src/main/java/randommcsomethin/fallingleaves/mixin/ParticleManagerMixin.java @@ -4,14 +4,22 @@ import net.fabricmc.api.Environment; import net.minecraft.client.particle.ParticleManager; import net.minecraft.client.world.ClientWorld; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import randommcsomethin.fallingleaves.init.Leaves; +import randommcsomethin.fallingleaves.particle.FallingLeafParticle; import randommcsomethin.fallingleaves.util.Wind; +import java.util.Map; + import static randommcsomethin.fallingleaves.init.Config.CONFIG; +import static randommcsomethin.fallingleaves.init.Leaves.FACTORIES; +import static randommcsomethin.fallingleaves.init.Leaves.IDS; @Environment(EnvType.CLIENT) @Mixin(ParticleManager.class) @@ -20,6 +28,20 @@ public abstract class ParticleManagerMixin { @Shadow protected ClientWorld world; + @Shadow @Final + private Map spriteAwareFactories; + + @Inject(method = "registerDefaultFactories", at = @At("RETURN")) + public void registerLeafFactories(CallbackInfo ci) { + for (var type : Leaves.TYPES) { + Identifier id = IDS.get(type); + + var spriteProvider = SimpleSpriteProviderInvoker.init(); + spriteAwareFactories.put(id, spriteProvider); + FACTORIES.put(type, new FallingLeafParticle.BlockStateFactory(spriteProvider)); + } + } + @Inject(method = "tick", at = @At("HEAD")) public void tickWind(CallbackInfo ci) { if (!CONFIG.enabled) diff --git a/src/main/java/randommcsomethin/fallingleaves/mixin/SimpleSpriteProviderInvoker.java b/src/main/java/randommcsomethin/fallingleaves/mixin/SimpleSpriteProviderInvoker.java new file mode 100644 index 0000000..73822e3 --- /dev/null +++ b/src/main/java/randommcsomethin/fallingleaves/mixin/SimpleSpriteProviderInvoker.java @@ -0,0 +1,13 @@ +package randommcsomethin.fallingleaves.mixin; + +import net.minecraft.client.particle.ParticleManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(ParticleManager.SimpleSpriteProvider.class) +public interface SimpleSpriteProviderInvoker { + @Invoker("") + static ParticleManager.SimpleSpriteProvider init() { + throw new AssertionError(); + } +} diff --git a/src/main/java/randommcsomethin/fallingleaves/util/LeafUtil.java b/src/main/java/randommcsomethin/fallingleaves/util/LeafUtil.java index e1c459d..fde45fb 100644 --- a/src/main/java/randommcsomethin/fallingleaves/util/LeafUtil.java +++ b/src/main/java/randommcsomethin/fallingleaves/util/LeafUtil.java @@ -5,6 +5,7 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.block.*; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.particle.Particle; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.BakedQuad; import net.minecraft.client.texture.NativeImage; @@ -133,6 +134,8 @@ public static void spawnSnowParticles(int count, boolean spawnInsideBlock, Block public static void spawnParticles(int count, BlockStateParticleEffect params, boolean spawnInsideBlock, BlockState state, World world, BlockPos pos, Random random, LeafSettingsEntry leafSettings) { if (count == 0) return; + MinecraftClient client = MinecraftClient.getInstance(); + for (int i = 0; i < count; i++) { // Particle position double x = pos.getX() + random.nextDouble(); @@ -148,7 +151,8 @@ public static void spawnParticles(int count, BlockStateParticleEffect params, bo continue; } - world.addParticle(params, x, y, z, 0, 0, 0); + Particle particle = Leaves.FACTORIES.get(params.getType()).createParticle(params, client.world, x, y, z, 0, 0, 0); + client.particleManager.addParticle(particle); } } diff --git a/src/main/resources/fallingleaves.accesswidener b/src/main/resources/fallingleaves.accesswidener new file mode 100644 index 0000000..92ea180 --- /dev/null +++ b/src/main/resources/fallingleaves.accesswidener @@ -0,0 +1,2 @@ +accessWidener v2 named +accessible class net/minecraft/client/particle/ParticleManager$SimpleSpriteProvider \ No newline at end of file diff --git a/src/main/resources/fallingleaves.mixins.json b/src/main/resources/fallingleaves.mixins.json index 156044c..75ccb22 100644 --- a/src/main/resources/fallingleaves.mixins.json +++ b/src/main/resources/fallingleaves.mixins.json @@ -11,6 +11,7 @@ "MinecraftClientMixin", "NativeImageAccessor", "ParticleManagerMixin", + "SimpleSpriteProviderInvoker", "SpriteContentsAccessor" ], "injectors": {