forked from Sk1erLLC/Patcher
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: ev chang <[email protected]>
- Loading branch information
1 parent
b9aeeda
commit 2761175
Showing
6 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/club/sk1er/patcher/mixins/performance/ChunkMixin_TileEntityUnload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/club/sk1er/patcher/mixins/performance/WorldMixin_TileEntityUnload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters