Skip to content

Commit

Permalink
fix loot table getting loaded without player opening chest
Browse files Browse the repository at this point in the history
closes #26
  • Loading branch information
deirn committed Dec 14, 2021
1 parent 78474aa commit 0214829
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/vanilla/java/badasintended/megane/impl/Minecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.minecraft.text.TranslatableText;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class Minecraft implements MeganeModule {

Expand Down Expand Up @@ -83,27 +84,28 @@ public void register(MeganeRegistrar registrar) {
))

.inventory(ChestBlockEntity.class, new InventoryProvider<>() {
@Nullable
Inventory inventory;

@Override
public boolean hasInventory(ChestBlockEntity chestBlockEntity) {
BlockState state = chestBlockEntity.getCachedState();
if (state.getBlock() instanceof ChestBlock block) {
inventory = ChestBlock.getInventory(block, state, chestBlockEntity.getWorld(), chestBlockEntity.getPos(), true);
return inventory != null;
} else {
return false;
if (((AccessorLootableContainerBlockEntity) chestBlockEntity).getLootTableId() == null) {
BlockState state = chestBlockEntity.getCachedState();
if (state.getBlock() instanceof ChestBlock block) {
inventory = ChestBlock.getInventory(block, state, chestBlockEntity.getWorld(), chestBlockEntity.getPos(), true);
}
}
return true;
}

@Override
public int size(ChestBlockEntity chestBlockEntity) {
return inventory.size();
return inventory != null ? inventory.size() : 0;
}

@Override
public @NotNull ItemStack getStack(ChestBlockEntity chestBlockEntity, int slot) {
return inventory.getStack(slot);
return inventory != null ? inventory.getStack(slot) : ItemStack.EMPTY;
}
})

Expand Down

0 comments on commit 0214829

Please sign in to comment.