Skip to content

Commit

Permalink
spawn particles without registering them
Browse files Browse the repository at this point in the history
  • Loading branch information
Fourmisain committed Jan 13, 2024
1 parent 75b5ee6 commit d864c8c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 18 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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/' }
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/randommcsomethin/fallingleaves/init/Leaves.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<BlockStateParticleEffect> FALLING_LEAF;
public static ParticleType<BlockStateParticleEffect> FALLING_CONIFER_LEAF;
public static ParticleType<BlockStateParticleEffect> FALLING_SNOW;
public static ParticleType<BlockStateParticleEffect> FALLING_LEAF = FabricParticleTypes.complex(true, BlockStateParticleEffect.PARAMETERS_FACTORY);
public static ParticleType<BlockStateParticleEffect> FALLING_CONIFER_LEAF = FabricParticleTypes.complex(true, BlockStateParticleEffect.PARAMETERS_FACTORY);
public static ParticleType<BlockStateParticleEffect> FALLING_SNOW = FabricParticleTypes.complex(true, BlockStateParticleEffect.PARAMETERS_FACTORY);

public static final List<ParticleType<BlockStateParticleEffect>> TYPES = List.of(FALLING_LEAF, FALLING_CONIFER_LEAF, FALLING_SNOW);
public static final Map<ParticleType<BlockStateParticleEffect>, 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<ParticleType<BlockStateParticleEffect>, ParticleFactory<BlockStateParticleEffect>> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,6 +28,20 @@ public abstract class ParticleManagerMixin {
@Shadow
protected ClientWorld world;

@Shadow @Final
private Map<Identifier, ParticleManager.SimpleSpriteProvider> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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("<init>")
static ParticleManager.SimpleSpriteProvider init() {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/fallingleaves.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
accessWidener v2 named
accessible class net/minecraft/client/particle/ParticleManager$SimpleSpriteProvider
1 change: 1 addition & 0 deletions src/main/resources/fallingleaves.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"MinecraftClientMixin",
"NativeImageAccessor",
"ParticleManagerMixin",
"SimpleSpriteProviderInvoker",
"SpriteContentsAccessor"
],
"injectors": {
Expand Down

0 comments on commit d864c8c

Please sign in to comment.