Skip to content

Commit

Permalink
Update state if contains loot
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Sep 7, 2024
1 parent 3ed1141 commit 9418e51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
15 changes: 7 additions & 8 deletions src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
Expand Down Expand Up @@ -72,7 +71,7 @@ public BlockState updateShape(BlockState state, Direction direction, BlockState
LevelAccessor worldAccessor, BlockPos pos, BlockPos posNeighbor) {
QuestionBlockEntity questionBlockEntity = (QuestionBlockEntity) worldAccessor.getBlockEntity(pos);

if (questionBlockEntity != null && (questionBlockEntity.hasItems() || questionBlockEntity.hasLootTableBeenProcessed())) {
if (questionBlockEntity != null && (questionBlockEntity.getLootTable() != null || questionBlockEntity.hasItems())) {
return state.setValue(EMPTY, Boolean.FALSE);
}
else return state.setValue(EMPTY, Boolean.TRUE);
Expand All @@ -87,6 +86,7 @@ protected boolean hasAnalogOutputSignal(BlockState state) {
protected int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos) {
return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(world.getBlockEntity(pos));
}

@NotNull
@Override
protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level world, BlockPos pos,
Expand All @@ -96,6 +96,8 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev

if (blockEntity instanceof QuestionBlockEntity questionBlockEntity) {
ItemStack blockStack = questionBlockEntity.getStackInSlot();
if (questionBlockEntity.getLootTable() != null)
this.unpackLootTable(player, questionBlockEntity);

if (!heldItem.isEmpty() && (ConfigRegistry.QUESTION_ADD_ITEMS.get() || player.isCreative())
&& (blockStack.isEmpty() || ItemStack.isSameItemSameComponents(heldItem, blockStack))) {
Expand All @@ -109,8 +111,6 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev
&& !state.getValue(EMPTY)) {
ItemStack storedItem = questionBlockEntity.getItems().getFirst();

this.unpackLootTable(world, world.getBlockState(pos), pos, questionBlockEntity, player);

if (!storedItem.isEmpty()) {
if (!world.isClientSide)
this.spawnEntity(world, pos, storedItem);
Expand Down Expand Up @@ -140,7 +140,7 @@ public void spawnEntity(Level world, BlockPos pos, ItemStack stack) {
if (world instanceof ServerLevel serverWorld && !entityType.is(TagRegistry.QUESTION_BLOCK_BLACKLIST)) {
if (world.getBlockState(pos.above()).isAir())
entityType.spawn(serverWorld, stack, null, pos.above(2), MobSpawnType.SPAWN_EGG, true, true);
else entityType.spawn(serverWorld, stack, null, pos.below((int) -entityType.getHeight()), MobSpawnType.SPAWN_EGG, true, true);
else entityType.spawn(serverWorld, stack, null, pos.below(1).below((int) -entityType.getHeight()), MobSpawnType.SPAWN_EGG, true, true);
stack.copyWithCount(1);
} else if (world.getBlockState(pos.above()).isAir()) {
ItemEntity itemEntity = new ItemEntity(world, pos.getX() + 0.5D, pos.getY() + 1.0D, pos.getZ() + 0.5D, stack.copyWithCount(1));
Expand Down Expand Up @@ -173,10 +173,9 @@ public void playCoinSound(Level world, Entity entity, BlockPos pos) {
world.playSound(entity, pos, SoundRegistry.COIN_PICKUP.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
}

public void unpackLootTable(Level world, BlockState state, BlockPos pos, QuestionBlockEntity questionBlockEntity, Entity entity) {
if (!questionBlockEntity.hasLootTableBeenProcessed() && entity instanceof Player player) {
public void unpackLootTable(Entity entity, QuestionBlockEntity questionBlockEntity) {
if (entity instanceof Player player) {
questionBlockEntity.unpackLootTable(player);
world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.FALSE), 3);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wenxin2.marioverse.blocks.entities;

import com.wenxin2.marioverse.blocks.QuestionBlock;
import com.wenxin2.marioverse.init.BlockEntityRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
Expand All @@ -16,7 +17,6 @@

public class QuestionBlockEntity extends RandomizableContainerBlockEntity {
private NonNullList<ItemStack> items = NonNullList.withSize(1, ItemStack.EMPTY);
private boolean lootTableProcessed = false;

public QuestionBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntityRegistry.QUESTION_BLOCK_ENTITY.get(), pos, state);
Expand Down Expand Up @@ -55,8 +55,13 @@ protected Component getDefaultName() {
return Component.translatable("menu.marioverse.question_block");
}

public boolean hasLootTableBeenProcessed() {
return lootTableProcessed;
@Override
public void setChanged() {
if (this.level != null && this.level.getBlockState(this.getBlockPos()).getBlock() instanceof QuestionBlock
&& (this.getLootTable() != null || this.hasItems())) {
this.level.setBlock(this.getBlockPos(), this.getBlockState().setValue(QuestionBlock.EMPTY, Boolean.FALSE), 3);
}
super.setChanged();
}

@Override
Expand All @@ -65,7 +70,6 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
if (!this.trySaveLootTable(tag)) {
ContainerHelper.saveAllItems(tag, this.items, provider);
}
tag.putBoolean("LootTableProcessed", lootTableProcessed);
}

@Override
Expand All @@ -75,7 +79,6 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) {
if (!this.tryLoadLootTable(tag)) {
ContainerHelper.loadAllItems(tag, this.items, provider);
}
lootTableProcessed = tag.getBoolean("LootTableProcessed");
}

public void addItem(ItemStack stack) {
Expand All @@ -98,8 +101,4 @@ public boolean removeItems() {
}
return false;
}

public void unpackLootTable() {
lootTableProcessed = true;
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,18 @@ public void baseTick() {

if (world.getBlockState(pos).getBlock() instanceof QuestionBlock questionBlock) {

questionBlock.unpackLootTable(world, world.getBlockState(pos), pos, questionBlockEntity, this);
if (questionBlockEntity.getLootTable() != null)
questionBlock.unpackLootTable(this, questionBlockEntity);

ItemStack storedItem = questionBlockEntity.getItems().getFirst();
if (!storedItem.isEmpty() && !world.getBlockState(pos).getValue(QuestionBlock.EMPTY)) {
if (!world.isClientSide)
questionBlock.spawnEntity(world, pos, storedItem);

if (storedItem.getItem() instanceof BlockItem blockItem && blockItem.getBlock() instanceof CoinBlock)
questionBlock.playCoinSound(world, this, pos);
else questionBlock.playPowerUpSound(world, this, pos);

if (!world.isClientSide)
questionBlock.spawnEntity(world, pos, storedItem);

questionBlockEntity.removeItems();
questionBlockEntity.setChanged();
world.playSound(null, pos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1.0F, 1.0F);
Expand Down

0 comments on commit 9418e51

Please sign in to comment.