Skip to content

Commit

Permalink
Clear Thaumcraft particles list whenever a dimension is unloaded (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCodex6824 committed Aug 19, 2024
1 parent 370735d commit 187c428
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

package thecodex6824.thaumcraftfix.client;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Random;

import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
Expand All @@ -32,18 +36,21 @@
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.relauncher.Side;
import thaumcraft.api.items.ItemsTC;
import thaumcraft.client.fx.FXDispatcher;
import thaumcraft.client.fx.ParticleEngine;
import thaumcraft.client.lib.events.RenderEventHandler;
import thaumcraft.common.entities.monster.EntityFireBat;
import thaumcraft.common.entities.monster.EntityWisp;
import thaumcraft.common.items.ItemTCBase;
import thaumcraft.common.items.tools.ItemThaumometer;
import thecodex6824.thaumcraftfix.ThaumcraftFix;
import thecodex6824.thaumcraftfix.api.ThaumcraftFixApi;

@EventBusSubscriber(modid = ThaumcraftFixApi.MODID, value = Side.CLIENT)
Expand Down Expand Up @@ -103,6 +110,68 @@ public static void onClientTick(TickEvent.ClientTickEvent event) {
}
}

private static Field PARTICLES = null;
private static Field PARTICLES_DELAYED = null;
private static Field PARTICLE_DELAY_DIM = null;

private static int getDimension(Object particleDelay) {
try {
return PARTICLE_DELAY_DIM.getInt(particleDelay);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}

@SubscribeEvent
@SuppressWarnings("unchecked")
public static void onClientWorldUnload(WorldEvent.Unload event) {
int unloadedDim = event.getWorld().provider.getDimension();
try {
if (PARTICLES == null || PARTICLES_DELAYED == null || PARTICLE_DELAY_DIM == null) {
PARTICLES = ParticleEngine.class.getDeclaredField("particles");
PARTICLES.setAccessible(true);

PARTICLES_DELAYED = ParticleEngine.class.getDeclaredField("particlesDelayed");
PARTICLES_DELAYED.setAccessible(true);

// ParticleDelay is not public so we can't name it
Class<?> particleDelay = null;
for (Class<?> c : ParticleEngine.class.getDeclaredClasses()) {
if (c.getSimpleName().equals("ParticleDelay")) {
particleDelay = c;
break;
}
}

if (particleDelay == null) {
throw new RuntimeException("Can't find ParticleEngine$ParticleDelay");
}

PARTICLE_DELAY_DIM = particleDelay.getDeclaredField("dim");
PARTICLE_DELAY_DIM.setAccessible(true);
}

Map<Integer, ? extends List<Particle>>[] particleMaps = (Map<Integer, ? extends List<Particle>>[]) PARTICLES.get(null);
for (int layer = 0; layer < particleMaps.length; ++layer) {
List<Particle> list = particleMaps[layer].get(unloadedDim);
if (list != null) {
for (Particle p : list) {
p.setExpired();
}

list.clear();
}
}

List<?> delayed = (List<?>) PARTICLES_DELAYED.get(null);
delayed.removeIf(p -> getDimension(p) == unloadedDim);
}
catch (Exception ex) {
ThaumcraftFix.instance.getLogger().error("Failed to clear Thaumcraft particle lists", ex);
}
}

// a size of 1 is already done in the EntityWisp class
private static final float[] BURST_SIZES = new float[] { 4.5F, 10.0F };

Expand Down

0 comments on commit 187c428

Please sign in to comment.