Skip to content

Commit

Permalink
sided
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightenom committed Sep 29, 2024
1 parent f7cebc0 commit 9abc099
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/ldtteam/common/fakelevel/FakeLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public FakeLevel(final SOURCE levelSource,
realLevel.registryAccess(),
realLevel.dimensionTypeRegistration(),
realLevel.getProfilerSupplier(),
true,
realLevel.isClientSide(),
false,
0,
0);
Expand All @@ -162,6 +162,11 @@ public void setRealLevel(final Level realLevel)
return;
}

if (realLevel != null && realLevel.isClientSide != this.isClientSide)
{
throw new IllegalArgumentException("Received wrong sided realLevel - fakeLevel.isClientSide = " + this.isClientSide);
}

this.realLevel = realLevel;
((FakeLevelData) this.getLevelData()).vanillaLevelData = realLevel == null ? null : realLevel.getLevelData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,22 @@ public void describeSelfInCrashReport(final CrashReportCategory category)
() -> blockEntity == null ? null : BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(blockEntity.getType()).toString());
}
}

public static class SidedSingleBlockFakeLevel
{
private SingleBlockFakeLevel client;
private SingleBlockFakeLevel server;

public SingleBlockFakeLevel get(final Level realLevel)
{
if (realLevel.isClientSide())
{
return client != null ? client : (client = new SingleBlockFakeLevel(realLevel));
}
else
{
return server != null ? server : (server = new SingleBlockFakeLevel(realLevel));
}
}
}
}
22 changes: 8 additions & 14 deletions src/main/java/com/ldtteam/common/util/BlockToItemHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ldtteam.common.util;

import com.ldtteam.blockui.mod.item.BlockStateRenderingData;
import com.ldtteam.common.fakelevel.SingleBlockFakeLevel;
import com.ldtteam.common.fakelevel.SingleBlockFakeLevel.SidedSingleBlockFakeLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -30,7 +30,7 @@
public class BlockToItemHelper
{
public static final HitResult ZERO_POS_HIT_RESULT = new BlockHitResult(Vec3.atCenterOf(BlockPos.ZERO), Direction.NORTH, BlockPos.ZERO, true);
private static SingleBlockFakeLevel fakeLevel;
private static final SidedSingleBlockFakeLevel fakeLevel = new SidedSingleBlockFakeLevel();

/**
* Mostly for use in UI where you dont have level instance (eg. player selects block, from xml, but not when displaying real world
Expand All @@ -47,18 +47,12 @@ public static ItemStack getItemStack(final BlockState blockState, final BlockEnt
return ItemStack.EMPTY;
}

if (fakeLevel == null)
{
fakeLevel = new SingleBlockFakeLevel(player.level());
}
// client vs server concurrency - we dont care if create two instances, the other should just disappear
synchronized (fakeLevel)
{
return fakeLevel.useFakeLevelContext(blockState,
blockEntity,
player.level(),
level -> getItemStackUsingPlayerPick(level, BlockPos.ZERO, player, ZERO_POS_HIT_RESULT));
}

return fakeLevel.get(player.level()).useFakeLevelContext(blockState,
blockEntity,
player.level(),
level -> getItemStackUsingPlayerPick(level, BlockPos.ZERO, player, ZERO_POS_HIT_RESULT));
}

/**
Expand Down Expand Up @@ -95,7 +89,7 @@ public static ItemStack getItemStackUsingPlayerPick(final Level level, final Blo
}

final BlockState blockState = level.getBlockState(pos);
ItemStack result = blockState.getCloneItemStack(hitResult, fakeLevel, pos, player);
ItemStack result = blockState.getCloneItemStack(hitResult, level, pos, player);

if (result.isEmpty())
{
Expand Down

0 comments on commit 9abc099

Please sign in to comment.