Skip to content

Commit 978bdab

Browse files
committed
Added KLibCodecErrors
1 parent 6d83f04 commit 978bdab

5 files changed

Lines changed: 65 additions & 10 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dev.latvian.mods.klib.codec;
2+
3+
import com.mojang.serialization.DataResult;
4+
import dev.latvian.mods.klib.util.Cast;
5+
6+
public interface KLibCodecErrors {
7+
static <T> DataResult<T> error(String message) {
8+
return DataResult.error(() -> message);
9+
}
10+
11+
static <T> DataResult<T> error(Throwable ex) {
12+
return DataResult.error(ex::getMessage);
13+
}
14+
15+
DataResult<?> VALUE_IS_NULL = error("Value is null");
16+
DataResult<?> VALUE_IS_EMPTY = error("Value is empty");
17+
DataResult<?> LIST_IS_NULL = error("List is null");
18+
DataResult<?> LIST_IS_EMPTY = error("List is empty");
19+
DataResult<?> MAP_IS_NULL = error("Map is null");
20+
DataResult<?> MAP_IS_EMPTY = error("Map is empty");
21+
22+
static <T> DataResult<T> valueIsNull() {
23+
return Cast.to(VALUE_IS_NULL);
24+
}
25+
26+
static <T> DataResult<T> valueIsEmpty() {
27+
return Cast.to(VALUE_IS_EMPTY);
28+
}
29+
30+
static <T> DataResult<T> listIsNull() {
31+
return Cast.to(LIST_IS_NULL);
32+
}
33+
34+
static <T> DataResult<T> listIsEmpty() {
35+
return Cast.to(LIST_IS_EMPTY);
36+
}
37+
38+
static <T> DataResult<T> mapIsNull() {
39+
return Cast.to(MAP_IS_NULL);
40+
}
41+
42+
static <T> DataResult<T> mapIsEmpty() {
43+
return Cast.to(MAP_IS_EMPTY);
44+
}
45+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ static <K, V> Codec<V> map(Supplier<Map<K, V>> mapGetter, Codec<K> keyCodec, Fun
193193
var map = mapGetter.get();
194194

195195
if (map == null) {
196-
return DataResult.error(() -> "Map is null");
196+
return KLibCodecErrors.mapIsNull();
197197
} else if (map.isEmpty()) {
198-
return DataResult.error(() -> "Map is empty");
198+
return KLibCodecErrors.mapIsEmpty();
199199
} else {
200200
var value = map.get(k);
201201
return value == null ? DataResult.error(() -> "No value for key " + k) : DataResult.success(value);
@@ -208,7 +208,7 @@ static <K, V> Codec<V> map(Map<K, V> map, Codec<K> keyCodec, Function<V, K> keyG
208208

209209
return keyCodec.flatXmap(k -> {
210210
if (map.isEmpty()) {
211-
return DataResult.error(() -> "Map is empty");
211+
return KLibCodecErrors.mapIsEmpty();
212212
} else {
213213
var value = map.get(k);
214214
return value == null ? DataResult.error(() -> "No value for key " + k) : DataResult.success(value);
@@ -228,7 +228,7 @@ static <K, V> Codec<V> partialMap(Map<K, V> map, Codec<K> keyCodec, boolean iden
228228

229229
return keyCodec.flatXmap(k -> {
230230
if (map.isEmpty()) {
231-
return DataResult.error(() -> "Map is empty");
231+
return KLibCodecErrors.mapIsEmpty();
232232
} else {
233233
var value = map.get(k);
234234
return value == null ? DataResult.error(() -> "No value for key " + k) : DataResult.success(value);

src/main/java/dev/latvian/mods/klib/color/PositionedColor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mojang.serialization.DataResult;
55
import com.mojang.serialization.codecs.RecordCodecBuilder;
66
import dev.latvian.mods.klib.codec.CompositeStreamCodec;
7+
import dev.latvian.mods.klib.codec.KLibCodecErrors;
78
import dev.latvian.mods.klib.codec.KLibCodecs;
89
import dev.latvian.mods.klib.interpolation.Interpolation;
910
import dev.latvian.mods.klib.registry.Ref;
@@ -120,11 +121,13 @@ public static List<Color> toSimpleList(List<PositionedColor> colors) {
120121
return list;
121122
}
122123

124+
private static final DataResult<List<Color>> NOT_A_SIMPLE_LIST = KLibCodecErrors.error("Not a simple list");
125+
123126
private static final Codec<List<PositionedColor>> LIST_CODEC_OF_SIMPLE_COLORS = Color.CODEC.listOf().flatComapMap(PositionedColor::fromSimpleList, colors -> {
124127
if (isSimple(colors)) {
125128
return DataResult.success(toSimpleList(colors));
126129
} else {
127-
return DataResult.error(() -> "Not a simple list");
130+
return NOT_A_SIMPLE_LIST;
128131
}
129132
});
130133

src/main/java/dev/latvian/mods/klib/registry/CustomRegistry.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mojang.serialization.Codec;
55
import com.mojang.serialization.DataResult;
66
import dev.latvian.mods.klib.KLib;
7+
import dev.latvian.mods.klib.codec.KLibCodecErrors;
78
import dev.latvian.mods.klib.codec.KLibCodecs;
89
import dev.latvian.mods.klib.codec.KLibStreamCodecs;
910
import dev.latvian.mods.klib.command.CustomRegistryArgument;
@@ -51,6 +52,7 @@ public class CustomRegistry<B extends ByteBuf, V> implements Iterable<Ref<V>> {
5152
public static final Map<String, CustomRegistry<?, ?>> ALL = Collections.unmodifiableMap(ALL0);
5253
private static final Map<DataType<?>, CustomRegistry<?, ?>> DATA_TYPE_TO_REGISTRY0 = new Reference2ObjectLinkedOpenHashMap<>();
5354
public static final Map<DataType<?>, CustomRegistry<?, ?>> DATA_TYPE_TO_REGISTRY = Collections.unmodifiableMap(DATA_TYPE_TO_REGISTRY0);
55+
private static final DataResult<?> ERROR_NOT_UNIT = KLibCodecErrors.error("Type is not a unit type");
5456

5557
public static void registerAll(Consumer<CustomRegistryCollector> callback) {
5658
ALL0.clear();
@@ -216,7 +218,7 @@ private CustomRegistry(
216218
if (ref instanceof UnitType<?, V>) {
217219
return DataResult.success(ref.key());
218220
} else {
219-
return DataResult.error(() -> "Type is not a unit type");
221+
return Cast.to(ERROR_NOT_UNIT);
220222
}
221223
});
222224

src/main/java/dev/latvian/mods/klib/util/ParsedEntitySelector.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.mojang.serialization.Codec;
66
import com.mojang.serialization.DataResult;
77
import com.mojang.serialization.codecs.RecordCodecBuilder;
8+
import dev.latvian.mods.klib.codec.KLibCodecErrors;
89
import dev.latvian.mods.klib.codec.KLibCodecs;
910
import dev.latvian.mods.klib.data.DataType;
1011
import io.netty.buffer.ByteBuf;
@@ -18,6 +19,10 @@
1819
import java.util.Objects;
1920

2021
public final class ParsedEntitySelector {
22+
private static final DataResult<EntitySelector> ERROR_ONLY_ONE_PLAYER = KLibCodecErrors.error("Only one player is allowed, but the provided selector allows more than one");
23+
private static final DataResult<EntitySelector> ERROR_ONLY_ONE_ENTITY = KLibCodecErrors.error("Only one entity is allowed, but the provided selector allows more than one");
24+
private static final DataResult<EntitySelector> ERROR_ONlY_PLAYERS = KLibCodecErrors.error("Only players are allowed, but the provided selector includes entities");
25+
2126
public static final Codec<ParsedEntitySelector> DIRECT_CODEC = RecordCodecBuilder.create(instance -> instance.group(
2227
Codec.STRING.fieldOf("selector").forGetter(ParsedEntitySelector::getInput),
2328
Codec.BOOL.optionalFieldOf("single", false).forGetter(ParsedEntitySelector::isSingle),
@@ -91,17 +96,17 @@ private DataResult<EntitySelector> parse() {
9196

9297
if (single && selector.getMaxResults() > 1) {
9398
if (playersOnly) {
94-
return DataResult.error(() -> "Only one player is allowed, but the provided selector allows more than one");
99+
return ERROR_ONLY_ONE_PLAYER;
95100
} else {
96-
return DataResult.error(() -> "Only one entity is allowed, but the provided selector allows more than one");
101+
return ERROR_ONLY_ONE_ENTITY;
97102
}
98103
} else if (selector.includesEntities() && playersOnly && !selector.isSelfSelector()) {
99-
return DataResult.error(() -> "Only players are allowed, but the provided selector includes entities");
104+
return ERROR_ONlY_PLAYERS;
100105
}
101106

102107
return DataResult.success(selector);
103108
} catch (CommandSyntaxException ex) {
104-
return DataResult.error(ex::getMessage);
109+
return KLibCodecErrors.error(ex);
105110
}
106111
}
107112

0 commit comments

Comments
 (0)