Search in sources :

Example 26 with PlayerEntity

use of net.minecraft.entity.player.PlayerEntity in project Bookshelf by Darkhax-Minecraft.

the class InventoryUtils method keepDamageableItems.

/**
 * An extension of the IRecipe getRemainingItems method which attempts to keep items that
 * have durability. Instead of being consumed these items will attempt to have their
 * durability decreased. Things like unbreaking and unbreakable nbt settings are considered
 * depending on the input arguments.
 *
 * @param inv The inventory doing the crafting.
 * @param keptItems The list of items being kept.
 * @param ignoreUnbreaking Whether or not unbreaking enchantments should be ignored.
 * @param damageAmount The amount of damage to set on the item.
 * @return The list of items being kept.
 */
public static NonNullList<ItemStack> keepDamageableItems(CraftingInventory inv, NonNullList<ItemStack> keptItems, boolean ignoreUnbreaking, int damageAmount) {
    for (int i = 0; i < keptItems.size(); i++) {
        final ItemStack stack = inv.getItem(i);
        // Checks if the item has durability or has the unbreaking tag.
        if (stack.getItem().canBeDepleted() || stack.hasTag() && stack.getTag().getBoolean("Unbreakable")) {
            @Nullable final PlayerEntity player = InventoryUtils.getCraftingPlayer(inv);
            final Random random = player != null ? player.getRandom() : Bookshelf.RANDOM;
            final ItemStack retainedStack = stack.copy();
            // Sometimes you may want to ignore/bypass the unbreaking enchantment.
            if (ignoreUnbreaking) {
                // other item damaging mechanics.
                if (retainedStack.isDamageableItem()) {
                    retainedStack.setDamageValue(retainedStack.getDamageValue() + damageAmount);
                }
            } else {
                // Attempts to damage the item, taking things like the unbreaking
                // enchantment into consideration.
                retainedStack.hurt(damageAmount, random, player instanceof ServerPlayerEntity ? (ServerPlayerEntity) player : null);
            }
            keptItems.set(i, retainedStack);
        }
    }
    return keptItems;
}
Also used : Random(java.util.Random) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 27 with PlayerEntity

use of net.minecraft.entity.player.PlayerEntity in project Bookshelf by Darkhax-Minecraft.

the class CommandLootChest method translate.

private int translate(CommandContext<CommandSource> context) throws CommandSyntaxException {
    final ItemStack item = new ItemStack(Items.CHEST);
    final CompoundNBT tag = item.getOrCreateTagElement("BlockEntityTag");
    tag.putString("LootTable", ArgumentTypeLootTable.getTableId(context, "table"));
    if (CommandUtils.hasArgument(context, "seed")) {
        tag.putLong("LootTableSeed", StringArgumentType.getString(context, "seed").hashCode());
    }
    for (final PlayerEntity player : EntityArgument.getPlayers(context, "targets")) {
        ItemHandlerHelper.giveItemToPlayer(player, item.copy());
    }
    return 0;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 28 with PlayerEntity

use of net.minecraft.entity.player.PlayerEntity in project BluePower by Qmunity.

the class BPEventHandler method onEntityAttack.

@SubscribeEvent
public void onEntityAttack(LivingAttackEvent event) {
    if (!isAttacking && event.getSource() instanceof EntityDamageSource) {
        // this event will be trigger recursively by EntityLiving#hurt,
        // so we need to stop the loop.
        EntityDamageSource entitySource = (EntityDamageSource) event.getSource();
        if (entitySource.getEntity() instanceof PlayerEntity) {
            PlayerEntity killer = (PlayerEntity) entitySource.getEntity();
            if (!killer.inventory.getSelected().isEmpty()) {
                if (EnchantmentHelper.getEnchantments(killer.inventory.getSelected()).containsKey(BPEnchantments.disjunction)) {
                    if (event.getEntityLiving() instanceof EndermanEntity || event.getEntityLiving() instanceof EnderDragonEntity) {
                        int level = EnchantmentHelper.getItemEnchantmentLevel(BPEnchantments.disjunction, killer.inventory.getSelected());
                        isAttacking = true;
                        event.getEntityLiving().hurt(event.getSource(), event.getAmount() * (level * 0.5F + 1));
                        isAttacking = false;
                    }
                }
            }
        }
    }
}
Also used : EnderDragonEntity(net.minecraft.entity.boss.dragon.EnderDragonEntity) EntityDamageSource(net.minecraft.util.EntityDamageSource) PlayerEntity(net.minecraft.entity.player.PlayerEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 29 with PlayerEntity

use of net.minecraft.entity.player.PlayerEntity in project BluePower by Qmunity.

the class BPEventHandler method itemPickUp.

@SubscribeEvent
public void itemPickUp(EntityItemPickupEvent event) {
    PlayerEntity player = event.getPlayer();
    ItemStack pickUp = event.getItem().getItem();
    if (!(player.containerMenu instanceof ContainerSeedBag)) {
        for (ItemStack is : player.inventory.items) {
            if (!is.isEmpty() && is.getItem() instanceof ItemSeedBag) {
                ItemStack seedType = ItemSeedBag.getSeedType(is);
                if (!seedType.isEmpty() && seedType.sameItem(pickUp)) {
                    ItemStackHandler seedBagInvHandler = new ItemStackHandler(9);
                    // Get Items from the NBT Handler
                    if (is.hasTag())
                        seedBagInvHandler.deserializeNBT(is.getTag().getCompound("inv"));
                    // Attempt to insert items
                    for (int j = 0; j < 9 && !pickUp.isEmpty(); ++j) {
                        pickUp = seedBagInvHandler.insertItem(j, pickUp, false);
                    }
                    // Update items in the NBT
                    if (!is.hasTag())
                        is.setTag(new CompoundNBT());
                    if (is.getTag() != null) {
                        is.getTag().put("inv", seedBagInvHandler.serializeNBT());
                    }
                    // Pickup Leftovers
                    if (pickUp.isEmpty()) {
                        event.setResult(Event.Result.ALLOW);
                        event.getItem().remove();
                        return;
                    } else {
                        event.getItem().setItem(pickUp);
                    }
                }
            }
        }
    }
}
Also used : ItemStackHandler(net.minecraftforge.items.ItemStackHandler) CompoundNBT(net.minecraft.nbt.CompoundNBT) ItemStack(net.minecraft.item.ItemStack) ItemSeedBag(com.bluepowermod.item.ItemSeedBag) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ContainerSeedBag(com.bluepowermod.container.ContainerSeedBag) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 30 with PlayerEntity

use of net.minecraft.entity.player.PlayerEntity in project BluePower by Qmunity.

the class BlockBPMicroblock method getStateForPlacement.

@Nullable
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    FluidState fluidstate = context.getLevel().getFluidState(context.getClickedPos());
    if (player != null && !player.isCrouching()) {
        return this.defaultBlockState().setValue(FACING, context.getClickedFace()).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
    }
    Vector3d vec = context.getPlayer().getLookAngle();
    return this.defaultBlockState().setValue(FACING, Direction.getNearest(vec.x, vec.y, vec.z)).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) PlayerEntity(net.minecraft.entity.player.PlayerEntity) FluidState(net.minecraft.fluid.FluidState) Nullable(javax.annotation.Nullable)

Aggregations

PlayerEntity (net.minecraft.entity.player.PlayerEntity)46 ItemStack (net.minecraft.item.ItemStack)22 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)19 World (net.minecraft.world.World)17 BlockPos (net.minecraft.util.math.BlockPos)16 CompoundNBT (net.minecraft.nbt.CompoundNBT)13 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)12 BlockState (net.minecraft.block.BlockState)10 ResourceLocation (net.minecraft.util.ResourceLocation)7 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)6 Nonnull (javax.annotation.Nonnull)6 AgriCraft (com.infinityraider.agricraft.AgriCraft)5 TileEntity (net.minecraft.tileentity.TileEntity)5 IAgriCrop (com.infinityraider.agricraft.api.v1.crop.IAgriCrop)4 IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)4 AgriTabs (com.infinityraider.agricraft.content.AgriTabs)4 Names (com.infinityraider.agricraft.reference.Names)4 ItemBase (com.infinityraider.infinitylib.item.ItemBase)4 Nullable (javax.annotation.Nullable)4 Blocks (net.minecraft.block.Blocks)4