Skip to content

Commit 1cb2453

Browse files
committed
Try to guess the level when level is missing.
1 parent 48bee14 commit 1cb2453

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/main/java/noobanidus/mods/lootr/mixins/MixinRandomizableContainerBlockEntity.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package noobanidus.mods.lootr.mixins;
22

33
import net.minecraft.resources.ResourceLocation;
4+
import net.minecraft.server.MinecraftServer;
45
import net.minecraft.world.level.Level;
56
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
7+
import net.minecraftforge.server.ServerLifecycleHooks;
8+
import noobanidus.mods.lootr.Lootr;
9+
import noobanidus.mods.lootr.api.LootrAPI;
610
import noobanidus.mods.lootr.api.blockentity.ILootBlockEntity;
711
import noobanidus.mods.lootr.block.entities.TileTicker;
812
import noobanidus.mods.lootr.config.ConfigManager;
@@ -21,14 +25,23 @@ private void lootrOnSetLootTable(ResourceLocation p_59627_, long p_59628_, Callb
2125

2226
RandomizableContainerBlockEntity incoming = (RandomizableContainerBlockEntity) (Object) this;
2327

24-
if (!incoming.hasLevel() || incoming instanceof ILootBlockEntity) {
28+
if (incoming instanceof ILootBlockEntity) {
2529
return;
2630
}
2731

2832
Level level = incoming.getLevel();
2933

3034
if (level == null) {
31-
return; // This should be false because `hasLevel`
35+
// This is over-protective because most instances of `setLootTable` are in structure post-processing which means the block entity should have already been promoted. In testing, specifically with `/lootr chest`, the block entity's level is already set by the time `setLootTable` is called so this is overprotective.
36+
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
37+
if (server == null) {
38+
// We're probably on the client side so do nothing
39+
return;
40+
}
41+
42+
// Otherwise, try to guess that we're on the overworld. This isn't ideal but I assume the majority of the time, this will be the overworld.
43+
level = server.overworld();
44+
LootrAPI.LOG.error("Block entity at {} had its loot table set before its level was set. It's not possible to determine its dimension, so presuming the overworld.", incoming.getBlockPos());
3245
}
3346

3447
if (level.isClientSide()) {

0 commit comments

Comments
 (0)