Skip to content

Commit

Permalink
Add Demo for implemented Inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Richie1710 committed Dec 20, 2023
1 parent f867b20 commit 392cf0d
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main/java/de/richardt/decorations/DemoBlock.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
58 changes: 58 additions & 0 deletions src/main/java/de/richardt/decorations/DemoBlockEntity.java
Original file line number Diff line number Diff line change
@@ -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<ItemStack> items = DefaultedList.ofSize(2, ItemStack.EMPTY);

@Override
public DefaultedList<ItemStack> 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;
}
}
131 changes: 131 additions & 0 deletions src/main/java/de/richardt/decorations/ImplementedInventory.java
Original file line number Diff line number Diff line change
@@ -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<ItemStack> getItems();

/**
* Creates an inventory from the item list.
*/
static ImplementedInventory of(DefaultedList<ItemStack> 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;
}
}
11 changes: 11 additions & 0 deletions src/main/java/de/richardt/decorations/Richardts_decorations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DemoBlockEntity> 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"))
Expand All @@ -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);

Expand Down Expand Up @@ -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()));
}
}

0 comments on commit 392cf0d

Please sign in to comment.