/
ModifierSalvage.java
143 lines (131 loc) · 5.49 KB
/
ModifierSalvage.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package slimeknights.tconstruct.library.recipe.modifiers;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import lombok.Getter;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import slimeknights.mantle.recipe.ICustomOutputRecipe;
import slimeknights.mantle.recipe.helper.LoggingRecipeSerializer;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.library.modifiers.ModifierId;
import slimeknights.tconstruct.library.recipe.TinkerRecipeTypes;
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationRecipe;
import slimeknights.tconstruct.library.tools.SlotType.SlotCount;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.utils.JsonUtils;
import slimeknights.tconstruct.tools.TinkerModifiers;
import javax.annotation.Nullable;
/**
* Shared logic for main types of salvage recipes
*/
public class ModifierSalvage implements ICustomOutputRecipe<Container> {
@Getter
protected final ResourceLocation id;
/** Ingredient determining tools matched by this */
protected final Ingredient toolIngredient;
/** Max size of the tool for this modifier. If the tool size is smaller, the salvage bonus will be reduced */
@Getter
protected final int maxToolSize;
/** Modifier represented by this recipe */
@Getter
protected final ModifierId modifier;
/** Minimum level of the modifier for this to be applicable */
protected final int minLevel;
/** Maximum level of the modifier for this to be applicable */
protected final int maxLevel;
/** Slots restored by this recipe, if null no slots are restored */
@Nullable
protected final SlotCount slots;
public ModifierSalvage(ResourceLocation id, Ingredient toolIngredient, int maxToolSize, ModifierId modifier, int minLevel, int maxLevel, @Nullable SlotCount slots) {
this.id = id;
this.toolIngredient = toolIngredient;
this.maxToolSize = maxToolSize;
this.modifier = modifier;
this.minLevel = minLevel;
this.maxLevel = maxLevel;
this.slots = slots;
ModifierRecipeLookup.addSalvage(this);
}
/**
* Checks if the given tool stack and level are applicable for this salvage
* @param stack Tool item stack
* @param tool Tool stack instance, for potential extensions
* @param originalLevel Level to check
* @return True if this salvage is applicable
*/
@SuppressWarnings("unused")
public boolean matches(ItemStack stack, IToolStackView tool, int originalLevel) {
return originalLevel >= minLevel && originalLevel <= maxLevel && toolIngredient.test(stack);
}
/**
* Updates the tool data in light of removing this modifier
* @param tool Tool instance
*/
public void updateTool(IToolStackView tool) {
if (slots != null) {
tool.getPersistentData().addSlots(slots.type(), slots.count());
}
}
@Override
public RecipeType<?> getType() {
return TinkerRecipeTypes.DATA.get();
}
/** @deprecated Use {@link #matches(ItemStack, IToolStackView, int)} */
@Deprecated
@Override
public boolean matches(Container inv, Level level) {
return false;
}
@Override
public RecipeSerializer<?> getSerializer() {
return TinkerModifiers.modifierSalvageSerializer.get();
}
/**
* Serializer instance
*/
public static class Serializer implements LoggingRecipeSerializer<ModifierSalvage> {
@Override
public ModifierSalvage fromJson(ResourceLocation id, JsonObject json) {
Ingredient toolIngredient = Ingredient.fromJson(JsonHelper.getElement(json, "tools"));
int maxToolSize = GsonHelper.getAsInt(json, "max_tool_size", ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE);
ModifierId modifier = ModifierId.PARSER.getIfPresent(json, "modifier");
int minLevel = JsonUtils.getIntMin(json, "min_level", 1);
int maxLevel = GsonHelper.getAsInt(json, "max_level", Integer.MAX_VALUE);
if (maxLevel < minLevel) {
throw new JsonSyntaxException("Max level must be greater than or equal to min level");
}
SlotCount slots = null;
if (json.has("slots")) {
slots = SlotCount.fromJson(GsonHelper.getAsJsonObject(json, "slots"));
}
return new ModifierSalvage(id, toolIngredient, maxToolSize, modifier, minLevel, maxLevel, slots);
}
@Nullable
@Override
public ModifierSalvage fromNetworkSafe(ResourceLocation id, FriendlyByteBuf buffer) {
Ingredient toolIngredient = Ingredient.fromNetwork(buffer);
int maxToolSize = buffer.readVarInt();
ModifierId modifier = ModifierId.PARSER.fromNetwork(buffer);
int minLevel = buffer.readVarInt();
int maxLevel = buffer.readVarInt();
SlotCount slots = SlotCount.read(buffer);
return new ModifierSalvage(id, toolIngredient, maxToolSize, modifier, minLevel, maxLevel, slots);
}
@Override
public void toNetworkSafe(FriendlyByteBuf buffer, ModifierSalvage recipe) {
recipe.toolIngredient.toNetwork(buffer);
buffer.writeVarInt(recipe.getMaxToolSize());
buffer.writeResourceLocation(recipe.modifier);
buffer.writeVarInt(recipe.minLevel);
buffer.writeVarInt(recipe.maxLevel);
SlotCount.write(recipe.slots, buffer);
}
}
}