Skip to content

Commit

Permalink
Fix default placement when block has loot
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Sep 8, 2024
1 parent 62bec8d commit c1b265d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 61 deletions.
51 changes: 8 additions & 43 deletions src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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.Containers;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -66,39 +65,6 @@ public BlockState getStateForPlacement(BlockPlaceContext placeContext) {
return this.defaultBlockState().setValue(EMPTY, Boolean.TRUE);
}

@Override
public void onPlace(BlockState state, Level world, BlockPos pos, BlockState oldState, boolean isMoving) {
super.onPlace(state, world, pos, oldState, isMoving);
if (world.getBlockEntity(pos) instanceof QuestionBlockEntity questionBlockEntity) {
boolean hasUnprocessedLoot = questionBlockEntity.getLootTable() != null && !questionBlockEntity.hasLootTableBeenProcessed();
if (hasUnprocessedLoot || !questionBlockEntity.hasItems())
state.setValue(QuestionBlock.EMPTY, Boolean.FALSE);
else state.setValue(QuestionBlock.EMPTY, Boolean.TRUE);
}
}

@Override
public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block, BlockPos neighborPos, boolean moved) {
super.neighborChanged(state, world, pos, block, neighborPos, moved);
if (world.getBlockEntity(pos) instanceof QuestionBlockEntity questionBlockEntity) {
boolean hasUnprocessedLoot = questionBlockEntity.getLootTable() != null && !questionBlockEntity.hasLootTableBeenProcessed();
if (hasUnprocessedLoot || !questionBlockEntity.hasItems())
state.setValue(QuestionBlock.EMPTY, Boolean.FALSE);
else state.setValue(QuestionBlock.EMPTY, Boolean.TRUE);
}
}

@Override
public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState,
LevelAccessor worldAccessor, BlockPos pos, BlockPos posNeighbor) {
QuestionBlockEntity questionBlockEntity = (QuestionBlockEntity) worldAccessor.getBlockEntity(pos);

if (questionBlockEntity != null && (questionBlockEntity.getLootTable() != null || questionBlockEntity.hasItems())) {
return state.setValue(EMPTY, Boolean.FALSE);
}
else return state.setValue(EMPTY, Boolean.TRUE);
}

@Override
protected boolean hasAnalogOutputSignal(BlockState state) {
return true;
Expand All @@ -116,14 +82,13 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev
ItemStack heldItem = player.getItemInHand(hand);
BlockEntity blockEntity = world.getBlockEntity(pos);

if (blockEntity instanceof QuestionBlockEntity questionBlockEntity) {
if (blockEntity instanceof QuestionBlockEntity questionBlockEntity && !heldItem.is(TagRegistry.QUESTION_BLOCK_ITEM_BLACKLIST)) {
ItemStack blockStack = questionBlockEntity.getStackInSlot();
if (questionBlockEntity.getLootTable() != null) {
if (questionBlockEntity.getLootTable() != null)
this.unpackLootTable(player, questionBlockEntity);
world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3);
}

if (!heldItem.isEmpty() && (ConfigRegistry.QUESTION_ADD_ITEMS.get() || player.isCreative())
if (!heldItem.isEmpty() && questionBlockEntity.getLootTable() == null/* && !heldItem.is(TagRegistry.QUESTION_BLOCK_ITEM_BLACKLIST)*/
&& (ConfigRegistry.QUESTION_ADD_ITEMS.get() || player.isCreative())
&& (blockStack.isEmpty() || ItemStack.isSameItemSameComponents(heldItem, blockStack))) {
world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.FALSE), 3);
questionBlockEntity.addItem(heldItem);
Expand All @@ -132,7 +97,7 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev
stack.shrink(heldItem.getCount());
return ItemInteractionResult.SUCCESS;
} else if (heldItem.isEmpty() && (ConfigRegistry.QUESTION_REMOVE_ITEMS.get() || player.isCreative())
&& (!state.getValue(EMPTY) || !questionBlockEntity.hasLootTableBeenProcessed())) {
&& !state.getValue(EMPTY)) {
ItemStack storedItem = questionBlockEntity.getItems().getFirst();

if (!storedItem.isEmpty()) {
Expand All @@ -153,16 +118,16 @@ else if (storedItem.getItem() instanceof SpawnEggItem)
world.setBlock(pos, state.setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3);
}
return ItemInteractionResult.SUCCESS;
} else return ItemInteractionResult.CONSUME;
} else return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}
return ItemInteractionResult.CONSUME;
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}

public void spawnEntity(Level world, BlockPos pos, ItemStack stack) {
if (stack.getItem() instanceof SpawnEggItem spawnEgg && ConfigRegistry.QUESTION_SPAWNS_MOBS.get()) {
EntityType<?> entityType = spawnEgg.getType(stack);

if (world instanceof ServerLevel serverWorld && !entityType.is(TagRegistry.QUESTION_BLOCK_BLACKLIST)) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,16 @@ public static void serverTick(Level p_155014_, BlockPos p_155015_, BlockState p_

}

// @Override
// public void setChanged() {
// boolean hasUnprocessedLoot = this.getLootTable() != null && !this.hasLootTableBeenProcessed();
//
// if (this.level != null && this.level.getBlockState(this.getBlockPos()).getBlock() instanceof QuestionBlock) {
// if (hasUnprocessedLoot || !this.items.isEmpty())
// 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);
// }
// super.setChanged();
// }
@Override
public void setChanged() {
if (this.level != null && this.level.getBlockState(this.getBlockPos()).getBlock() instanceof QuestionBlock) {
if (this.getLootTable() != null || !this.items.getFirst().isEmpty())
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);
}
super.setChanged();
}

@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/wenxin2/marioverse/init/TagRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
public class TagRegistry {
public static final TagKey<Block> BONKABLE_BLOCKS = blockTags(Marioverse.MOD_ID, "bonkable_blocks");
public static final TagKey<Block> DYEABLE_WARP_PIPE_BLOCKS = blockTags(Marioverse.MOD_ID, "dyeable_warp_pipes");
public static final TagKey<Block> WARP_PIPE_BLOCKS = blockTags(Marioverse.MOD_ID, "warp_pipes");
public static final TagKey<Block> QUESTION_BLOCK_BLOCKS = blockTags(Marioverse.MOD_ID, "question_blocks");
public static final TagKey<Item> QUESTION_BLOCK_ITEMS = itemTags(Marioverse.MOD_ID, "question_blocks");
public static final TagKey<Block> WARP_PIPE_BLOCKS = blockTags(Marioverse.MOD_ID, "warp_pipes");
public static final TagKey<Block> WRENCH_EFFICIENT = blockTags(Marioverse.MOD_ID, "wrench_efficient");
public static final TagKey<Item> DYEABLE_WARP_PIPE_ITEMS = itemTags(Marioverse.MOD_ID, "dyeable_warp_pipes");
public static final TagKey<Item> QUESTION_BLOCK_ITEM_BLACKLIST = itemTags(Marioverse.MOD_ID, "question_block_blacklist");
public static final TagKey<Item> QUESTION_BLOCK_ITEMS = itemTags(Marioverse.MOD_ID, "question_blocks");
public static final TagKey<Item> WARP_PIPE_ITEMS = itemTags(Marioverse.MOD_ID, "warp_pipes");
public static final TagKey<EntityType<?>> WARP_BLACKLIST = entityTypeTags(Marioverse.MOD_ID, "warp_blacklist");
public static final TagKey<EntityType<?>> QUESTION_BLOCK_BLACKLIST = entityTypeTags(Marioverse.MOD_ID, "question_block_blacklist");
public static final TagKey<EntityType<?>> QUESTION_BLOCK_ENTITY_BLACKLIST = entityTypeTags(Marioverse.MOD_ID, "question_block_blacklist");
public static final TagKey<EntityType<?>> QUICK_TRAVEL_BLACKLIST = entityTypeTags(Marioverse.MOD_ID, "quick_travel_blacklist");

public static TagKey<Block> blockTags(String id, String name) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ public void baseTick() {

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

if (questionBlockEntity.getLootTable() != null) {
if (questionBlockEntity.getLootTable() != null)
questionBlock.unpackLootTable(this, questionBlockEntity);
world.setBlock(pos, world.getBlockState(pos).setValue(QuestionBlock.EMPTY, Boolean.TRUE), 3);
}

ItemStack storedItem = questionBlockEntity.getItems().getFirst();
if (!storedItem.isEmpty() && !world.getBlockState(pos).getValue(QuestionBlock.EMPTY)) {
Expand Down

0 comments on commit c1b265d

Please sign in to comment.