3030import com .github .chainmailstudios .astromine .common .component .inventory .SimpleFluidInventoryComponent ;
3131import com .github .chainmailstudios .astromine .common .component .inventory .SimpleItemInventoryComponent ;
3232import com .github .chainmailstudios .astromine .common .utilities .tier .MachineTier ;
33- import com .github .chainmailstudios .astromine .common .volume .fraction .Fraction ;
3433import com .github .chainmailstudios .astromine .common .volume .fluid .FluidVolume ;
34+ import com .github .chainmailstudios .astromine .common .volume .fraction .Fraction ;
3535import com .github .chainmailstudios .astromine .common .volume .handler .FluidHandler ;
3636import com .github .chainmailstudios .astromine .common .volume .handler .ItemHandler ;
37+ import com .github .chainmailstudios .astromine .registry .AstromineConfig ;
3738import com .github .chainmailstudios .astromine .technologies .common .block .entity .machine .FluidSizeProvider ;
3839import com .github .chainmailstudios .astromine .technologies .common .block .entity .machine .SpeedProvider ;
3940import com .github .chainmailstudios .astromine .technologies .common .block .entity .machine .TierProvider ;
4041import com .github .chainmailstudios .astromine .technologies .registry .AstromineTechnologiesBlockEntityTypes ;
41- import com .github .chainmailstudios .astromine .registry .AstromineConfig ;
4242import net .minecraft .block .entity .BlockEntityType ;
43+ import net .minecraft .fluid .Fluid ;
4344import net .minecraft .item .BucketItem ;
45+ import net .minecraft .item .Item ;
4446import net .minecraft .item .ItemStack ;
4547import net .minecraft .item .Items ;
4648
49+ import java .util .Optional ;
50+
4751public abstract class TankBlockEntity extends ComponentFluidInventoryBlockEntity implements TierProvider , FluidSizeProvider , SpeedProvider {
4852 public TankBlockEntity (BlockEntityType <?> type ) {
4953 super (type );
@@ -61,52 +65,99 @@ protected ItemInventoryComponent createItemComponent() {
6165 return new SimpleItemInventoryComponent (2 );
6266 }
6367
68+ // return true to consume bucket contents, false to not consume
69+ private boolean handleBucketInput (FluidVolume currentTank , Fluid bucketFluid ) {
70+ if (getMachineTier () == MachineTier .CREATIVE ) {
71+ currentTank .setFluid (bucketFluid );
72+ } else if (currentTank .canAccept (bucketFluid ) && currentTank .hasAvailable (Fraction .bucket ())) {
73+ currentTank .setFluid (bucketFluid );
74+ currentTank .add (Fraction .bucket ());
75+ return true ;
76+ }
77+ return false ;
78+ }
79+
80+ private ItemStack handleLeftItem (FluidVolume currentTank , ItemStack inputStack ) {
81+ Item inputItem = inputStack .getItem ();
82+ if (inputItem == Items .BUCKET )
83+ return inputStack ; // Do not pull from empty buckets
84+ if (inputStack .getCount () != 1 )
85+ return inputStack ; // Do not operate on multiple items at once (per slot)
86+ if (inputItem instanceof BucketItem ) {
87+ // Handle fluid items manually, since operations have to be exactly one Fraction.bucket() at a time.
88+ Fluid f = ((BucketItem ) inputItem ).fluid ;
89+ if (handleBucketInput (currentTank , f ))
90+ return new ItemStack (Items .BUCKET );
91+ else return inputStack ;
92+ }
93+ Optional <FluidHandler > opt = FluidHandler .ofOptional (inputStack );
94+ if (!opt .isPresent ())
95+ return inputStack ; // Reject non fluid container items
96+ FluidHandler stackFluids = opt .get ();
97+ FluidVolume stackVolume = stackFluids .getFirst ();
98+ if (stackVolume == null )
99+ return inputStack ; // Do not operate on null fluids
100+ if (stackVolume .isEmpty ())
101+ return inputStack ; // Do not operate on empty fluid containers
102+
103+ if (getMachineTier () == MachineTier .CREATIVE ) {
104+ // Creative tanks just copy the fluid without consuming or space checks
105+ currentTank .setFluid (stackVolume .getFluid ());
106+ return inputStack ;
107+ }
108+
109+ // Fluid type is already checked in moveFrom
110+ currentTank .moveFrom (stackVolume , Fraction .ofDecimal (getMachineSpeed ()));
111+ return inputStack ;
112+ }
113+
114+ private ItemStack handleRightItem (FluidVolume currentTank , ItemStack outputStack ) {
115+ Item outputItem = outputStack .getItem ();
116+ if (currentTank .isEmpty ())
117+ return outputStack ; // Do not operate if we are empty
118+ if (outputStack .getCount () != 1 )
119+ return outputStack ; // Do not operate on multiple Items
120+ if (outputItem instanceof BucketItem ) {
121+ if (outputItem != Items .BUCKET )
122+ return outputStack ; // Do not insert into filled buckets
123+ if (currentTank .hasStored (Fraction .bucket ())) {
124+ currentTank .minus (Fraction .bucket ());
125+ return new ItemStack (currentTank .getFluid ().getBucketItem ());
126+ }
127+ return outputStack ;
128+ }
129+ Optional <FluidHandler > opt = FluidHandler .ofOptional (outputStack );
130+ if (!opt .isPresent ())
131+ return outputStack ; // Reject non fluid container items
132+ FluidHandler stackFluids = opt .get ();
133+ FluidVolume stackVolume = stackFluids .getFirst ();
134+
135+ stackVolume .moveFrom (currentTank , Fraction .ofDecimal (getMachineSpeed ()));
136+
137+ return outputStack ;
138+ }
139+
64140 @ Override
65141 public void tick () {
66142 super .tick ();
67143
68- if (world == null ) return ;
69- if (world .isClient ) return ;
144+ if (world == null )
145+ return ;
146+ if (world .isClient )
147+ return ;
70148
71149 FluidHandler .ofOptional (this ).ifPresent (fluids -> {
150+ FluidVolume ourVolume = fluids .getFirst ();
72151 ItemHandler .ofOptional (this ).ifPresent (items -> {
73- FluidHandler .ofOptional (items .getFirst ()).ifPresent (stackFluids -> {
74- FluidVolume ourVolume = fluids .getFirst ();
75- FluidVolume stackVolume = stackFluids .getFirst ();
76-
77- if (ourVolume .canAccept (stackVolume .getFluid ())) {
78- if (items .getFirst ().getItem () instanceof BucketItem ) {
79- if (items .getFirst ().getItem () != Items .BUCKET && items .getFirst ().getCount () == 1 ) {
80- if (ourVolume .hasAvailable (Fraction .bucket ())) {
81- ourVolume .moveFrom (stackVolume , Fraction .bucket ());
82-
83- items .setFirst (new ItemStack (Items .BUCKET ));
84- }
85- }
86- } else {
87- ourVolume .moveFrom (stackVolume , Fraction .ofDecimal (getMachineSpeed ()));
88- }
89- }
90- });
91-
92- FluidHandler .ofOptional (items .getSecond ()).ifPresent (stackFluids -> {
93- FluidVolume ourVolume = fluids .getFirst ();
94- FluidVolume stackVolume = stackFluids .getFirst ();
95-
96- if (ourVolume .canAccept (stackVolume .getFluid ())) {
97- if (items .getSecond ().getItem () instanceof BucketItem ) {
98- if (items .getSecond ().getItem () == Items .BUCKET && items .getSecond ().getCount () == 1 ) {
99- if (ourVolume .hasStored (Fraction .bucket ())) {
100- ourVolume .add (stackVolume , Fraction .bucket ());
101-
102- items .setSecond (new ItemStack (stackVolume .getFluid ().getBucketItem ()));
103- }
104- }
105- } else {
106- ourVolume .add (stackVolume , Fraction .ofDecimal (getMachineSpeed ()));
107- }
108- }
109- });
152+ ItemStack leftItem = items .getFirst ();
153+ ItemStack rightItem = items .getSecond ();
154+ ItemStack newLeftItem = handleLeftItem (ourVolume , leftItem );
155+ ItemStack newRightItem = handleRightItem (ourVolume , rightItem );
156+ if (newLeftItem != leftItem )
157+ items .setFirst (newLeftItem );
158+ if (newRightItem != rightItem )
159+ items .setSecond (newRightItem );
160+
110161 });
111162 });
112163 }
0 commit comments