Skip to content

Commit 82621d6

Browse files
committed
Fix potential leak, compile error
1 parent 3a06969 commit 82621d6

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/main/java/com/ldtteam/blockui/mod/ClientEventSubscriber.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import com.ldtteam.blockui.mod.container.ContainerHook;
1111
import com.ldtteam.blockui.util.resloc.OutOfJarResourceLocation;
1212
import com.ldtteam.blockui.views.BOWindow;
13+
import com.ldtteam.common.util.BlockToItemHelper;
1314
import com.mojang.blaze3d.platform.InputConstants;
1415
import net.minecraft.client.Minecraft;
1516
import net.minecraft.client.gui.screens.Screen;
1617
import net.minecraft.network.chat.Component;
1718
import net.minecraft.resources.ResourceLocation;
1819
import net.neoforged.bus.api.EventPriority;
1920
import net.neoforged.bus.api.SubscribeEvent;
21+
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
2022
import net.neoforged.neoforge.client.event.InputEvent.MouseScrollingEvent;
2123
import net.neoforged.neoforge.client.event.RenderGuiOverlayEvent;
2224
import net.neoforged.neoforge.client.gui.overlay.VanillaGuiOverlay;
@@ -143,4 +145,19 @@ public static void renderOverlay(final RenderGuiOverlayEvent.Pre event)
143145
event.setCanceled(true);
144146
}
145147
}
148+
149+
@SubscribeEvent
150+
public static void onClientPlayerLoggingOut(final ClientPlayerNetworkEvent.LoggingOut event)
151+
{
152+
BlockToItemHelper.releaseFakeLevelInstance();
153+
}
154+
155+
@SubscribeEvent
156+
public static void onClientPlayerDimChange(final ClientPlayerNetworkEvent.Clone event)
157+
{
158+
if (event.getNewPlayer().level() != event.getOldPlayer().level())
159+
{
160+
BlockToItemHelper.releaseFakeLevelInstance();
161+
}
162+
}
146163
}

src/main/java/com/ldtteam/common/fakelevel/FakeLevel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void setRealLevel(final Level realLevel)
145145
}
146146

147147
this.realLevel = realLevel;
148-
((FakeLevelData) this.getLevelData()).vanillaLevelData = realLevel.getLevelData();
148+
((FakeLevelData) this.getLevelData()).vanillaLevelData = realLevel == null ? null : realLevel.getLevelData();
149149
}
150150

151151
public Level realLevel()
@@ -760,15 +760,15 @@ public void updateSkyBrightness()
760760

761761
@Override
762762
@Nullable
763-
public <T, C> @Nullable T getCapability(BlockCapability<T, C> cap, BlockPos pos, C context)
763+
public <T, C> T getCapability(BlockCapability<T, C> cap, BlockPos pos, C context)
764764
{
765765
// Noop
766766
return null;
767767
}
768768

769769
@Override
770770
@Nullable
771-
public <T, C> @Nullable T getCapability(BlockCapability<T, C> cap,
771+
public <T, C> T getCapability(BlockCapability<T, C> cap,
772772
BlockPos pos,
773773
@Nullable BlockState state,
774774
@Nullable BlockEntity blockEntity,

src/main/java/com/ldtteam/common/util/BlockToItemHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public class BlockToItemHelper
3333
private static final HitResult ZERO_POS_HIT_RESULT = new BlockHitResult(Vec3.atCenterOf(BlockPos.ZERO), Direction.NORTH, BlockPos.ZERO, true);
3434
private static FakeLevel<SingleBlockFakeLevelGetter> fakeLevel;
3535

36+
public static void releaseFakeLevelInstance()
37+
{
38+
if (fakeLevel != null)
39+
{
40+
fakeLevel.setRealLevel(null);
41+
fakeLevel = null;
42+
}
43+
}
44+
3645
/**
3746
* Mostly for use in UI where you dont have level instance (eg. player selects block, from xml, but not when displaying real world
3847
* info - see {@link BlockStateRenderingData#of(Level, BlockPos, Player)}). NOT thread safe!

0 commit comments

Comments
 (0)