Skip to content

Commit

Permalink
use enhanced switches
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Jan 26, 2025
1 parent d1fad7e commit fe1a888
Showing 1 changed file with 38 additions and 40 deletions.
78 changes: 38 additions & 40 deletions src/main/java/mekanism/common/util/StorageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ public static FluidStack getContainedFluid(@NotNull IFluidHandlerItem fluidHandl
@NotNull
public static FluidStack getStoredFluidFromAttachment(ItemStack stack) {
List<IExtendedFluidTank> containers = ContainerType.FLUID.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return FluidStack.EMPTY;
case 1:
return containers.getFirst().getFluid().copy();
default:
return switch (containers.size()) {
case 0 -> FluidStack.EMPTY;
case 1 -> containers.getFirst().getFluid().copy();
default -> {
FluidStack fluid = FluidStack.EMPTY;
for (IExtendedFluidTank tank : containers) {
if (tank.isEmpty()) {
Expand All @@ -207,8 +205,9 @@ public static FluidStack getStoredFluidFromAttachment(ItemStack stack) {
}
//Note: If we have multiple tanks that have different types stored we only return the first type
}
return fluid;
}
yield fluid;
}
};
}

/**
Expand All @@ -219,20 +218,20 @@ public static FluidStack getStoredFluidFromAttachment(ItemStack stack) {
*/
public static FluidStack getFirstFluidFromAttachment(ItemStack stack) {
List<IExtendedFluidTank> containers = ContainerType.FLUID.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return FluidStack.EMPTY;
case 1:
return containers.getFirst().getFluid();
default:
for (IExtendedFluidTank tank : containers) {
if (tank.isEmpty()) {
continue;
int size = containers.size();
return switch (size) {
case 0 -> FluidStack.EMPTY;
case 1 -> containers.getFirst().getFluid();
default -> {
for (int i = 0; i < size; i++) {
FluidStack fluid = containers.get(i).getFluid();
if (!fluid.isEmpty()) {
yield fluid;
}
return tank.getFluid();
}
return FluidStack.EMPTY;
}
yield FluidStack.EMPTY;
}
};
}

/**
Expand All @@ -242,12 +241,10 @@ public static FluidStack getFirstFluidFromAttachment(ItemStack stack) {
@NotNull
public static ChemicalStack getStoredChemicalFromAttachment(ItemStack stack) {
List<IChemicalTank> containers = ContainerType.CHEMICAL.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return ChemicalStack.EMPTY;
case 1:
return containers.getFirst().getStack().copy();
default:
return switch (containers.size()) {
case 0 -> ChemicalStack.EMPTY;
case 1 -> containers.getFirst().getStack().copy();
default -> {
ChemicalStack chemicalStack = ChemicalStack.EMPTY;
for (IChemicalTank tank : containers) {
if (tank.isEmpty()) {
Expand All @@ -264,8 +261,9 @@ public static ChemicalStack getStoredChemicalFromAttachment(ItemStack stack) {
}
//Note: If we have multiple tanks that have different types stored we only return the first type
}
return chemicalStack;
}
yield chemicalStack;
}
};
}

/**
Expand All @@ -277,20 +275,20 @@ public static ChemicalStack getStoredChemicalFromAttachment(ItemStack stack) {
@NotNull
public static ChemicalStack getFirstChemicalFromAttachment(ItemStack stack) {
List<IChemicalTank> containers = ContainerType.CHEMICAL.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return ChemicalStack.EMPTY;
case 1:
return containers.getFirst().getStack();
default:
for (IChemicalTank tank : containers) {
if (tank.isEmpty()) {
continue;
int size = containers.size();
return switch (size) {
case 0 -> ChemicalStack.EMPTY;
case 1 -> containers.getFirst().getStack();
default -> {
for (int i = 0; i < size; i++) {
ChemicalStack chemicalStack = containers.get(i).getStack();
if (!chemicalStack.isEmpty()) {
yield chemicalStack;
}
return tank.getStack();
}
return ChemicalStack.EMPTY;
}
yield ChemicalStack.EMPTY;
}
};
}

/**
Expand Down

0 comments on commit fe1a888

Please sign in to comment.