Skip to content

Commit 757260a

Browse files
committed
Use map to handle duplicate output merging
1 parent 1d8d1b7 commit 757260a

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

src/main/java/gregtech/api/metatileentity/multiblock/ui/MultiblockUIFactory.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.jetbrains.annotations.Nullable;
4949

5050
import java.util.ArrayList;
51-
import java.util.HashMap;
51+
import java.util.LinkedHashMap;
5252
import java.util.List;
5353
import java.util.Map;
5454
import java.util.function.BiFunction;
@@ -872,29 +872,20 @@ public Builder addRecipeOutputLine(@NotNull List<ItemStack> itemOutputs, @NotNul
872872
*/
873873
public Builder addItemOutputLine(@NotNull List<ItemStack> itemOutputs, int recipeLength) {
874874
Preconditions.checkNotNull(itemOutputs, "Passed null item list to MultiblockUIFactory#addItemOutputLine");
875-
if (!isStructureFormed || !isActive) return this;
876-
877-
if (!itemOutputs.isEmpty()) {
878-
Map<String, Long> nameMap = new HashMap<>();
879-
880-
for (ItemStack stack : itemOutputs) {
881-
if (stack.isEmpty()) continue;
882-
nameMap.merge(stack.getDisplayName(), (long) stack.getCount(), Long::sum);
883-
}
884-
885-
for (Map.Entry<String, Long> entry : nameMap.entrySet()) {
875+
if (!isStructureFormed || !isActive || itemOutputs.isEmpty()) return this;
886876

887-
}
877+
Map<String, Long> itemMap = new LinkedHashMap<>();
878+
for (ItemStack itemStack : itemOutputs) {
879+
if (itemStack.isEmpty()) continue;
880+
itemMap.merge(itemStack.getDisplayName(), (long) itemStack.getCount(), Long::sum);
888881
}
889882

890-
itemOutputs.forEach(stack -> {
891-
int amount = stack.getCount();
892-
893-
IKey itemName = KeyUtil.string(TextFormatting.AQUA, stack.getDisplayName());
894-
IKey itemAmount = KeyUtil.number(TextFormatting.GOLD, amount);
895-
IKey itemRate = KeyUtil.string(TextFormatting.WHITE, formatRecipeRate(recipeLength, amount));
883+
for (Map.Entry<String, Long> entry : itemMap.entrySet()) {
884+
IKey itemName = KeyUtil.string(TextFormatting.AQUA, entry.getKey());
885+
IKey itemAmount = KeyUtil.number(TextFormatting.GOLD, entry.getValue());
886+
IKey itemRate = KeyUtil.string(TextFormatting.WHITE, formatRecipeRate(recipeLength, entry.getValue()));
896887
addKey(KeyUtil.string(TextFormatting.WHITE, "%s x %s (%s)", itemName, itemAmount, itemRate));
897-
});
888+
}
898889

899890
return this;
900891
}
@@ -908,26 +899,30 @@ public Builder addItemOutputLine(@NotNull List<ItemStack> itemOutputs, int recip
908899
public Builder addFluidOutputLine(@NotNull List<FluidStack> fluidOutputs, int recipeLength) {
909900
Preconditions.checkNotNull(fluidOutputs,
910901
"Passed null fluid list to MultiblockUIFactory#addFluidOutputLine");
911-
if (!isStructureFormed || !isActive) return this;
902+
if (!isStructureFormed || !isActive || fluidOutputs.isEmpty()) return this;
912903

913-
fluidOutputs.forEach(stack -> {
914-
int amount = stack.amount;
904+
Map<FluidStack, Long> fluidMap = new LinkedHashMap<>();
905+
for (FluidStack fluidStack : fluidOutputs) {
906+
if (fluidStack.amount < 1) continue;
907+
fluidMap.merge(fluidStack, (long) fluidStack.amount, Long::sum);
908+
}
915909

916-
IKey fluidName = KeyUtil.fluid(TextFormatting.AQUA, stack);
917-
IKey fluidAmount = KeyUtil.number(TextFormatting.GOLD, amount);
918-
IKey fluidRate = KeyUtil.string(TextFormatting.WHITE, formatRecipeRate(recipeLength, amount));
910+
for (Map.Entry<FluidStack, Long> entry : fluidMap.entrySet()) {
911+
IKey fluidName = KeyUtil.fluid(TextFormatting.AQUA, entry.getKey());
912+
IKey fluidAmount = KeyUtil.number(TextFormatting.GOLD, entry.getValue());
913+
IKey fluidRate = KeyUtil.string(TextFormatting.WHITE, formatRecipeRate(recipeLength, entry.getValue()));
919914
addKey(KeyUtil.string(TextFormatting.WHITE, "%s x %s (%s)", fluidName, fluidAmount, fluidRate));
920-
});
915+
}
921916

922917
return this;
923918
}
924919

925-
private static String formatRecipeRate(int recipeLength, int amount) {
920+
private static String formatRecipeRate(int recipeLength, long amount) {
926921
float perSecond = ((float) amount / recipeLength) * 20f;
927922

928923
String rate;
929924
if (perSecond > 1) {
930-
rate = String.format("%.2f", perSecond).replaceAll("\\.?0+$", "") + "s";
925+
rate = String.format("%.2f", perSecond).replaceAll("\\.?0+$", "") + "/s";
931926
} else {
932927
rate = String.format("%.2f", 1 / (perSecond)).replaceAll("\\.?0+$", "") + "s/ea";
933928
}

0 commit comments

Comments
 (0)