-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement modifier material repair recipe
Cross between two existing recipes, to repair tools with a modifier using items of a material
- Loading branch information
1 parent
e704dc7
commit 04ebad7
Showing
7 changed files
with
254 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...ts/tconstruct/library/recipe/tinkerstation/repairing/ModifierMaterialRepairKitRecipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package slimeknights.tconstruct.library.recipe.tinkerstation.repairing; | ||
|
||
import com.mojang.datafixers.util.Pair; | ||
import lombok.Getter; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.inventory.CraftingContainer; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.crafting.RecipeSerializer; | ||
import net.minecraft.world.level.Level; | ||
import slimeknights.tconstruct.library.materials.definition.MaterialId; | ||
import slimeknights.tconstruct.library.modifiers.ModifierId; | ||
import slimeknights.tconstruct.library.recipe.tinkerstation.repairing.ModifierMaterialRepairSerializer.IModifierMaterialRepairRecipe; | ||
import slimeknights.tconstruct.library.tools.nbt.IToolStackView; | ||
import slimeknights.tconstruct.library.tools.nbt.ToolStack; | ||
import slimeknights.tconstruct.library.tools.part.IMaterialItem; | ||
import slimeknights.tconstruct.tables.recipe.CraftingTableRepairKitRecipe; | ||
import slimeknights.tconstruct.tools.TinkerModifiers; | ||
|
||
/** Recipe for using a repair kit in a crafting station for a specialized tool */ | ||
public class ModifierMaterialRepairKitRecipe extends CraftingTableRepairKitRecipe implements IModifierMaterialRepairRecipe { | ||
/** Tool that can be repaired with this recipe */ | ||
@Getter | ||
private final ModifierId modifier; | ||
/** ID of material used in repairing */ | ||
@Getter | ||
private final MaterialId repairMaterial; | ||
public ModifierMaterialRepairKitRecipe(ResourceLocation id, ModifierId modifier, MaterialId repairMaterial) { | ||
super(id); | ||
this.modifier = modifier; | ||
this.repairMaterial = repairMaterial; | ||
} | ||
|
||
@Override | ||
protected boolean toolMatches(ItemStack stack) { | ||
return ToolStack.from(stack).getModifierLevel(modifier) > 0; | ||
} | ||
|
||
@Override | ||
public boolean matches(CraftingContainer inv, Level worldIn) { | ||
Pair<ToolStack, ItemStack> inputs = getRelevantInputs(inv); | ||
return inputs != null && repairMaterial.equals(IMaterialItem.getMaterialFromStack(inputs.getSecond()).getId()); | ||
} | ||
|
||
@Override | ||
protected float getRepairAmount(IToolStackView tool, ItemStack repairStack) { | ||
return super.getRepairAmount(tool, repairStack) * tool.getModifierLevel(modifier); | ||
} | ||
|
||
@Override | ||
public RecipeSerializer<?> getSerializer() { | ||
return TinkerModifiers.craftingModifierMaterialRepair.get(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...ights/tconstruct/library/recipe/tinkerstation/repairing/ModifierMaterialRepairRecipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package slimeknights.tconstruct.library.recipe.tinkerstation.repairing; | ||
|
||
import lombok.Getter; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.crafting.RecipeSerializer; | ||
import net.minecraft.world.level.Level; | ||
import slimeknights.tconstruct.library.materials.definition.LazyMaterial; | ||
import slimeknights.tconstruct.library.materials.definition.MaterialId; | ||
import slimeknights.tconstruct.library.modifiers.ModifierId; | ||
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationContainer; | ||
import slimeknights.tconstruct.library.recipe.tinkerstation.repairing.ModifierMaterialRepairSerializer.IModifierMaterialRepairRecipe; | ||
import slimeknights.tconstruct.library.tools.nbt.IToolStackView; | ||
import slimeknights.tconstruct.library.tools.nbt.ToolStack; | ||
import slimeknights.tconstruct.tables.recipe.TinkerStationRepairRecipe; | ||
import slimeknights.tconstruct.tools.TinkerModifiers; | ||
|
||
/** | ||
* Recipe to repair a specialized tool in the tinker station | ||
*/ | ||
public class ModifierMaterialRepairRecipe extends TinkerStationRepairRecipe implements IModifierMaterialRepairRecipe { | ||
/** Tool that can be repaired with this recipe */ | ||
@Getter | ||
private final ModifierId modifier; | ||
/** ID of material used in repairing */ | ||
private final LazyMaterial repairMaterial; | ||
public ModifierMaterialRepairRecipe(ResourceLocation id, ModifierId modifier, MaterialId repairMaterialID) { | ||
super(id); | ||
this.modifier = modifier; | ||
this.repairMaterial = LazyMaterial.of(repairMaterialID); | ||
} | ||
|
||
@Override | ||
public MaterialId getRepairMaterial() { | ||
return repairMaterial.getId(); | ||
} | ||
|
||
@Override | ||
protected MaterialId getPrimaryMaterial(IToolStackView tool) { | ||
return repairMaterial.getId(); | ||
} | ||
|
||
@Override | ||
public boolean matches(ITinkerStationContainer inv, Level world) { | ||
if (repairMaterial.isUnknown()) { | ||
return false; | ||
} | ||
// must have the modifier | ||
if (ToolStack.from(inv.getTinkerableStack()).getModifierLevel(modifier) == 0) { | ||
return false; | ||
} | ||
return SpecializedRepairRecipe.findMaterialItem(inv, repairMaterial); | ||
} | ||
|
||
@Override | ||
protected float getRepairPerItem(ToolStack tool, ITinkerStationContainer inv, int slot, MaterialId repairMaterial) { | ||
return super.getRepairPerItem(tool, inv, slot, repairMaterial) * tool.getModifierLevel(modifier); | ||
} | ||
|
||
@Override | ||
public RecipeSerializer<?> getSerializer() { | ||
return TinkerModifiers.modifierMaterialRepair.get(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...construct/library/recipe/tinkerstation/repairing/ModifierMaterialRepairRecipeBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package slimeknights.tconstruct.library.recipe.tinkerstation.repairing; | ||
|
||
import com.google.gson.JsonObject; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import net.minecraft.data.recipes.FinishedRecipe; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.crafting.RecipeSerializer; | ||
import slimeknights.mantle.recipe.data.AbstractRecipeBuilder; | ||
import slimeknights.tconstruct.library.materials.definition.MaterialId; | ||
import slimeknights.tconstruct.library.modifiers.ModifierId; | ||
import slimeknights.tconstruct.library.modifiers.util.LazyModifier; | ||
import slimeknights.tconstruct.tools.TinkerModifiers; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.function.Consumer; | ||
|
||
/** Builds a recipe to repair a tool using a modifier via a material */ | ||
@RequiredArgsConstructor(staticName = "repair") | ||
public class ModifierMaterialRepairRecipeBuilder extends AbstractRecipeBuilder<ModifierMaterialRepairRecipeBuilder> { | ||
private final ModifierId modifier; | ||
private final MaterialId material; | ||
|
||
public static ModifierMaterialRepairRecipeBuilder repair(LazyModifier modifier, MaterialId material) { | ||
return repair(modifier.getId(), material); | ||
} | ||
|
||
@Override | ||
public void save(Consumer<FinishedRecipe> consumer) { | ||
save(consumer, modifier); | ||
} | ||
|
||
/** Builds the recipe for the crafting table using a repair kit */ | ||
public ModifierMaterialRepairRecipeBuilder saveCraftingTable(Consumer<FinishedRecipe> consumer, ResourceLocation id) { | ||
ResourceLocation advancementId = buildOptionalAdvancement(id, "tinker_station"); | ||
consumer.accept(new Finished(id, advancementId, TinkerModifiers.craftingModifierMaterialRepair.get())); | ||
return this; | ||
} | ||
|
||
@Override | ||
public void save(Consumer<FinishedRecipe> consumer, ResourceLocation id) { | ||
ResourceLocation advancementId = buildOptionalAdvancement(id, "tinker_station"); | ||
consumer.accept(new Finished(id, advancementId, TinkerModifiers.modifierMaterialRepair.get())); | ||
} | ||
|
||
private class Finished extends AbstractFinishedRecipe { | ||
@Getter | ||
private final RecipeSerializer<?> type; | ||
|
||
public Finished(ResourceLocation ID, @Nullable ResourceLocation advancementID, RecipeSerializer<?> type) { | ||
super(ID, advancementID); | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public void serializeRecipeData(JsonObject json) { | ||
json.addProperty("modifier", modifier.toString()); | ||
json.addProperty("material", material.toString()); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...s/tconstruct/library/recipe/tinkerstation/repairing/ModifierMaterialRepairSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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.Ingredient; | ||
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> extends LoggingRecipeSerializer<T> { | ||
private final IFactory<T> factory; | ||
|
||
@Override | ||
public T fromJson(ResourceLocation id, JsonObject json) { | ||
Ingredient tool = Ingredient.fromJson(JsonHelper.getElement(json, "tool")); | ||
ModifierId modifier = new ModifierId(JsonHelper.getResourceLocation(json, "modifier")); | ||
MaterialId repairMaterial = MaterialId.fromJson(json, "repair_material"); | ||
return factory.create(id, modifier, repairMaterial); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected 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 | ||
protected 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters