-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f867b20
commit 392cf0d
Showing
4 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
src/main/java/de/richardt/decorations/DemoBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
131
src/main/java/de/richardt/decorations/ImplementedInventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters