From 9abc0995b504e5194db787041f981024ba4350aa Mon Sep 17 00:00:00 2001 From: Nightenom <17338378+Nightenom@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:34:42 +0200 Subject: [PATCH] sided --- .../ldtteam/common/fakelevel/FakeLevel.java | 7 +++++- .../fakelevel/SingleBlockFakeLevel.java | 18 +++++++++++++++ .../common/util/BlockToItemHelper.java | 22 +++++++------------ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/ldtteam/common/fakelevel/FakeLevel.java b/src/main/java/com/ldtteam/common/fakelevel/FakeLevel.java index f49565e..d0a89a9 100644 --- a/src/main/java/com/ldtteam/common/fakelevel/FakeLevel.java +++ b/src/main/java/com/ldtteam/common/fakelevel/FakeLevel.java @@ -135,7 +135,7 @@ public FakeLevel(final SOURCE levelSource, realLevel.registryAccess(), realLevel.dimensionTypeRegistration(), realLevel.getProfilerSupplier(), - true, + realLevel.isClientSide(), false, 0, 0); @@ -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(); } diff --git a/src/main/java/com/ldtteam/common/fakelevel/SingleBlockFakeLevel.java b/src/main/java/com/ldtteam/common/fakelevel/SingleBlockFakeLevel.java index cd837ba..87b5e3b 100644 --- a/src/main/java/com/ldtteam/common/fakelevel/SingleBlockFakeLevel.java +++ b/src/main/java/com/ldtteam/common/fakelevel/SingleBlockFakeLevel.java @@ -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)); + } + } + } } diff --git a/src/main/java/com/ldtteam/common/util/BlockToItemHelper.java b/src/main/java/com/ldtteam/common/util/BlockToItemHelper.java index 0ef779b..dc85834 100644 --- a/src/main/java/com/ldtteam/common/util/BlockToItemHelper.java +++ b/src/main/java/com/ldtteam/common/util/BlockToItemHelper.java @@ -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; @@ -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 @@ -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)); } /** @@ -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()) {