User Tools

Site Tools


tutorial:inventory

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:inventory [2019/08/06 18:29] fudgetutorial:inventory [2019/08/21 20:24] – change giveItemStack to inventory.offerOrDrop fudge
Line 1: Line 1:
 ====== Storing items in a block as an Inventory ====== ====== Storing items in a block as an Inventory ======
 +Make sure you've [[tutorial:blockentity|made a block entity]] before reading this tutorial.  
 +
 The standard way to store items in a BlockEntity is to make it an ''Inventory'' The standard way to store items in a BlockEntity is to make it an ''Inventory''
 This allows hoppers (or other mods) to insert and extract items from your BlockEntity without any extra work.  This allows hoppers (or other mods) to insert and extract items from your BlockEntity without any extra work. 
Line 122: Line 124:
         return items;         return items;
     }     }
-    ...+    [...]
  
 } }
Line 131: Line 133:
 <code java> <code java>
 public class DemoBlockEntity extends BlockEntity implements ImplementedInventory { public class DemoBlockEntity extends BlockEntity implements ImplementedInventory {
-    ...+    [...]
     @Override     @Override
     public void fromTag(CompoundTag tag) {     public void fromTag(CompoundTag tag) {
Line 154: Line 156:
 <code java> <code java>
 public class ExampleBlock extends Block implements BlockEntityProvider { public class ExampleBlock extends Block implements BlockEntityProvider {
-    ...+    [...]
     @Override     @Override
     public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {     public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {
Line 187: Line 189:
 <code java> <code java>
 public class ExampleBlock extends Block implements BlockEntityProvider { public class ExampleBlock extends Block implements BlockEntityProvider {
-    ...+    [...]
     @Override     @Override
     public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {     public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {
Line 196: Line 198:
             // If the player is not holding anything we'll get give him the items in the block entity one by one             // If the player is not holding anything we'll get give him the items in the block entity one by one
  
-            // Find the first slot that has an item and give it to the player+             // Find the first slot that has an item and give it to the player
             if (!blockEntity.getInvStack(1).isEmpty()) {             if (!blockEntity.getInvStack(1).isEmpty()) {
                 // Give the player the stack in the inventory                 // Give the player the stack in the inventory
-                player.giveItemStack(blockEntity.getInvStack(1));+                player.inventory.offerOrDrop(world, blockEntity.getInvStack(1));
                 // Remove the stack from the inventory                 // Remove the stack from the inventory
                 blockEntity.removeInvStack(1);                 blockEntity.removeInvStack(1);
             } else if (!blockEntity.getInvStack(0).isEmpty()) {             } else if (!blockEntity.getInvStack(0).isEmpty()) {
-                player.giveItemStack(blockEntity.getInvStack(0));+                player.inventory.offerOrDrop(world, blockEntity.getInvStack(0));
                 blockEntity.removeInvStack(0);                 blockEntity.removeInvStack(0);
             }             }
Line 219: Line 221:
 <code java> <code java>
 public class DemoBlockEntity extends BlockEntity implements ImplementedInventory, SidedInventory { public class DemoBlockEntity extends BlockEntity implements ImplementedInventory, SidedInventory {
-    ...+    [...]
     @Override     @Override
     public int[] getInvAvailableSlots(Direction var1) {     public int[] getInvAvailableSlots(Direction var1) {
tutorial/inventory.txt · Last modified: 2023/11/06 23:28 by binaris00