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

+2
View File
@@ -20,6 +20,7 @@ import carpet.commands.TestCommand;
import carpet.commands.TickCommand; import carpet.commands.TickCommand;
import carpet.network.ServerNetworkHandler; import carpet.network.ServerNetworkHandler;
import carpet.helpers.HopperCounter; import carpet.helpers.HopperCounter;
import carpet.helpers.ParticleDisplay;
import carpet.helpers.TickSpeed; import carpet.helpers.TickSpeed;
import carpet.logging.LoggerRegistry; import carpet.logging.LoggerRegistry;
import carpet.script.CarpetScriptServer; import carpet.script.CarpetScriptServer;
@@ -194,6 +195,7 @@ public class CarpetServer // static for now - easier to handle all around the co
LoggerRegistry.stopLoggers(); LoggerRegistry.stopLoggers();
HUDController.resetScarpetHUDs(); HUDController.resetScarpetHUDs();
ParticleDisplay.resetCache();
extensions.forEach(e -> e.onServerClosed(server)); extensions.forEach(e -> e.onServerClosed(server));
minecraft_server = null; minecraft_server = null;
} }
@@ -5,42 +5,45 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import net.minecraft.commands.arguments.ParticleArgument; import net.minecraft.commands.arguments.ParticleArgument;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.particles.ParticleOptions; import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
public class ParticleDisplay 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 try
{ {
return ParticleArgument.readParticle(new StringReader(name), level.holderLookup(Registries.PARTICLE_TYPE)); return ParticleArgument.readParticle(new StringReader(name), lookup);
} }
catch (CommandSyntaxException e) 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; if (name == null) return null;
ParticleOptions res = particleCache.get(name); return particleCache.computeIfAbsent(name, particle -> parseParticle(particle, lookup));
if (res != null) return res; }
particleCache.put(name, parseParticle(name, level));
return particleCache.get(name); public static void resetCache() {
particleCache.clear();
} }
public static void drawParticleLine(ServerPlayer player, Vec3 from, Vec3 to, String main, String accent, int count, double spread) public static void drawParticleLine(ServerPlayer player, Vec3 from, Vec3 to, String main, String accent, int count, double spread)
{ {
ParticleOptions accentParticle = getEffect(accent, player.getLevel()); HolderLookup<ParticleType<?>> lookup = player.getLevel().holderLookup(Registries.PARTICLE_TYPE);
ParticleOptions mainParticle = getEffect(main, player.getLevel()); ParticleOptions accentParticle = getEffect(accent, lookup);
ParticleOptions mainParticle = getEffect(main, lookup);
if (accentParticle != null) ((ServerLevel)player.level).sendParticles( if (accentParticle != null) player.getLevel().sendParticles(
player, player,
accentParticle, accentParticle,
true, true,
@@ -52,10 +55,10 @@ public class ParticleDisplay
Vec3 incvec = to.subtract(from).normalize();// multiply(50/sqrt(lineLengthSq)); Vec3 incvec = to.subtract(from).normalize();// multiply(50/sqrt(lineLengthSq));
for (Vec3 delta = new Vec3(0.0,0.0,0.0); 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()))) delta = delta.add(incvec.scale(player.level.random.nextFloat())))
{ {
((ServerLevel)player.level).sendParticles( player.getLevel().sendParticles(
player, player,
mainParticle, mainParticle,
true, true,
@@ -1,6 +1,7 @@
package carpet.script.utils; package carpet.script.utils;
import carpet.CarpetSettings; import carpet.CarpetSettings;
import carpet.helpers.ParticleDisplay;
import carpet.network.ServerNetworkHandler; import carpet.network.ServerNetworkHandler;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement; import carpet.script.exception.ThrowStatement;
@@ -23,16 +24,11 @@ import carpet.script.value.ValueConversions;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.mojang.blaze3d.vertex.VertexFormat; import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexFormat.Mode; import com.mojang.blaze3d.vertex.VertexFormat.Mode;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.arguments.ParticleArgument;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.core.particles.ParticleOptions; import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.ByteTag; import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
@@ -70,14 +66,12 @@ import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import static java.util.Map.entry; import static java.util.Map.entry;
public class ShapeDispatcher public class ShapeDispatcher
{ {
private static final Map<String, ParticleOptions> particleCache = new HashMap<>();
public static record ShapeWithConfig(ExpiringShape shape, Map<String, Value> config) {} public static record ShapeWithConfig(ExpiringShape shape, Map<String, Value> config) {}
public static ShapeWithConfig fromFunctionArgs( public static ShapeWithConfig fromFunctionArgs(
@@ -177,19 +171,14 @@ public class ShapeDispatcher
public static ParticleOptions getParticleData(String name, RegistryAccess regs) public static ParticleOptions getParticleData(String name, RegistryAccess regs)
{ {
ParticleOptions particle = particleCache.get(name); // [SCARY SHIT] cache should be cleared between worlds
if (particle != null)
return particle;
try try
{ {
particle = ParticleArgument.readParticle(new StringReader(name), regs.lookupOrThrow(Registries.PARTICLE_TYPE)); return ParticleDisplay.getEffect(name, regs.lookupOrThrow(Registries.PARTICLE_TYPE));
} }
catch (CommandSyntaxException e) catch (IllegalArgumentException e)
{ {
throw new ThrowStatement(name, Throwables.UNKNOWN_PARTICLE); throw new ThrowStatement(name, Throwables.UNKNOWN_PARTICLE);
} }
particleCache.put(name, particle);
return particle;
} }
public static Map<String, Value> parseParams(List<Value> items) public static Map<String, Value> parseParams(List<Value> items)
@@ -261,7 +250,7 @@ public class ShapeDispatcher
public abstract static class ExpiringShape public abstract static class ExpiringShape
{ {
public static final Map<String, BiFunction<Map<String, Value>, RegistryAccess, ExpiringShape>> shapeProviders = new HashMap<String, BiFunction<Map<String, Value>, RegistryAccess, ExpiringShape>>(){{ public static final Map<String, BiFunction<Map<String, Value>, RegistryAccess, ExpiringShape>> shapeProviders = new HashMap<>(){{
put("line", creator(Line::new)); put("line", creator(Line::new));
put("box", creator(Box::new)); put("box", creator(Box::new));
put("sphere", creator(Sphere::new)); put("sphere", creator(Sphere::new));
@@ -1200,7 +1189,7 @@ public class ShapeDispatcher
"GROUND", "GROUND",
"FIXED") "FIXED")
{ {
public Value validate(Map o, MinecraftServer s, Value v) public Value validate(Map<String, Value> o, MinecraftServer s, Value v)
{ {
return super.validate(o, s ,new StringValue(v.getString().toUpperCase(Locale.ROOT))); return super.validate(o, s ,new StringValue(v.getString().toUpperCase(Locale.ROOT)));
} }