diff --git a/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java b/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java index bc2d31c9..6b678d26 100644 --- a/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java +++ b/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java @@ -5,7 +5,6 @@ import com.wenxin2.marioverse.init.TagRegistry; import com.wenxin2.marioverse.init.SoundRegistry; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundSource; import net.minecraft.world.Containers; @@ -24,7 +23,6 @@ import net.minecraft.world.item.SpawnEggItem; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.Level; -import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.EntityBlock; import net.minecraft.world.level.block.entity.BlockEntity; @@ -120,14 +118,15 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev if (blockEntity instanceof QuestionBlockEntity questionBlockEntity && !heldItem.is(TagRegistry.QUESTION_BLOCK_ITEM_BLACKLIST)) { ItemStack blockStack = questionBlockEntity.getStackInSlot(); + if (questionBlockEntity.getLootTable() != null) this.unpackLootTable(player, questionBlockEntity); if (!heldItem.isEmpty() && questionBlockEntity.getLootTable() == null && (ConfigRegistry.QUESTION_ADD_ITEMS.get() || player.isCreative()) && (!questionBlockEntity.hasItems() || ItemStack.isSameItemSameComponents(heldItem, blockStack))) { - world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.FALSE), 3); questionBlockEntity.addItem(heldItem); + world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.FALSE), 3); questionBlockEntity.setChanged(); if(!player.isCreative()) stack.shrink(heldItem.getCount()); @@ -166,7 +165,7 @@ public void spawnEntity(Level world, BlockPos pos, ItemStack stack) { if (world instanceof ServerLevel serverWorld && !entityType.is(TagRegistry.QUESTION_BLOCK_ENTITY_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(Math.round(entityType.getHeight())), MobSpawnType.SPAWN_EGG, true, true); + else entityType.spawn(serverWorld, stack, null, pos.below((int) Math.max(1, 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)); @@ -208,10 +207,12 @@ public void playCoinSound(Level world, BlockPos pos) { } public void unpackLootTable(Entity entity, QuestionBlockEntity questionBlockEntity) { + ItemStack storedItem = questionBlockEntity.getItems().getFirst(); + if (entity instanceof Player player) { questionBlockEntity.unpackLootTable(player); - questionBlockEntity.processLootTable(); - questionBlockEntity.setChanged(); } + questionBlockEntity.processLootTable(); + questionBlockEntity.setChanged(); } } diff --git a/src/main/java/com/wenxin2/marioverse/blocks/entities/QuestionBlockEntity.java b/src/main/java/com/wenxin2/marioverse/blocks/entities/QuestionBlockEntity.java index 8b530172..b188a0b2 100644 --- a/src/main/java/com/wenxin2/marioverse/blocks/entities/QuestionBlockEntity.java +++ b/src/main/java/com/wenxin2/marioverse/blocks/entities/QuestionBlockEntity.java @@ -11,8 +11,6 @@ import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity; import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; @@ -60,17 +58,18 @@ protected Component getDefaultName() { return Component.translatable("menu.marioverse.question_block"); } - public static void serverTick(Level p_155014_, BlockPos p_155015_, BlockState p_155016_) { - - } - @Override public void setChanged() { if (this.level != null && this.level.getBlockState(this.getBlockPos()).getBlock() instanceof QuestionBlock) { - if (this.getLootTable() != null || !this.items.getFirst().isEmpty()) + if (this.getLootTable() != null || this.hasItems()) this.level.setBlock(this.getBlockPos(), this.getBlockState().setValue(QuestionBlock.EMPTY, Boolean.FALSE), 3); else this.level.setBlock(this.getBlockPos(), this.getBlockState().setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); + + if (!this.level.isClientSide()) { + this.level.sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3); + this.level.updateNeighborsAt(this.getBlockPos(), this.getBlockState().getBlock()); + } } super.setChanged(); } @@ -104,13 +103,18 @@ public void addItem(ItemStack stack) { int countToAdd = Math.min(stack.getMaxStackSize() - existingStack.getCount(), stack.getCount()); existingStack.grow(countToAdd); } + this.setChanged(); } public boolean removeItems() { ItemStack storedStack = items.getFirst(); if (!storedStack.isEmpty() && storedStack.getCount() > 0) { - items.set(0, storedStack); storedStack.shrink(1); // Remove one item + if (storedStack.isEmpty()) { + items.set(0, ItemStack.EMPTY); + if (!this.hasItems() && this.hasLootTableBeenProcessed()) + this.clearLootTable(); + } this.setChanged(); return true; } @@ -124,6 +128,11 @@ public boolean hasLootTableBeenProcessed() { public void processLootTable() { lootTableProcessed = true; } + + public void clearLootTable() { + this.lootTable = null; + } + public boolean isLastPowered() { return lastPowered; } diff --git a/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java b/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java index b3a4c996..db535c9d 100644 --- a/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java +++ b/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java @@ -88,6 +88,8 @@ public void baseTick() { if (stateAboveEntity.is(TagRegistry.BONKABLE_BLOCKS) && this.getDeltaMovement().y > 0) if (stateAboveEntity.getValue(QuestionBlock.EMPTY)) world.playSound(null, pos, SoundRegistry.BLOCK_BONK.get(), SoundSource.BLOCKS, 1.0F, 1.0F); + else if (!(stateAboveEntity.getBlock() instanceof QuestionBlock)) + world.playSound(null, pos, SoundRegistry.BLOCK_BONK.get(), SoundSource.BLOCKS, 1.0F, 1.0F); } @Unique