Skip to content

Commit

Permalink
Merge pull request #23 from DancingSnow0517/fix-1.21
Browse files Browse the repository at this point in the history
check count to avoid crash by NumberFormatException
  • Loading branch information
DancingSnow0517 authored Nov 25, 2024
2 parents 81f152d + 767e7b4 commit ff83d9d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ mod_authors= DancingSnow
mod_description = A mod added more ae cells and so on.
mod_license = MIT
mod_url = https://github.com/GregTech-Exploring-Pioneer/BiggerAE2
mod_version = 1.4.3
mod_version = 1.4.4

maven_group = cn.dancingsnow.bigger_ae2
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import net.minecraft.network.codec.StreamCodec;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import lombok.Getter;
import org.jetbrains.annotations.Nullable;

import java.math.BigInteger;
import java.util.Objects;

@Getter
Expand All @@ -18,31 +20,49 @@ public class DigitalSingularityStorage {
RecordCodecBuilder.create(instance -> instance
.group(
AEKey.CODEC.fieldOf("key").forGetter(DigitalSingularityStorage::getStoredItem),
Codec.STRING.fieldOf("count").forGetter(DigitalSingularityStorage::getCount))
Codec.STRING
.flatXmap(
it -> {
try {
return DataResult.success(new BigInteger(it));
} catch (NumberFormatException e) {
return DataResult.success(BigInteger.ZERO);
}
},
it -> DataResult.success(it.toString()))
.fieldOf("count")
.forGetter(DigitalSingularityStorage::getCount))
.apply(instance, DigitalSingularityStorage::new));

public static final StreamCodec<RegistryFriendlyByteBuf, DigitalSingularityStorage> STREAM_CODEC =
StreamCodec.ofMember(DigitalSingularityStorage::encode, DigitalSingularityStorage::new);

@Nullable private final AEKey storedItem;

private final String count;
private final BigInteger count;

public DigitalSingularityStorage(@Nullable AEKey storedItem, String count) {
public DigitalSingularityStorage(@Nullable AEKey storedItem, BigInteger count) {
this.storedItem = storedItem;
this.count = count;
}

public DigitalSingularityStorage(RegistryFriendlyByteBuf buf) {
storedItem = AEKey.readOptionalKey(buf);
count = buf.readUtf();
String integerStr = buf.readUtf();
BigInteger c;
try {
c = new BigInteger(integerStr);
} catch (NumberFormatException e) {
c = BigInteger.ZERO;
}
count = c;
}

public void encode(RegistryFriendlyByteBuf buf) {
AEKey.writeOptionalKey(buf, storedItem);
buf.writeUtf(count);
buf.writeUtf(count.toString());
}

public static final StreamCodec<RegistryFriendlyByteBuf, DigitalSingularityStorage> STREAM_CODEC =
StreamCodec.ofMember(DigitalSingularityStorage::encode, DigitalSingularityStorage::new);

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DigitalSingularityStorageCell(ItemStack stack, ISaveProvider container) {
filterItem = cell.getConfigInventory(stack).getKey(0);
type = cell.getKeyType();

count = storage != null ? new BigInteger(storage.getCount()) : BigInteger.ZERO;
count = storage != null ? storage.getCount() : BigInteger.ZERO;
}

@Override
Expand All @@ -73,7 +73,7 @@ public void persist() {
if (storedItem == null || count.signum() < 1) {
stack.remove(ModComponents.SINGULARITY_STORAGE);
} else {
var storage = new DigitalSingularityStorage(storedItem, count.toString());
var storage = new DigitalSingularityStorage(storedItem, count);
stack.set(ModComponents.SINGULARITY_STORAGE, storage);
}

Expand Down

0 comments on commit ff83d9d

Please sign in to comment.