Unicopia/src/main/java/com/minelittlepony/unicopia/particle/MagicParticleEffect.java

87 lines
2.6 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.particle;
2020-04-22 16:28:20 +02:00
import java.util.Locale;
import java.util.Random;
2020-04-22 16:28:20 +02:00
2020-06-26 11:44:47 +02:00
import com.minelittlepony.common.util.Color;
2020-04-22 16:28:20 +02:00
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
2021-12-22 12:10:45 +01:00
import net.minecraft.particle.AbstractDustParticleEffect;
2020-04-22 16:28:20 +02:00
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleType;
2020-06-26 11:44:47 +02:00
import net.minecraft.network.PacketByteBuf;
2021-12-22 12:10:45 +01:00
import net.minecraft.util.math.Vec3f;
2020-04-22 16:28:20 +02:00
import net.minecraft.util.registry.Registry;
public class MagicParticleEffect implements ParticleEffect {
2021-12-22 12:10:45 +01:00
public static final MagicParticleEffect UNICORN = new MagicParticleEffect(false, Vec3f.ZERO);
2021-12-23 23:38:02 +01:00
@SuppressWarnings("deprecation")
2021-12-22 12:10:45 +01:00
public static final ParticleEffect.Factory<MagicParticleEffect> FACTORY = ParticleFactoryHelper.of(MagicParticleEffect::new, MagicParticleEffect::new);
2020-04-22 16:28:20 +02:00
private final boolean tinted;
2021-12-22 12:10:45 +01:00
private final Vec3f color;
2020-04-22 16:28:20 +02:00
2021-12-22 12:10:45 +01:00
protected MagicParticleEffect(ParticleType<MagicParticleEffect> particleType, StringReader reader) throws CommandSyntaxException {
this(ParticleFactoryHelper.readBoolean(reader), AbstractDustParticleEffect.readColor(reader));
2020-04-22 16:28:20 +02:00
}
2021-12-22 12:10:45 +01:00
protected MagicParticleEffect(ParticleType<MagicParticleEffect> particleType, PacketByteBuf buf) {
this(buf.readBoolean(), AbstractDustParticleEffect.readColor(buf));
2020-04-22 16:28:20 +02:00
}
2021-12-22 12:10:45 +01:00
public MagicParticleEffect(int tint) {
this(true, new Vec3f(Color.r(tint), Color.g(tint), Color.b(tint)));
2020-04-22 16:28:20 +02:00
}
2021-12-22 12:10:45 +01:00
public MagicParticleEffect(Vec3f color) {
this(true, color);
2020-04-22 16:28:20 +02:00
}
2021-12-22 12:10:45 +01:00
protected MagicParticleEffect(boolean tint, Vec3f color) {
tinted = tint;
this.color = color;
2020-04-22 16:28:20 +02:00
}
2021-12-22 12:10:45 +01:00
public boolean hasTint() {
return tinted;
2020-04-22 16:28:20 +02:00
}
public Vec3f getColor(Random random) {
if (hasTint()) {
return color;
}
float r = random.nextBoolean() ? 0.9F : 1;
float g = 0.3F;
float b = random.nextBoolean() ? 0.4F : 1;
if (random.nextBoolean()) {
g *= 2F;
} else if (random.nextBoolean()) {
r *= 3.9F;
}
return new Vec3f(r, g, b);
2020-04-22 16:28:20 +02:00
}
@Override
public ParticleType<?> getType() {
return UParticles.UNICORN_MAGIC;
}
@Override
public void write(PacketByteBuf buf) {
buf.writeBoolean(tinted);
2021-12-22 12:10:45 +01:00
buf.writeFloat(color.getX());
buf.writeFloat(color.getY());
buf.writeFloat(color.getZ());
2020-04-22 16:28:20 +02:00
}
@Override
public String asString() {
2021-12-22 12:10:45 +01:00
return String.format(Locale.ROOT, "%s %.2f %.2f %.2f", Registry.PARTICLE_TYPE.getId(getType()), color.getX(), color.getY(), color.getZ());
2020-04-22 16:28:20 +02:00
}
}