Skip to content

Commit

Permalink
perf: improved piston performance
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Aug 26, 2023
1 parent e35444e commit 66e7df0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/github/zly2006/reden/access/PlayerData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class PlayerData(
.expand(0.1),
) { x -> x !is PlayerEntity && x !is TntEntity }
list.forEach { entity ->
if (lastThread == null) lastThread = Thread.currentThread()
else if (lastThread!!.id != Thread.currentThread().id) {
println("aaaaaaaaaaa")
throw IllegalStateException("Not thread safe")
}
entities.computeIfAbsent(entity.uuid) {
EntityEntry(entity.type, NbtCompound().apply(entity::writeNbt), entity.blockPos)
}
Expand Down Expand Up @@ -106,3 +111,5 @@ class PlayerData(
val pos: BlockPos
)
}

var lastThread: Thread? = null
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.github.zly2006.reden.access.UndoableAccess;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import com.github.zly2006.reden.utils.DebugKt;
import net.minecraft.block.BlockState;
import net.minecraft.block.PistonExtensionBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.PistonBlockEntity;
Expand All @@ -21,13 +21,22 @@ public class MixinMovingPiston {
*/
@Overwrite
@Nullable
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
public <T extends PistonBlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return (type == BlockEntityType.PISTON) ? (world1, pos, state1, be) -> {
if (be instanceof UndoableAccess access) {
UpdateMonitorHelper.INSTANCE.setRecording(UpdateMonitorHelper.INSTANCE.getUndoRecordsMap().get(access.getUndoId()));
boolean shouldTick = be.progress >= 1.0f;
if (shouldTick) {
if (be instanceof UndoableAccess access) {
DebugKt.debugLogger.invoke("Before piston block entity tick: " + pos.toShortString() + ", id" + access.getUndoId());
UpdateMonitorHelper.INSTANCE.setRecording(UpdateMonitorHelper.INSTANCE.getUndoRecordsMap().get(access.getUndoId()));
}
}
PistonBlockEntity.tick(world1, pos, state1, be);
if (shouldTick) {
if (be instanceof UndoableAccess access) {
DebugKt.debugLogger.invoke("After piston block entity tick: " + pos.toShortString() + ", id" + access.getUndoId());
UpdateMonitorHelper.INSTANCE.setRecording(null);
}
}
PistonBlockEntity.tick(world1, pos, state1, (PistonBlockEntity) be);
UpdateMonitorHelper.INSTANCE.setRecording(null);
} : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ private void afterRunSchedule(CallbackInfo ci) {
)
)
private <T> void onAddSchedule(OrderedTick<T> orderedTick, CallbackInfo ci) {
if (orderedTick.pos().equals(new BlockPos(5, -57, -4))) {
System.out.println("bbbbb");
}
UndoableAccess access = (UndoableAccess) orderedTick;
PlayerData.UndoRecord recording = UpdateMonitorHelper.INSTANCE.getRecording();
if (recording != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object UpdateMonitorHelper {

@JvmStatic
fun onUpdate(world: World, entry: ChainRestrictedNeighborUpdater.Entry) {
debugLogger("UpdateMonitorHelper.onUpdate")
//debugLogger("UpdateMonitorHelper.onUpdate")
listeners.forEach { (k, v) ->
k.invoke(world, entry)
if (v == LifeTime.ONCE) {
Expand All @@ -58,7 +58,7 @@ object UpdateMonitorHelper {

@JvmStatic
fun onChainFinish(world: World) {
debugLogger("UpdateMonitorHelper.finish")
//debugLogger("UpdateMonitorHelper.finish")
listeners.forEach { (k, v) ->
if (v == LifeTime.CHAIN) {
listeners.remove(k)
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/reden.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ accessible field net/minecraft/world/chunk/Chunk heightmaps Ljava/util/Map;
accessible field net/minecraft/command/EntitySelector PASSTHROUGH_FILTER Lnet/minecraft/util/TypeFilter;
accessible field net/minecraft/server/integrated/IntegratedServer localPlayerUuid Ljava/util/UUID;
accessible field net/minecraft/server/world/ServerWorld syncedBlockEventQueue Lit/unimi/dsi/fastutil/objects/ObjectLinkedOpenHashSet;
accessible field net/minecraft/world/tick/WorldTickScheduler tickableTicks Ljava/util/Queue;
accessible field net/minecraft/world/tick/WorldTickScheduler tickedTicks Ljava/util/List;
accessible field net/minecraft/world/tick/WorldTickScheduler copiedTickableTicksList Ljava/util/Set;
accessible field net/minecraft/world/tick/ChunkTickScheduler queuedTicks Ljava/util/Set;
accessible field net/minecraft/block/entity/PistonBlockEntity progress F
accessible field net/minecraft/block/entity/PistonBlockEntity lastProgress F

0 comments on commit 66e7df0

Please sign in to comment.