Skip to content

Commit

Permalink
Particle API: port and some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Apr 11, 2024
1 parent 639ffcb commit b87e67c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.api.client.particle.v1;

import org.jetbrains.annotations.ApiStatus;

import net.minecraft.client.particle.ParticleFactory;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleType;
Expand All @@ -29,6 +31,7 @@
*
* @see FabricParticleTypes
*/
@ApiStatus.NonExtendable
public interface ParticleFactoryRegistry {
static ParticleFactoryRegistry getInstance() {
return ParticleFactoryRegistryImpl.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@

import java.util.function.Function;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;

import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.particle.DefaultParticleType;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleType;
import net.minecraft.particle.SimpleParticleType;

/**
* Methods for creating particle types, both simple and using an existing attribute factory.
*
* <p>Usage:
* <blockquote>
* <pre>
* public static final DefaultParticleType SIMPLE_TEST_PARTICLE = FabricParticleTypes.simple();
* public static final DefaultParticleType CUSTOM_TEST_PARTICLE = FabricParticleTypes.simple();
* public static final SimpleParticleType SIMPLE_TEST_PARTICLE = FabricParticleTypes.simple();
* public static final SimpleParticleType CUSTOM_TEST_PARTICLE = FabricParticleTypes.simple();
*
* {@literal @}Override
* public void onInitialize() {
Expand All @@ -50,7 +49,7 @@ private FabricParticleTypes() { }
/**
* Creates a new, default particle type for the given id.
*/
public static DefaultParticleType simple() {
public static SimpleParticleType simple() {
return simple(false);
}

Expand All @@ -59,31 +58,29 @@ public static DefaultParticleType simple() {
*
* @param alwaysSpawn True to always spawn the particle regardless of distance.
*/
public static DefaultParticleType simple(boolean alwaysSpawn) {
return new DefaultParticleType(alwaysSpawn) { };
public static SimpleParticleType simple(boolean alwaysSpawn) {
return new SimpleParticleType(alwaysSpawn) { };
}

/**
* Creates a new particle type with a custom factory and codecs for packet/data serialization.
*
* @param factory A factory for serializing string command parameters into a particle effect.
* @param codec The codec for serialization.
* @param packetCodec The packet codec for network serialization.
*/
public static <T extends ParticleEffect> ParticleType<T> complex(ParticleEffect.Factory<T> factory, final Function<ParticleType<T>, Codec<T>> codecGetter, final MapCodec<T> codec, final PacketCodec<? super RegistryByteBuf, T> packetCodec) {
return complex(false, factory, codec, packetCodec);
public static <T extends ParticleEffect> ParticleType<T> complex(final MapCodec<T> codec, final PacketCodec<? super RegistryByteBuf, T> packetCodec) {
return complex(false, codec, packetCodec);
}

/**
* Creates a new particle type with a custom factory and codecs for packet/data serialization.
*
* @param alwaysSpawn True to always spawn the particle regardless of distance.
* @param factory A factory for serializing string command parameters into a particle effect.
* @param codec The codec for serialization.
* @param packetCodec The packet codec for network serialization.
*/
public static <T extends ParticleEffect> ParticleType<T> complex(boolean alwaysSpawn, ParticleEffect.Factory<T> factory, final MapCodec<T> codec, final PacketCodec<? super RegistryByteBuf, T> packetCodec) {
return new ParticleType<T>(alwaysSpawn, factory) {
public static <T extends ParticleEffect> ParticleType<T> complex(boolean alwaysSpawn, final MapCodec<T> codec, final PacketCodec<? super RegistryByteBuf, T> packetCodec) {
return new ParticleType<>(alwaysSpawn) {
@Override
public MapCodec<T> getCodec() {
return codec;
Expand All @@ -100,25 +97,23 @@ public PacketCodec<? super RegistryByteBuf, T> getPacketCodec() {
* Creates a new particle type with a custom factory and codecs for packet/data serialization.
* This method is useful when two different {@link ParticleType}s share the same {@link ParticleEffect} implementation.
*
* @param factory A factory for serializing string command parameters into a particle effect.
* @param codecGetter A function that, given the newly created type, returns the codec for serialization.
* @param packetCodecGetter A function that, given the newly created type, returns the packet codec for network serialization.
*/
public static <T extends ParticleEffect> ParticleType<T> complex(ParticleEffect.Factory<T> factory, final Function<ParticleType<T>, MapCodec<T>> codecGetter, final Function<ParticleType<T>, PacketCodec<? super RegistryByteBuf, T>> packetCodecGetter) {
return complex(false, factory, codecGetter, packetCodecGetter);
public static <T extends ParticleEffect> ParticleType<T> complex(final Function<ParticleType<T>, MapCodec<T>> codecGetter, final Function<ParticleType<T>, PacketCodec<? super RegistryByteBuf, T>> packetCodecGetter) {
return complex(false, codecGetter, packetCodecGetter);
}

/**
* Creates a new particle type with a custom factory and codecs for packet/data serialization.
* This method is useful when two different {@link ParticleType}s share the same {@link ParticleEffect} implementation.
*
* @param alwaysSpawn True to always spawn the particle regardless of distance.
* @param factory A factory for serializing string command parameters into a particle effect.
* @param codecGetter A function that, given the newly created type, returns the codec for serialization.
* @param packetCodecGetter A function that, given the newly created type, returns the packet codec for network serialization.
*/
public static <T extends ParticleEffect> ParticleType<T> complex(boolean alwaysSpawn, ParticleEffect.Factory<T> factory, final Function<ParticleType<T>, MapCodec<T>> codecGetter, final Function<ParticleType<T>, PacketCodec<? super RegistryByteBuf, T>> packetCodecGetter) {
return new ParticleType<T>(alwaysSpawn, factory) {
public static <T extends ParticleEffect> ParticleType<T> complex(boolean alwaysSpawn, final Function<ParticleType<T>, MapCodec<T>> codecGetter, final Function<ParticleType<T>, PacketCodec<? super RegistryByteBuf, T>> packetCodecGetter) {
return new ParticleType<>(alwaysSpawn) {
@Override
public MapCodec<T> getCodec() {
return codecGetter.apply(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onInitialize() {

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("addparticletestblocks").executes(context -> {
PlayerInventory inventory = context.getSource().getPlayer().getInventory();
PlayerInventory inventory = context.getSource().getPlayerOrThrow().getInventory();
inventory.offerOrDrop(new ItemStack(ALWAYS_TINTED));
inventory.offerOrDrop(new ItemStack(TINTED_OVER_WATER));
inventory.offerOrDrop(new ItemStack(NEVER_TINTED));
Expand Down

0 comments on commit b87e67c

Please sign in to comment.