Search in sources :

Example 1 with SalvagingRecipe

use of net.silentchaos512.gear.crafting.recipe.salvage.SalvagingRecipe in project Silent-Gear by SilentChaos512.

the class SalvagerTileEntity method getSalvagedPartsWithChance.

private Collection<ItemStack> getSalvagedPartsWithChance(SalvagingRecipe recipe, ItemStack stack) {
    double lossRate = getLossRate(stack);
    SilentGear.LOGGER.debug("Loss rate for '{}': {}", stack, lossRate);
    ImmutableList.Builder<ItemStack> builder = ImmutableList.builder();
    for (ItemStack part : recipe.getPossibleResults(this)) {
        ItemStack copy = part.copy();
        int count = copy.getCount();
        PartData partData = PartData.from(part);
        double partLossRate = partData != null ? partData.get().getSalvageLossRate(partData, stack, lossRate) : lossRate;
        for (int i = 0; i < count; ++i) {
            if (MathUtils.tryPercentage(SilentGear.RANDOM, partLossRate)) {
                copy.shrink(1);
            }
        }
        if (!copy.isEmpty()) {
            builder.add(copy);
        }
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) PartData(net.silentchaos512.gear.gear.part.PartData) ItemStack(net.minecraft.world.item.ItemStack)

Example 2 with SalvagingRecipe

use of net.silentchaos512.gear.crafting.recipe.salvage.SalvagingRecipe in project Silent-Gear by SilentChaos512.

the class SalvagerTileEntity method tick.

public static void tick(Level level, BlockPos pos, BlockState state, SalvagerTileEntity blockEntity) {
    ItemStack input = blockEntity.getItem(0);
    SalvagingRecipe recipe = blockEntity.getRecipe();
    if (recipe != null) {
        if (blockEntity.progress < BASE_WORK_TIME) {
            ++blockEntity.progress;
        }
        if (blockEntity.progress >= BASE_WORK_TIME && blockEntity.areAllOutputSlotsFree()) {
            for (ItemStack stack : blockEntity.getSalvagedPartsWithChance(recipe, input)) {
                int slot = blockEntity.getFreeOutputSlot();
                if (slot > 0) {
                    blockEntity.setItem(slot, stack);
                } else {
                    SilentGear.LOGGER.warn("Item lost in salvager: {}", stack);
                }
            }
            blockEntity.progress = 0;
            input.shrink(1);
            if (input.isEmpty()) {
                blockEntity.setItem(0, ItemStack.EMPTY);
            }
        }
    } else {
        blockEntity.progress = 0;
    }
}
Also used : SalvagingRecipe(net.silentchaos512.gear.crafting.recipe.salvage.SalvagingRecipe) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

ItemStack (net.minecraft.world.item.ItemStack)2 ImmutableList (com.google.common.collect.ImmutableList)1 SalvagingRecipe (net.silentchaos512.gear.crafting.recipe.salvage.SalvagingRecipe)1 PartData (net.silentchaos512.gear.gear.part.PartData)1