User Tools

Site Tools


tutorial:transfer-api_simpletank

Differences

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

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
tutorial:transfer-api_simpletank [2021/10/29 20:46] – created technici4ntutorial:transfer-api_simpletank [2022/10/07 16:55] nexus-dino
Line 1: Line 1:
 ===== Fabric Transfer API: Creating a simple tank ===== ===== Fabric Transfer API: Creating a simple tank =====
 //This article is part of a series on the Fabric Transfer API. [[tutorial:transfer-api|Link to the home page of the series]].// //This article is part of a series on the Fabric Transfer API. [[tutorial:transfer-api|Link to the home page of the series]].//
 +
 +=== But wait, what is a FluidVariant ? ===
 +A ''FluidVariant'' is what the Transfer API uses to represent the "type" of a fluid. It contains the ''Fluid'' in the minecraft sense, and also an optional NBT compound tag:
 +<code java>
 +// Creating a fluid variant from a fluid, without an NBT tag.
 +FluidVariant waterVariant = FluidVariant.of(Fluids.WATER);
 +waterVariant.getFluid() // returns Fluids.WATER
 +waterVariant.copyNbt() // returns a copy of the optional nbt tag, in this case null
 +// Creating a fluid variant from a fluid, with an NBT tag.
 +NbtCompound customTag = new NbtCompound();
 +customTag.putBoolean("magic", true);
 +FluidVariant magicWater = FluidVariant.of(Fluids.WATER, customTag);
 +</code>
 +
 +Variants are always compared with ''.equals'', NEVER WITH ''=='' !
 +<code java>
 +waterVariant.equals(waterVariant); // returns true
 +waterVariant.equals(magicWater); // returns false
 +// You can easily test if a variant has some fluid:
 +waterVariant.isOf(Fluids.WATER); // returns true
 +magicWater.isOf(Fluids.WATER); // returns true
 +</code>
 +
 +They can easily be serialized to and from NBT or network packets:
 +<code java>
 +// NBT
 +NbtCompound compound = variant.toNbt();
 +FluidVariant variant = FluidVariant.fromNbt(compound);
 +// Network packets
 +variant.toPacket(buf);
 +FluidVariant variant = FluidVariant.fromPacket(buf);
 +</code>
 +
 +CAUTION: make sure that you know the base understanding of [[tutorial:blockentity|making a block entity]] and [[tutorial:inventory|creating block entity inventories]] before continuing to read.
  
 Let's see how we can make a block entity contain some fluid: Let's see how we can make a block entity contain some fluid:
 +
 <code java> <code java>
-public class MyTankBlockEntity extends BlockEntity {+// Make sure you implement ExtendedScreenHandlerFactory because we need to write the pos of this entity... 
 +public class MyTankBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory, ImplementedInventory { 
 +        private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(3, ItemStack.EMPTY);
  // This field is going to contain the amount, and the fluid variant (more on that in a bit).  // This field is going to contain the amount, and the fluid variant (more on that in a bit).
  public final SingleVariantStorage<FluidVariant> fluidStorage = new SingleVariantStorage<>() {  public final SingleVariantStorage<FluidVariant> fluidStorage = new SingleVariantStorage<>() {
Line 16: Line 53:
  // Here, you can pick your capacity depending on the fluid variant.  // Here, you can pick your capacity depending on the fluid variant.
  // For example, if we want to store 8 buckets of any fluid:  // For example, if we want to store 8 buckets of any fluid:
- return 8 * FluidConstants.BUCKET;+ return (8 * FluidConstants.BUCKET) / 81 // This will convert it to mB amount to be consistent;
  }  }
  
Line 23: Line 60:
  // Called after a successful insertion or extraction, markDirty to ensure the new amount and variant will be saved properly.  // Called after a successful insertion or extraction, markDirty to ensure the new amount and variant will be saved properly.
  markDirty();  markDirty();
 +                        if (!world.isClient) {
 +                           var buf = PacketByteBufs.create();
 +                           // Write your data here.
 +                           PlayerLookup.tracking(MyTankBlockEntity.this).forEach(player -> {
 +                               ServerPlayNetworking.send(player, YOUR_IDENTIFIER, buf);
 +                           });
 +                        }
  }  }
  };  };
Line 48: Line 92:
 // Put this in your mod initializer, after you have created the block entity type: // Put this in your mod initializer, after you have created the block entity type:
 FluidStorage.SIDED.registerForBlockEntity((myTank, direction) -> myTank.fluidStorage, MY_TANK); FluidStorage.SIDED.registerForBlockEntity((myTank, direction) -> myTank.fluidStorage, MY_TANK);
-</code> 
- 
-=== But wait, what is a FluidVariant ? === 
-A ''FluidVariant'' is what the Transfer API uses to represent the "type" of a fluid. It contains the ''Fluid'' in the minecraft sense, and also an optional NBT compound tag: 
-<code java> 
-// Creating a fluid variant from a fluid, without an NBT tag. 
-FluidVariant waterVariant = FluidVariant.of(Fluids.WATER); 
-waterVariant.getFluid() // returns Fluids.WATER 
-waterVariant.copyNbt() // returns a copy of the optional nbt tag, in this case null 
-// Creating a fluid variant from a fluid, with an NBT tag. 
-NbtCompound customTag = new NbtCompound(); 
-customTag.putBoolean("magic", true); 
-FluidVariant magicWater = FluidVariant.of(Fluids.WATER, customTag); 
-</code> 
- 
-Variants are always compared with ''.equals'', NEVER WITH ''=='' ! 
-<code java> 
-waterVariant.equals(waterVariant); // returns true 
-waterVariant.equals(magicWater); // returns false 
-// You can easily test if a variant has some fluid: 
-waterVariant.isOf(Fluids.WATER); // returns true 
-magicWater.isOf(Fluids.WATER); // returns true 
-</code> 
- 
-They can easily be serialized to and from NBT or network packets: 
-<code java> 
-// NBT 
-NbtCompound compound = variant.toNbt(); 
-FluidVariant variant = FluidVariant.fromNbt(compound); 
-// Network packets 
-variant.toPacket(buf); 
-FluidVariant variant = FluidVariant.fromPacket(buf); 
 </code> </code>
  
tutorial/transfer-api_simpletank.txt · Last modified: 2023/02/22 08:22 by redgrapefruit