diff --git a/src/main/java/de/richardt/decorations/DemoBlock.java b/src/main/java/de/richardt/decorations/DemoBlock.java new file mode 100644 index 0000000..1327f42 --- /dev/null +++ b/src/main/java/de/richardt/decorations/DemoBlock.java @@ -0,0 +1,68 @@ +package de.richardt.decorations; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockEntityProvider; +import net.minecraft.block.BlockRenderType; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventory; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + + +public class DemoBlock extends Block implements BlockEntityProvider { + + public DemoBlock(Settings settings) { + super(settings); + } + + @Override + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { + return new DemoBlockEntity(pos, state); + } + + @Override + public BlockRenderType getRenderType(BlockState state) { + // With inheriting from BlockWithEntity this defaults to INVISIBLE, so we need to change that! + return BlockRenderType.MODEL; + } + + @Override + public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) { + if (world.isClient) return ActionResult.SUCCESS; + Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos); + + + if (!player.getStackInHand(hand).isEmpty()) { + // Check what is the first open slot and put an item from the player's hand there + if (blockEntity.getStack(0).isEmpty()) { + // Put the stack the player is holding into the inventory + blockEntity.setStack(0, player.getStackInHand(hand).copy()); + // Remove the stack from the player's hand + player.getStackInHand(hand).setCount(0); + } else if (blockEntity.getStack(1).isEmpty()) { + blockEntity.setStack(1, player.getStackInHand(hand).copy()); + player.getStackInHand(hand).setCount(0); + } else { + // If the inventory is full we'll print it's contents + System.out.println("The first slot holds " + + blockEntity.getStack(0) + " and the second slot holds " + blockEntity.getStack(1)); + } + }else{ + if (!blockEntity.getStack(1).isEmpty()) { + // Give the player the stack in the inventory + player.getInventory().offerOrDrop(blockEntity.getStack(1)); + // Remove the stack from the inventory + blockEntity.removeStack(1); + } else if (!blockEntity.getStack(0).isEmpty()) { + player.getInventory().offerOrDrop(blockEntity.getStack(0)); + blockEntity.removeStack(0); + } + } + return ActionResult.SUCCESS; + } +} diff --git a/src/main/java/de/richardt/decorations/DemoBlockEntity.java b/src/main/java/de/richardt/decorations/DemoBlockEntity.java new file mode 100644 index 0000000..fc2aea3 --- /dev/null +++ b/src/main/java/de/richardt/decorations/DemoBlockEntity.java @@ -0,0 +1,58 @@ +package de.richardt.decorations; + +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.inventory.Inventories; +import net.minecraft.inventory.SidedInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.util.collection.DefaultedList; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; + +public class DemoBlockEntity extends BlockEntity implements ImplementedInventory, SidedInventory { + + public DemoBlockEntity(BlockPos pos, BlockState state) { + super(Richardts_decorations.DEMO_BLOCK_ENTITY, pos, state); + } + + private final DefaultedList items = DefaultedList.ofSize(2, ItemStack.EMPTY); + + @Override + public DefaultedList getItems() { + return items; + } + @Override + public void writeNbt(NbtCompound nbt) { + Inventories.writeNbt(nbt, items); + super.writeNbt(nbt); + } + + @Override + public void readNbt(NbtCompound nbt) { + + super.readNbt(nbt); + Inventories.writeNbt(nbt, items); + } + + @Override + public int[] getAvailableSlots(Direction side) { + // Just return an array of all slots + int[] result = new int[getItems().size()]; + for (int i = 0; i < result.length; i++) { + result[i] = i; + } + + return result; + } + + @Override + public boolean canInsert(int slot, ItemStack stack, Direction direction) { + return direction != Direction.UP; + } + + @Override + public boolean canExtract(int slot, ItemStack stack, Direction direction) { + return true; + } +} diff --git a/src/main/java/de/richardt/decorations/ImplementedInventory.java b/src/main/java/de/richardt/decorations/ImplementedInventory.java new file mode 100644 index 0000000..b172270 --- /dev/null +++ b/src/main/java/de/richardt/decorations/ImplementedInventory.java @@ -0,0 +1,131 @@ +package de.richardt.decorations; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventories; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.util.collection.DefaultedList; + +/** + * A simple {@code Inventory} implementation with only default methods + an item list getter. + * + * Originally by Juuz + */ +public interface ImplementedInventory extends Inventory { + + /** + * Retrieves the item list of this inventory. + * Must return the same instance every time it's called. + */ + DefaultedList getItems(); + + /** + * Creates an inventory from the item list. + */ + static ImplementedInventory of(DefaultedList items) { + return () -> items; + } + + /** + * Creates a new inventory with the specified size. + */ + static ImplementedInventory ofSize(int size) { + return of(DefaultedList.ofSize(size, ItemStack.EMPTY)); + } + + /** + * Returns the inventory size. + */ + @Override + default int size() { + return getItems().size(); + } + + /** + * Checks if the inventory is empty. + * @return true if this inventory has only empty stacks, false otherwise. + */ + @Override + default boolean isEmpty() { + for (int i = 0; i < size(); i++) { + ItemStack stack = getStack(i); + if (!stack.isEmpty()) { + return false; + } + } + return true; + } + + /** + * Retrieves the item in the slot. + */ + @Override + default ItemStack getStack(int slot) { + return getItems().get(slot); + } + + /** + * Removes items from an inventory slot. + * @param slot The slot to remove from. + * @param count How many items to remove. If there are less items in the slot than what are requested, + * takes all items in that slot. + */ + @Override + default ItemStack removeStack(int slot, int count) { + ItemStack result = Inventories.splitStack(getItems(), slot, count); + if (!result.isEmpty()) { + markDirty(); + } + return result; + } + + /** + * Removes all items from an inventory slot. + * @param slot The slot to remove from. + */ + @Override + default ItemStack removeStack(int slot) { + return Inventories.removeStack(getItems(), slot); + } + + /** + * Replaces the current stack in an inventory slot with the provided stack. + * @param slot The inventory slot of which to replace the itemstack. + * @param stack The replacing itemstack. If the stack is too big for + * this inventory ({@link Inventory#getMaxCountPerStack()}), + * it gets resized to this inventory's maximum amount. + */ + @Override + default void setStack(int slot, ItemStack stack) { + getItems().set(slot, stack); + if (stack.getCount() > stack.getMaxCount()) { + stack.setCount(stack.getMaxCount()); + } + } + + /** + * Clears the inventory. + */ + @Override + default void clear() { + getItems().clear(); + } + + /** + * Marks the state as dirty. + * Must be called after changes in the inventory, so that the game can properly save + * the inventory contents and notify neighboring blocks of inventory changes. + */ + @Override + default void markDirty() { + // Override if you want behavior. + } + + /** + * @return true if the player can use the inventory, false otherwise. + */ + @Override + default boolean canPlayerUse(PlayerEntity player) { + return true; + } +} diff --git a/src/main/java/de/richardt/decorations/Richardts_decorations.java b/src/main/java/de/richardt/decorations/Richardts_decorations.java index bba6796..f388aa1 100644 --- a/src/main/java/de/richardt/decorations/Richardts_decorations.java +++ b/src/main/java/de/richardt/decorations/Richardts_decorations.java @@ -4,8 +4,10 @@ import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; +import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.fabricmc.fabric.api.registry.CompostingChanceRegistry; import net.fabricmc.fabric.api.registry.FuelRegistry; +import net.minecraft.block.entity.BlockEntityType; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; @@ -34,6 +36,12 @@ public class Richardts_decorations implements ModInitializer { public static final FloorLampBlock FLOORLAMPBLOCK = new FloorLampBlock(FabricBlockSettings.create().strength(4.0f).luminance(state -> state.get(FloorLampBlock.GLOWING) ? 15 : 0)); public static final ThermoMixBlock THERMOMIXBLOCK = new ThermoMixBlock(FabricBlockSettings.create().strength(4.0f)); public static final NotebookBlock NOTEBOOKBLOCK = new NotebookBlock(FabricBlockSettings.create().strength(4.0f)); + public static final DemoBlock DEMO_BLOCK = new DemoBlock(FabricBlockSettings.create()); + public static final BlockEntityType DEMO_BLOCK_ENTITY = Registry.register( + Registries.BLOCK_ENTITY_TYPE, + new Identifier("richardts_decorations", "demo_block_entity"), + FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build() + ); private static final ItemGroup ITEM_GROUP = FabricItemGroup.builder() .icon(() -> new ItemStack(THERMOMIXBLOCK)) .displayName(Text.translatable("itemGroup.richardts_decorations.test_group")) @@ -49,6 +57,7 @@ public class Richardts_decorations implements ModInitializer { entries.add(NOTEBOOKBLOCK); entries.add(LIGHT_BULB_ITEM); entries.add(QUAD_BLADE_ITEM); + entries.add(DEMO_BLOCK); entries.add(TV_BLOCK); entries.add(TV_BLOCK_LARGE); @@ -85,5 +94,7 @@ public void onInitialize() { Registry.register(Registries.ITEM, new Identifier("richardts_decorations", "thermo_mix_block"), new BlockItem(THERMOMIXBLOCK, new FabricItemSettings())); Registry.register(Registries.BLOCK, new Identifier("richardts_decorations", "notebook_block"), NOTEBOOKBLOCK); Registry.register(Registries.ITEM, new Identifier("richardts_decorations", "notebook_block"), new BlockItem(NOTEBOOKBLOCK, new FabricItemSettings())); + Registry.register(Registries.BLOCK, new Identifier("richardts_decorations", "demo_block"), DEMO_BLOCK); + Registry.register(Registries.ITEM, new Identifier("richardts_decorations", "demo_block"), new BlockItem(DEMO_BLOCK, new FabricItemSettings())); } }