Search in sources :

Example 16 with Inventory

use of net.minecraft.inventory.Inventory in project MiniUtilities by OneLemonyBoi.

the class MoltenHeadHandler method handleBlockBreak.

public static void handleBlockBreak(BreakEvent event) {
    if (!event.getWorld().isRemote() && EnchantmentHelper.getEnchantmentLevel(EnchantmentList.MoltenHead.get(), event.getPlayer().getHeldItem(Hand.MAIN_HAND)) > 0) {
        // Checks if running on server and enchant is on tool
        // Casts IWorld to ServerWorld
        ServerWorld serverWorld = ((ServerWorld) event.getWorld());
        ItemStack pick = event.getPlayer().getHeldItem(Hand.MAIN_HAND);
        int fortuneAmount = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, pick);
        int silkTouchAmount = EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, pick);
        LootContext.Builder lootcontext$builder = (new LootContext.Builder(serverWorld).withRandom(serverWorld.rand).withParameter(LootParameters.ORIGIN, Vector3d.copyCentered(event.getPos())).withParameter(LootParameters.TOOL, // Makes lootcontext to calculate drops
        pick));
        // Calculates drops
        List<ItemStack> drops = event.getState().getDrops(lootcontext$builder);
        for (ItemStack item : drops) {
            // Iteration
            ItemStack stack = serverWorld.getRecipeManager().getRecipe(IRecipeType.SMELTING, new Inventory(item), serverWorld).map(FurnaceRecipe::getRecipeOutput).filter(itemStack -> !itemStack.isEmpty()).map(itemStack -> ItemHandlerHelper.copyStackWithSize(itemStack, item.getCount() * itemStack.getCount())).orElse(// Recipe as var
            item);
            if (fortuneAmount >= 1 && silkTouchAmount <= 0 && !stack.isItemEqual(item)) {
                int addedCount = new Random().nextInt(fortuneAmount + 2) - 1;
                if (addedCount < 0) {
                    addedCount = 0;
                }
                addedCount++;
                stack.setCount(stack.getCount() * addedCount);
            }
            // Spawns Itemstack
            InventoryHelper.spawnItemStack(event.getPlayer().world, event.getPos().getX(), event.getPos().getY(), event.getPos().getZ(), stack);
        }
        // Breaks block
        event.getPlayer().world.destroyBlock(event.getPos(), false);
        event.setResult(Event.Result.DENY);
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ServerWorld(net.minecraft.world.server.ServerWorld) InventoryHelper(net.minecraft.inventory.InventoryHelper) BreakEvent(net.minecraftforge.event.world.BlockEvent.BreakEvent) Inventory(net.minecraft.inventory.Inventory) LootParameters(net.minecraft.loot.LootParameters) Random(java.util.Random) MiniUtilities(onelemonyboi.miniutilities.MiniUtilities) FurnaceRecipe(net.minecraft.item.crafting.FurnaceRecipe) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) IRecipeType(net.minecraft.item.crafting.IRecipeType) ItemStack(net.minecraft.item.ItemStack) LootContext(net.minecraft.loot.LootContext) Vector3d(net.minecraft.util.math.vector.Vector3d) List(java.util.List) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) Enchantments(net.minecraft.enchantment.Enchantments) Event(net.minecraftforge.eventbus.api.Event) Hand(net.minecraft.util.Hand) EnchantmentList(onelemonyboi.miniutilities.init.EnchantmentList) Random(java.util.Random) LootContext(net.minecraft.loot.LootContext) ItemStack(net.minecraft.item.ItemStack) Inventory(net.minecraft.inventory.Inventory)

Example 17 with Inventory

use of net.minecraft.inventory.Inventory in project Capybara-Fabricated by ZestyBlaze.

the class CapybaraEntity method interactMob.

@Override
public ActionResult interactMob(PlayerEntity player, Hand hand) {
    final ItemStack stack = player.getStackInHand(hand);
    if (player.isSneaking()) {
        if (stack.getItem() == Blocks.CHEST.asItem()) {
            if (inventory == null || inventory.size() < 27) {
                inventory = new SimpleInventory(27);
                dataTracker.set(CHESTS, 1);
                if (!player.getAbilities().creativeMode) {
                    stack.decrement(1);
                }
                return ActionResult.SUCCESS;
            } else if (inventory.size() < 54) {
                Inventory inv = new SimpleInventory(54);
                for (int i = 0; i < 27; i++) {
                    inv.setStack(i, inventory.getStack(i));
                }
                inventory = inv;
                dataTracker.set(CHESTS, 2);
                if (!player.getAbilities().creativeMode) {
                    stack.decrement(1);
                }
                return ActionResult.SUCCESS;
            }
        }
        if (stack.getItem() == Items.STICK) {
            this.setSitting(!this.isSitting());
        } else {
            player.openHandledScreen(this);
            return ActionResult.SUCCESS;
        }
    } else if (TEMPT_ITEMS.get().contains(stack.getItem()) && !isTamed()) {
        if (this.random.nextInt(3) == 0) {
            this.setOwner(player);
            this.navigation.stop();
            this.setTarget(null);
            this.setSitting(true);
            this.world.sendEntityStatus(this, (byte) 7);
        }
        if (!player.getAbilities().creativeMode) {
            stack.decrement(1);
        } else {
            this.world.sendEntityStatus(this, (byte) 6);
        }
        return ActionResult.SUCCESS;
    } else if (!this.hasPassengers() && !player.shouldCancelInteraction() && !this.isBaby() && !isInSittingPose()) {
        boolean flag = this.isBreedingItem(player.getStackInHand(hand));
        if (!flag && !this.hasPassengers() && !player.shouldCancelInteraction()) {
            if (!this.world.isClient) {
                player.startRiding(this);
            }
            return ActionResult.SUCCESS;
        }
    } else if (!this.getPassengerList().isEmpty()) {
        removeAllPassengers();
    }
    return super.interactMob(player, hand);
}
Also used : ItemStack(net.minecraft.item.ItemStack) SimpleInventory(net.minecraft.inventory.SimpleInventory) Inventory(net.minecraft.inventory.Inventory) SimpleInventory(net.minecraft.inventory.SimpleInventory) PlayerInventory(net.minecraft.entity.player.PlayerInventory)

Example 18 with Inventory

use of net.minecraft.inventory.Inventory in project alaskanativecraft by Platymemo.

the class DryingRackBlockEntity method updateItemsBeingDried.

@SuppressWarnings("unused")
public static void updateItemsBeingDried(World world, BlockPos pos, BlockState state, @NotNull DryingRackBlockEntity dryingRackBlockEntity) {
    for (int i = 0; i < dryingRackBlockEntity.itemsBeingDried.size(); ++i) {
        ItemStack itemStack = dryingRackBlockEntity.itemsBeingDried.get(i);
        if (!itemStack.isEmpty()) {
            dryingRackBlockEntity.dryingTimes[i]++;
            if (dryingRackBlockEntity.dryingTimes[i] >= dryingRackBlockEntity.dryingTotalTimes[i]) {
                // Don't want it to keep counting up unnecessarily high
                dryingRackBlockEntity.dryingTimes[i] = dryingRackBlockEntity.dryingTotalTimes[i];
                Inventory inventory = new SimpleInventory(itemStack);
                ItemStack itemStack2 = world.getRecipeManager().getFirstMatch(AlaskaRecipes.DRYING, inventory, world).map((dryingRecipe) -> dryingRecipe.craft(inventory)).orElse(itemStack);
                dryingRackBlockEntity.itemsBeingDried.set(i, itemStack2);
                dryingRackBlockEntity.updateListeners();
            }
        }
    }
}
Also used : Packet(net.minecraft.network.Packet) BlockEntity(net.minecraft.block.entity.BlockEntity) World(net.minecraft.world.World) DryingRecipe(com.github.platymemo.alaskanativecraft.recipe.DryingRecipe) Inventory(net.minecraft.inventory.Inventory) AlaskaBlocks(com.github.platymemo.alaskanativecraft.block.AlaskaBlocks) SimpleInventory(net.minecraft.inventory.SimpleInventory) BlockPos(net.minecraft.util.math.BlockPos) DefaultedList(net.minecraft.util.collection.DefaultedList) AlaskaRecipes(com.github.platymemo.alaskanativecraft.recipe.AlaskaRecipes) BlockEntityUpdateS2CPacket(net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) Nullable(org.jetbrains.annotations.Nullable) ClientPlayPacketListener(net.minecraft.network.listener.ClientPlayPacketListener) MathHelper(net.minecraft.util.math.MathHelper) Inventories(net.minecraft.inventory.Inventories) Optional(java.util.Optional) BlockState(net.minecraft.block.BlockState) NotNull(org.jetbrains.annotations.NotNull) Clearable(net.minecraft.util.Clearable) ItemScatterer(net.minecraft.util.ItemScatterer) ItemStack(net.minecraft.item.ItemStack) Inventory(net.minecraft.inventory.Inventory) SimpleInventory(net.minecraft.inventory.SimpleInventory) SimpleInventory(net.minecraft.inventory.SimpleInventory)

Example 19 with Inventory

use of net.minecraft.inventory.Inventory in project Client by MatHax.

the class EChestMemory method onOpenScreenEvent.

@EventHandler
private static void onOpenScreenEvent(OpenScreenEvent event) {
    if (echestOpenedState == 1 && event.screen instanceof GenericContainerScreen) {
        echestOpenedState = 2;
        return;
    }
    if (echestOpenedState == 0)
        return;
    if (!(mc.currentScreen instanceof GenericContainerScreen))
        return;
    GenericContainerScreenHandler container = ((GenericContainerScreen) mc.currentScreen).getScreenHandler();
    if (container == null)
        return;
    Inventory inv = container.getInventory();
    for (int i = 0; i < 27; i++) {
        ITEMS.set(i, inv.getStack(i));
    }
    echestOpenedState = 0;
}
Also used : GenericContainerScreen(net.minecraft.client.gui.screen.ingame.GenericContainerScreen) GenericContainerScreenHandler(net.minecraft.screen.GenericContainerScreenHandler) Inventory(net.minecraft.inventory.Inventory) EventHandler(mathax.client.eventbus.EventHandler)

Example 20 with Inventory

use of net.minecraft.inventory.Inventory in project FarmersDelight by vectorwing.

the class SkilletTileEntity method addItemToCook.

public ItemStack addItemToCook(ItemStack addedStack, @Nullable PlayerEntity player) {
    Optional<CampfireCookingRecipe> recipe = getMatchingRecipe(new Inventory(addedStack));
    if (recipe.isPresent()) {
        cookingTimeTotal = SkilletBlock.getSkilletCookingTime(recipe.get().getCookingTime(), fireAspectLevel);
        boolean wasEmpty = getStoredStack().isEmpty();
        ItemStack remainderStack = inventory.insertItem(0, addedStack.copy(), false);
        if (!ItemStack.matches(remainderStack, addedStack)) {
            lastRecipeID = recipe.get().getId();
            cookingTime = 0;
            if (wasEmpty && level != null && isHeated(level, worldPosition)) {
                level.playSound(null, worldPosition.getX() + 0.5F, worldPosition.getY() + 0.5F, worldPosition.getZ() + 0.5F, ModSounds.BLOCK_SKILLET_ADD_FOOD.get(), SoundCategory.BLOCKS, 0.8F, 1.0F);
            }
            return remainderStack;
        }
    } else if (player != null) {
        player.displayClientMessage(TextUtils.getTranslation("block.skillet.invalid_item"), true);
    }
    return addedStack;
}
Also used : ItemStack(net.minecraft.item.ItemStack) Inventory(net.minecraft.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) CampfireCookingRecipe(net.minecraft.item.crafting.CampfireCookingRecipe)

Aggregations

Inventory (net.minecraft.inventory.Inventory)39 ItemStack (net.minecraft.item.ItemStack)23 IInventory (net.minecraft.inventory.IInventory)8 SimpleInventory (net.minecraft.inventory.SimpleInventory)6 BlockPos (net.minecraft.util.math.BlockPos)6 PlayerInventory (net.minecraft.entity.player.PlayerInventory)5 CampfireCookingRecipe (net.minecraft.item.crafting.CampfireCookingRecipe)5 NbtCompound (net.minecraft.nbt.NbtCompound)4 GenericContainerScreenHandler (net.minecraft.screen.GenericContainerScreenHandler)4 List (java.util.List)3 Optional (java.util.Optional)3 BlockEntity (net.minecraft.block.entity.BlockEntity)3 Entity (net.minecraft.entity.Entity)3 PlayerEntity (net.minecraft.entity.player.PlayerEntity)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 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2