From 69095fecac34e699030209ed10dab62648cc43c4 Mon Sep 17 00:00:00 2001 From: WenXin2 Date: Thu, 5 Sep 2024 23:18:59 -0500 Subject: [PATCH] Add item and entity spawn from Question Block --- .../marioverse/blocks/QuestionBlock.java | 76 ++++++++++++++++--- .../blocks/entities/QuestionBlockEntity.java | 5 +- .../wenxin2/marioverse/mixin/PlayerMixin.java | 53 +++++++------ 3 files changed, 94 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java b/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java index 38d7e714..2750557a 100644 --- a/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java +++ b/src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java @@ -1,13 +1,21 @@ package com.wenxin2.marioverse.blocks; import com.wenxin2.marioverse.blocks.entities.QuestionBlockEntity; -import com.wenxin2.marioverse.init.BlockRegistry; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.ItemInteractionResult; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.MobSpawnType; +import net.minecraft.world.entity.decoration.ArmorStand; +import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +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; @@ -60,7 +68,7 @@ public BlockState updateShape(BlockState state, Direction direction, BlockState @Override protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) { - if (!world.isClientSide) { +// if (!world.isClientSide) { ItemStack heldItem = player.getItemInHand(hand); BlockEntity blockEntity = world.getBlockEntity(pos); @@ -68,27 +76,71 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev ItemStack blockStack = questionBlockEntity.getStackInSlot(); if (!heldItem.isEmpty() && (blockStack.isEmpty() || ItemStack.isSameItemSameComponents(heldItem, blockStack))) { world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.FALSE), 3); - questionBlockEntity.addItem(player, heldItem); + questionBlockEntity.addItem(heldItem); questionBlockEntity.setChanged(); if(!player.isCreative()) stack.shrink(heldItem.getCount()); return ItemInteractionResult.SUCCESS; } else if (player.isCreative() && heldItem.isEmpty()) { - world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); -// questionBlockEntity = (QuestionBlockEntity) world.getBlockEntity(pos); +// world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); +//// questionBlockEntity = (QuestionBlockEntity) world.getBlockEntity(pos); +// ItemStack droppedItem = questionBlockEntity.getItems().getStackInSlot(0); +// player.addItem(droppedItem.split(1)); +// questionBlockEntity.removeOneItem(); +// questionBlockEntity.setChanged(); +// +// if (!player.addItem(droppedItem)) { +// player.drop(droppedItem, false); +// } ItemStack droppedItem = questionBlockEntity.getItems().getStackInSlot(0); - player.addItem(droppedItem); - questionBlockEntity.removeOneItem(); - questionBlockEntity.setChanged(); - if (!player.addItem(droppedItem)) { - player.drop(droppedItem, false); + if (!droppedItem.isEmpty()) { + // Spawn the stored item/entity above the block + if (world.getBlockState(pos.above()).isAir() && !world.isClientSide) + spawnEntity(world, pos.above(), droppedItem, false); + else if (!world.isClientSide) spawnEntity(world, pos.below(), droppedItem, false); + + questionBlockEntity.removeOneItem(); + questionBlockEntity.setChanged(); + + world.playSound(null, pos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1.0F, 1.0F); + } + + if (droppedItem.isEmpty()) { + world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); } return ItemInteractionResult.SUCCESS; - } else return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; + } else return ItemInteractionResult.CONSUME; + } +// } + return ItemInteractionResult.CONSUME; + } + + public void spawnEntity(Level world, BlockPos pos, ItemStack stack, Boolean dropEntireStack) { + + // Check if the item is a spawn egg (for mobs) + if (stack.getItem() instanceof SpawnEggItem spawnEgg) { + EntityType entityType = spawnEgg.getType(stack); + if (world instanceof ServerLevel serverWorld) { // Check this + 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); } } - return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; + // Check if the item is armor stand + else if (stack.getItem() == Items.ARMOR_STAND) { + ArmorStand armorStand = new ArmorStand(EntityType.ARMOR_STAND, world); + armorStand.setPos(pos.getX() + 0.5D, pos.getY(), pos.getZ() + 0.5D); + world.addFreshEntity(armorStand); + } + else if (dropEntireStack) { + ItemEntity itemEntity = new ItemEntity(world, pos.getX() + 0.5D, pos.getY(), pos.getZ() + 0.5D, stack); + world.addFreshEntity(itemEntity); + } + else { + ItemEntity itemEntity = new ItemEntity(world, pos.getX() + 0.5D, pos.getY(), pos.getZ() + 0.5D, stack.split(1)); + world.addFreshEntity(itemEntity); + } } } 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 16fc166f..fc976da6 100644 --- a/src/main/java/com/wenxin2/marioverse/blocks/entities/QuestionBlockEntity.java +++ b/src/main/java/com/wenxin2/marioverse/blocks/entities/QuestionBlockEntity.java @@ -4,7 +4,6 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; @@ -43,7 +42,7 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) { items.deserializeNBT(provider, tag.getCompound(INVENTORY)); } - public void addItem(Player player, ItemStack stack) { + public void addItem(ItemStack stack) { ItemStack existingStack = items.getStackInSlot(0); if (existingStack.isEmpty()) { items.setStackInSlot(0, stack.split(stack.getMaxStackSize())); @@ -57,7 +56,7 @@ public boolean removeOneItem() { ItemStack storedStack = items.getStackInSlot(0); if (!storedStack.isEmpty() && storedStack.getCount() > 0) { storedStack.shrink(1); // Remove one item - items.setStackInSlot(0, storedStack); +// items.setStackInSlot(0, storedStack); return true; } return false; diff --git a/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java b/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java index d742201d..cb6b9af0 100644 --- a/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java +++ b/src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java @@ -16,6 +16,7 @@ import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.RandomSource; +import net.minecraft.world.ItemInteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.item.ItemEntity; @@ -92,31 +93,33 @@ public void baseTick() { if (world.getBlockEntity(posAboveEntity) instanceof QuestionBlockEntity questionBlockEntity && !stateAboveEntity.getValue(QuestionBlock.EMPTY) && this.getDeltaMovement().y > 0) - { - if (!world.isClientSide) { - boolean removedItem = questionBlockEntity.removeOneItem(); - world.playSound(null, posAboveEntity, SoundEvents.CHISELED_BOOKSHELF_PICKUP, SoundSource.BLOCKS, 1.0F, 1.0F); - world.setBlock(pos.above(Math.round(this.getBbHeight())), stateAboveEntity.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); - -// if (removedItem) { - -// questionBlockEntity = (QuestionBlockEntity) world.getBlockEntity(pos); - ItemStack droppedItem = questionBlockEntity.getItems().getStackInSlot(0); - - if (!stateAboveEntityAndBlock.isSolid()) { - marioverse$dropItem(world, posAboveEntityAndBlock, droppedItem); - questionBlockEntity.removeOneItem(); - questionBlockEntity.setChanged(); - world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); - } else { - marioverse$dropItem(world, posAboveEntityAndBelowBlock, droppedItem); - questionBlockEntity.removeOneItem(); - questionBlockEntity.setChanged(); - world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); - } - world.playSound(null, pos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1.0F, 1.0F); -// } - world.gameEvent(this, GameEvent.BLOCK_CHANGE, posAboveEntity); + this.marioverse$hitQuestionBlock(world, state, posAboveEntity, questionBlockEntity); + } + + @Unique + private void marioverse$hitQuestionBlock(Level world, BlockState state, BlockPos pos, QuestionBlockEntity questionBlockEntity) { + BlockPos posAboveEntity = pos.above(Math.round(this.getBbHeight())); + BlockState stateAboveEntity = world.getBlockState(posAboveEntity); + + if (!world.isClientSide && world.getBlockState(pos).getBlock() instanceof QuestionBlock questionBlock) { + boolean removedItem = questionBlockEntity.removeOneItem(); + world.playSound(null, pos, SoundEvents.CHISELED_BOOKSHELF_PICKUP, SoundSource.BLOCKS, 1.0F, 1.0F); +// world.setBlock(pos.above(Math.round(this.getBbHeight())), stateAboveEntity.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); + ItemStack storedItem = questionBlockEntity.getItems().getStackInSlot(0); + if (!storedItem.isEmpty()) { + if (world.getBlockState(pos.above()).isAir()) + questionBlock.spawnEntity(world, pos.above(), storedItem, false); + else questionBlock.spawnEntity(world, pos.below(), storedItem, false); + + questionBlockEntity.removeOneItem(); + questionBlockEntity.setChanged(); + world.playSound(null, pos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1.0F, 1.0F); + } + + if (storedItem.isEmpty() && world.getBlockState(pos).getBlock() instanceof QuestionBlock) { + BlockState currentState = world.getBlockState(pos); + if (currentState.getBlock() instanceof QuestionBlock) + world.setBlock(pos, currentState.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3); } } }