Skip to content

Commit

Permalink
Add a "field" for error factories
Browse files Browse the repository at this point in the history
Allows throwing the proper exception type in the constructor for throwing constructors since fields know whether they are in JSON or buffers (constructors don't)
  • Loading branch information
KnightMiner committed Apr 15, 2024
1 parent d51ba38 commit 60b233c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.JsonSyntaxException;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.EncoderException;
import slimeknights.mantle.data.loadable.field.ConstantField;
import slimeknights.mantle.data.loadable.field.LoadableField;

import java.util.function.Consumer;

Expand All @@ -26,6 +28,8 @@ public RuntimeException create(RuntimeException base) {
return base;
}
};
/** Field for constructors wishing to possibly throw */
LoadableField<ErrorFactory,Object> FIELD = new ConstantField<>(JSON_SYNTAX_ERROR, DECODER_EXCEPTION);

/** Throws an exception from the given error */
@Override
Expand Down
@@ -0,0 +1,27 @@
package slimeknights.mantle.data.loadable.field;

import com.google.gson.JsonObject;
import net.minecraft.network.FriendlyByteBuf;

/** Record field that always returns the same value, used mainly to pass a different object in JSON vs buffer parsing */
public record ConstantField<T>(T fromJson, T fromBuffer) implements LoadableField<T,Object> {
public ConstantField(T value) {
this(value, value);
}

@Override
public T get(JsonObject json) {
return fromJson;
}

@Override
public T decode(FriendlyByteBuf buffer) {
return fromBuffer;
}

@Override
public void serialize(Object parent, JsonObject json) {}

@Override
public void encode(FriendlyByteBuf buffer, Object parent) {}
}

0 comments on commit 60b233c

Please sign in to comment.