Search in sources :

Example 1 with SmeltingRecipe

use of org.spongepowered.api.item.recipe.smelting.SmeltingRecipe in project SpongeCommon by SpongePowered.

the class MixinFurnaceRecipes method findMatchingRecipe.

@Override
public Optional<SmeltingRecipe> findMatchingRecipe(ItemStackSnapshot ingredient) {
    checkNotNull(ingredient, "ingredient");
    for (SmeltingRecipe customRecipe : this.customRecipes) {
        if (customRecipe.isValid(ingredient)) {
            return Optional.of(customRecipe);
        }
    }
    ItemStack nativeIngredient = ItemStackUtil.fromSnapshotToNative(ingredient);
    for (Map.Entry<ItemStack, ItemStack> entry : this.smeltingList.entrySet()) {
        ItemStack nativeIngredientPrecise = entry.getKey();
        if (compareItemStacks(nativeIngredient, nativeIngredientPrecise)) {
            ItemStack nativeExemplaryResult = entry.getValue();
            ItemStackSnapshot result = ItemStackUtil.snapshotOf(nativeExemplaryResult);
            ItemStackSnapshot ingredientPrecise = ItemStackUtil.snapshotOf(nativeIngredientPrecise);
            Predicate<ItemStackSnapshot> ingredientPredicate = new MatchSmeltingVanillaItemStack(ingredientPrecise);
            double experience = this.experienceList.get(nativeExemplaryResult);
            SmeltingRecipe recipe = new SpongeSmeltingRecipe(result, ingredientPrecise, ingredientPredicate, experience);
            return Optional.of(recipe);
        }
    }
    return Optional.empty();
}
Also used : SpongeSmeltingRecipe(org.spongepowered.common.item.recipe.smelting.SpongeSmeltingRecipe) SmeltingRecipe(org.spongepowered.api.item.recipe.smelting.SmeltingRecipe) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SpongeSmeltingRecipe(org.spongepowered.common.item.recipe.smelting.SpongeSmeltingRecipe) ItemStack(net.minecraft.item.ItemStack) MatchSmeltingVanillaItemStack(org.spongepowered.common.item.recipe.smelting.MatchSmeltingVanillaItemStack) MatchSmeltingVanillaItemStack(org.spongepowered.common.item.recipe.smelting.MatchSmeltingVanillaItemStack) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 2 with SmeltingRecipe

use of org.spongepowered.api.item.recipe.smelting.SmeltingRecipe in project LanternServer by LanternPowered.

the class FurnaceShiftClickBehavior method getTarget.

@Override
public IInventory getTarget(LanternContainer container, AbstractInventorySlot slot) {
    if (container.getOpenInventory().containsInventory(slot)) {
        return getDefaultTarget(container, slot);
    }
    // The item stack should be present
    // Wrap, peek creates a copy
    final ItemStackSnapshot snapshot = LanternItemStackSnapshot.wrap(slot.peek().get());
    // Check if the item can be used as a ingredient
    final Optional<SmeltingRecipe> optSmeltingRecipe = Lantern.getRegistry().getSmeltingRecipeRegistry().findMatchingRecipe(snapshot);
    final List<IInventory> inventories = new ArrayList<>();
    final FurnaceInventory furnaceInventory = (FurnaceInventory) container.getOpenInventory();
    if (optSmeltingRecipe.isPresent()) {
        inventories.add(furnaceInventory.getInputSlot());
    }
    // Check if the item can be used as a fuel
    final Optional<IFuel> optFuel = Lantern.getRegistry().getFuelRegistry().findMatching(snapshot);
    if (optFuel.isPresent()) {
        inventories.add(furnaceInventory.getFuelSlot());
    }
    return inventories.isEmpty() ? getDefaultTarget(container, slot) : inventories.size() == 1 ? inventories.get(0) : inventories.get(0).union(inventories.get(1));
}
Also used : IInventory(org.lanternpowered.server.inventory.IInventory) SmeltingRecipe(org.spongepowered.api.item.recipe.smelting.SmeltingRecipe) ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) LanternItemStackSnapshot(org.lanternpowered.server.inventory.LanternItemStackSnapshot) IFuel(org.lanternpowered.server.item.recipe.fuel.IFuel)

Example 3 with SmeltingRecipe

use of org.spongepowered.api.item.recipe.smelting.SmeltingRecipe in project LanternServer by LanternPowered.

the class LanternFurnace method pulse.

@Override
public void pulse() {
    super.pulse();
    if (this.lastTick == -1) {
        this.lastTick = LanternGame.currentTimeTicks();
        return;
    }
    final long ticks = LanternGame.currentTimeTicks();
    long elapsed = ticks - this.lastTick;
    // This shouldn't happen
    if (elapsed == 0) {
        return;
    }
    this.lastTick = ticks;
    while (elapsed > 0) {
        int maxCookTime = 0;
        Optional<SmeltingResult> smeltingResult = Optional.empty();
        Optional<SmeltingRecipe> smeltingRecipe = Optional.empty();
        ItemStack itemStack = this.inventory.getInputSlot().getRawItemStack();
        final ItemStackSnapshot inputSlotItemSnapshot = itemStack == null ? null : itemStack.createSnapshot();
        if (inputSlotItemSnapshot != null) {
            // Check if the item can be smelted, this means finding a compatible
            // recipe and the output has to be empty.
            smeltingRecipe = Lantern.getRegistry().getSmeltingRecipeRegistry().findMatchingRecipe(inputSlotItemSnapshot);
            if (smeltingRecipe.isPresent()) {
                final int quantity = ((ISmeltingRecipe) smeltingRecipe.get()).getIngredient().getQuantity(inputSlotItemSnapshot);
                if (inputSlotItemSnapshot.getQuantity() >= quantity) {
                    smeltingResult = smeltingRecipe.get().getResult(inputSlotItemSnapshot);
                    // Check if the item can be smelted
                    if (smeltingResult.isPresent()) {
                        // Check if the result could be added to the output
                        final PeekedOfferTransactionResult peekResult = this.inventory.getOutputSlot().peekOffer(smeltingResult.get().getResult().createStack());
                        if (peekResult.isSuccess()) {
                            maxCookTime = ((ISmeltingRecipe) smeltingRecipe.get()).getSmeltTime(inputSlotItemSnapshot).orElse(200);
                        }
                    }
                }
            }
        }
        // The ticks that are elapsed in this loop, limit
        // this to one cooking cycle, this can only happen
        // if actually a item is being cooked
        long elapsed1 = elapsed;
        int elapsedCookTime = get(Keys.PASSED_COOK_TIME).get();
        int remainingCookTime = maxCookTime - elapsedCookTime;
        if (maxCookTime > 0 && elapsed1 > remainingCookTime) {
            elapsed1 = remainingCookTime;
        }
        elapsed -= elapsed1;
        // Burn items until the furnace is burning properly
        int maxBurnTime = get(Keys.MAX_BURN_TIME).get();
        int elapsedBurnTime = get(Keys.PASSED_BURN_TIME).get();
        int remainingBurnTime = maxBurnTime - elapsedBurnTime;
        long elapsed2 = elapsed1;
        while (elapsed2 >= remainingBurnTime) {
            elapsed2 -= remainingBurnTime;
            // Reset the max burn time
            maxBurnTime = 0;
            // Only burn a new item if the target item can be smelted
            itemStack = this.inventory.getFuelSlot().getRawItemStack();
            if (itemStack != null && maxCookTime > 0) {
                // Check for the next fuel item
                final ItemStackSnapshot itemStackSnapshot = itemStack.createSnapshot();
                final Optional<IFuel> result = Lantern.getRegistry().getFuelRegistry().findMatching(itemStackSnapshot);
                if (result.isPresent()) {
                    final OptionalInt optBurnTime = result.get().getBurnTime(itemStackSnapshot);
                    // We have a next matching burn item, check if we can poll one and then continue burning
                    if (optBurnTime.isPresent() && this.inventory.getFuelSlot().poll(1).isPresent()) {
                        maxBurnTime = optBurnTime.getAsInt();
                        remainingBurnTime = maxBurnTime;
                        elapsedBurnTime = 0;
                        // Put the rest item in the slot, if the slot is empty
                        if (this.inventory.getFuelSlot().size() == 0) {
                            final IIngredient ingredient = result.get().getIngredient();
                            final Optional<ItemStack> remainingItem = ingredient.getRemainingItem(itemStackSnapshot);
                            remainingItem.ifPresent(this.inventory.getFuelSlot()::setForced);
                        }
                    }
                }
            }
            if (maxBurnTime == 0) {
                break;
            }
        }
        elapsedBurnTime = maxBurnTime == 0 ? 0 : (int) (elapsedBurnTime + elapsed2);
        remainingBurnTime = maxBurnTime - elapsedBurnTime;
        offer(Keys.MAX_BURN_TIME, maxBurnTime);
        offer(Keys.PASSED_BURN_TIME, elapsedBurnTime);
        if (maxCookTime > 0) {
            // The furnace is still burning
            if (remainingBurnTime > 0) {
                // The item is smelted
                if (elapsed1 >= remainingCookTime) {
                    offer(Keys.MAX_COOK_TIME, 0);
                    offer(Keys.PASSED_COOK_TIME, 0);
                    final int quantity = ((ISmeltingRecipe) smeltingRecipe.get()).getIngredient().getQuantity(inputSlotItemSnapshot);
                    this.inventory.getOutputSlot().offer(smeltingResult.get().getResult().createStack());
                    this.inventory.getInputSlot().poll(quantity);
                    // Put the rest item in the slot
                    if (this.inventory.getInputSlot().size() == 0) {
                        final IIngredient ingredient = ((ISmeltingRecipe) smeltingRecipe.get()).getIngredient();
                        final Optional<ItemStack> remainingItem = ingredient.getRemainingItem(inputSlotItemSnapshot);
                        remainingItem.ifPresent(this.inventory.getInputSlot()::set);
                    }
                } else {
                    // Keep on smelting
                    offer(Keys.MAX_COOK_TIME, maxCookTime);
                    offer(Keys.PASSED_COOK_TIME, (int) (elapsedCookTime + elapsed1));
                    break;
                }
            } else if (elapsedCookTime > 0) {
                // Undo smelting progress
                final long time = elapsedCookTime - elapsed1 * 2;
                offer(Keys.MAX_COOK_TIME, time <= 0 ? 0 : maxCookTime);
                offer(Keys.PASSED_COOK_TIME, (int) (time <= 0 ? 0 : time));
                break;
            }
        } else {
            offer(Keys.MAX_COOK_TIME, 0);
            offer(Keys.PASSED_COOK_TIME, 0);
        }
    }
    BlockState blockState = getLocation().getBlock();
    final boolean burning = get(Keys.PASSED_BURN_TIME).get() < get(Keys.MAX_BURN_TIME).get();
    final boolean blockBurning = blockState.getType() == BlockTypes.LIT_FURNACE;
    if (burning != blockBurning) {
        blockState = (burning ? BlockTypes.LIT_FURNACE : BlockTypes.FURNACE).getDefaultState().withTrait(LanternEnumTraits.HORIZONTAL_FACING, blockState.getTraitValue(LanternEnumTraits.HORIZONTAL_FACING).get()).get();
        getLocation().setBlock(blockState);
    }
}
Also used : IIngredient(org.lanternpowered.server.item.recipe.IIngredient) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) OptionalInt(java.util.OptionalInt) BlockState(org.spongepowered.api.block.BlockState) SmeltingRecipe(org.spongepowered.api.item.recipe.smelting.SmeltingRecipe) ISmeltingRecipe(org.lanternpowered.server.item.recipe.smelting.ISmeltingRecipe) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) IFuel(org.lanternpowered.server.item.recipe.fuel.IFuel) ISmeltingRecipe(org.lanternpowered.server.item.recipe.smelting.ISmeltingRecipe) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SmeltingResult(org.spongepowered.api.item.recipe.smelting.SmeltingResult)

Example 4 with SmeltingRecipe

use of org.spongepowered.api.item.recipe.smelting.SmeltingRecipe in project LanternServer by LanternPowered.

the class LanternFurnace method smelt.

@Override
public boolean smelt() {
    final ItemStack itemStack = this.inventory.getInputSlot().getRawItemStack();
    if (itemStack != null) {
        // Check if the item can be smelted, this means finding a compatible
        // recipe and the output has to be empty.
        final ItemStackSnapshot itemStackSnapshot = itemStack.createSnapshot();
        final Optional<SmeltingRecipe> smeltingRecipe = Lantern.getRegistry().getSmeltingRecipeRegistry().findMatchingRecipe(itemStackSnapshot);
        final Optional<SmeltingResult> smeltingResult = smeltingRecipe.flatMap(recipe -> recipe.getResult(itemStackSnapshot));
        // Check if the item can be smelted
        if (smeltingResult.isPresent()) {
            final int quantity = ((ISmeltingRecipe) smeltingRecipe.get()).getIngredient().getQuantity(itemStackSnapshot);
            if (itemStack.getQuantity() >= quantity) {
                final ItemStack result = smeltingResult.get().getResult().createStack();
                // Check if the result could be added to the output
                final PeekedOfferTransactionResult peekResult = this.inventory.getOutputSlot().peekOffer(result);
                if (peekResult.isSuccess()) {
                    this.inventory.getInputSlot().poll(quantity);
                    this.inventory.getOutputSlot().offer(result);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : SmeltingRecipe(org.spongepowered.api.item.recipe.smelting.SmeltingRecipe) ISmeltingRecipe(org.lanternpowered.server.item.recipe.smelting.ISmeltingRecipe) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SmeltingResult(org.spongepowered.api.item.recipe.smelting.SmeltingResult)

Example 5 with SmeltingRecipe

use of org.spongepowered.api.item.recipe.smelting.SmeltingRecipe in project SpongeCommon by SpongePowered.

the class MixinFurnaceRecipes method getRecipes.

@Override
public Collection<SmeltingRecipe> getRecipes() {
    ImmutableList.Builder<SmeltingRecipe> builder = ImmutableList.builder();
    for (Map.Entry<ItemStack, ItemStack> smeltingEntry : this.smeltingList.entrySet()) {
        ItemStack nativeIngredient = smeltingEntry.getKey();
        // If not a custom recipe, add first
        if (!this.nativeIngredientToCustomRecipe.containsKey(nativeIngredient)) {
            ItemStack nativeExemplaryResult = smeltingEntry.getValue();
            ItemStackSnapshot exemplaryResult = ItemStackUtil.snapshotOf(nativeExemplaryResult);
            ItemStackSnapshot exemplaryIngredient = ItemStackUtil.snapshotOf(nativeIngredient);
            Predicate<ItemStackSnapshot> ingredientPredicate = new MatchSmeltingVanillaItemStack(exemplaryIngredient);
            double experience = (double) this.experienceList.get(nativeExemplaryResult);
            builder.add(new SpongeSmeltingRecipe(exemplaryResult, exemplaryIngredient, ingredientPredicate, experience));
        }
    }
    builder.addAll(this.customRecipes);
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SpongeSmeltingRecipe(org.spongepowered.common.item.recipe.smelting.SpongeSmeltingRecipe) SmeltingRecipe(org.spongepowered.api.item.recipe.smelting.SmeltingRecipe) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SpongeSmeltingRecipe(org.spongepowered.common.item.recipe.smelting.SpongeSmeltingRecipe) ItemStack(net.minecraft.item.ItemStack) MatchSmeltingVanillaItemStack(org.spongepowered.common.item.recipe.smelting.MatchSmeltingVanillaItemStack) MatchSmeltingVanillaItemStack(org.spongepowered.common.item.recipe.smelting.MatchSmeltingVanillaItemStack) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Aggregations

ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)5 SmeltingRecipe (org.spongepowered.api.item.recipe.smelting.SmeltingRecipe)5 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 ItemStack (net.minecraft.item.ItemStack)2 PeekedOfferTransactionResult (org.lanternpowered.server.inventory.PeekedOfferTransactionResult)2 IFuel (org.lanternpowered.server.item.recipe.fuel.IFuel)2 ISmeltingRecipe (org.lanternpowered.server.item.recipe.smelting.ISmeltingRecipe)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 SmeltingResult (org.spongepowered.api.item.recipe.smelting.SmeltingResult)2 MatchSmeltingVanillaItemStack (org.spongepowered.common.item.recipe.smelting.MatchSmeltingVanillaItemStack)2 SpongeSmeltingRecipe (org.spongepowered.common.item.recipe.smelting.SpongeSmeltingRecipe)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 OptionalInt (java.util.OptionalInt)1 IInventory (org.lanternpowered.server.inventory.IInventory)1 LanternItemStackSnapshot (org.lanternpowered.server.inventory.LanternItemStackSnapshot)1 IIngredient (org.lanternpowered.server.item.recipe.IIngredient)1 BlockState (org.spongepowered.api.block.BlockState)1