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/07 06:59] fudgetutorial:inventory [2020/07/16 23:54] – update activate references to onUse user11681
Line 148: Line 148:
 </code> </code>
 ===== Extracting and inserting from your inventory (or any inventory) ===== ===== Extracting and inserting from your inventory (or any inventory) =====
-In our block class, we'll override the `activate` behavior to insert and extract items from our inventory. +In our block class, we'll override the `onUse` behavior to insert and extract items from our inventory. 
 Note that this can be done to any ''Inventory'' instance, not just our own (so you could do the same thing to a chest block, for example). Note that this can be done to any ''Inventory'' instance, not just our own (so you could do the same thing to a chest block, for example).
 First we'll handle inserting into the inventory. The player will insert the item he is holding if he is holding one. First we'll handle inserting into the inventory. The player will insert the item he is holding if he is holding one.
Line 158: Line 158:
     [...]     [...]
     @Override     @Override
-    public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {+    public boolean onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {
         if (world.isClient) return true;         if (world.isClient) return true;
         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);
Line 191: Line 191:
     [...]     [...]
     @Override     @Override
-    public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {+    public boolean onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {
         ...         ...
         if (!player.getStackInHand(hand).isEmpty()) {         if (!player.getStackInHand(hand).isEmpty()) {
Line 198: 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);
             }             }
tutorial/inventory.txt · Last modified: 2023/11/06 23:28 by binaris00