Skip to content

Commit e6e910a

Browse files
committed
Added Instant and Base64 byte[] codecs
1 parent 5fce35b commit e6e910a

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

src/main/java/dev/latvian/mods/klib/codec/KLibCodecs.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import com.mojang.serialization.Codec;
77
import com.mojang.serialization.DataResult;
88
import com.mojang.util.UndashedUuid;
9+
import dev.latvian.mods.klib.util.StringUtils;
910
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
1011
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
1112
import net.minecraft.commands.arguments.TimeArgument;
1213
import net.minecraft.util.StringRepresentable;
1314

15+
import java.time.Instant;
16+
import java.time.format.DateTimeFormatter;
1417
import java.util.ArrayList;
18+
import java.util.Arrays;
1519
import java.util.HashMap;
1620
import java.util.HashSet;
1721
import java.util.LinkedHashSet;
@@ -78,6 +82,48 @@ static <T> Codec<List<T>> listOrSelf(Codec<T> elementCodec) {
7882
Codec<Integer> INT16 = Codec.intRange(Short.MIN_VALUE, Short.MAX_VALUE);
7983
Codec<Integer> UINT16 = Codec.intRange(0, -Short.MIN_VALUE + Short.MAX_VALUE);
8084

85+
Codec<byte[]> B64_BYTE_ARRAY = Codec.STRING.flatXmap(string -> {
86+
try {
87+
return DataResult.success(StringUtils.B64_DECODER.decode(string));
88+
} catch (Exception ex) {
89+
return DataResult.error(() -> "Invalid Base64 string: " + string);
90+
}
91+
}, array -> {
92+
try {
93+
return DataResult.success(StringUtils.B64_ENCODER.encodeToString(array));
94+
} catch (Exception ex) {
95+
return DataResult.error(() -> "Invalid Base64 array: " + Arrays.toString(array));
96+
}
97+
});
98+
99+
Codec<Instant> INSTANT = Codec.STRING.flatXmap(string -> {
100+
try {
101+
return DataResult.success(Instant.parse(string));
102+
} catch (Exception ex) {
103+
return DataResult.error(() -> "Invalid date: " + string);
104+
}
105+
}, instant -> {
106+
try {
107+
return DataResult.success(instant.toString());
108+
} catch (Exception ex) {
109+
return DataResult.error(() -> "Invalid date: " + instant);
110+
}
111+
});
112+
113+
Codec<Instant> ISO_INSTANT = Codec.STRING.flatXmap(string -> {
114+
try {
115+
return DataResult.success(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(string)));
116+
} catch (Exception ex) {
117+
return DataResult.error(() -> "Invalid ISO date: " + string);
118+
}
119+
}, instant -> {
120+
try {
121+
return DataResult.success(DateTimeFormatter.ISO_INSTANT.format(instant));
122+
} catch (Exception ex) {
123+
return DataResult.error(() -> "Invalid ISO date: " + instant);
124+
}
125+
});
126+
81127
static <E> Codec<E> anyEnumCodec(E[] enumValues, Function<E, String> nameGetter) {
82128
var map = new HashMap<String, E>(enumValues.length);
83129

src/main/java/dev/latvian/mods/klib/codec/KLibStreamCodecs.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.minecraft.tags.TagKey;
1313
import org.jetbrains.annotations.Nullable;
1414

15+
import java.time.Instant;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Objects;
@@ -142,6 +143,8 @@ public void encode(RegistryFriendlyByteBuf buf, String value) {
142143
}
143144
};
144145

146+
StreamCodec<ByteBuf, Instant> INSTANT = ByteBufCodecs.LONG.map(Instant::ofEpochMilli, Instant::toEpochMilli);
147+
145148
static <T> StreamCodec<ByteBuf, ResourceKey<T>> resourceKey(ResourceKey<? extends Registry<T>> registry) {
146149
return ResourceLocation.STREAM_CODEC.map(id -> ResourceKey.create(registry, id), ResourceKey::location);
147150
}

src/main/java/dev/latvian/mods/klib/data/DataTypes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import net.minecraft.world.level.material.FluidState;
6666
import net.minecraft.world.phys.Vec3;
6767

68+
import java.time.Instant;
6869
import java.util.UUID;
6970

7071
public interface DataTypes {
@@ -79,6 +80,9 @@ public interface DataTypes {
7980
DataType<Double> DOUBLE = DataType.of(Codec.DOUBLE, ByteBufCodecs.DOUBLE, Double.class);
8081
DataType<String> STRING = DataType.of(Codec.STRING, ByteBufCodecs.STRING_UTF8, String.class);
8182
DataType<UUID> UUID = DataType.of(KLibCodecs.UUID, KLibStreamCodecs.UUID, UUID.class);
83+
DataType<byte[]> B64_BYTE_ARRAY = DataType.of(KLibCodecs.B64_BYTE_ARRAY, ByteBufCodecs.BYTE_ARRAY, byte[].class);
84+
DataType<Instant> INSTANT = DataType.of(KLibCodecs.INSTANT, KLibStreamCodecs.INSTANT, Instant.class);
85+
DataType<Instant> ISO_INSTANT = DataType.of(KLibCodecs.ISO_INSTANT, KLibStreamCodecs.INSTANT, Instant.class);
8286

8387
DataType<Component> TEXT_COMPONENT = DataType.of(ComponentSerialization.CODEC, ComponentSerialization.STREAM_CODEC, Component.class);
8488
DataType<Mirror> MIRROR = DataType.of(Mirror.values());
@@ -109,6 +113,9 @@ static void register() {
109113
DataType.register(ID.java("double"), DOUBLE, () -> DoubleArgumentType.doubleArg(), DoubleArgumentType::getDouble);
110114
DataType.register(ID.java("string"), STRING, StringArgumentType::string, StringArgumentType::getString);
111115
DataType.register(ID.java("uuid"), UUID, UuidArgument::uuid, UuidArgument::getUuid);
116+
DataType.register(ID.java("b64_byte_array"), B64_BYTE_ARRAY);
117+
DataType.register(ID.java("instant"), INSTANT);
118+
DataType.register(ID.java("iso_instant"), ISO_INSTANT);
112119

113120
DataType.register(ID.mc("id"), ID.DATA_TYPE, ResourceLocationArgument::id, ResourceLocationArgument::getId);
114121
DataType.register(ID.mc("text_component"), TEXT_COMPONENT, ComponentArgument::textComponent, ComponentArgument::getResolvedComponent);

src/main/java/dev/latvian/mods/klib/io/IOUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,22 @@ static void appendBytes(Path path, ByteBuffer buf, long remainingBytes) throws I
158158
}
159159

160160
static byte[] digest(String algorithm, Path file) throws NoSuchAlgorithmException, IOException {
161-
var md5 = MessageDigest.getInstance(algorithm);
161+
var md = MessageDigest.getInstance(algorithm);
162162

163163
try (var channel = Files.newByteChannel(file)) {
164164
var buf = ByteBuffer.allocate(2048);
165165

166166
while (channel.read(buf) != -1) {
167167
buf.flip();
168-
md5.update(buf);
168+
md.update(buf);
169169
buf.clear();
170170
}
171171

172-
return md5.digest();
172+
return md.digest();
173173
}
174174
}
175175

176176
static String md5(Path file) throws NoSuchAlgorithmException, IOException {
177177
return StringUtils.toHex(digest("MD5", file));
178178
}
179179
}
180-

0 commit comments

Comments
 (0)