/
ModifierRepairRecipeSerializer.java
63 lines (55 loc) · 2.43 KB
/
ModifierRepairRecipeSerializer.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
59
60
61
62
63
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.util.GsonHelper;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import slimeknights.mantle.recipe.helper.LoggingRecipeSerializer;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.library.modifiers.ModifierId;
import slimeknights.tconstruct.library.recipe.tinkerstation.repairing.ModifierRepairRecipeSerializer.IModifierRepairRecipe;
import javax.annotation.Nullable;
/**
* Serializer for the recipe
*/
@RequiredArgsConstructor
public class ModifierRepairRecipeSerializer<T extends Recipe<?> & IModifierRepairRecipe> implements LoggingRecipeSerializer<T> {
private final IFactory<T> factory;
@Override
public T fromJson(ResourceLocation id, JsonObject json) {
ModifierId modifier = ModifierId.PARSER.getIfPresent(json, "modifier");
Ingredient ingredient = Ingredient.fromJson(JsonHelper.getElement(json, "ingredient"));
int repairAmount = GsonHelper.getAsInt(json, "repair_amount");
return factory.create(id, modifier, ingredient, repairAmount);
}
@Nullable
@Override
public T fromNetworkSafe(ResourceLocation id, FriendlyByteBuf buffer) {
ModifierId modifier = ModifierId.PARSER.fromNetwork(buffer);
Ingredient ingredient = Ingredient.fromNetwork(buffer);
int repairAmount = buffer.readVarInt();
return factory.create(id, modifier, ingredient, repairAmount);
}
@Override
public void toNetworkSafe(FriendlyByteBuf buffer, T recipe) {
buffer.writeResourceLocation(recipe.getModifier());
recipe.getIngredient().toNetwork(buffer);
buffer.writeVarInt(recipe.getRepairAmount());
}
/** Interface for serializing the recipe */
public interface IModifierRepairRecipe {
/** Gets the modifier needed to perform this recipe */
ModifierId getModifier();
/** Gets the ingredient used to repair this item */
Ingredient getIngredient();
/** Gets the amount repaired per item */
int getRepairAmount();
}
/** Factory constructor for this serializer */
@FunctionalInterface
public interface IFactory<T extends Recipe<?> & IModifierRepairRecipe> {
T create(ResourceLocation id, ModifierId modifier, Ingredient ingredient, int repairAmount);
}
}