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
+24 -30

No files matched your search

+2
View File
@@ -20,6 +20,7 @@ import carpet.commands.TestCommand;
import carpet.commands.TickCommand;
import carpet.network.ServerNetworkHandler;
import carpet.helpers.HopperCounter;
import carpet.helpers.ParticleDisplay;
import carpet.helpers.TickSpeed;
import carpet.logging.LoggerRegistry;
import carpet.script.CarpetScriptServer;
@@ -194,6 +195,7 @@ public class CarpetServer // static for now - easier to handle all around the co
LoggerRegistry.stopLoggers();
HUDController.resetScarpetHUDs();
ParticleDisplay.resetCache();
extensions.forEach(e -> e.onServerClosed(server));
minecraft_server = null;
}
@@ -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,
@@ -55,7 +58,7 @@ public class ParticleDisplay
delta.lengthSqr() < lineLengthSq;
delta = delta.add(incvec.scale(player.level.random.nextFloat())))
{
((ServerLevel)player.level).sendParticles(
player.getLevel().sendParticles(
player,
mainParticle,
true,
@@ -1,6 +1,7 @@
package carpet.script.utils;
import carpet.CarpetSettings;
import carpet.helpers.ParticleDisplay;
import carpet.network.ServerNetworkHandler;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
@@ -23,16 +24,11 @@ import carpet.script.value.ValueConversions;
import com.google.common.collect.Sets;
import com.mojang.blaze3d.vertex.VertexFormat;
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.Direction;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CompoundTag;
@@ -70,14 +66,12 @@ import java.util.Random;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.Map.entry;
public class ShapeDispatcher
{
private static final Map<String, ParticleOptions> particleCache = new HashMap<>();
public static record ShapeWithConfig(ExpiringShape shape, Map<String, Value> config) {}
public static ShapeWithConfig fromFunctionArgs(
@@ -177,19 +171,14 @@ public class ShapeDispatcher
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
{
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);
}
particleCache.put(name, particle);
return particle;
}
public static Map<String, Value> parseParams(List<Value> items)
@@ -261,7 +250,7 @@ public class ShapeDispatcher
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("box", creator(Box::new));
put("sphere", creator(Sphere::new));
@@ -1200,7 +1189,7 @@ public class ShapeDispatcher
"GROUND",
"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)));
}