Skip to content

Commit

Permalink
Use switch pattern matching wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Jul 29, 2024
1 parent 8ce4b0e commit b6fcd38
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public Collection<Value<T>> getValues() {

@Override
public String name(Value<T> value) {
// TODO: Use pattern matching
return value instanceof Value.Some<T> some ? some.value.asString() : NONE_NAME;
return value instanceof Value.Some<T>(T inner) ? inner.asString() : NONE_NAME;
}

public EnumProperty<T> getDelegate() {
Expand Down Expand Up @@ -76,13 +75,11 @@ public boolean isPresent() {
}

@Override
public int compareTo(OptionalProperty.Value<T> o) {
// TODO: Use pattern matching
if (o instanceof Some<T> other) {
return value.compareTo(other.value);
}

return 1;
public int compareTo(Value<T> o) {
return switch (o) {
case Some<T>(var otherValue) -> value.compareTo(otherValue);
case None<T> none -> 1;
};
}
}

Expand All @@ -98,7 +95,7 @@ public boolean isPresent() {
}

@Override
public int compareTo(OptionalProperty.Value<T> o) {
public int compareTo(Value<T> o) {
return o instanceof None<T> ? 0 : -1;
}
}
Expand Down
18 changes: 4 additions & 14 deletions common/src/main/java/juuxel/adorn/compat/emi/AdornEmiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
import juuxel.adorn.recipe.AdornRecipes;
import juuxel.adorn.recipe.FluidBrewingRecipe;
import juuxel.adorn.recipe.ItemBrewingRecipe;
import juuxel.adorn.util.Logging;
import org.slf4j.Logger;

@EmiEntrypoint
public final class AdornEmiPlugin implements EmiPlugin {
private static final Logger LOGGER = Logging.logger();

public static final EmiRecipeCategory BREWER_CATEGORY = new EmiRecipeCategory(
AdornCommon.id("brewer"),
EmiStack.of(AdornBlocks.INSTANCE.getBREWER()),
Expand All @@ -33,16 +29,10 @@ public void register(EmiRegistry registry) {
var recipeManager = registry.getRecipeManager();

for (var entry : recipeManager.listAllOfType(AdornRecipes.BREWING_TYPE.get())) {
BrewingEmiRecipe emiRecipe;
// TODO: Pattern matching
if (entry.value() instanceof ItemBrewingRecipe recipe) {
emiRecipe = new BrewingEmiRecipe(entry.id(), recipe);
} else if (entry.value() instanceof FluidBrewingRecipe recipe) {
emiRecipe = new BrewingEmiRecipe(entry.id(), recipe);
} else {
LOGGER.error("Unknown brewing recipe: {}", entry.value());
continue;
}
BrewingEmiRecipe emiRecipe = switch (entry.value()) {
case ItemBrewingRecipe recipe -> new BrewingEmiRecipe(entry.id(), recipe);
case FluidBrewingRecipe recipe -> new BrewingEmiRecipe(entry.id(), recipe);
};

registry.addRecipe(emiRecipe);
}
Expand Down
17 changes: 8 additions & 9 deletions common/src/main/java/juuxel/adorn/compat/jei/BrewerCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,15 @@ public void setRecipe(IRecipeLayoutBuilder layoutBuilder, BrewingRecipe recipe,
.setFluidRenderer(capacity, false, 16, BrewerScreen.FLUID_AREA_HEIGHT)
.setOverlay(guiHelper.createDrawable(TEXTURE, 154, 17, 16, BrewerScreen.FLUID_AREA_HEIGHT), 0, 0);

if (recipe instanceof ItemBrewingRecipe r) {
firstSlot.addIngredients(r.firstIngredient());
secondSlot.addIngredients(r.secondIngredient());
resultSlot.addItemStack(r.result());
} else if (recipe instanceof FluidBrewingRecipe r) {
firstSlot.addIngredients(r.firstIngredient());
secondSlot.addIngredients(r.secondIngredient());
resultSlot.addItemStack(r.result());
if (recipe instanceof ItemBrewingRecipe(var firstIngredient, var secondIngredient, var result)) {
firstSlot.addIngredients(firstIngredient);
secondSlot.addIngredients(secondIngredient);
resultSlot.addItemStack(result);
} else if (recipe instanceof FluidBrewingRecipe(var firstIngredient, var secondIngredient, var ingredient, var result)) {
firstSlot.addIngredients(firstIngredient);
secondSlot.addIngredients(secondIngredient);
resultSlot.addItemStack(result);

var ingredient = r.fluid();
var amount = FluidUnit.convert(ingredient.getAmount(), ingredient.getUnit(), FluidBridge.get().getFluidUnit());
for (Fluid fluid : ingredient.fluid().getFluids()) {
tank.addFluidStack(fluid, amount, ingredient.nbt());
Expand Down
11 changes: 3 additions & 8 deletions common/src/main/java/juuxel/adorn/fluid/FluidKeyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ public String toString() {
).xmap(
// TODO (Java 21): Use pattern matching
either -> either.map(Function.identity(), Function.identity()),
key -> {
if (key instanceof Simple simple) {
return Either.left(simple);
} else if (key instanceof OfArray ofArray) {
return Either.right(ofArray);
} else {
throw new IllegalArgumentException();
}
key -> switch (key) {
case Simple simple -> Either.left(simple);
case OfArray ofArray -> Either.right(ofArray);
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeType;

public interface BrewingRecipe extends Recipe<BrewerInventory> {
public sealed interface BrewingRecipe extends Recipe<BrewerInventory> permits FluidBrewingRecipe, ItemBrewingRecipe {
@Override
default RecipeType<?> getType() {
return AdornRecipes.BREWING_TYPE.get();
Expand Down

0 comments on commit b6fcd38

Please sign in to comment.