Skip to content

Commit 4748215

Browse files
committed
Make use of new switch statements to allow cleaning up some of our chained instance of if else statements
1 parent 3f15c5d commit 4748215

File tree

21 files changed

+304
-342
lines changed

21 files changed

+304
-342
lines changed

src/api/java/mekanism/api/recipes/inputs/BoxedChemicalInputHandler.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,18 @@ public BoxedChemicalStack getRecipeInput(ChemicalStackIngredient<?, ?> recipeIng
7171
//All recipes currently require that we have an input. If we don't then return that we failed
7272
return BoxedChemicalStack.EMPTY;
7373
}
74-
if (recipeIngredient instanceof GasStackIngredient ingredient) {
75-
if (input.getChemicalType() == ChemicalType.GAS) {
76-
return BoxedChemicalStack.box(ingredient.getMatchingInstance((GasStack) input.getChemicalStack()));
77-
}
78-
} else if (recipeIngredient instanceof InfusionStackIngredient ingredient) {
79-
if (input.getChemicalType() == ChemicalType.INFUSION) {
80-
return BoxedChemicalStack.box(ingredient.getMatchingInstance((InfusionStack) input.getChemicalStack()));
81-
}
82-
} else if (recipeIngredient instanceof PigmentStackIngredient ingredient) {
83-
if (input.getChemicalType() == ChemicalType.PIGMENT) {
84-
return BoxedChemicalStack.box(ingredient.getMatchingInstance((PigmentStack) input.getChemicalStack()));
85-
}
86-
} else if (recipeIngredient instanceof SlurryStackIngredient ingredient) {
87-
if (input.getChemicalType() == ChemicalType.SLURRY) {
88-
return BoxedChemicalStack.box(ingredient.getMatchingInstance((SlurryStack) input.getChemicalStack()));
89-
}
90-
} else {
91-
throw new IllegalStateException("Unknown Chemical Type");
92-
}
93-
//Something went wrong, input doesn't match types with ingredient
94-
return BoxedChemicalStack.EMPTY;
74+
return switch (recipeIngredient) {
75+
case GasStackIngredient ingredient when input.getChemicalType() == ChemicalType.GAS ->
76+
BoxedChemicalStack.box(ingredient.getMatchingInstance((GasStack) input.getChemicalStack()));
77+
case InfusionStackIngredient ingredient when input.getChemicalType() == ChemicalType.INFUSION ->
78+
BoxedChemicalStack.box(ingredient.getMatchingInstance((InfusionStack) input.getChemicalStack()));
79+
case PigmentStackIngredient ingredient when input.getChemicalType() == ChemicalType.PIGMENT ->
80+
BoxedChemicalStack.box(ingredient.getMatchingInstance((PigmentStack) input.getChemicalStack()));
81+
case SlurryStackIngredient ingredient when input.getChemicalType() == ChemicalType.SLURRY ->
82+
BoxedChemicalStack.box(ingredient.getMatchingInstance((SlurryStack) input.getChemicalStack()));
83+
//Something went wrong, input doesn't match types with ingredient
84+
default -> BoxedChemicalStack.EMPTY;
85+
};
9586
}
9687

9788
/**

src/api/java/mekanism/api/text/TextComponentUtil.java

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,43 +59,27 @@ public static MutableComponent build(Object... components) {
5959
continue;
6060
}
6161
MutableComponent current = null;
62-
if (component instanceof IHasTextComponent hasTextComponent) {
63-
current = hasTextComponent.getTextComponent().copy();
64-
} else if (component instanceof IHasTranslationKey hasTranslationKey) {
65-
current = translate(hasTranslationKey.getTranslationKey());
66-
} else if (component instanceof EnumColor color) {
67-
cachedStyle = cachedStyle.withColor(color.getColor());
68-
} else if (component instanceof TextColor color) {
69-
cachedStyle = cachedStyle.withColor(color);
70-
} else if (component instanceof Component c) {
62+
switch (component) {
63+
case IHasTextComponent hasTextComponent -> current = hasTextComponent.getTextComponent().copy();
64+
case IHasTranslationKey hasTranslationKey -> current = translate(hasTranslationKey.getTranslationKey());
65+
case EnumColor color -> cachedStyle = cachedStyle.withColor(color.getColor());
66+
case TextColor color -> cachedStyle = cachedStyle.withColor(color);
7167
//Just append if a text component is being passed
72-
current = c.copy();
73-
} else if (component instanceof ChatFormatting formatting) {
74-
cachedStyle = cachedStyle.applyFormat(formatting);
75-
} else if (component instanceof ClickEvent event) {
76-
cachedStyle = cachedStyle.withClickEvent(event);
77-
} else if (component instanceof HoverEvent event) {
78-
cachedStyle = cachedStyle.withHoverEvent(event);
79-
} else if (component instanceof Block block) {
80-
current = block.getName().copy();
81-
} else if (component instanceof Item item) {
82-
current = item.getDescription().copy();
83-
} else if (component instanceof ItemStack stack) {
84-
current = stack.getHoverName().copy();
85-
} else if (component instanceof FluidStack stack) {
86-
current = stack.getHoverName().copy();
87-
} else if (component instanceof Fluid fluid) {
88-
current = fluid.getFluidType().getDescription().copy();
89-
} else if (component instanceof EntityType<?> entityType) {
90-
current = entityType.getDescription().copy();
91-
} else if (component instanceof Direction direction) {
92-
current = getTranslatedDirection(direction);
93-
} else if (component instanceof Boolean bool) {
94-
current = getTranslatedBoolean(bool);
95-
} else {
68+
case Component c -> current = c.copy();
69+
case ChatFormatting formatting -> cachedStyle = cachedStyle.applyFormat(formatting);
70+
case ClickEvent event -> cachedStyle = cachedStyle.withClickEvent(event);
71+
case HoverEvent event -> cachedStyle = cachedStyle.withHoverEvent(event);
72+
case Block block -> current = block.getName().copy();
73+
case Item item -> current = item.getDescription().copy();
74+
case ItemStack stack -> current = stack.getHoverName().copy();
75+
case FluidStack stack -> current = stack.getHoverName().copy();
76+
case Fluid fluid -> current = fluid.getFluidType().getDescription().copy();
77+
case EntityType<?> entityType -> current = entityType.getDescription().copy();
78+
case Direction direction -> current = getTranslatedDirection(direction);
79+
case Boolean bool -> current = getTranslatedBoolean(bool);
9680
//Fallback to a generic replacement
9781
// this handles strings, numbers, and any type we don't necessarily know about
98-
current = getString(component.toString());
82+
default -> current = getString(component.toString());
9983
}
10084
if (current == null) {
10185
//If we don't have a component to add, don't

src/main/java/mekanism/client/gui/GuiFilterHolder.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ protected void addGuiElements() {
7171

7272
private List<ItemStack> getRenderStacks(@Nullable IFilter<?> filter) {
7373
if (filter != null) {
74-
if (filter instanceof IItemStackFilter<?> itemFilter) {
75-
return List.of(itemFilter.getItemStack());
76-
} else if (filter instanceof ITagFilter<?> tagFilter) {
77-
return getTagStacks(tagFilter.getTagName());
78-
} else if (filter instanceof IModIDFilter<?> modIDFilter) {
79-
return getModIDStacks(modIDFilter.getModID());
80-
}
74+
return switch (filter) {
75+
case IItemStackFilter<?> itemFilter -> List.of(itemFilter.getItemStack());
76+
case ITagFilter<?> tagFilter -> getTagStacks(tagFilter.getTagName());
77+
case IModIDFilter<?> modIDFilter -> getModIDStacks(modIDFilter.getModID());
78+
default -> Collections.emptyList();
79+
};
8180
}
8281
return Collections.emptyList();
8382
}

src/main/java/mekanism/client/gui/element/GuiElement.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,17 +393,12 @@ public ComponentPath nextFocusPath(@NotNull FocusNavigationEvent event) {
393393
return null;
394394
}
395395
if (!isFocused()) {
396-
if (event instanceof ArrowNavigation) {
397-
if (supportsArrowNavigation()) {
398-
return ComponentPath.leaf(this);
399-
}
400-
} else if (event instanceof TabNavigation) {
401-
if (supportsTabNavigation()) {
402-
return ComponentPath.leaf(this);
403-
}
404-
} else if (event instanceof InitialFocus) {
405-
return ComponentPath.leaf(this);
406-
}
396+
return switch (event) {
397+
case ArrowNavigation arrowNavigation when supportsArrowNavigation() -> ComponentPath.leaf(this);
398+
case TabNavigation tabNavigation when supportsTabNavigation() -> ComponentPath.leaf(this);
399+
case InitialFocus initialFocus -> ComponentPath.leaf(this);
400+
default -> ContainerEventHandler.super.nextFocusPath(event);
401+
};
407402
}
408403
return ContainerEventHandler.super.nextFocusPath(event);
409404
}

src/main/java/mekanism/client/gui/element/bar/GuiChemicalBar.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ protected boolean isEmpty(STACK stack) {
3636
@Override
3737
protected TankType getType(STACK stack) {
3838
CHEMICAL type = getHandler().getStack().getChemical();
39-
if (type instanceof Gas) {
40-
return TankType.GAS_TANK;
41-
} else if (type instanceof InfuseType) {
42-
return TankType.INFUSION_TANK;
43-
} else if (type instanceof Pigment) {
44-
return TankType.PIGMENT_TANK;
45-
} else if (type instanceof Slurry) {
46-
return TankType.SLURRY_TANK;
47-
}
48-
return null;
39+
return switch (type) {
40+
case Gas gas -> TankType.GAS_TANK;
41+
case InfuseType infuseType -> TankType.INFUSION_TANK;
42+
case Pigment pigment -> TankType.PIGMENT_TANK;
43+
case Slurry slurry -> TankType.SLURRY_TANK;
44+
default -> null;
45+
};
4946
}
5047

5148
@Override

src/main/java/mekanism/client/gui/element/button/FilterButton.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,13 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
131131
slotDisplay.updateStackList();
132132
prevFilter = filter;
133133
}
134-
Component filterDescriptor;
135-
if (filter instanceof IItemStackFilter<?> item) {
136-
filterDescriptor = item.getItemStack().getHoverName();
137-
} else if (filter instanceof ITagFilter<?> tag) {
138-
filterDescriptor = Component.literal(tag.getTagName());
139-
} else if (filter instanceof IModIDFilter<?> modId) {
140-
filterDescriptor = Component.literal(modId.getModID());
141-
} else if (filter instanceof OredictionificatorFilter<?, ?, ?> oredictionificatorFilter) {
142-
filterDescriptor = Component.literal(oredictionificatorFilter.getFilterText());
143-
} else {
144-
filterDescriptor = Component.empty();
145-
}
134+
Component filterDescriptor = switch (filter) {
135+
case IItemStackFilter<?> item -> item.getItemStack().getHoverName();
136+
case ITagFilter<?> tag -> Component.literal(tag.getTagName());
137+
case IModIDFilter<?> modId -> Component.literal(modId.getModID());
138+
case OredictionificatorFilter<?, ?, ?> oredictionificatorFilter -> Component.literal(oredictionificatorFilter.getFilterText());
139+
case null, default -> Component.empty();
140+
};
146141
drawFilterDescriptor(guiGraphics, filterDescriptor, relativeX, relativeY);
147142

148143
if (filter instanceof SorterFilter<?> sorterFilter) {

src/main/java/mekanism/client/gui/element/button/MovableFilterButton.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,12 @@ private void updateButtonVisibility(@Nullable IFilter<?> filter) {
7979
public void drawBackground(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
8080
super.drawBackground(guiGraphics, mouseX, mouseY, partialTicks);
8181
IFilter<?> filter = getFilter();
82-
EnumColor color;
83-
if (filter instanceof IItemStackFilter) {
84-
color = EnumColor.INDIGO;
85-
} else if (filter instanceof ITagFilter) {
86-
color = EnumColor.BRIGHT_GREEN;
87-
} else if (filter instanceof IModIDFilter) {
88-
color = EnumColor.RED;
89-
} else {
90-
color = null;
91-
}
82+
EnumColor color = switch (filter) {
83+
case IItemStackFilter<?> stackFilter -> EnumColor.INDIGO;
84+
case ITagFilter<?> tagFilter -> EnumColor.BRIGHT_GREEN;
85+
case IModIDFilter<?> modIDFilter -> EnumColor.RED;
86+
case null, default -> null;
87+
};
9288
if (color != null) {
9389
GuiUtils.fill(guiGraphics, getButtonX(), getButtonY(), getButtonWidth(), getButtonHeight(), MekanismRenderer.getColorARGB(color, 0.3F));
9490
}

0 commit comments

Comments
 (0)