Search in sources :

Example 1 with AbstractModifierSalvage

use of slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage in project TinkersConstruct by SlimeKnights.

the class ModifierRemovalRecipe method getValidatedResult.

@Override
public ValidatedResult getValidatedResult(ITinkerStationContainer inv) {
    ItemStack toolStack = inv.getTinkerableStack();
    ToolStack tool = ToolStack.from(toolStack);
    List<ModifierEntry> modifiers = tool.getUpgrades().getModifiers();
    if (modifiers.isEmpty()) {
        return NO_MODIFIERS;
    }
    // find the modifier to remove
    ModifierEntry toRemove = getModifierToRemove(inv, modifiers);
    if (toRemove == null) {
        return ValidatedResult.PASS;
    }
    // salvage
    tool = tool.copy();
    Modifier modifier = toRemove.getModifier();
    AbstractModifierSalvage salvage = ModifierRecipeLookup.getSalvage(toolStack, tool, modifier, toRemove.getLevel());
    // restore the slots
    if (salvage != null) {
        salvage.updateTool(tool);
    }
    // first remove hook, primarily for removing raw NBT which is highly discouraged using
    int newLevel = tool.getModifierLevel(modifier) - 1;
    if (newLevel <= 0) {
        modifier.beforeRemoved(tool, tool.getRestrictedNBT());
    }
    // remove the actual modifier
    tool.removeModifier(modifier, 1);
    // second remove hook, useful for removing modifier specific state data
    if (newLevel <= 0) {
        modifier.onRemoved(tool);
    }
    // ensure the tool is still valid
    ValidatedResult validated = tool.validate();
    if (validated.hasError()) {
        return validated;
    }
    // if this was the last level, validate the tool is still valid without it
    if (newLevel <= 0) {
        validated = modifier.validate(tool, 0);
        if (validated.hasError()) {
            return validated;
        }
    }
    // check the modifier requirements
    // creating a stack to make it as accurate as possible, though the old stack should be sufficient
    ItemStack resultStack = tool.createStack(Math.min(toolStack.getCount(), shrinkToolSlotBy()));
    validated = ModifierRecipeLookup.checkRequirements(resultStack, tool);
    if (validated.hasError()) {
        return validated;
    }
    // successfully removed
    return ValidatedResult.success(resultStack);
}
Also used : ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) ItemStack(net.minecraft.world.item.ItemStack) ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) Modifier(slimeknights.tconstruct.library.modifiers.Modifier) AbstractModifierSalvage(slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage)

Example 2 with AbstractModifierSalvage

use of slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage in project TinkersConstruct by SlimeKnights.

the class ModifierRemovalRecipe method updateInputs.

@Override
public void updateInputs(ItemStack result, IMutableTinkerStationContainer inv, boolean isServer) {
    // return salvage items for modifier, using the original tool as that still has the modifier
    if (isServer) {
        ItemStack toolStack = inv.getTinkerableStack();
        ToolStack tool = ToolStack.from(toolStack);
        ModifierEntry toRemove = getModifierToRemove(inv, tool.getUpgrades().getModifiers());
        if (toRemove != null) {
            AbstractModifierSalvage salvage = ModifierRecipeLookup.getSalvage(toolStack, tool, toRemove.getModifier(), toRemove.getLevel());
            if (salvage != null) {
                int salvageMax = Math.min(toolStack.getMaxStackSize(), salvage.getMaxToolSize());
                int currentSize = result.getCount();
                Consumer<ItemStack> consumer;
                // if the size is smaller than 16, shrink all salvage stack sizes to prevent a salvage dupe
                if (currentSize < salvageMax) {
                    consumer = stack -> {
                        int newSize = stack.getCount() * currentSize / salvageMax;
                        if (newSize > 0) {
                            stack.setCount(newSize);
                            inv.giveItem(stack);
                        }
                    };
                // if larger, grow salvage
                } else if (currentSize > salvageMax) {
                    consumer = stack -> {
                        int newSize = stack.getCount() * currentSize / salvageMax;
                        int maxStackSize = stack.getMaxStackSize();
                        while (newSize > maxStackSize) {
                            inv.giveItem(ItemHandlerHelper.copyStackWithSize(stack, maxStackSize));
                            newSize -= maxStackSize;
                        }
                        if (newSize > 0) {
                            stack.setCount(newSize);
                            inv.giveItem(stack);
                        }
                    };
                } else {
                    consumer = inv::giveItem;
                }
                salvage.acceptItems(tool, consumer, TConstruct.RANDOM);
            }
        }
    }
    // remove the input item, done second as we need its location for salvage
    for (int i = 0; i < inv.getInputCount(); i++) {
        ItemStack stack = inv.getInput(i);
        if (!stack.isEmpty() && ingredient.test(stack)) {
            inv.shrinkInput(i, 1, container.copy());
            break;
        }
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) JsonObject(com.google.gson.JsonObject) LoggingRecipeSerializer(slimeknights.mantle.recipe.helper.LoggingRecipeSerializer) Getter(lombok.Getter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ModifierRecipeLookup(slimeknights.tconstruct.library.recipe.modifiers.ModifierRecipeLookup) TinkerModifiers(slimeknights.tconstruct.tools.TinkerModifiers) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) Ingredient(net.minecraft.world.item.crafting.Ingredient) AbstractRecipeBuilder(slimeknights.mantle.recipe.data.AbstractRecipeBuilder) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) AbstractModifierSalvage(slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage) Nullable(javax.annotation.Nullable) RecipeSerializer(net.minecraft.world.item.crafting.RecipeSerializer) TinkerTags(slimeknights.tconstruct.common.TinkerTags) IncrementalModifierRecipeBuilder(slimeknights.tconstruct.library.recipe.modifiers.adding.IncrementalModifierRecipeBuilder) ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) FinishedRecipe(net.minecraft.data.recipes.FinishedRecipe) ITinkerStationContainer(slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationContainer) IncrementalModifierRecipe(slimeknights.tconstruct.library.recipe.modifiers.adding.IncrementalModifierRecipe) ITinkerStationRecipe(slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationRecipe) TConstruct(slimeknights.tconstruct.TConstruct) Objects(java.util.Objects) Consumer(java.util.function.Consumer) JsonHelper(slimeknights.mantle.util.JsonHelper) List(java.util.List) Modifier(slimeknights.tconstruct.library.modifiers.Modifier) IMutableTinkerStationContainer(slimeknights.tconstruct.library.recipe.tinkerstation.IMutableTinkerStationContainer) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) ItemStack(net.minecraft.world.item.ItemStack) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) AbstractModifierSalvage(slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage)

Aggregations

ItemStack (net.minecraft.world.item.ItemStack)2 Modifier (slimeknights.tconstruct.library.modifiers.Modifier)2 ModifierEntry (slimeknights.tconstruct.library.modifiers.ModifierEntry)2 AbstractModifierSalvage (slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage)2 ValidatedResult (slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult)2 ToolStack (slimeknights.tconstruct.library.tools.nbt.ToolStack)2 JsonObject (com.google.gson.JsonObject)1 List (java.util.List)1 Objects (java.util.Objects)1 Consumer (java.util.function.Consumer)1 Nullable (javax.annotation.Nullable)1 Getter (lombok.Getter)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 FinishedRecipe (net.minecraft.data.recipes.FinishedRecipe)1 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 Ingredient (net.minecraft.world.item.crafting.Ingredient)1 RecipeSerializer (net.minecraft.world.item.crafting.RecipeSerializer)1 Level (net.minecraft.world.level.Level)1 ItemHandlerHelper (net.minecraftforge.items.ItemHandlerHelper)1