Skip to content

Commit

Permalink
Add item and entity spawn from Question Block
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Sep 6, 2024
1 parent 8ed92e8 commit 69095fe
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 40 deletions.
76 changes: 64 additions & 12 deletions src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,35 +68,79 @@ 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);

if (blockEntity instanceof QuestionBlockEntity questionBlockEntity) {
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand All @@ -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;
Expand Down
53 changes: 28 additions & 25 deletions src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 69095fe

Please sign in to comment.