Skip to content

Commit 8d4aeb8

Browse files
authored
Fix various divide by zero errors in material class (GregTechCEu#3048)
1 parent 32c1ab4 commit 8d4aeb8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/main/java/com/gregtechceu/gtceu/api/data/chemical/material/Material.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,11 @@ public long getProtons() {
433433
return Math.max(1, 43);
434434
long totalProtons = 0, totalAmount = 0;
435435
for (MaterialStack material : materialInfo.componentList) {
436+
if (material.isEmpty()) continue;
436437
totalAmount += material.amount();
437438
totalProtons += material.amount() * material.material().getProtons();
438439
}
440+
if (totalAmount == 0) return 0;
439441
return totalProtons / totalAmount;
440442
}
441443

@@ -446,22 +448,26 @@ public long getNeutrons() {
446448
return 55;
447449
long totalNeutrons = 0, totalAmount = 0;
448450
for (MaterialStack material : materialInfo.componentList) {
451+
if (material.isEmpty()) continue;
449452
totalAmount += material.amount();
450453
totalNeutrons += material.amount() * material.material().getNeutrons();
451454
}
455+
if (totalAmount == 0) return 0;
452456
return totalNeutrons / totalAmount;
453457
}
454458

455459
public long getMass() {
456460
if (materialInfo.element != null)
457461
return materialInfo.element.mass();
458-
if (materialInfo.componentList.size() == 0)
462+
if (materialInfo.componentList.isEmpty())
459463
return 98;
460464
long totalMass = 0, totalAmount = 0;
461465
for (MaterialStack material : materialInfo.componentList) {
466+
if (material.isEmpty()) continue;
462467
totalAmount += material.amount();
463468
totalMass += material.amount() * material.material().getMass();
464469
}
470+
if (totalAmount == 0) return 0;
465471
return totalMass / totalAmount;
466472
}
467473

src/main/java/com/gregtechceu/gtceu/api/data/chemical/material/stack/MaterialStack.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public boolean isEmpty() {
5050
@Override
5151
public String toString() {
5252
String string = "";
53-
if (material.getChemicalFormula().isEmpty()) {
53+
if (this.isEmpty()) return "";
54+
if (material.getChemicalFormula() == null || material.getChemicalFormula().isEmpty()) {
5455
string += "?";
5556
} else if (material.getMaterialComponents().size() > 1) {
5657
string += '(' + material.getChemicalFormula() + ')';

0 commit comments

Comments
 (0)