Skip to content

Commit

Permalink
cleanup spilling fluid manager contains and find
Browse files Browse the repository at this point in the history
Should fix any potential bugs from #4907
  • Loading branch information
KnightMiner committed Jun 12, 2022
1 parent 56abd36 commit 4cacbc1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,20 @@ void updateFromServer(List<SpillingFluid> fluids) {
}

/** Finds a fluid without checking the cache, returns null if missing */
@Nullable
private SpillingFluid findUncached(Fluid fluid) {
// find all severing recipes for the entity
for (SpillingFluid recipe : fluids) {
if (recipe.matches(fluid)) {
cache.put(fluid, recipe);
return recipe;
}
}
// cache null if nothing
cache.put(fluid, null);
return null;
return EMPTY;
}

/** Checks if the given fluid has a recipe */
public boolean contains(Fluid fluid) {
if (cache.containsKey(fluid)) {
return cache.get(fluid) != null;
}
return findUncached(fluid) != null;
return find(fluid).hasEffects();
}

/**
Expand All @@ -143,9 +137,6 @@ public boolean contains(Fluid fluid) {
* @return Fluid, or empty if none exists
*/
public SpillingFluid find(Fluid fluid) {
if (cache.containsKey(fluid)) {
return cache.getOrDefault(fluid, EMPTY);
}
return Objects.requireNonNullElse(findUncached(fluid), EMPTY);
return cache.computeIfAbsent(fluid, this::findUncached);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package slimeknights.tconstruct.library.modifiers.spilling;

import com.google.common.collect.ImmutableList;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import lombok.RequiredArgsConstructor;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.network.NetworkEvent.Context;
import slimeknights.mantle.network.packet.ISimplePacket;
import slimeknights.mantle.recipe.ingredient.FluidIngredient;
import slimeknights.tconstruct.library.tools.context.ToolAttackContext;

import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -19,8 +24,9 @@ public class UpdateSpillingFluidsPacket implements ISimplePacket {
public UpdateSpillingFluidsPacket(FriendlyByteBuf buf) {
int size = buf.readVarInt();
ImmutableList.Builder<SpillingFluid> fluids = ImmutableList.builder();
List<ISpillingEffect> effects = Collections.singletonList(NoEffect.INSTANCE); // list with a single effect for the client
for (int i = 0; i < size; i++) {
fluids.add(new SpillingFluid(FluidIngredient.read(buf)));
fluids.add(new SpillingFluid(FluidIngredient.read(buf), effects));
}
this.fluids = fluids.build();
}
Expand All @@ -37,4 +43,16 @@ public void encode(FriendlyByteBuf buf) {
public void handle(Supplier<Context> context) {
SpillingFluidManager.INSTANCE.updateFromServer(fluids);
}

private static class NoEffect implements ISpillingEffect {
private static final ISpillingEffect INSTANCE = new NoEffect();

@Override
public void applyEffects(FluidStack fluid, float scale, ToolAttackContext context) {}

@Override
public JsonObject serialize(JsonSerializationContext context) {
throw new UnsupportedOperationException("Cannot serialize spilling fluids on the client");
}
}
}

0 comments on commit 4cacbc1

Please sign in to comment.