mirror of
https://github.com/Oth3r/Sit.git
synced 2025-09-19 16:03:22 +02:00
custom list type adapter to stop null lists from bad data
This commit is contained in:
parent
14f232e3fc
commit
f1f3e54d22
1 changed files with 50 additions and 0 deletions
|
@ -35,6 +35,8 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -351,10 +353,19 @@ public class Utl {
|
||||||
/**
|
/**
|
||||||
* the LenientTypeAdapter, doesn't throw anything when reading a weird JSON entry, good for human entered JSONs
|
* the LenientTypeAdapter, doesn't throw anything when reading a weird JSON entry, good for human entered JSONs
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public static class LenientTypeAdapterFactory implements TypeAdapterFactory {
|
public static class LenientTypeAdapterFactory implements TypeAdapterFactory {
|
||||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
||||||
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
|
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
|
||||||
|
|
||||||
|
// Check if the type is a List, then run the custom list type adapter
|
||||||
|
if (List.class.isAssignableFrom(type.getRawType())) {
|
||||||
|
Type elementType = ((ParameterizedType) type.getType()).getActualTypeArguments()[0];
|
||||||
|
TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType));
|
||||||
|
// the custom adapter
|
||||||
|
return (TypeAdapter<T>) new RemoveNullListTypeAdapter<>(elementAdapter);
|
||||||
|
}
|
||||||
|
|
||||||
return new TypeAdapter<>() {
|
return new TypeAdapter<>() {
|
||||||
// normal writer
|
// normal writer
|
||||||
public void write(JsonWriter out, T value) throws IOException {
|
public void write(JsonWriter out, T value) throws IOException {
|
||||||
|
@ -375,6 +386,45 @@ public class Utl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* type adapter that doesnt allow null / bad entries
|
||||||
|
*/
|
||||||
|
private static class RemoveNullListTypeAdapter<E> extends TypeAdapter<List<E>> {
|
||||||
|
private final TypeAdapter<E> elementAdapter;
|
||||||
|
|
||||||
|
RemoveNullListTypeAdapter(TypeAdapter<E> elementAdapter) {
|
||||||
|
this.elementAdapter = elementAdapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(JsonWriter out, List<E> value) throws IOException {
|
||||||
|
out.beginArray();
|
||||||
|
for (E element : value) {
|
||||||
|
elementAdapter.write(out, element);
|
||||||
|
}
|
||||||
|
out.endArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<E> read(JsonReader in) throws IOException {
|
||||||
|
List<E> list = new ArrayList<>();
|
||||||
|
in.beginArray();
|
||||||
|
while (in.hasNext()) {
|
||||||
|
try {
|
||||||
|
E element = elementAdapter.read(in);
|
||||||
|
// skip null entry
|
||||||
|
if (element == null) continue;
|
||||||
|
list.add(element);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// skip invalid entry
|
||||||
|
in.skipValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
in.endArray();
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static BlockPos getBlockPosPlayerIsLookingAt(ServerWorld world, PlayerEntity player, double range) {
|
public static BlockPos getBlockPosPlayerIsLookingAt(ServerWorld world, PlayerEntity player, double range) {
|
||||||
// pos, adjusted to player eye level
|
// pos, adjusted to player eye level
|
||||||
Vec3d rayStart = player.getPos().add(0, player.getEyeHeight(player.getPose()), 0);
|
Vec3d rayStart = player.getPos().add(0, player.getEyeHeight(player.getPose()), 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue