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
tutorial:inventory [2020/07/20 13:48] – update ImplementedInventory with new bindings zentraletutorial:inventory [2023/11/06 23:28] (current) binaris00
Line 103: Line 103:
     default void setStack(int slot, ItemStack stack) {     default void setStack(int slot, ItemStack stack) {
         getItems().set(slot, stack);         getItems().set(slot, stack);
-        if (stack.getCount() > getMaxCountPerStack()) { +        if (stack.getCount() > stack.getMaxCount()) { 
-            stack.setCount(getMaxCountPerStack());+            stack.setCount(stack.getMaxCount());
         }         }
     }     }
Line 130: Line 130:
      */       */ 
     @Override     @Override
-    default boolean canPlayerUseInv(PlayerEntity player) {+    default boolean canPlayerUse(PlayerEntity player) {
         return true;         return true;
     }     }
Line 158: Line 158:
     [...]     [...]
     @Override     @Override
-    public void fromTag(CompoundTag tag) { +    public void readNbt(NbtCompound nbt) { 
-        super.fromTag(tag); +        super.readNbt(nbt); 
-        Inventories.fromTag(tag,items);+        Inventories.readNbt(nbt, items);
     }     }
  
     @Override     @Override
-    public CompoundTag toTag(CompoundTag tag) { +    public NbtCompound writeNbt(NbtCompound nbt) { 
-        Inventories.toTag(tag,items); +        Inventories.writeNbt(nbt, items); 
-        return super.toTag(tag);+        return super.writeNbt(nbt);
     }     }
 } }
Line 181: Line 181:
     [...]     [...]
     @Override     @Override
-    public boolean onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) { +    public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) { 
-        if (world.isClient) return true;+        if (world.isClient) return ActionResult.SUCCESS;
         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);
  
Line 188: Line 188:
         if (!player.getStackInHand(hand).isEmpty()) {         if (!player.getStackInHand(hand).isEmpty()) {
             // Check what is the first open slot and put an item from the player's hand there             // Check what is the first open slot and put an item from the player's hand there
-            if (blockEntity.getInvStack(0).isEmpty()) {+            if (blockEntity.getStack(0).isEmpty()) {
                 // Put the stack the player is holding into the inventory                 // Put the stack the player is holding into the inventory
-                blockEntity.setInvStack(0, player.getStackInHand(hand).copy());+                blockEntity.setStack(0, player.getStackInHand(hand).copy());
                 // Remove the stack from the player's hand                 // Remove the stack from the player's hand
                 player.getStackInHand(hand).setCount(0);                 player.getStackInHand(hand).setCount(0);
-            } else if (blockEntity.getInvStack(1).isEmpty()) { +            } else if (blockEntity.getStack(1).isEmpty()) { 
-                blockEntity.setInvStack(1, player.getStackInHand(hand).copy());+                blockEntity.setStack(1, player.getStackInHand(hand).copy());
                 player.getStackInHand(hand).setCount(0);                 player.getStackInHand(hand).setCount(0);
             } else {             } else {
                 // If the inventory is full we'll print it's contents                 // If the inventory is full we'll print it's contents
                 System.out.println("The first slot holds "                 System.out.println("The first slot holds "
-                        + blockEntity.getInvStack(0) + " and the second slot holds " + blockEntity.getInvStack(1));+                        + blockEntity.getStack(0) + " and the second slot holds " + blockEntity.getStack(1));
             }             }
         }          } 
-        return true;+        return ActionResult.SUCCESS;
     }     }
 } }
Line 214: Line 214:
     [...]     [...]
     @Override     @Override
-    public boolean onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {+    public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {
         ...         ...
         if (!player.getStackInHand(hand).isEmpty()) {         if (!player.getStackInHand(hand).isEmpty()) {
Line 222: Line 222:
  
              // 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.getStack(1).isEmpty()) {
                 // Give the player the stack in the inventory                 // Give the player the stack in the inventory
-                player.inventory.offerOrDrop(world, blockEntity.getInvStack(1));+                player.getInventory().offerOrDrop(blockEntity.getStack(1));
                 // Remove the stack from the inventory                 // Remove the stack from the inventory
-                blockEntity.removeInvStack(1); +                blockEntity.removeStack(1); 
-            } else if (!blockEntity.getInvStack(0).isEmpty()) { +            } else if (!blockEntity.getStack(0).isEmpty()) { 
-                player.inventory.offerOrDrop(world, blockEntity.getInvStack(0)); +                player.getInventory().offerOrDrop(blockEntity.getStack(0)); 
-                blockEntity.removeInvStack(0);+                blockEntity.removeStack(0);
             }             }
         }         }
                  
-        return true;+        return ActionResult.SUCCESS;
     }     }
 } }
Line 246: Line 246:
     [...]     [...]
     @Override     @Override
-    public int[] getInvAvailableSlots(Direction var1) {+    public int[] getInvAvailableSlots(Direction side) {
         // Just return an array of all slots         // Just return an array of all slots
         int[] result = new int[getItems().size()];         int[] result = new int[getItems().size()];
Line 257: Line 257:
  
     @Override     @Override
-    public boolean canInsertInvStack(int slot, ItemStack stack, Direction direction) {+    public boolean canInsert(int slot, ItemStack stack, Direction direction) {
         return direction != Direction.UP;         return direction != Direction.UP;
     }     }
  
     @Override     @Override
-    public boolean canExtractInvStack(int slot, ItemStack stack, Direction direction) {+    public boolean canExtract(int slot, ItemStack stack, Direction direction) {
         return true;         return true;
     }     }
tutorial/inventory.1595252921.txt.gz · Last modified: 2020/07/20 13:48 by zentrale