Ported particles.
This commit is contained in:
@ -1,22 +1,32 @@
|
||||
package dev.micle.firefly_bush_backport.particle;
|
||||
|
||||
import dev.micle.firefly_bush_backport.config.Config;
|
||||
import net.minecraft.client.multiplayer.ClientLevel;
|
||||
import net.minecraft.client.particle.*;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.particles.SimpleParticleType;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.client.world.ClientWorld;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.particles.BasicParticleType;
|
||||
import net.minecraft.util.ReuseableStream;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.math.shapes.ISelectionContext;
|
||||
import net.minecraft.util.math.vector.Vector3d;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.awt.*;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class FireflyParticle extends TextureSheetParticle {
|
||||
protected FireflyParticle(ClientLevel clientLevel, double x, double y, double z, double xSpeed, double ySpeed, double zSpeed) {
|
||||
public class FireflyParticle extends SpriteTexturedParticle {
|
||||
private static final double MAXIMUM_COLLISION_VELOCITY_SQUARED = MathHelper.square(100.00F);
|
||||
private boolean stoppedByCollision;
|
||||
private final float friction;
|
||||
private final boolean speedUpWhenYMotionIsBlocked;
|
||||
|
||||
protected FireflyParticle(ClientWorld clientLevel, double x, double y, double z, double xSpeed, double ySpeed, double zSpeed) {
|
||||
super(clientLevel, x, y, z, xSpeed, ySpeed, zSpeed);
|
||||
this.speedUpWhenYMotionIsBlocked = true;
|
||||
this.friction = Config.Client.particleExtraFriction.get().floatValue();
|
||||
@ -27,8 +37,8 @@ public class FireflyParticle extends TextureSheetParticle {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ParticleRenderType getRenderType() {
|
||||
return ParticleRenderType.PARTICLE_SHEET_TRANSLUCENT;
|
||||
public IParticleRenderType getRenderType() {
|
||||
return IParticleRenderType.PARTICLE_SHEET_TRANSLUCENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -49,13 +59,34 @@ public class FireflyParticle extends TextureSheetParticle {
|
||||
}
|
||||
|
||||
private float getLifetimeProgress(float age) {
|
||||
return Mth.clamp(age / this.lifetime, 0.0F, 1.0F);
|
||||
return MathHelper.clamp(age / this.lifetime, 0.0F, 1.0F);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
super.tick();
|
||||
if (!this.level.getBlockState(BlockPos.containing(this.x, this.y, this.z)).isAir()) {
|
||||
this.xo = this.x;
|
||||
this.yo = this.y;
|
||||
this.zo = this.z;
|
||||
if (this.age++ >= this.lifetime) {
|
||||
this.remove();
|
||||
} else {
|
||||
this.yd -= 0.04D * (double) this.gravity;
|
||||
this.move(this.xd, this.yd, this.zd);
|
||||
if (this.speedUpWhenYMotionIsBlocked && this.y == this.yd) {
|
||||
this.xd *= 1.1D;
|
||||
this.zd *= 1.1D;
|
||||
}
|
||||
|
||||
this.xd *= this.friction;
|
||||
this.yd *= this.friction;
|
||||
this.zd *= this.friction;
|
||||
if (this.onGround) {
|
||||
this.xd *= 0.7F;
|
||||
this.zd *= 0.7F;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.level.getBlockState(new BlockPos(this.x, this.y, this.z)).isAir()) {
|
||||
this.remove();
|
||||
} else {
|
||||
this.setAlpha(getFadeAmount(this.getLifetimeProgress(this.age),
|
||||
@ -68,18 +99,57 @@ public class FireflyParticle extends TextureSheetParticle {
|
||||
}
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public static class FireflyProvider implements ParticleProvider<SimpleParticleType> {
|
||||
private final SpriteSet sprite;
|
||||
@Override
|
||||
public void move(double pX, double pY, double pZ) {
|
||||
if (!this.stoppedByCollision) {
|
||||
double d0 = pX;
|
||||
double d1 = pY;
|
||||
double d2 = pZ;
|
||||
if (this.hasPhysics && (pX != 0.0D || pY != 0.0D || pZ != 0.0D) && pX * pX + pY * pY + pZ * pZ < MAXIMUM_COLLISION_VELOCITY_SQUARED) {
|
||||
Vector3d vec3 = Entity.collideBoundingBoxHeuristically((Entity) null, new Vector3d(pX, pY, pZ), this.getBoundingBox(), this.level, ISelectionContext.empty(), new ReuseableStream<>(Stream.empty()));
|
||||
pX = vec3.x;
|
||||
pY = vec3.y;
|
||||
pZ = vec3.z;
|
||||
}
|
||||
|
||||
public FireflyProvider(SpriteSet spriteSet) {
|
||||
if (pX != 0.0D || pY != 0.0D || pZ != 0.0D) {
|
||||
this.setBoundingBox(this.getBoundingBox().move(pX, pY, pZ));
|
||||
this.setLocationFromBoundingbox();
|
||||
}
|
||||
|
||||
if (Math.abs(d1) >= (double) 1.0E-5F && Math.abs(pY) < (double) 1.0E-5F) {
|
||||
this.stoppedByCollision = true;
|
||||
}
|
||||
|
||||
this.onGround = d1 != pY && d1 < 0.00;
|
||||
if (d0 != pX) {
|
||||
this.xd = 0.0D;
|
||||
}
|
||||
|
||||
if (d2 != pZ) {
|
||||
this.zd = 0.0D;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setParticleSpeed(double xd, double yd, double zd) {
|
||||
this.xd = xd;
|
||||
this.yd = yd;
|
||||
this.zd = zd;
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public static class FireflyProvider implements IParticleFactory<BasicParticleType> {
|
||||
private final IAnimatedSprite sprite;
|
||||
|
||||
public FireflyProvider(IAnimatedSprite spriteSet) {
|
||||
this.sprite = spriteSet;
|
||||
}
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
public Particle createParticle(
|
||||
SimpleParticleType simpleParticleType,
|
||||
ClientLevel clientLevel,
|
||||
BasicParticleType simpleParticleType,
|
||||
ClientWorld clientLevel,
|
||||
double x,
|
||||
double y,
|
||||
double z,
|
||||
@ -96,8 +166,8 @@ public class FireflyParticle extends TextureSheetParticle {
|
||||
clientLevel.random.nextBoolean() ? ySpeed : -ySpeed,
|
||||
0.5 - clientLevel.random.nextDouble()
|
||||
);
|
||||
fireflyParticle.setLifetime(clientLevel.random.nextIntBetweenInclusive(
|
||||
Config.Client.particleMinLifetime.get(), Config.Client.particleMaxLifetime.get()
|
||||
fireflyParticle.setLifetime(nextIntBetweenInclusive(
|
||||
clientLevel.random, Config.Client.particleMinLifetime.get(), Config.Client.particleMaxLifetime.get()
|
||||
));
|
||||
fireflyParticle.scale(1.5F);
|
||||
fireflyParticle.pickSprite(this.sprite);
|
||||
@ -110,5 +180,9 @@ public class FireflyParticle extends TextureSheetParticle {
|
||||
|
||||
return fireflyParticle;
|
||||
}
|
||||
|
||||
private int nextIntBetweenInclusive(Random random, int low, int high) {
|
||||
return random.nextInt(high - low + 1) + low;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
package dev.micle.firefly_bush_backport.particle;
|
||||
|
||||
import dev.micle.firefly_bush_backport.FireflyBushBackport;
|
||||
import net.minecraft.core.particles.ParticleType;
|
||||
import net.minecraft.core.particles.SimpleParticleType;
|
||||
import net.minecraft.particles.BasicParticleType;
|
||||
import net.minecraft.particles.ParticleType;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
|
||||
public class ModParticles {
|
||||
public static final DeferredRegister<ParticleType<?>> PARTICLE_TYPES = DeferredRegister.create(ForgeRegistries.PARTICLE_TYPES, FireflyBushBackport.MOD_ID);
|
||||
|
||||
public static final RegistryObject<SimpleParticleType> FIREFLY = PARTICLE_TYPES.register("firefly",
|
||||
() -> new SimpleParticleType(false));
|
||||
public static final RegistryObject<BasicParticleType> FIREFLY = PARTICLE_TYPES.register("firefly",
|
||||
() -> new BasicParticleType(false));
|
||||
|
||||
public static void register(IEventBus modEventBus) {
|
||||
PARTICLE_TYPES.register(modEventBus);
|
||||
|
||||
@ -101,7 +101,7 @@ public class Proxy implements IProxy {
|
||||
private static void postSetup(FMLLoadCompleteEvent event) {}
|
||||
|
||||
private static void registerParticleFactories(ParticleFactoryRegisterEvent event) {
|
||||
event.registerSpriteSet(ModParticles.FIREFLY.get(), FireflyParticle.FireflyProvider::new);
|
||||
Minecraft.getInstance().particleEngine.register(ModParticles.FIREFLY.get(), FireflyParticle.FireflyProvider::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user