Skip to content

Commit

Permalink
Updates to drop logic to ensure that correct items are dropped
Browse files Browse the repository at this point in the history
 - Changed drops for chest and barrel to use onBreak and onExploded, fixes #102 for Fabric
 - Updated logic for state replaced to ensure no duplication on rotation, fixes #158 for Fabric
  • Loading branch information
witchica committed Feb 11, 2024
1 parent 6c0410c commit 5452c2b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 59 deletions.
8 changes: 4 additions & 4 deletions src/client/resources/assets/compact_storage/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"container.compact_storage.backpack": "Backpack",

"item.compact_storage.backpack": "Backpack",
"item.compact_storage.upgrade_column": "Storage Upgrade (+1 Height)",
"item.compact_storage.upgrade_row": "Storage Upgrade (+1 Width)",
"item.compact_storage.upgrade_column": "Storage Upgrade (Height)",
"item.compact_storage.upgrade_row": "Storage Upgrade (Width)",

"item.compact_storage.backpack_white": "White Backpack",
"item.compact_storage.backpack_orange": "Orange Backpack",
Expand Down Expand Up @@ -72,8 +72,8 @@
"block.compact_storage.dark_oak_drum": "Dark Oak Item Drum",
"block.compact_storage.warped_drum": "Warped Item Drum",

"tooltip.compact_storage.row_upgrade_descriptor": "Use this on a Compact Storage block to add an extra row",
"tooltip.compact_storage.column_upgrade_descriptor": "Use this on a Compact Storage block to add an extra column",
"tooltip.compact_storage.row_upgrade_descriptor": "Use this on a Compact Storage block to add extra width",
"tooltip.compact_storage.column_upgrade_descriptor": "Use this on a Compact Storage block to add extra height",
"tooltip.compact_storage.upgrade_backpack": "Can also be applied to backpacks by placing in your off-hand",

"text.compact_storage.upgrade_success": "Upgrade successfully applied!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.BiConsumer;

public class CompactBarrelBlock extends BlockWithEntity {
public static final DirectionProperty FACING = DirectionProperty.of("facing");
Expand Down Expand Up @@ -151,35 +153,15 @@ public BlockRenderType getRenderType(BlockState state) {
}

@Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if(newState.getBlock() instanceof CompactBarrelBlock) {
return;
}

BlockEntity blockEntity = world.getBlockEntity(pos);

if(blockEntity instanceof CompactBarrelBlockEntity compactBarrelBlockEntity) {
ItemStack chestStack = new ItemStack(this, 1);

NbtCompound chestTag = new NbtCompound();
chestTag.putInt("inventory_width", compactBarrelBlockEntity.inventoryWidth);
chestTag.putInt("inventory_height", compactBarrelBlockEntity.inventoryHeight);

if(compactBarrelBlockEntity.inventoryWidth != 9 || compactBarrelBlockEntity.inventoryHeight != 6) {
chestStack.setNbt(chestTag);
}


if(compactBarrelBlockEntity.hasCustomName()) {
chestStack.setCustomName(compactBarrelBlockEntity.getCustomName());
}

ItemScatterer.spawn(world, pos, (Inventory) compactBarrelBlockEntity);
ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), chestStack);
world.updateComparators(pos, this);
}
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
CompactStorageUtil.dropContents(world, pos, state.getBlock(), player);
return super.onBreak(world, pos, state, player);
}

super.onStateReplaced(state, world, pos, newState, moved);
@Override
public void onExploded(BlockState state, World world, BlockPos pos, Explosion explosion, BiConsumer<ItemStack, BlockPos> stackMerger) {
CompactStorageUtil.dropContents(world, pos, this, null);
super.onExploded(state, world, pos, explosion, stackMerger);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.explosion.Explosion;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.BiConsumer;

public class CompactChestBlock extends BlockWithEntity {
public static final DirectionProperty FACING = DirectionProperty.of("facing", Direction.NORTH, Direction.EAST,
Expand Down Expand Up @@ -198,34 +201,15 @@ public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos po
}

@Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if(newState.getBlock() instanceof CompactChestBlock) {
return;
}

BlockEntity blockEntity = world.getBlockEntity(pos);

if(blockEntity instanceof CompactChestBlockEntity compactChestBlockEntity) {
ItemStack chestStack = new ItemStack(this, 1);

NbtCompound chestTag = new NbtCompound();
chestTag.putInt("inventory_width", compactChestBlockEntity.inventoryWidth);
chestTag.putInt("inventory_height", compactChestBlockEntity.inventoryHeight);

if(compactChestBlockEntity.inventoryWidth != 9 || compactChestBlockEntity.inventoryHeight != 6) {
chestStack.setNbt(chestTag);
}

if(compactChestBlockEntity.hasCustomName()) {
chestStack.setCustomName(compactChestBlockEntity.getCustomName());
}

ItemScatterer.spawn(world, pos, (Inventory) compactChestBlockEntity);
ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), chestStack);
world.updateComparators(pos, this);
}
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
CompactStorageUtil.dropContents(world, pos, state.getBlock(), player);
return super.onBreak(world, pos, state, player);
}

super.onStateReplaced(state, world, pos, newState, moved);
@Override
public void onExploded(BlockState state, World world, BlockPos pos, Explosion explosion, BiConsumer<ItemStack, BlockPos> stackMerger) {
CompactStorageUtil.dropContents(world, pos, this, null);
super.onExploded(state, world, pos, explosion, stackMerger);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.witchica.compactstorage.util;

import com.witchica.compactstorage.block.entity.CompactBarrelBlockEntity;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.ItemScatterer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand Down Expand Up @@ -46,4 +55,39 @@ public static void appendTooltip(ItemStack stack, @Nullable BlockView world, Lis
tooltip.add(Text.translatable("text.compact_storage.tooltip.size_y").formatted(Formatting.WHITE).append(Text.literal("" + inventoryY).formatted(Formatting.DARK_PURPLE)));
tooltip.add(Text.translatable("text.compact_storage.tooltip.slots", slots).formatted(Formatting.DARK_PURPLE, Formatting.ITALIC));
}

public static void dropContents(World world, BlockPos pos, Block block, PlayerEntity player) {
if(world.isClient) {
return;
}

BlockEntity blockEntity = world.getBlockEntity(pos);

if(blockEntity instanceof CompactStorageInventoryImpl inventory) {
ItemStack chestStack = new ItemStack(block, 1);

NbtCompound chestTag = new NbtCompound();
chestTag.putInt("inventory_width", inventory.getInventoryWidth());
chestTag.putInt("inventory_height", inventory.getInventoryHeight());

if(inventory.getInventoryWidth() != 9 || inventory.getInventoryHeight() != 6) {
chestStack.setNbt(chestTag);
}


if(inventory instanceof LootableContainerBlockEntity lootableContainerBlockEntity) {
if(lootableContainerBlockEntity.hasCustomName()) {
chestStack.setCustomName(lootableContainerBlockEntity.getCustomName());
}
}

ItemScatterer.spawn(world, pos, (Inventory) inventory);

if(player == null || !player.isCreative()) {
ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), chestStack);
}

world.updateComparators(pos, block);
}
}
}

0 comments on commit 5452c2b

Please sign in to comment.