Skip to content

Commit b6fcd38

Browse files
committed
Use switch pattern matching wherever possible
1 parent 8ce4b0e commit b6fcd38

File tree

5 files changed

+23
-42
lines changed

5 files changed

+23
-42
lines changed

common/src/main/java/juuxel/adorn/block/property/OptionalProperty.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public Collection<Value<T>> getValues() {
4545

4646
@Override
4747
public String name(Value<T> value) {
48-
// TODO: Use pattern matching
49-
return value instanceof Value.Some<T> some ? some.value.asString() : NONE_NAME;
48+
return value instanceof Value.Some<T>(T inner) ? inner.asString() : NONE_NAME;
5049
}
5150

5251
public EnumProperty<T> getDelegate() {
@@ -76,13 +75,11 @@ public boolean isPresent() {
7675
}
7776

7877
@Override
79-
public int compareTo(OptionalProperty.Value<T> o) {
80-
// TODO: Use pattern matching
81-
if (o instanceof Some<T> other) {
82-
return value.compareTo(other.value);
83-
}
84-
85-
return 1;
78+
public int compareTo(Value<T> o) {
79+
return switch (o) {
80+
case Some<T>(var otherValue) -> value.compareTo(otherValue);
81+
case None<T> none -> 1;
82+
};
8683
}
8784
}
8885

@@ -98,7 +95,7 @@ public boolean isPresent() {
9895
}
9996

10097
@Override
101-
public int compareTo(OptionalProperty.Value<T> o) {
98+
public int compareTo(Value<T> o) {
10299
return o instanceof None<T> ? 0 : -1;
103100
}
104101
}

common/src/main/java/juuxel/adorn/compat/emi/AdornEmiPlugin.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
import juuxel.adorn.recipe.AdornRecipes;
1313
import juuxel.adorn.recipe.FluidBrewingRecipe;
1414
import juuxel.adorn.recipe.ItemBrewingRecipe;
15-
import juuxel.adorn.util.Logging;
16-
import org.slf4j.Logger;
1715

1816
@EmiEntrypoint
1917
public final class AdornEmiPlugin implements EmiPlugin {
20-
private static final Logger LOGGER = Logging.logger();
21-
2218
public static final EmiRecipeCategory BREWER_CATEGORY = new EmiRecipeCategory(
2319
AdornCommon.id("brewer"),
2420
EmiStack.of(AdornBlocks.INSTANCE.getBREWER()),
@@ -33,16 +29,10 @@ public void register(EmiRegistry registry) {
3329
var recipeManager = registry.getRecipeManager();
3430

3531
for (var entry : recipeManager.listAllOfType(AdornRecipes.BREWING_TYPE.get())) {
36-
BrewingEmiRecipe emiRecipe;
37-
// TODO: Pattern matching
38-
if (entry.value() instanceof ItemBrewingRecipe recipe) {
39-
emiRecipe = new BrewingEmiRecipe(entry.id(), recipe);
40-
} else if (entry.value() instanceof FluidBrewingRecipe recipe) {
41-
emiRecipe = new BrewingEmiRecipe(entry.id(), recipe);
42-
} else {
43-
LOGGER.error("Unknown brewing recipe: {}", entry.value());
44-
continue;
45-
}
32+
BrewingEmiRecipe emiRecipe = switch (entry.value()) {
33+
case ItemBrewingRecipe recipe -> new BrewingEmiRecipe(entry.id(), recipe);
34+
case FluidBrewingRecipe recipe -> new BrewingEmiRecipe(entry.id(), recipe);
35+
};
4636

4737
registry.addRecipe(emiRecipe);
4838
}

common/src/main/java/juuxel/adorn/compat/jei/BrewerCategory.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,15 @@ public void setRecipe(IRecipeLayoutBuilder layoutBuilder, BrewingRecipe recipe,
6969
.setFluidRenderer(capacity, false, 16, BrewerScreen.FLUID_AREA_HEIGHT)
7070
.setOverlay(guiHelper.createDrawable(TEXTURE, 154, 17, 16, BrewerScreen.FLUID_AREA_HEIGHT), 0, 0);
7171

72-
if (recipe instanceof ItemBrewingRecipe r) {
73-
firstSlot.addIngredients(r.firstIngredient());
74-
secondSlot.addIngredients(r.secondIngredient());
75-
resultSlot.addItemStack(r.result());
76-
} else if (recipe instanceof FluidBrewingRecipe r) {
77-
firstSlot.addIngredients(r.firstIngredient());
78-
secondSlot.addIngredients(r.secondIngredient());
79-
resultSlot.addItemStack(r.result());
72+
if (recipe instanceof ItemBrewingRecipe(var firstIngredient, var secondIngredient, var result)) {
73+
firstSlot.addIngredients(firstIngredient);
74+
secondSlot.addIngredients(secondIngredient);
75+
resultSlot.addItemStack(result);
76+
} else if (recipe instanceof FluidBrewingRecipe(var firstIngredient, var secondIngredient, var ingredient, var result)) {
77+
firstSlot.addIngredients(firstIngredient);
78+
secondSlot.addIngredients(secondIngredient);
79+
resultSlot.addItemStack(result);
8080

81-
var ingredient = r.fluid();
8281
var amount = FluidUnit.convert(ingredient.getAmount(), ingredient.getUnit(), FluidBridge.get().getFluidUnit());
8382
for (Fluid fluid : ingredient.fluid().getFluids()) {
8483
tank.addFluidStack(fluid, amount, ingredient.nbt());

common/src/main/java/juuxel/adorn/fluid/FluidKeyImpl.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,9 @@ public String toString() {
5151
).xmap(
5252
// TODO (Java 21): Use pattern matching
5353
either -> either.map(Function.identity(), Function.identity()),
54-
key -> {
55-
if (key instanceof Simple simple) {
56-
return Either.left(simple);
57-
} else if (key instanceof OfArray ofArray) {
58-
return Either.right(ofArray);
59-
} else {
60-
throw new IllegalArgumentException();
61-
}
54+
key -> switch (key) {
55+
case Simple simple -> Either.left(simple);
56+
case OfArray ofArray -> Either.right(ofArray);
6257
}
6358
);
6459

common/src/main/java/juuxel/adorn/recipe/BrewingRecipe.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import net.minecraft.recipe.Recipe;
44
import net.minecraft.recipe.RecipeType;
55

6-
public interface BrewingRecipe extends Recipe<BrewerInventory> {
6+
public sealed interface BrewingRecipe extends Recipe<BrewerInventory> permits FluidBrewingRecipe, ItemBrewingRecipe {
77
@Override
88
default RecipeType<?> getType() {
99
return AdornRecipes.BREWING_TYPE.get();

0 commit comments

Comments
 (0)