Skip to content

Commit

Permalink
Make use of new switch statements to allow cleaning up some of our ch…
Browse files Browse the repository at this point in the history
…ained instance of if else statements
  • Loading branch information
pupnewfster committed Apr 28, 2024
1 parent da67ded commit 05cff52
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,18 @@ public BoxedChemicalStack getRecipeInput(ChemicalStackIngredient<?, ?> recipeIng
//All recipes currently require that we have an input. If we don't then return that we failed
return BoxedChemicalStack.EMPTY;
}
if (recipeIngredient instanceof GasStackIngredient ingredient) {
if (input.getChemicalType() == ChemicalType.GAS) {
return BoxedChemicalStack.box(ingredient.getMatchingInstance((GasStack) input.getChemicalStack()));
}
} else if (recipeIngredient instanceof InfusionStackIngredient ingredient) {
if (input.getChemicalType() == ChemicalType.INFUSION) {
return BoxedChemicalStack.box(ingredient.getMatchingInstance((InfusionStack) input.getChemicalStack()));
}
} else if (recipeIngredient instanceof PigmentStackIngredient ingredient) {
if (input.getChemicalType() == ChemicalType.PIGMENT) {
return BoxedChemicalStack.box(ingredient.getMatchingInstance((PigmentStack) input.getChemicalStack()));
}
} else if (recipeIngredient instanceof SlurryStackIngredient ingredient) {
if (input.getChemicalType() == ChemicalType.SLURRY) {
return BoxedChemicalStack.box(ingredient.getMatchingInstance((SlurryStack) input.getChemicalStack()));
}
} else {
throw new IllegalStateException("Unknown Chemical Type");
}
//Something went wrong, input doesn't match types with ingredient
return BoxedChemicalStack.EMPTY;
return switch (recipeIngredient) {
case GasStackIngredient ingredient when input.getChemicalType() == ChemicalType.GAS ->
BoxedChemicalStack.box(ingredient.getMatchingInstance((GasStack) input.getChemicalStack()));
case InfusionStackIngredient ingredient when input.getChemicalType() == ChemicalType.INFUSION ->
BoxedChemicalStack.box(ingredient.getMatchingInstance((InfusionStack) input.getChemicalStack()));
case PigmentStackIngredient ingredient when input.getChemicalType() == ChemicalType.PIGMENT ->
BoxedChemicalStack.box(ingredient.getMatchingInstance((PigmentStack) input.getChemicalStack()));
case SlurryStackIngredient ingredient when input.getChemicalType() == ChemicalType.SLURRY ->
BoxedChemicalStack.box(ingredient.getMatchingInstance((SlurryStack) input.getChemicalStack()));
//Something went wrong, input doesn't match types with ingredient
default -> BoxedChemicalStack.EMPTY;
};
}

/**
Expand Down
52 changes: 18 additions & 34 deletions src/api/java/mekanism/api/text/TextComponentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,43 +59,27 @@ public static MutableComponent build(Object... components) {
continue;
}
MutableComponent current = null;
if (component instanceof IHasTextComponent hasTextComponent) {
current = hasTextComponent.getTextComponent().copy();
} else if (component instanceof IHasTranslationKey hasTranslationKey) {
current = translate(hasTranslationKey.getTranslationKey());
} else if (component instanceof EnumColor color) {
cachedStyle = cachedStyle.withColor(color.getColor());
} else if (component instanceof TextColor color) {
cachedStyle = cachedStyle.withColor(color);
} else if (component instanceof Component c) {
switch (component) {
case IHasTextComponent hasTextComponent -> current = hasTextComponent.getTextComponent().copy();
case IHasTranslationKey hasTranslationKey -> current = translate(hasTranslationKey.getTranslationKey());
case EnumColor color -> cachedStyle = cachedStyle.withColor(color.getColor());
case TextColor color -> cachedStyle = cachedStyle.withColor(color);
//Just append if a text component is being passed
current = c.copy();
} else if (component instanceof ChatFormatting formatting) {
cachedStyle = cachedStyle.applyFormat(formatting);
} else if (component instanceof ClickEvent event) {
cachedStyle = cachedStyle.withClickEvent(event);
} else if (component instanceof HoverEvent event) {
cachedStyle = cachedStyle.withHoverEvent(event);
} else if (component instanceof Block block) {
current = block.getName().copy();
} else if (component instanceof Item item) {
current = item.getDescription().copy();
} else if (component instanceof ItemStack stack) {
current = stack.getHoverName().copy();
} else if (component instanceof FluidStack stack) {
current = stack.getHoverName().copy();
} else if (component instanceof Fluid fluid) {
current = fluid.getFluidType().getDescription().copy();
} else if (component instanceof EntityType<?> entityType) {
current = entityType.getDescription().copy();
} else if (component instanceof Direction direction) {
current = getTranslatedDirection(direction);
} else if (component instanceof Boolean bool) {
current = getTranslatedBoolean(bool);
} else {
case Component c -> current = c.copy();
case ChatFormatting formatting -> cachedStyle = cachedStyle.applyFormat(formatting);
case ClickEvent event -> cachedStyle = cachedStyle.withClickEvent(event);
case HoverEvent event -> cachedStyle = cachedStyle.withHoverEvent(event);
case Block block -> current = block.getName().copy();
case Item item -> current = item.getDescription().copy();
case ItemStack stack -> current = stack.getHoverName().copy();
case FluidStack stack -> current = stack.getHoverName().copy();
case Fluid fluid -> current = fluid.getFluidType().getDescription().copy();
case EntityType<?> entityType -> current = entityType.getDescription().copy();
case Direction direction -> current = getTranslatedDirection(direction);
case Boolean bool -> current = getTranslatedBoolean(bool);
//Fallback to a generic replacement
// this handles strings, numbers, and any type we don't necessarily know about
current = getString(component.toString());
default -> current = getString(component.toString());
}
if (current == null) {
//If we don't have a component to add, don't
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/mekanism/client/gui/GuiFilterHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ protected void addGuiElements() {

private List<ItemStack> getRenderStacks(@Nullable IFilter<?> filter) {
if (filter != null) {
if (filter instanceof IItemStackFilter<?> itemFilter) {
return List.of(itemFilter.getItemStack());
} else if (filter instanceof ITagFilter<?> tagFilter) {
return getTagStacks(tagFilter.getTagName());
} else if (filter instanceof IModIDFilter<?> modIDFilter) {
return getModIDStacks(modIDFilter.getModID());
}
return switch (filter) {
case IItemStackFilter<?> itemFilter -> List.of(itemFilter.getItemStack());
case ITagFilter<?> tagFilter -> getTagStacks(tagFilter.getTagName());
case IModIDFilter<?> modIDFilter -> getModIDStacks(modIDFilter.getModID());
default -> Collections.emptyList();
};
}
return Collections.emptyList();
}
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/mekanism/client/gui/element/GuiElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,17 +393,12 @@ public ComponentPath nextFocusPath(@NotNull FocusNavigationEvent event) {
return null;
}
if (!isFocused()) {
if (event instanceof ArrowNavigation) {
if (supportsArrowNavigation()) {
return ComponentPath.leaf(this);
}
} else if (event instanceof TabNavigation) {
if (supportsTabNavigation()) {
return ComponentPath.leaf(this);
}
} else if (event instanceof InitialFocus) {
return ComponentPath.leaf(this);
}
return switch (event) {
case ArrowNavigation arrowNavigation when supportsArrowNavigation() -> ComponentPath.leaf(this);
case TabNavigation tabNavigation when supportsTabNavigation() -> ComponentPath.leaf(this);
case InitialFocus initialFocus -> ComponentPath.leaf(this);
default -> ContainerEventHandler.super.nextFocusPath(event);
};
}
return ContainerEventHandler.super.nextFocusPath(event);
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/mekanism/client/gui/element/bar/GuiChemicalBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ protected boolean isEmpty(STACK stack) {
@Override
protected TankType getType(STACK stack) {
CHEMICAL type = getHandler().getStack().getChemical();
if (type instanceof Gas) {
return TankType.GAS_TANK;
} else if (type instanceof InfuseType) {
return TankType.INFUSION_TANK;
} else if (type instanceof Pigment) {
return TankType.PIGMENT_TANK;
} else if (type instanceof Slurry) {
return TankType.SLURRY_TANK;
}
return null;
return switch (type) {
case Gas gas -> TankType.GAS_TANK;
case InfuseType infuseType -> TankType.INFUSION_TANK;
case Pigment pigment -> TankType.PIGMENT_TANK;
case Slurry slurry -> TankType.SLURRY_TANK;
default -> null;
};
}

@Override
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/mekanism/client/gui/element/button/FilterButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,13 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
slotDisplay.updateStackList();
prevFilter = filter;
}
Component filterDescriptor;
if (filter instanceof IItemStackFilter<?> item) {
filterDescriptor = item.getItemStack().getHoverName();
} else if (filter instanceof ITagFilter<?> tag) {
filterDescriptor = Component.literal(tag.getTagName());
} else if (filter instanceof IModIDFilter<?> modId) {
filterDescriptor = Component.literal(modId.getModID());
} else if (filter instanceof OredictionificatorFilter<?, ?, ?> oredictionificatorFilter) {
filterDescriptor = Component.literal(oredictionificatorFilter.getFilterText());
} else {
filterDescriptor = Component.empty();
}
Component filterDescriptor = switch (filter) {
case IItemStackFilter<?> item -> item.getItemStack().getHoverName();
case ITagFilter<?> tag -> Component.literal(tag.getTagName());
case IModIDFilter<?> modId -> Component.literal(modId.getModID());
case OredictionificatorFilter<?, ?, ?> oredictionificatorFilter -> Component.literal(oredictionificatorFilter.getFilterText());
case null, default -> Component.empty();
};
drawFilterDescriptor(guiGraphics, filterDescriptor, relativeX, relativeY);

if (filter instanceof SorterFilter<?> sorterFilter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,12 @@ private void updateButtonVisibility(@Nullable IFilter<?> filter) {
public void drawBackground(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
super.drawBackground(guiGraphics, mouseX, mouseY, partialTicks);
IFilter<?> filter = getFilter();
EnumColor color;
if (filter instanceof IItemStackFilter) {
color = EnumColor.INDIGO;
} else if (filter instanceof ITagFilter) {
color = EnumColor.BRIGHT_GREEN;
} else if (filter instanceof IModIDFilter) {
color = EnumColor.RED;
} else {
color = null;
}
EnumColor color = switch (filter) {
case IItemStackFilter<?> stackFilter -> EnumColor.INDIGO;
case ITagFilter<?> tagFilter -> EnumColor.BRIGHT_GREEN;
case IModIDFilter<?> modIDFilter -> EnumColor.RED;
case null, default -> null;
};
if (color != null) {
GuiUtils.fill(guiGraphics, getButtonX(), getButtonY(), getButtonWidth(), getButtonHeight(), MekanismRenderer.getColorARGB(color, 0.3F));
}
Expand Down
Loading

0 comments on commit 05cff52

Please sign in to comment.