1212import net .minecraftforge .fluids .FluidTank ;
1313import net .minecraftforge .fluids .IFluidTank ;
1414import net .minecraftforge .fluids .capability .CapabilityFluidHandler ;
15+ import net .minecraftforge .fluids .capability .IFluidHandler ;
1516import net .minecraftforge .fluids .capability .IFluidHandlerItem ;
1617
1718import com .cleanroommc .modularui .network .NetworkUtils ;
19+ import com .cleanroommc .modularui .utils .FluidTankHandler ;
1820import com .cleanroommc .modularui .utils .MouseData ;
1921import com .cleanroommc .modularui .value .sync .SyncHandler ;
2022import org .jetbrains .annotations .NotNull ;
@@ -24,16 +26,22 @@ public class GTFluidSyncHandler extends SyncHandler {
2426 public static final int TRY_CLICK_CONTAINER = 1 ;
2527 public static final int UPDATE_TANK = 2 ;
2628 public static final int UPDATE_AMOUNT = 3 ;
29+ public static final int PHANTOM_SCROLL = 4 ;
30+ public static final int LOCK_FLUID = 5 ;
2731
2832 private final IFluidTank tank ;
33+ private final IFluidHandler handler ;
2934 private FluidStack lastFluid ;
3035 private FluidStack lockedFluid ;
36+ private FluidStack phantomFluid ;
3137 private boolean canDrainSlot = true ;
3238 private boolean canFillSlot = true ;
3339 private boolean phantom ;
40+ private boolean showAmount = true ;
3441
3542 public GTFluidSyncHandler (IFluidTank tank ) {
3643 this .tank = tank ;
44+ this .handler = FluidTankHandler .getTankFluidHandler (tank );
3745 }
3846
3947 @ Override
@@ -59,6 +67,15 @@ public void setFluid(FluidStack fluid) {
5967 tank .drain (Integer .MAX_VALUE , true );
6068 tank .fill (fluid , true );
6169 }
70+ if (!isPhantom () || fluid == null ) return ;
71+ if (this .phantomFluid == null || this .phantomFluid .getFluid () != fluid .getFluid ()) {
72+ this .phantomFluid = fluid ;
73+ }
74+ }
75+
76+ public void lockFluid (FluidStack fluid ) {
77+ this .lockedFluid = fluid ;
78+ sync (LOCK_FLUID , buffer -> NetworkUtils .writeFluidStack (buffer , fluid ));
6279 }
6380
6481 public void setAmount (int amount ) {
@@ -90,13 +107,24 @@ public boolean canFillSlot() {
90107
91108 public GTFluidSyncHandler phantom (boolean phantom ) {
92109 this .phantom = phantom ;
110+ if (phantom && this .tank .getFluid () != null )
111+ this .phantomFluid = this .tank .getFluid ().copy ();
93112 return this ;
94113 }
95114
96115 public boolean isPhantom () {
97116 return phantom ;
98117 }
99118
119+ public GTFluidSyncHandler showAmount (boolean showAmount ) {
120+ this .showAmount = showAmount ;
121+ return this ;
122+ }
123+
124+ public boolean showAmount () {
125+ return this .showAmount ;
126+ }
127+
100128 public String getFormattedFluidAmount () {
101129 return String .format ("%,d" , tank .getFluid () == null ? 0 : tank .getFluid ().amount );
102130 }
@@ -111,16 +139,122 @@ public void readOnClient(int id, PacketBuffer buf) {
111139 case TRY_CLICK_CONTAINER -> replaceCursorItemStack (NetworkUtils .readItemStack (buf ));
112140 case UPDATE_TANK -> setFluid (NetworkUtils .readFluidStack (buf ));
113141 case UPDATE_AMOUNT -> setAmount (buf .readInt ());
142+ case LOCK_FLUID -> lockFluid (NetworkUtils .readFluidStack (buf ));
114143 }
115144 }
116145
117146 @ Override
118147 public void readOnServer (int id , PacketBuffer buf ) {
119- if (id == TRY_CLICK_CONTAINER ) {
120- var data = MouseData .readPacket (buf );
121- var stack = tryClickContainer (data .mouseButton == 0 );
122- if (!stack .isEmpty ())
123- syncToClient (TRY_CLICK_CONTAINER , buffer -> NetworkUtils .writeItemStack (buffer , stack ));
148+ switch (id ) {
149+ case TRY_CLICK_CONTAINER -> {
150+ var data = MouseData .readPacket (buf );
151+ if (isPhantom ()) {
152+ tryClickPhantom (data );
153+ } else {
154+ var stack = tryClickContainer (data .mouseButton == 0 );
155+ if (!stack .isEmpty ())
156+ syncToClient (TRY_CLICK_CONTAINER , buffer -> NetworkUtils .writeItemStack (buffer , stack ));
157+ }
158+ }
159+ case UPDATE_TANK -> {
160+ var fluid = NetworkUtils .readFluidStack (buf );
161+ setFluid (fluid );
162+ }
163+ case PHANTOM_SCROLL -> tryScrollPhantom (MouseData .readPacket (buf ));
164+ case LOCK_FLUID -> lockFluid (NetworkUtils .readFluidStack (buf ));
165+ }
166+ }
167+
168+ public void tryClickPhantom (MouseData data ) {
169+ EntityPlayer player = getSyncManager ().getPlayer ();
170+ ItemStack currentStack = player .inventory .getItemStack ();
171+ FluidStack currentFluid = this .tank .getFluid ();
172+ if (currentStack .getCount () > 1 ) currentStack = GTUtility .copy (1 , currentStack );
173+ IFluidHandlerItem fluidHandlerItem = currentStack
174+ .getCapability (CapabilityFluidHandler .FLUID_HANDLER_ITEM_CAPABILITY , null );
175+
176+ switch (data .mouseButton ) {
177+ case 0 -> {
178+ if (currentStack .isEmpty () || fluidHandlerItem == null ) {
179+ if (this .canDrainSlot ()) {
180+ this .tank .drain (data .shift ? Integer .MAX_VALUE : 1000 , true );
181+ }
182+ } else {
183+ FluidStack cellFluid = fluidHandlerItem .drain (Integer .MAX_VALUE , false );
184+ if ((this .showAmount || currentFluid == null ) && cellFluid != null ) {
185+ if (this .canFillSlot ()) {
186+ if (!this .showAmount ) {
187+ cellFluid .amount = 1 ;
188+ }
189+ if (this .tank .fill (cellFluid , true ) > 0 ) {
190+ this .phantomFluid = cellFluid .copy ();
191+ }
192+ }
193+ } else {
194+ if (this .canDrainSlot ()) {
195+ this .tank .drain (data .shift ? Integer .MAX_VALUE : 1000 , true );
196+ }
197+ }
198+ }
199+ }
200+ case 1 -> {
201+ if (this .canFillSlot ()) {
202+ if (currentFluid != null ) {
203+ if (this .showAmount ) {
204+ FluidStack toFill = currentFluid .copy ();
205+ toFill .amount = 1000 ;
206+ this .tank .fill (toFill , true );
207+ }
208+ } else if (this .phantomFluid != null ) {
209+ FluidStack toFill = this .phantomFluid .copy ();
210+ toFill .amount = this .showAmount ? 1 : toFill .amount ;
211+ this .tank .fill (toFill , true );
212+ }
213+ }
214+ }
215+ case 2 -> {
216+ if (currentFluid != null && canDrainSlot ())
217+ this .tank .drain (data .shift ? Integer .MAX_VALUE : 1000 , true );
218+ }
219+ }
220+ }
221+
222+ public void tryScrollPhantom (MouseData mouseData ) {
223+ FluidStack currentFluid = this .tank .getFluid ();
224+ int amount = mouseData .mouseButton ;
225+ if (!this .showAmount ()) {
226+ int newAmt = amount == 1 ? 1 : 0 ;
227+ if (newAmt == 0 ) {
228+ setFluid (null );
229+ return ;
230+ } else if (currentFluid != null && currentFluid .amount != newAmt ) {
231+ setAmount (newAmt );
232+ return ;
233+ }
234+ }
235+ if (mouseData .shift ) {
236+ amount *= 10 ;
237+ }
238+ if (mouseData .ctrl ) {
239+ amount *= 100 ;
240+ }
241+ if (mouseData .alt ) {
242+ amount *= 1000 ;
243+ }
244+ if (currentFluid == null ) {
245+ if (amount > 0 && this .phantomFluid != null ) {
246+ FluidStack toFill = this .phantomFluid .copy ();
247+ toFill .amount = this .showAmount () ? amount : 1 ;
248+ this .tank .fill (toFill , true );
249+ }
250+ return ;
251+ }
252+ if (amount > 0 ) {
253+ FluidStack toFill = currentFluid .copy ();
254+ toFill .amount = amount ;
255+ this .tank .fill (toFill , true );
256+ } else if (amount < 0 ) {
257+ this .tank .drain (-amount , true );
124258 }
125259 }
126260
0 commit comments