-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fixed codecs and implement toString in LibX codecs.
- Loading branch information
1 parent
b0278b3
commit 86d88f2
Showing
9 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/org/moddingx/libx/impl/codec/FixedCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.moddingx.libx.impl.codec; | ||
|
||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.Dynamic; | ||
import com.mojang.serialization.DynamicOps; | ||
import net.minecraft.util.Unit; | ||
|
||
import java.util.Objects; | ||
|
||
public class FixedCodec<A> implements Codec<Unit> { | ||
|
||
private final Dynamic<A> serializedValue; | ||
|
||
public FixedCodec(Dynamic<A> serializedValue) { | ||
this.serializedValue = serializedValue; | ||
} | ||
|
||
|
||
@Override | ||
public <T> DataResult<T> encode(Unit unit, DynamicOps<T> ops, T prefix) { | ||
return ops.mergeToPrimitive(prefix, this.serializedValue.convert(ops).getValue()); | ||
} | ||
|
||
@Override | ||
public <T> DataResult<Pair<Unit, T>> decode(DynamicOps<T> ops, T input) { | ||
Dynamic<A> dynamic = new Dynamic<>(ops, input).convert(this.serializedValue.getOps()); | ||
if (Objects.equals(this.serializedValue, dynamic)) { | ||
return DataResult.success(Pair.of(Unit.INSTANCE, ops.empty())); | ||
} else { | ||
return DataResult.error(() -> "Wrong value in fixed codec."); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "FixedCodec[" + this.serializedValue + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters