Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.20.3] Fix issue with converting between formats #204

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/main/java/io/wispforest/owo/serialization/Endec.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import io.wispforest.owo.serialization.endec.*;
import io.wispforest.owo.serialization.format.edm.EdmDeserializer;
import io.wispforest.owo.serialization.format.edm.EdmEndec;
import io.wispforest.owo.serialization.format.edm.EdmOps;
import io.wispforest.owo.serialization.format.edm.EdmSerializer;
import io.wispforest.owo.serialization.format.edm.*;
import net.minecraft.util.Util;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -411,7 +408,7 @@ default Codec<T> codec(SerializationAttribute... assumedAttributes) {
@Override
public <D> DataResult<Pair<T, D>> decode(DynamicOps<D> ops, D input) {
try {
return DataResult.success(new Pair<>(Endec.this.decode(new EdmDeserializer(ops.convertTo(EdmOps.INSTANCE, input), assumedAttributes)), input));
return DataResult.success(new Pair<>(Endec.this.decode(new LenientEdmDeserializer(ops.convertTo(EdmOps.INSTANCE, input), assumedAttributes)), input));
} catch (Exception e) {
return DataResult.error(e::getMessage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.wispforest.owo.serialization;

import com.mojang.serialization.*;
import io.wispforest.owo.serialization.format.edm.EdmDeserializer;
import io.wispforest.owo.serialization.format.edm.EdmElement;
import io.wispforest.owo.serialization.format.edm.EdmOps;
import io.wispforest.owo.serialization.format.edm.EdmSerializer;
import io.wispforest.owo.serialization.format.edm.*;
import net.minecraft.util.Util;

import java.util.HashMap;
Expand Down Expand Up @@ -57,7 +54,7 @@ public <T1> DataResult<T> decode(DynamicOps<T1> ops, MapLike<T1> input) {
);
});

return DataResult.success(StructEndec.this.decode(new EdmDeserializer(EdmElement.wrapMap(map))));
return DataResult.success(StructEndec.this.decode(new LenientEdmDeserializer(EdmElement.wrapMap(map))));
} catch (Exception e) {
return DataResult.error(e::getMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public long readLong() {
public float readFloat() {
return this.getValue().cast();
}

@Override
public double readDouble() {
return this.getValue().cast();
Expand All @@ -64,12 +65,12 @@ public double readDouble() {

@Override
public int readVarInt() {
return this.getValue().cast();
return this.readInt();
}

@Override
public long readVarLong() {
return this.getValue().cast();
return this.readLong();
}

// ---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.wispforest.owo.serialization.format.edm;

import io.wispforest.owo.serialization.SerializationAttribute;

public class LenientEdmDeserializer extends EdmDeserializer {

public LenientEdmDeserializer(EdmElement<?> serialized, SerializationAttribute... extraAttributes) {
super(serialized, extraAttributes);
}

// ---

@Override
public byte readByte() {
return this.getValue().<Number>cast().byteValue();
}

@Override
public short readShort() {
return this.getValue().<Number>cast().shortValue();
}

@Override
public int readInt() {
return this.getValue().<Number>cast().intValue();
}

@Override
public long readLong() {
return this.getValue().<Number>cast().longValue();
}

// ---

@Override
public float readFloat() {
return this.getValue().<Number>cast().floatValue();
}

@Override
public double readDouble() {
return this.getValue().<Number>cast().doubleValue();
}

// ---

@Override
public boolean readBoolean() {
if(this.getValue().value() instanceof Number number){
return number.byteValue() == 1;
}

return super.readBoolean();
}
}
Loading