/
ModifierMaterialRepairSerializer.java
58 lines (49 loc) · 2.24 KB
/
ModifierMaterialRepairSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package slimeknights.tconstruct.library.recipe.tinkerstation.repairing;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;
import slimeknights.mantle.recipe.helper.LoggingRecipeSerializer;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.library.materials.definition.MaterialId;
import slimeknights.tconstruct.library.modifiers.ModifierId;
import slimeknights.tconstruct.library.recipe.tinkerstation.repairing.ModifierMaterialRepairSerializer.IModifierMaterialRepairRecipe;
import javax.annotation.Nullable;
/**
* Serializer for the recipe
*/
@RequiredArgsConstructor
public class ModifierMaterialRepairSerializer<T extends Recipe<?> & IModifierMaterialRepairRecipe> implements LoggingRecipeSerializer<T> {
private final IFactory<T> factory;
@Override
public T fromJson(ResourceLocation id, JsonObject json) {
ModifierId modifier = new ModifierId(JsonHelper.getResourceLocation(json, "modifier"));
MaterialId repairMaterial = MaterialId.PARSER.getIfPresent(json, "repair_material");
return factory.create(id, modifier, repairMaterial);
}
@Nullable
@Override
public T fromNetworkSafe(ResourceLocation id, FriendlyByteBuf buffer) {
ModifierId modifierId = new ModifierId(buffer.readUtf(Short.MAX_VALUE));
MaterialId repairMaterial = new MaterialId(buffer.readUtf(Short.MAX_VALUE));
return factory.create(id, modifierId, repairMaterial);
}
@Override
public void toNetworkSafe(FriendlyByteBuf buffer, T recipe) {
buffer.writeUtf(recipe.getModifier().toString());
buffer.writeUtf(recipe.getRepairMaterial().toString());
}
/** Interface for serializing the recipe */
public interface IModifierMaterialRepairRecipe {
/** Gets the modifier required to apply this repair */
ModifierId getModifier();
/** Gets the material ID from the recipe */
MaterialId getRepairMaterial();
}
/** Factory constructor for this serializer */
@FunctionalInterface
public interface IFactory<T extends Recipe<?> & IModifierMaterialRepairRecipe> {
T create(ResourceLocation id, ModifierId modifierId, MaterialId repairMaterial);
}
}