Merge particle caches (#1528)

* Merge particle caches

* Reset this cache on world reload
This commit is contained in:
altrisi authored and GitHub committed 2023-01-20 13:56:16 +01:00
1 parent 3050a82b96
commit 914e129f6e
3 files changed
+25 -31

No files matched your search

@@ -5,42 +5,45 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.commands.arguments.ParticleArgument;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.registries.Registries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec3;
public class ParticleDisplay
{
private static Map<String, ParticleOptions> particleCache = new HashMap<>();
private static final Map<String, ParticleOptions> particleCache = new HashMap<>(); // we reset this on reloads, but probably need something better
private static ParticleOptions parseParticle(String name, ServerLevel level) // [SCARY SHIT] persistent caches over server reloads
private static ParticleOptions parseParticle(String name, HolderLookup<ParticleType<?>> lookup)
{
try
{
return ParticleArgument.readParticle(new StringReader(name), level.holderLookup(Registries.PARTICLE_TYPE));
return ParticleArgument.readParticle(new StringReader(name), lookup);
}
catch (CommandSyntaxException e)
{
throw new RuntimeException("No such particle: "+name);
throw new IllegalArgumentException("No such particle: " + name);
}
}
public static ParticleOptions getEffect(String name, ServerLevel level)
public static ParticleOptions getEffect(String name, HolderLookup<ParticleType<?>> lookup)
{
if (name == null) return null;
ParticleOptions res = particleCache.get(name);
if (res != null) return res;
particleCache.put(name, parseParticle(name, level));
return particleCache.get(name);
return particleCache.computeIfAbsent(name, particle -> parseParticle(particle, lookup));
}
public static void resetCache() {
particleCache.clear();
}
public static void drawParticleLine(ServerPlayer player, Vec3 from, Vec3 to, String main, String accent, int count, double spread)
{
ParticleOptions accentParticle = getEffect(accent, player.getLevel());
ParticleOptions mainParticle = getEffect(main, player.getLevel());
HolderLookup<ParticleType<?>> lookup = player.getLevel().holderLookup(Registries.PARTICLE_TYPE);
ParticleOptions accentParticle = getEffect(accent, lookup);
ParticleOptions mainParticle = getEffect(main, lookup);
if (accentParticle != null) ((ServerLevel)player.level).sendParticles(
if (accentParticle != null) player.getLevel().sendParticles(
player,
accentParticle,
true,
@@ -52,10 +55,10 @@ public class ParticleDisplay
Vec3 incvec = to.subtract(from).normalize();// multiply(50/sqrt(lineLengthSq));
for (Vec3 delta = new Vec3(0.0,0.0,0.0);
delta.lengthSqr()<lineLengthSq;
delta.lengthSqr() < lineLengthSq;
delta = delta.add(incvec.scale(player.level.random.nextFloat())))
{
((ServerLevel)player.level).sendParticles(
player.getLevel().sendParticles(
player,
mainParticle,
true,