Skip to content

Commit a961f30

Browse files
committed
Merge branch 'development-1.16.3' of https://github.com/Mixinors/Astromine into development-1.16.3
2 parents 4efb4f5 + 4c7eca1 commit a961f30

File tree

6 files changed

+250
-97
lines changed

6 files changed

+250
-97
lines changed

astromine-core/src/main/java/com/github/chainmailstudios/astromine/common/widget/blade/FluidVerticalBarWidget.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public void setVolume(Supplier<FluidVolume> volume) {
7676
@Environment(EnvType.CLIENT)
7777
@Override
7878
public List<Text> getTooltip() {
79-
return Lists.newArrayList(FluidUtilities.rawFraction(progressFraction.get(), limitFraction.get(), new TranslatableText("text.astromine.fluid")), new TranslatableText("text.astromine.tooltip.fractional_value", progressFraction.get().toDecimalString(), limitFraction.get()
79+
Identifier fluidId = getFluidVolume().getFluidId();
80+
return Lists.newArrayList(FluidUtilities.rawFraction(progressFraction.get(), limitFraction.get(), new TranslatableText(String.format("block.%s.%s", fluidId.getNamespace(), fluidId.getPath()))), new TranslatableText("text.astromine.tooltip.fractional_value", progressFraction.get().toDecimalString(), limitFraction.get()
8081
.toDecimalString()));
8182
}
8283

astromine-technologies/src/main/java/com/github/chainmailstudios/astromine/technologies/common/block/LiquidGeneratorBlock.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,55 @@
2424

2525
package com.github.chainmailstudios.astromine.technologies.common.block;
2626

27+
import com.github.chainmailstudios.astromine.common.block.base.WrenchableHorizontalFacingTieredBlockWithEntity;
28+
import com.github.chainmailstudios.astromine.common.volume.fluid.FluidVolume;
29+
import com.github.chainmailstudios.astromine.common.volume.fraction.Fraction;
30+
import com.github.chainmailstudios.astromine.technologies.common.block.entity.LiquidGeneratorBlockEntity;
31+
import com.github.chainmailstudios.astromine.technologies.common.screenhandler.LiquidGeneratorScreenHandler;
2732
import net.minecraft.block.BlockState;
2833
import net.minecraft.block.entity.BlockEntity;
2934
import net.minecraft.entity.player.PlayerEntity;
3035
import net.minecraft.entity.player.PlayerInventory;
36+
import net.minecraft.fluid.Fluid;
37+
import net.minecraft.item.BucketItem;
38+
import net.minecraft.item.Item;
39+
import net.minecraft.item.ItemStack;
40+
import net.minecraft.item.Items;
3141
import net.minecraft.network.PacketByteBuf;
3242
import net.minecraft.screen.ScreenHandler;
3343
import net.minecraft.server.network.ServerPlayerEntity;
44+
import net.minecraft.util.ActionResult;
45+
import net.minecraft.util.Hand;
46+
import net.minecraft.util.hit.BlockHitResult;
3447
import net.minecraft.util.math.BlockPos;
3548
import net.minecraft.world.World;
3649

37-
import com.github.chainmailstudios.astromine.common.block.base.WrenchableHorizontalFacingTieredBlockWithEntity;
38-
import com.github.chainmailstudios.astromine.technologies.common.block.entity.LiquidGeneratorBlockEntity;
39-
import com.github.chainmailstudios.astromine.technologies.common.screenhandler.LiquidGeneratorScreenHandler;
40-
4150
public abstract class LiquidGeneratorBlock extends WrenchableHorizontalFacingTieredBlockWithEntity {
4251
public LiquidGeneratorBlock(Settings settings) {
4352
super(settings);
4453
}
4554

55+
@Override
56+
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
57+
ItemStack stack = player.getStackInHand(hand);
58+
Item item = stack.getItem();
59+
if (item instanceof BucketItem && item != Items.BUCKET) {
60+
BlockEntity blockEntity = world.getBlockEntity(pos);
61+
if(blockEntity instanceof LiquidGeneratorBlockEntity){
62+
Fluid bucketFluid = ((BucketItem)item).fluid;
63+
LiquidGeneratorBlockEntity generatorEntity = (LiquidGeneratorBlockEntity) blockEntity;
64+
FluidVolume volume = generatorEntity.getFluidComponent().getVolume(0);
65+
if(volume.canAccept(bucketFluid) && generatorEntity.getBurningRecipeFor(bucketFluid).isPresent()){
66+
volume.setFluid(bucketFluid);
67+
volume.add(Fraction.bucket());
68+
}
69+
player.setStackInHand(hand, new ItemStack(Items.BUCKET));
70+
return ActionResult.SUCCESS;
71+
}
72+
}
73+
return super.onUse(state, world, pos, player, hand, hit);
74+
}
75+
4676
public abstract static class Base extends LiquidGeneratorBlock {
4777
public Base(Settings settings) {
4878
super(settings);

astromine-technologies/src/main/java/com/github/chainmailstudios/astromine/technologies/common/block/entity/FluidMixerBlockEntity.java

Lines changed: 105 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.github.chainmailstudios.astromine.common.component.inventory.FluidInventoryComponent;
3030
import com.github.chainmailstudios.astromine.common.component.inventory.SimpleEnergyInventoryComponent;
3131
import com.github.chainmailstudios.astromine.common.component.inventory.SimpleFluidInventoryComponent;
32-
import com.github.chainmailstudios.astromine.common.inventory.BaseInventory;
3332
import com.github.chainmailstudios.astromine.common.utilities.tier.MachineTier;
3433
import com.github.chainmailstudios.astromine.common.volume.energy.EnergyVolume;
3534
import com.github.chainmailstudios.astromine.common.volume.fluid.FluidVolume;
@@ -41,16 +40,13 @@
4140
import com.github.chainmailstudios.astromine.technologies.common.block.entity.machine.SpeedProvider;
4241
import com.github.chainmailstudios.astromine.technologies.common.block.entity.machine.TierProvider;
4342
import com.github.chainmailstudios.astromine.technologies.common.recipe.FluidMixingRecipe;
44-
import com.github.chainmailstudios.astromine.technologies.common.recipe.LiquidGeneratingRecipe;
4543
import com.github.chainmailstudios.astromine.technologies.registry.AstromineTechnologiesBlockEntityTypes;
4644
import com.github.chainmailstudios.astromine.technologies.registry.AstromineTechnologiesBlocks;
47-
import com.github.chainmailstudios.astromine.technologies.registry.AstromineTechnologiesRecipeSerializers;
4845
import net.minecraft.block.Block;
4946
import net.minecraft.block.BlockState;
5047
import net.minecraft.block.entity.BlockEntityType;
48+
import net.minecraft.fluid.Fluid;
5149
import net.minecraft.nbt.CompoundTag;
52-
import net.minecraft.recipe.RecipeType;
53-
import net.minecraft.recipe.SmeltingRecipe;
5450
import org.jetbrains.annotations.NotNull;
5551

5652
import java.util.Optional;
@@ -72,48 +68,122 @@ protected EnergyInventoryComponent createEnergyComponent() {
7268
return new SimpleEnergyInventoryComponent(getEnergySize());
7369
}
7470

71+
/**
72+
* Determine whether the recipe matches the provided fluids.
73+
* The existing fluid may be null, indicating that the inserting fluid is the only one present in the system
74+
*
75+
* @param r
76+
* the recipe to match against
77+
* @param existing
78+
* the fluid already present in the machine. may be null.
79+
* @param inserting
80+
* the newly inserted fluid.
81+
* @return truthy if a recipe matching the provided fluids exists, falsy if no such recipe exists
82+
*/
83+
private static boolean isRecipeValid(FluidMixingRecipe r, Fluid existing, Fluid inserting) {
84+
if (r.getFirstInputFluid() == inserting)
85+
return true;
86+
else if (r.getFirstInputFluid() != existing)
87+
return false;
88+
89+
if (r.getSecondInputFluid() == inserting)
90+
return true;
91+
else if (r.getSecondInputFluid() != existing)
92+
return false;
93+
return false;
94+
}
95+
96+
/**
97+
* Determine whether a recipe exists such that its input criteria are fulfilled by the provided fluids.
98+
* The existing fluid may be null, indicating that the inserting fluid is the only one present in the system
99+
*
100+
* @param existing
101+
* the fluid already present in the machine. may be null.
102+
* @param inserting
103+
* the newly inserted fluid.
104+
* @return truthy if a recipe matching the provided fluids exists, falsy if no such recipe exists
105+
*/
106+
private boolean hasMatchingRecipe(Fluid existing, Fluid inserting) {
107+
return world.getRecipeManager().getAllOfType(FluidMixingRecipe.Type.INSTANCE).values().stream().filter(recipe -> recipe instanceof FluidMixingRecipe).anyMatch(recipe -> isRecipeValid((FluidMixingRecipe) recipe, existing, inserting));
108+
}
109+
110+
/**
111+
* Find a slot already filled with the matching fluid
112+
*
113+
* @param target
114+
* the fluid to find
115+
* @return -1 if no such slot exists, otherwise the 0-based index of the slot.
116+
*/
117+
private int findMergeableSlot(Fluid target) {
118+
for (int i = 0; i < 2; i++) {
119+
FluidVolume v = fluidComponent.getVolume(i);
120+
if (v == null)
121+
continue;
122+
if (v.getFluid() == target) {
123+
return i;
124+
}
125+
}
126+
return -1;
127+
}
128+
75129
@Override
76130
protected FluidInventoryComponent createFluidComponent() {
77-
FluidInventoryComponent fluidComponent = new SimpleFluidInventoryComponent(3)
78-
.withInsertPredicate((direction, volume, slot) -> {
79-
if (slot != 0 && slot != 1) {
80-
return false;
81-
}
131+
FluidInventoryComponent fC = new SimpleFluidInventoryComponent(3).withInsertPredicate((direction, volume, slot) -> {
132+
if (slot != 0 && slot != 1) {
133+
return false;
134+
}
82135

83-
FluidInventoryComponent inventory = new SimpleFluidInventoryComponent(2);
136+
Fluid insertedFluid = volume.getFluid();
137+
int existingSlot = findMergeableSlot(insertedFluid);
84138

85-
inventory.setVolume(0, volume);
86-
inventory.setVolume(1, FluidHandler.of(this).getSecond());
87-
inventory.setVolume(2, FluidHandler.of(this).getThird());
139+
// Allow merging into existing fluids, assuming a valid recipe
140+
if (existingSlot == slot) {
141+
return true;
142+
}
88143

89-
if (world != null) {
90-
optionalRecipe = (Optional) world.getRecipeManager().getAllOfType(FluidMixingRecipe.Type.INSTANCE).values().stream().filter(recipe -> recipe instanceof FluidMixingRecipe).filter(recipe -> ((FluidMixingRecipe) recipe).matches(inventory)).findFirst();
91-
return optionalRecipe.isPresent();
92-
}
144+
// Don't allow overflow in the second input, if the first input is full
145+
if (existingSlot != -1) {
146+
return false;
147+
}
148+
149+
Fluid otherFluid = null;
150+
for (int i = 0; i < 2; i++) {
151+
FluidVolume v = fluidComponent.getVolume(i);
152+
if (v == null)
153+
continue;
154+
if (!v.isEmpty()) {
155+
// No space left
156+
if (otherFluid != null)
157+
return false;
158+
otherFluid = v.getFluid();
159+
}
160+
}
161+
162+
return hasMatchingRecipe(otherFluid, insertedFluid);
163+
}).withExtractPredicate((direction, volume, slot) -> {
164+
return slot == 2;
165+
}).withListener((inventory) -> {
166+
shouldTry = true;
167+
progress = 0;
168+
limit = 100;
169+
optionalRecipe = Optional.empty();
170+
});
171+
172+
FluidHandler.of(fC).getFirst().setSize(getFluidSize());
173+
FluidHandler.of(fC).getSecond().setSize(getFluidSize());
174+
FluidHandler.of(fC).getThird().setSize(getFluidSize());
93175

94-
return false;
95-
}).withExtractPredicate((direction, volume, slot) -> {
96-
return slot == 2;
97-
}).withListener((inventory) -> {
98-
shouldTry = true;
99-
progress = 0;
100-
limit = 100;
101-
optionalRecipe = Optional.empty();
102-
});
103-
104-
FluidHandler.of(fluidComponent).getFirst().setSize(getFluidSize());
105-
FluidHandler.of(fluidComponent).getSecond().setSize(getFluidSize());
106-
FluidHandler.of(fluidComponent).getThird().setSize(getFluidSize());
107-
108-
return fluidComponent;
176+
return fC;
109177
}
110178

111179
@Override
112180
public void tick() {
113181
super.tick();
114182

115-
if (world == null) return;
116-
if (world.isClient) return;
183+
if (world == null)
184+
return;
185+
if (world.isClient)
186+
return;
117187

118188
FluidHandler.ofOptional(this).ifPresent(fluids -> {
119189
EnergyVolume energyVolume = getEnergyComponent().getVolume();

astromine-technologies/src/main/java/com/github/chainmailstudios/astromine/technologies/common/block/entity/LiquidGeneratorBlockEntity.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
import com.github.chainmailstudios.astromine.technologies.common.block.entity.machine.FluidSizeProvider;
3939
import com.github.chainmailstudios.astromine.technologies.common.block.entity.machine.SpeedProvider;
4040
import com.github.chainmailstudios.astromine.technologies.common.block.entity.machine.TierProvider;
41-
import com.github.chainmailstudios.astromine.technologies.common.recipe.ElectrolyzingRecipe;
4241
import com.github.chainmailstudios.astromine.technologies.common.recipe.LiquidGeneratingRecipe;
4342
import com.github.chainmailstudios.astromine.technologies.registry.AstromineTechnologiesBlockEntityTypes;
4443
import com.github.chainmailstudios.astromine.technologies.registry.AstromineTechnologiesBlocks;
4544
import net.minecraft.block.Block;
4645
import net.minecraft.block.BlockState;
4746
import net.minecraft.block.entity.BlockEntityType;
47+
import net.minecraft.fluid.Fluid;
4848
import net.minecraft.nbt.CompoundTag;
4949
import org.jetbrains.annotations.NotNull;
5050

@@ -66,6 +66,15 @@ protected EnergyInventoryComponent createEnergyComponent() {
6666
return new SimpleEnergyInventoryComponent(getEnergySize());
6767
}
6868

69+
public Optional<LiquidGeneratingRecipe> getBurningRecipeFor(Fluid f) {
70+
return world.getRecipeManager()
71+
.getAllOfType(LiquidGeneratingRecipe.Type.INSTANCE).values().stream()
72+
.filter(recipe -> recipe instanceof LiquidGeneratingRecipe)
73+
.map(recipe -> ((LiquidGeneratingRecipe) recipe))
74+
.filter(recipe -> recipe.getFluid() == f)
75+
.findAny();
76+
}
77+
6978
@Override
7079
protected FluidInventoryComponent createFluidComponent() {
7180
FluidInventoryComponent fluidComponent = new SimpleFluidInventoryComponent(1)
@@ -74,19 +83,10 @@ protected FluidInventoryComponent createFluidComponent() {
7483
return false;
7584
}
7685

77-
FluidInventoryComponent inventory = new SimpleFluidInventoryComponent(1);
78-
79-
inventory.setVolume(0, volume);
80-
81-
if (world != null) {
82-
optionalRecipe = (Optional) world.getRecipeManager().getAllOfType(LiquidGeneratingRecipe.Type.INSTANCE).values().stream().filter(recipe -> recipe instanceof LiquidGeneratingRecipe).filter(recipe -> ((LiquidGeneratingRecipe) recipe).matches(inventory)).findFirst();
83-
return optionalRecipe.isPresent();
84-
}
85-
86-
return false;
87-
}).withExtractPredicate((direction, volume, slot) -> {
88-
return false;
89-
}).withListener((inventory) -> {
86+
if (world == null) return false;
87+
return getBurningRecipeFor(volume.getFluid()).isPresent();
88+
}).withExtractPredicate((direction, volume, slot) -> false)
89+
.withListener((inventory) -> {
9090
shouldTry = true;
9191
progress = 0;
9292
limit = 100;
@@ -108,7 +108,7 @@ public void tick() {
108108
FluidHandler.ofOptional(this).ifPresent(fluids -> {
109109
EnergyVolume energyVolume = getEnergyComponent().getVolume();
110110
if (!optionalRecipe.isPresent() && shouldTry) {
111-
optionalRecipe = (Optional) world.getRecipeManager().getAllOfType(LiquidGeneratingRecipe.Type.INSTANCE).values().stream().filter(recipe -> recipe instanceof LiquidGeneratingRecipe).filter(recipe -> ((LiquidGeneratingRecipe) recipe).matches(fluidComponent)).findFirst();
111+
optionalRecipe = getBurningRecipeFor(fluidComponent.getVolume(0).getFluid());
112112
shouldTry = false;
113113
}
114114

0 commit comments

Comments
 (0)