Skip to content

Commit

Permalink
fix MC-117075 (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: ev chang <[email protected]>
  • Loading branch information
MicrocontrollersDev and Wyvest authored Jul 16, 2024
1 parent b9aeeda commit 2761175
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ This work, "PolyPatcher", is adapted from ["Patcher"](https://sk1er.club/mods/pa
- Boost performance by reducing quad counts in item models
- Boost performance by decreasing size of sine and cosine lookup tables
- Boost performance by only rendering special tile entities once instead of twice per frame
- Boost performance by unloading tile entities quickly
- Improve speed when changing language, mipmap level, and anisotropic filtering level
- Fix Forge held item lighting to match vanilla
- Fix vanilla bug where entering an entity in spectator mode while in third person applies shaders
Expand Down
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ dependencies {
isTransitive = false
}

if (platform.mcMinor < 12) {
shade("it.unimi.dsi:fastutil:8.5.6")
}

modRuntimeOnly("me.djtheredstoner:DevAuth-${if (platform.isFabric) "fabric" else if (platform.isLegacyForge) "forge-legacy" else "forge-latest"}:1.2.0")

// If we are building for legacy forge, includes the launch wrapper with `shade` as we configured earlier.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/club/sk1er/patcher/ducks/WorldExt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package club.sk1er.patcher.ducks;

import net.minecraft.world.chunk.Chunk;

public interface WorldExt {
void patcher$markTileEntitiesInChunkForRemoval(Chunk chunk);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package club.sk1er.patcher.mixins.performance;

import club.sk1er.patcher.ducks.WorldExt;
import com.google.common.collect.Iterators;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Collection;
import java.util.Iterator;

@Mixin(Chunk.class)
public class ChunkMixin_TileEntityUnload {
//#if MC==10809
@Shadow
@Final
private World worldObj;

@Redirect(method = "onChunkUnload", at = @At(value = "INVOKE", target = "Ljava/util/Collection;iterator()Ljava/util/Iterator;", ordinal = 0))
private Iterator patcher$unloadTileEntity(Collection instance) {
((WorldExt) this.worldObj).patcher$markTileEntitiesInChunkForRemoval((Chunk)(Object)this);
return Iterators.emptyIterator();
}
//#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package club.sk1er.patcher.mixins.performance;

import club.sk1er.patcher.ducks.WorldExt;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

@Mixin(World.class)
public class WorldMixin_TileEntityUnload implements WorldExt {
//#if MC==10809
@Shadow
@Final
public List<TileEntity> loadedTileEntityList;
@Shadow
@Final
public List<TileEntity> tickableTileEntities;
@Unique
private LongOpenHashSet patcher$tileEntitiesChunkToBeRemoved = new LongOpenHashSet();

@Override
public void patcher$markTileEntitiesInChunkForRemoval(Chunk chunk) {
if (!chunk.getTileEntityMap().isEmpty()) {
long pos = ChunkCoordIntPair.chunkXZ2Int(chunk.xPosition, chunk.zPosition);
this.patcher$tileEntitiesChunkToBeRemoved.add(pos);
}
}

@Inject(method = "updateEntities", at = @At(value = "FIELD", target = "Lnet/minecraft/world/World;processingLoadedTiles:Z", opcode = Opcodes.PUTFIELD, ordinal = 1))
private void removeInUnloaded(CallbackInfo ci) {
if (!this.patcher$tileEntitiesChunkToBeRemoved.isEmpty()) {
java.util.function.Predicate<TileEntity> isInChunk = (tileEntity) -> {
long tileChunkPos = ChunkCoordIntPair.chunkXZ2Int(tileEntity.getPos().getX() >> 4, tileEntity.getPos().getZ() >> 4);
return this.patcher$tileEntitiesChunkToBeRemoved.contains(tileChunkPos);
};
java.util.function.Predicate<TileEntity> isInChunkDoUnload = (tileEntity) -> {
boolean inChunk = isInChunk.test(tileEntity);
if (inChunk)
{
tileEntity.onChunkUnload();
}
return inChunk;
};
this.tickableTileEntities.removeIf(isInChunk);
this.loadedTileEntityList.removeIf(isInChunkDoUnload);
this.patcher$tileEntitiesChunkToBeRemoved.clear();
}
}
//#endif
}
2 changes: 2 additions & 0 deletions src/main/resources/mixins.patcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
"performance.BlockPosMixin_ReduceAllocations",
"performance.BlockRedstoneTorchMixin_MemoryLeak",
"performance.ChunkMixin_Optimization",
"performance.ChunkMixin_TileEntityUnload",
"performance.EffectRendererMixin_ParticleCulling",
"performance.EnchantmentHelperMixin_MemoryLeak",
"performance.EntityMixin_Capability",
Expand Down Expand Up @@ -255,6 +256,7 @@
"performance.VisGraphMixin_LimitScan",
"performance.WorldClientMixin_AnimationTick",
"performance.WorldMixin_Optimization",
"performance.WorldMixin_TileEntityUnload",
"performance.forge.ASMDataTableMixin_ComputeParallel",
"performance.forge.FluidRegistryMixin_Optimization",
"performance.forge.FMLClientHandlerMixin_Optimization",
Expand Down

0 comments on commit 2761175

Please sign in to comment.