11package gregtech .api .mui .sync ;
22
3+ import com .cleanroommc .modularui .utils .MouseData ;
4+
35import gregtech .api .util .GTUtility ;
46
57import net .minecraft .entity .item .EntityItem ;
911import net .minecraft .util .SoundCategory ;
1012import net .minecraft .util .SoundEvent ;
1113import net .minecraftforge .fluids .FluidStack ;
14+ import net .minecraftforge .fluids .FluidTank ;
1215import net .minecraftforge .fluids .IFluidTank ;
1316import net .minecraftforge .fluids .capability .CapabilityFluidHandler ;
1417import net .minecraftforge .fluids .capability .IFluidHandlerItem ;
1922
2023public class GTFluidSyncHandler extends SyncHandler {
2124
22- private static final int TRY_CLICK_CONTAINER = 1 ;
23- private static final int UPDATE_TANK = 2 ;
25+ public static final int TRY_CLICK_CONTAINER = 1 ;
26+ public static final int UPDATE_TANK = 2 ;
27+ public static final int UPDATE_AMOUNT = 3 ;
2428
2529 private final IFluidTank tank ;
30+ private FluidStack lastFluid ;
31+ private FluidStack lockedFluid ;
2632 private boolean canDrainSlot = true ;
2733 private boolean canFillSlot = true ;
34+ private boolean phantom ;
2835
2936 public GTFluidSyncHandler (IFluidTank tank ) {
3037 this .tank = tank ;
3138 }
3239
40+ @ Override
41+ public void detectAndSendChanges (boolean init ) {
42+ var current = getFluid ();
43+ if (current == null && lastFluid == null ) return ;
44+ if (current == null || lastFluid == null || lastFluid .getFluid () != current .getFluid ()) {
45+ lastFluid = current == null ? null : current .copy ();
46+ syncToClient (UPDATE_TANK , buffer -> NetworkUtils .writeFluidStack (buffer , current ));
47+ } else if (current .amount != lastFluid .amount ) {
48+ syncToClient (UPDATE_AMOUNT , buffer -> buffer .writeInt (current .amount ));
49+ }
50+ }
51+
3352 public FluidStack getFluid () {
3453 return this .tank .getFluid ();
3554 }
3655
56+ public void setFluid (FluidStack fluid ) {
57+ if (tank instanceof FluidTank fluidTank ) {
58+ fluidTank .setFluid (fluid );
59+ } else {
60+ tank .drain (Integer .MAX_VALUE , true );
61+ tank .fill (fluid , true );
62+ }
63+ }
64+
65+ public void setAmount (int amount ) {
66+ if (getFluid () == null ) return ;
67+ getFluid ().amount = amount ;
68+ }
69+
3770 public int getCapacity () {
3871 return this .tank .getCapacity ();
3972 }
@@ -56,6 +89,15 @@ public boolean canFillSlot() {
5689 return this .canFillSlot ;
5790 }
5891
92+ public GTFluidSyncHandler phantom (boolean phantom ) {
93+ this .phantom = phantom ;
94+ return this ;
95+ }
96+
97+ public boolean isPhantom () {
98+ return phantom ;
99+ }
100+
59101 public String getFormattedFluidAmount () {
60102 return String .format ("%,d" , tank .getFluid () == null ? 0 : tank .getFluid ().amount );
61103 }
@@ -68,17 +110,16 @@ public String getFluidLocalizedName() {
68110 public void readOnClient (int id , PacketBuffer buf ) {
69111 switch (id ) {
70112 case TRY_CLICK_CONTAINER -> replaceCursorItemStack (NetworkUtils .readItemStack (buf ));
71- case UPDATE_TANK -> {
72- tank .drain (Integer .MAX_VALUE , true );
73- tank .fill (NetworkUtils .readFluidStack (buf ), true );
74- }
113+ case UPDATE_TANK -> setFluid (NetworkUtils .readFluidStack (buf ));
114+ case UPDATE_AMOUNT -> setAmount (buf .readInt ());
75115 }
76116 }
77117
78118 @ Override
79119 public void readOnServer (int id , PacketBuffer buf ) {
80120 if (id == TRY_CLICK_CONTAINER ) {
81- var stack = tryClickContainer (buf .readBoolean ());
121+ var data = MouseData .readPacket (buf );
122+ var stack = tryClickContainer (data .mouseButton == 0 );
82123 if (!stack .isEmpty ())
83124 syncToClient (TRY_CLICK_CONTAINER , buffer -> NetworkUtils .writeItemStack (buffer , stack ));
84125 }
@@ -162,27 +203,27 @@ private ItemStack drainTankIntoStack(IFluidHandlerItem fluidHandler, FluidStack
162203 ItemStack heldItem = getSyncManager ().getCursorItem ();
163204 if (heldItem .isEmpty ()) return ItemStack .EMPTY ;
164205
165- ItemStack fluidContainer = fluidHandler . getContainer () ;
206+ ItemStack fluidContainer = ItemStack . EMPTY ;
166207 int filled = fluidHandler .fill (tankFluid , false );
208+ int stored = tankFluid .amount ;
167209 if (filled > 0 ) {
168210 fluidHandler .fill (tankFluid , true );
169211 tank .drain (filled , true );
212+ fluidContainer = fluidHandler .getContainer ();
170213 if (tryFillAll ) {
171214 // Determine how many more items we can fill. One item is already filled.
172215 // Integer division means it will round down, so it will only fill equivalent fluid amounts.
173216 // For example:
174217 // Click with 3 cells, with 2500L of fluid in the tank.
175218 // 2 cells will be filled, and 500L will be left behind in the tank.
176- int additional = Math .min (heldItem .getCount (), tankFluid . amount / filled ) - 1 ;
219+ int additional = Math .min (heldItem .getCount (), stored / filled ) - 1 ;
177220 tank .drain (filled * additional , true );
178221 fluidContainer .grow (additional );
179222 }
180- fluidContainer = fluidHandler .getContainer ();
181223 replaceCursorItemStack (fluidContainer );
182224 playSound (tankFluid , false );
183- return fluidContainer ;
184225 }
185- return ItemStack . EMPTY ;
226+ return fluidContainer ;
186227 }
187228
188229 /**
0 commit comments