Search in sources :

Example 6 with Inventory

use of net.minecraft.inventory.Inventory in project Polymorph by TheIllusiveC4.

the class AbstractRecipeData method getRecipe.

@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Inventory> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, World pWorld, List<T> pRecipes) {
    this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().get(id)).ifPresent(selected -> {
        try {
            if (selected.getType() == pType && (((T) selected).matches(pInventory, pWorld) || isEmpty(pInventory))) {
                this.setSelectedRecipe(selected);
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
        }
        this.loadedRecipe = null;
    });
    if (this.isEmpty(pInventory)) {
        this.setFailing(false);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    AtomicReference<T> ref = new AtomicReference<>(null);
    this.getLastRecipe().ifPresent(recipe -> {
        try {
            if (recipe.getType() == pType && ((T) recipe).matches(pInventory, pWorld)) {
                this.getSelectedRecipe().ifPresent(selected -> {
                    try {
                        if (selected.getType() == pType && ((T) selected).matches(pInventory, pWorld)) {
                            ref.set((T) selected);
                        }
                    } catch (ClassCastException e) {
                        PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
                    }
                });
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", recipe.getId(), pInventory);
        }
    });
    T result = ref.get();
    if (result != null) {
        this.setFailing(false);
        this.sendRecipesListToListeners(false);
        return Optional.of(result);
    }
    SortedSet<RecipePair> newDataset = new TreeSet<>();
    List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getAllMatches(pType, pInventory, pWorld) : pRecipes;
    if (recipes.isEmpty()) {
        this.setFailing(true);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    for (T entry : recipes) {
        Identifier id = entry.getId();
        if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
            ref.set(entry);
        }
        newDataset.add(new RecipePairImpl(id, entry.craft(pInventory)));
    }
    this.setRecipesList(newDataset);
    result = ref.get();
    result = result != null ? result : recipes.get(0);
    this.lastRecipe = result;
    this.setSelectedRecipe(result);
    this.setFailing(false);
    this.sendRecipesListToListeners(false);
    return Optional.of(result);
}
Also used : Pair(net.minecraft.util.Pair) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) SortedSet(java.util.SortedSet) NbtElement(net.minecraft.nbt.NbtElement) World(net.minecraft.world.World) NbtList(net.minecraft.nbt.NbtList) Set(java.util.Set) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) PolymorphMod(top.theillusivec4.polymorph.common.PolymorphMod) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) List(java.util.List) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) NbtType(net.fabricmc.fabric.api.util.NbtType) RecipeData(top.theillusivec4.polymorph.api.common.component.RecipeData) Optional(java.util.Optional) Identifier(net.minecraft.util.Identifier) PolymorphApi(top.theillusivec4.polymorph.api.PolymorphApi) RecipeType(net.minecraft.recipe.RecipeType) Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) TreeSet(java.util.TreeSet) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 7 with Inventory

use of net.minecraft.inventory.Inventory in project Polymorph by TheIllusiveC4.

the class AbstractStackRecipeData method getRecipe.

@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Inventory> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, World pWorld, List<T> pRecipes) {
    this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().get(id)).ifPresent(selected -> {
        try {
            if (selected.getType() == pType && (((T) selected).matches(pInventory, pWorld) || isEmpty(pInventory))) {
                this.setSelectedRecipe(selected);
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
        }
        this.loadedRecipe = null;
    });
    if (this.isEmpty(pInventory)) {
        this.setFailing(false);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    AtomicReference<T> ref = new AtomicReference<>(null);
    this.getLastRecipe().ifPresent(recipe -> {
        try {
            if (recipe.getType() == pType && ((T) recipe).matches(pInventory, pWorld)) {
                this.getSelectedRecipe().ifPresent(selected -> {
                    try {
                        if (selected.getType() == pType && ((T) selected).matches(pInventory, pWorld)) {
                            ref.set((T) selected);
                        }
                    } catch (ClassCastException e) {
                        PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
                    }
                });
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", recipe.getId(), pInventory);
        }
    });
    T result = ref.get();
    if (result != null) {
        this.setFailing(false);
        this.sendRecipesListToListeners(false);
        return Optional.of(result);
    }
    SortedSet<RecipePair> newDataset = new TreeSet<>();
    List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getAllMatches(pType, pInventory, pWorld) : pRecipes;
    if (recipes.isEmpty()) {
        this.setFailing(true);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    for (T entry : recipes) {
        Identifier id = entry.getId();
        if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
            ref.set(entry);
        }
        newDataset.add(new RecipePairImpl(id, entry.craft(pInventory)));
    }
    this.setRecipesList(newDataset);
    result = ref.get();
    result = result != null ? result : recipes.get(0);
    this.lastRecipe = result;
    this.setSelectedRecipe(result);
    this.setFailing(false);
    this.sendRecipesListToListeners(false);
    return Optional.of(result);
}
Also used : Pair(net.minecraft.util.Pair) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) SortedSet(java.util.SortedSet) NbtElement(net.minecraft.nbt.NbtElement) World(net.minecraft.world.World) NbtList(net.minecraft.nbt.NbtList) Set(java.util.Set) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) PolymorphMod(top.theillusivec4.polymorph.common.PolymorphMod) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) List(java.util.List) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) NbtType(net.fabricmc.fabric.api.util.NbtType) ItemComponent(dev.onyxstudios.cca.api.v3.item.ItemComponent) Optional(java.util.Optional) Identifier(net.minecraft.util.Identifier) StackRecipeData(top.theillusivec4.polymorph.api.common.component.StackRecipeData) PolymorphApi(top.theillusivec4.polymorph.api.PolymorphApi) RecipeType(net.minecraft.recipe.RecipeType) Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) TreeSet(java.util.TreeSet) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 8 with Inventory

use of net.minecraft.inventory.Inventory in project Mekanism by mekanism.

the class EntityFlame method smeltBlock.

private void smeltBlock(PlayerEntity shooter, BlockState hitState, BlockPos blockPos, Direction hitSide) {
    if (hitState.isAir(level, blockPos)) {
        return;
    }
    ItemStack stack = new ItemStack(hitState.getBlock());
    if (stack.isEmpty()) {
        return;
    }
    Optional<FurnaceRecipe> recipe;
    try {
        recipe = level.getRecipeManager().getRecipeFor(IRecipeType.SMELTING, new Inventory(stack), level);
    } catch (Exception e) {
        return;
    }
    if (recipe.isPresent()) {
        if (!level.isClientSide) {
            if (MinecraftForge.EVENT_BUS.post(new BlockEvent.BreakEvent(level, blockPos, hitState, shooter))) {
                // We can't break the block exit
                return;
            }
            ItemStack result = recipe.get().getResultItem();
            if (!(result.getItem() instanceof BlockItem) || !tryPlace(shooter, blockPos, hitSide, Block.byItem(result.getItem()).defaultBlockState())) {
                level.removeBlock(blockPos, false);
                ItemEntity item = new ItemEntity(level, blockPos.getX() + 0.5, blockPos.getY() + 0.5, blockPos.getZ() + 0.5, result.copy());
                item.setDeltaMovement(0, 0, 0);
                level.addFreshEntity(item);
            }
            level.levelEvent(WorldEvents.BREAK_BLOCK_EFFECTS, blockPos, Block.getId(hitState));
            spawnParticlesAt((ServerWorld) level, blockPos);
        }
    }
}
Also used : ItemEntity(net.minecraft.entity.item.ItemEntity) FurnaceRecipe(net.minecraft.item.crafting.FurnaceRecipe) ItemStack(net.minecraft.item.ItemStack) BlockItem(net.minecraft.item.BlockItem) Inventory(net.minecraft.inventory.Inventory) BlockEvent(net.minecraftforge.event.world.BlockEvent)

Example 9 with Inventory

use of net.minecraft.inventory.Inventory in project bewitchment by MoriyaShiine.

the class MakeEntitiesWetRitualFunction method start.

@Override
public void start(ServerWorld world, BlockPos glyphPos, BlockPos effectivePos, Inventory inventory, boolean catFamiliar) {
    int radius = catFamiliar ? 9 : 3;
    world.getEntitiesByClass(Entity.class, new Box(effectivePos).expand(radius), Entity::isAlive).forEach(entity -> BWComponents.ADDITIONAL_WATER_DATA_COMPONENT.get(entity).setWetTimer(6000 * (catFamiliar ? 3 : 1)));
    BWUtil.getBlockPoses(effectivePos, radius, currentPos -> world.getBlockState(currentPos).getBlock() instanceof AbstractFireBlock).forEach(foundPos -> world.setBlockState(foundPos, Blocks.AIR.getDefaultState()));
    super.start(world, glyphPos, effectivePos, inventory, catFamiliar);
}
Also used : Entity(net.minecraft.entity.Entity) ParticleType(net.minecraft.particle.ParticleType) LivingEntity(net.minecraft.entity.LivingEntity) Predicate(java.util.function.Predicate) AbstractFireBlock(net.minecraft.block.AbstractFireBlock) ServerWorld(net.minecraft.server.world.ServerWorld) Box(net.minecraft.util.math.Box) BWUtil(moriyashiine.bewitchment.common.misc.BWUtil) Inventory(net.minecraft.inventory.Inventory) BlockPos(net.minecraft.util.math.BlockPos) Blocks(net.minecraft.block.Blocks) RitualFunction(moriyashiine.bewitchment.api.registry.RitualFunction) BWComponents(moriyashiine.bewitchment.common.registry.BWComponents) Entity(net.minecraft.entity.Entity) LivingEntity(net.minecraft.entity.LivingEntity) Box(net.minecraft.util.math.Box) AbstractFireBlock(net.minecraft.block.AbstractFireBlock)

Example 10 with Inventory

use of net.minecraft.inventory.Inventory in project SophisticatedBackpacks by P3pp3rF1y.

the class CraftingUpgradeTweakProvider method rotateGrid.

@Override
public void rotateGrid(PlayerEntity entityPlayer, BackpackContainer container, int id, boolean counterClockwise) {
    IInventory craftMatrix = getCraftMatrix(entityPlayer, container, id);
    if (craftMatrix != null) {
        int start = getCraftingGridStart(entityPlayer, container, id);
        int size = getCraftingGridSize(entityPlayer, container, id);
        IInventory matrixClone = new Inventory(size);
        int i;
        int slotIndex;
        for (i = 0; i < size; ++i) {
            slotIndex = container.getSlot(start + i).getSlotIndex();
            matrixClone.setItem(i, craftMatrix.getItem(slotIndex));
        }
        for (i = 0; i < size; ++i) {
            if (!ROTATION_HANDLER.ignoreSlotId(i)) {
                slotIndex = container.getSlot(start + ROTATION_HANDLER.rotateSlotId(i, counterClockwise)).getSlotIndex();
                craftMatrix.setItem(slotIndex, matrixClone.getItem(i));
            }
        }
        container.sendSlotUpdates();
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) Inventory(net.minecraft.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory)

Aggregations

Inventory (net.minecraft.inventory.Inventory)42 ItemStack (net.minecraft.item.ItemStack)26 IInventory (net.minecraft.inventory.IInventory)9 PlayerInventory (net.minecraft.entity.player.PlayerInventory)6 SimpleInventory (net.minecraft.inventory.SimpleInventory)6 BlockPos (net.minecraft.util.math.BlockPos)6 CampfireCookingRecipe (net.minecraft.item.crafting.CampfireCookingRecipe)5 GenericContainerScreenHandler (net.minecraft.screen.GenericContainerScreenHandler)5 List (java.util.List)4 PlayerEntity (net.minecraft.entity.player.PlayerEntity)4 NbtCompound (net.minecraft.nbt.NbtCompound)4 Optional (java.util.Optional)3 BlockEntity (net.minecraft.block.entity.BlockEntity)3 Entity (net.minecraft.entity.Entity)3 Recipe (net.minecraft.recipe.Recipe)3 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)3 Identifier (net.minecraft.util.Identifier)3 World (net.minecraft.world.World)3 Map (java.util.Map)2 Set (java.util.Set)2