Skip to content

Commit

Permalink
fix Sodium compat.
Browse files Browse the repository at this point in the history
fix player == null (?)
fix unloading of placements not resetting the verifier.
  • Loading branch information
sakura-ryoko committed Feb 17, 2025
1 parent d841dd7 commit e011ef7
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ public Inventory getEntityInventory(World world, int entityId, boolean useNbt)
{
inv = (Inventory) entity;
}
else if (entity instanceof PlayerEntity player)
else if (entity instanceof PlayerEntity player && player != null)
{
inv = new SimpleInventory(player.getInventory().main.toArray(new ItemStack[36]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static List<MaterialListEntry> getMaterialList(
{
List<MaterialListEntry> list = new ArrayList<>();

if (countsTotal.isEmpty() == false)
if (player != null && !countsTotal.isEmpty())
{
MaterialCache cache = MaterialCache.getInstance();
Object2IntOpenHashMap<ItemType> itemTypesTotal = new Object2IntOpenHashMap<>();
Expand Down Expand Up @@ -128,6 +128,7 @@ private static void convertStatesToStacks(

public static void updateAvailableCounts(List<MaterialListEntry> list, PlayerEntity player)
{
if (player == null) return;
Object2IntOpenHashMap<ItemType> playerInvItems = getInventoryItemCounts(player.getInventory());

for (MaterialListEntry entry : list)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fi.dy.masa.litematica.mixin;

import net.minecraft.util.profiler.ProfilerSystem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ProfilerSystem.class)
public interface IMixinProfilerSystem
{
@Accessor("tickStarted")
boolean litematica_isStarted();
}
46 changes: 30 additions & 16 deletions src/main/java/fi/dy/masa/litematica/mixin/MixinWorldRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.util.profiler.Profilers;
import org.joml.Matrix4f;

import net.minecraft.client.MinecraftClient;
Expand All @@ -11,6 +10,8 @@
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.util.profiler.ProfilerSystem;
import net.minecraft.util.profiler.Profilers;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -19,7 +20,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import fi.dy.masa.litematica.Reference;
import fi.dy.masa.litematica.render.LitematicaRenderer;
import fi.dy.masa.litematica.util.SchematicWorldRefresher;

Expand All @@ -41,25 +41,31 @@ private void onLoadRenderers(CallbackInfo ci)
if (this.profiler == null)
{
this.profiler = Profilers.get();
}
if (this.profiler instanceof ProfilerSystem ps && !((IMixinProfilerSystem) ps).litematica_isStarted())
{
this.profiler.startTick();
}

this.profiler.push(Reference.MOD_ID+"_world_renderer");
LitematicaRenderer.getInstance().loadRenderers(this.profiler);
SchematicWorldRefresher.INSTANCE.updateAll();
this.profiler.pop();
}
}

@Inject(method = "setupTerrain", at = @At("TAIL"))
private void litematica_onPostSetupTerrain(
Camera camera, Frustum frustum, boolean hasForcedFrustum, boolean spectator, CallbackInfo ci,
@Local Profiler profiler)
Camera camera, Frustum frustum, boolean hasForcedFrustum, boolean spectator, CallbackInfo ci)
{
this.profiler = profiler;
this.profiler.push(Reference.MOD_ID+"_world_renderer");
if (this.profiler == null)
{
this.profiler = Profilers.get();
}
if (this.profiler instanceof ProfilerSystem ps && !((IMixinProfilerSystem) ps).litematica_isStarted())
{
this.profiler.startTick();
}

LitematicaRenderer.getInstance().piecewisePrepareAndUpdate(frustum, this.profiler);
this.profiler.pop();
}

@Inject(method = "render",
Expand All @@ -84,8 +90,10 @@ private void onRenderLayer(RenderLayer renderLayer, double x, double y, double z
{
this.profiler = Profilers.get();
}

this.profiler.push(Reference.MOD_ID+"_world_renderer");
if (this.profiler instanceof ProfilerSystem ps && !((IMixinProfilerSystem) ps).litematica_isStarted())
{
this.profiler.startTick();
}

if (renderLayer == RenderLayer.getSolid())
{
Expand All @@ -104,8 +112,11 @@ else if (renderLayer == RenderLayer.getTranslucent())
LitematicaRenderer.getInstance().piecewiseRenderTranslucent(viewMatrix, posMatrix, this.profiler);
LitematicaRenderer.getInstance().piecewiseRenderOverlay(viewMatrix, posMatrix, this.profiler);
}

this.profiler.pop();
/*
else if (renderLayer == RenderLayer.getTripwire())
{
}
*/
}

@Inject(method = "renderEntities",
Expand All @@ -120,9 +131,12 @@ private void onPostRenderEntities(MatrixStack matrices, VertexConsumerProvider.I
this.profiler = Profilers.get();
}

this.profiler.push(Reference.MOD_ID+"_world_renderer");
LitematicaRenderer.getInstance().piecewiseRenderEntities(this.posMatrix, this.ticks.getTickDelta(false), this.profiler);
this.profiler.pop();
if (this.profiler instanceof ProfilerSystem ps && !((IMixinProfilerSystem) ps).litematica_isStarted())
{
this.profiler.startTick();
}

LitematicaRenderer.getInstance().piecewiseRenderEntities(this.posMatrix, this.ticks.getTickProgress(false), this.profiler);
this.posMatrix = null;
this.ticks = null;
//this.profiler = null;
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/fi/dy/masa/litematica/render/LitematicaRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.client.render.RenderLayer;
import net.minecraft.util.profiler.Profiler;

import fi.dy.masa.litematica.Reference;
import fi.dy.masa.litematica.compat.iris.IrisCompat;
import fi.dy.masa.litematica.config.Configs;
import fi.dy.masa.litematica.config.Hotkeys;
Expand Down Expand Up @@ -274,13 +275,13 @@ public void piecewisePrepareAndUpdate(Frustum frustum, Profiler profiler)

if (this.renderPiecewiseSchematic)
{
profiler.push("culling");
profiler.push(Reference.MOD_ID+"_culling");
this.calculateFinishTime();

profiler.swap("terrain_setup");
profiler.swap(Reference.MOD_ID+"_terrain_setup");
worldRenderer.setupTerrain(this.getCamera(), frustum, this.frameCount++, this.mc.player.isSpectator(), profiler);

profiler.swap("update_chunks");
profiler.swap(Reference.MOD_ID+"_update_chunks");
worldRenderer.updateChunks(this.finishTimeNano, profiler);

profiler.pop();
Expand All @@ -294,7 +295,7 @@ public void piecewiseRenderSolid(Matrix4f viewMatrix, Matrix4f posMatrix, Profil
{
if (this.renderPiecewiseBlocks)
{
profiler.push("blocks_solid");
profiler.push(Reference.MOD_ID+"_solid");

if (this.renderCollidingSchematicBlocks)
{
Expand All @@ -319,7 +320,7 @@ public void piecewiseRenderCutoutMipped(Matrix4f viewMatrix, Matrix4f posMatrix,
{
if (this.renderPiecewiseBlocks)
{
profiler.push("blocks_cutout_mipped");
profiler.push(Reference.MOD_ID+"_cutout_mipped");

if (this.renderCollidingSchematicBlocks)
{
Expand All @@ -344,7 +345,7 @@ public void piecewiseRenderCutout(Matrix4f viewMatrix, Matrix4f posMatrix, Profi
{
if (this.renderPiecewiseBlocks)
{
profiler.push("blocks_cutout");
profiler.push(Reference.MOD_ID+"_cutout");

if (this.renderCollidingSchematicBlocks)
{
Expand All @@ -369,7 +370,7 @@ public void piecewiseRenderTranslucent(Matrix4f viewMatrix, Matrix4f posMatrix,
{
if (this.renderPiecewiseBlocks)
{
profiler.push("translucent");
profiler.push(Reference.MOD_ID+"_translucent");

if (this.renderCollidingSchematicBlocks)
{
Expand All @@ -394,21 +395,25 @@ public void piecewiseRenderOverlay(Matrix4f viewMatrix, Matrix4f posMatrix, Prof
{
if (this.renderPiecewiseSchematic)
{
profiler.push("schematic_overlay");
profiler.push(Reference.MOD_ID+"_schematic_overlay");

/*
Framebuffer fb = MinecraftClient.isFabulousGraphicsOrBetter() ? this.mc.worldRenderer.getTranslucentFramebuffer() : null;
if (fb != null)
{
fb.beginWrite(false);
}
*/

this.renderSchematicOverlay(viewMatrix, posMatrix, profiler);

/*
if (fb != null)
{
this.mc.getFramebuffer().beginWrite(false);
}
*/

profiler.pop();
}
Expand All @@ -420,7 +425,7 @@ public void piecewiseRenderEntities(Matrix4f posMatrix, float partialTicks, Prof
{
if (this.renderPiecewiseBlocks)
{
profiler.push("entities");
profiler.push(Reference.MOD_ID+"_entities");

this.getWorldRenderer().renderEntities(this.getCamera(), this.frustum, posMatrix, partialTicks, profiler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import net.minecraft.util.crash.CrashException;
import net.minecraft.util.crash.CrashReport;
import net.minecraft.util.crash.CrashReportSection;
import net.minecraft.util.math.random.Random;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.Direction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void scheduleTask(ITask task, int interval)

public void runTasks()
{
if (MinecraftClient.getInstance().player == null) return;
Profiler profiler = Profilers.get();

profiler.push(Reference.MOD_ID+"_run_tasks");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ public boolean removeSchematicPlacement(SchematicPlacement placement, boolean up
this.selectedPlacement = null;
}

if (placement.hasVerifier())
{
placement.getSchematicVerifier().reset();
}

boolean ret = this.schematicPlacements.remove(placement);
this.removeTouchedChunksFor(placement);

Expand Down Expand Up @@ -427,6 +432,11 @@ public void removeAllPlacementsOfSchematic(LitematicaSchematic schematic)

if (placement.getSchematic() == schematic)
{
if (placement.hasVerifier())
{
placement.getSchematicVerifier().reset();
}

removed |= this.removeSchematicPlacement(placement, false);
--i;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/fi/dy/masa/litematica/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ public static void setPickBlockableSlots(String configStr)

public static void setPickedItemToHand(ItemStack stack, MinecraftClient mc)
{
if (mc.player == null) return;
int slotNum = mc.player.getInventory().getSlotWithStack(stack);
setPickedItemToHand(slotNum, stack, mc);
}

public static void setPickedItemToHand(int sourceSlot, ItemStack stack, MinecraftClient mc)
{
if (mc.player == null) return;
PlayerEntity player = mc.player;
PlayerInventory inventory = player.getInventory();

Expand Down Expand Up @@ -199,7 +201,7 @@ private static boolean canPickToSlot(PlayerInventory inventory, int slotNum)

private static int getPickBlockTargetSlot(PlayerEntity player)
{
if (PICK_BLOCKABLE_SLOTS.isEmpty())
if (PICK_BLOCKABLE_SLOTS.isEmpty() || player == null)
{
return -1;
}
Expand Down Expand Up @@ -514,6 +516,7 @@ public static void preRestockHand(PlayerEntity player,
int threshold,
boolean allowHotbar)
{
if (player == null) return;
PlayerInventory container = player.getInventory();
final ItemStack handStack = player.getStackInHand(hand);
final int count = handStack.getCount();
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/mixins.litematica.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"MixinStairsBlock",
"MixinWorld",
"MixinWorldChunk",
"MixinWorldRenderer"
"MixinWorldRenderer",
"IMixinProfilerSystem"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit e011ef7

Please sign in to comment.