Don't crash when block entities are placed

- Block entities got a huge refactor in 1.17, a lot of the old code needs to be rethought
This commit is contained in:
Jozufozu 2021-09-15 14:28:44 -07:00
parent d67362cacd
commit 9976de110d
3 changed files with 10 additions and 36 deletions

View file

@ -1,7 +1,5 @@
package com.jozufozu.flywheel.backend.instancing; package com.jozufozu.flywheel.backend.instancing;
import javax.annotation.Nonnull;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent;
@ -42,13 +40,11 @@ public class InstancedRenderDispatcher {
getEntities(entity.level).queueUpdate(entity); getEntities(entity.level).queueUpdate(entity);
} }
@Nonnull
public static InstanceManager<BlockEntity> getTiles(LevelAccessor world) { public static InstanceManager<BlockEntity> getTiles(LevelAccessor world) {
return instanceWorlds.get(world) return instanceWorlds.get(world)
.getTileEntityInstanceManager(); .getTileEntityInstanceManager();
} }
@Nonnull
public static InstanceManager<Entity> getEntities(LevelAccessor world) { public static InstanceManager<Entity> getEntities(LevelAccessor world) {
return instanceWorlds.get(world) return instanceWorlds.get(world)
.getEntityInstanceManager(); .getEntityInstanceManager();

View file

@ -20,7 +20,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
@Mixin(LevelRenderer.class) @Mixin(LevelRenderer.class)
public class FixFabulousDepthMixin { public class FixFabulousDepthMixin {
@Inject(method = "renderLevel", at = @At(value = "INVOKE", ordinal = 1, target = "Lnet/minecraft/client/shader/ShaderGroup;process(F)V")) @Inject(method = "renderLevel", at = @At(value = "INVOKE", ordinal = 1, target = "Lnet/minecraft/client/renderer/PostChain;process(F)V"))
private void disableTransparencyShaderDepth(PoseStack p_228426_1_, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera p_228426_6_, GameRenderer p_228426_7_, LightTexture p_228426_8_, Matrix4f p_228426_9_, CallbackInfo ci) { private void disableTransparencyShaderDepth(PoseStack p_228426_1_, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera p_228426_6_, GameRenderer p_228426_7_, LightTexture p_228426_8_, Matrix4f p_228426_9_, CallbackInfo ci) {
GlStateManager._depthMask(false); GlStateManager._depthMask(false);
} }

View file

@ -1,9 +1,5 @@
package com.jozufozu.flywheel.mixin; package com.jozufozu.flywheel.mixin;
import java.util.Set;
import net.minecraft.world.level.block.entity.TickingBlockEntity;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
@ -11,46 +7,28 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.jozufozu.flywheel.backend.instancing.InstanceManager;
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
@Mixin(value = Level.class, priority = 1100) // this and create.mixins.json have high priority to load after Performant @Mixin(value = LevelChunk.class, priority = 1100) // this and create.mixins.json have high priority to load after Performant
public class TileWorldHookMixin { public class TileWorldHookMixin {
final Level self = (Level) (Object) this;
@Shadow @Shadow
@Final @Final
public boolean isClientSide; Level level;
@Shadow @Inject(at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/level/block/entity/BlockEntity;clearRemoved()V"),
@Final method = "setBlockEntity")
protected Set<BlockEntity> f_46434_; // FIXME: is this correct? private void onAddTile(BlockEntity te, CallbackInfo ci) {
if (level.isClientSide) {
@Inject(at = @At("TAIL"), method = "addBlockEntityTicker(Lnet/minecraft/world/level/block/entity/TickingBlockEntity;)V") InstancedRenderDispatcher.getTiles(level)
private void onAddTile(TickingBlockEntity te, CallbackInfo ci) { .queueAdd(te);
if (isClientSide) {
InstancedRenderDispatcher.getTiles(self)
.queueAdd((BlockEntity) te);
}
}
/**
* Without this we don't unload instances when a chunk unloads.
*/
@Inject(at = @At(value = "INVOKE", target = "Ljava/util/Set;clear()V", ordinal = 0), method = "tickBlockEntities")
private void onChunkUnload(CallbackInfo ci) {
if (isClientSide) {
InstanceManager<BlockEntity> kineticRenderer = InstancedRenderDispatcher.getTiles(self);
for (BlockEntity tile : f_46434_) {
kineticRenderer.remove(tile);
}
} }
} }
} }