Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Sep 8, 2024
1 parent ed1212d commit bda46d6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/wenxin2/marioverse/blocks/QuestionBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -124,6 +128,11 @@ public boolean hasLootTableBeenProcessed() {
public void processLootTable() {
lootTableProcessed = true;
}

public void clearLootTable() {
this.lootTable = null;
}

public boolean isLastPowered() {
return lastPowered;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bda46d6

Please sign in to comment.