Search in sources :

Example 11 with EnumHand

use of net.minecraft.util.EnumHand in project MorePlanets by SteveKunG.

the class GeneralEventHandler method setFarmland.

private void setFarmland(UseHoeEvent event, World world, BlockPos pos, Block farmland) {
    world.setBlockState(pos, farmland.getDefaultState());
    event.setResult(Result.ALLOW);
    world.playSound(null, pos, SoundType.GROUND.getStepSound(), SoundCategory.BLOCKS, (SoundType.GROUND.getVolume() + 1.0F) / 2.0F, SoundType.GROUND.getPitch() * 0.8F);
    for (EnumHand hand : CachedEnum.handValues) {
        event.getEntityPlayer().swingArm(hand);
    }
}
Also used : EnumHand(net.minecraft.util.EnumHand)

Example 12 with EnumHand

use of net.minecraft.util.EnumHand in project MorePlanets by SteveKunG.

the class GeneralEventHandler method setFarmland.

private void setFarmland(UseHoeEvent event, World world, BlockPos pos, IBlockState state, Block coarse, Block dirt, Block farmland) {
    if (state.getBlock() == coarse) {
        world.setBlockState(pos, dirt.getDefaultState());
    } else {
        world.setBlockState(pos, farmland.getDefaultState());
    }
    event.setResult(Result.ALLOW);
    world.playSound(null, pos, SoundType.GROUND.getStepSound(), SoundCategory.BLOCKS, (SoundType.GROUND.getVolume() + 1.0F) / 2.0F, SoundType.GROUND.getPitch() * 0.8F);
    for (EnumHand hand : CachedEnum.handValues) {
        event.getEntityPlayer().swingArm(hand);
    }
}
Also used : EnumHand(net.minecraft.util.EnumHand)

Example 13 with EnumHand

use of net.minecraft.util.EnumHand in project Bewitchment by Um-Mitternacht.

the class SpellDisarming method performEffect.

@Override
public void performEffect(RayTraceResult rtrace, EntityLivingBase caster, World world) {
    if (rtrace.typeOfHit == Type.ENTITY && rtrace.entityHit instanceof EntityLivingBase) {
        EntityLivingBase entity = (EntityLivingBase) rtrace.entityHit;
        EnumHand hand = null;
        if (!entity.getHeldItemMainhand().isEmpty())
            hand = EnumHand.MAIN_HAND;
        else if (!entity.getHeldItemOffhand().isEmpty())
            hand = EnumHand.OFF_HAND;
        if (hand != null) {
            ItemStack stack = entity.getHeldItem(hand).copy();
            entity.setHeldItem(hand, ItemStack.EMPTY);
            if (!(entity instanceof EntityPlayer) && stack.isItemStackDamageable() && stack.getItemDamage() == 0) {
                stack.setItemDamage((int) (stack.getMaxDamage() * (0.5D + 0.5D * Math.random())));
            }
            EntityItem ei = new EntityItem(world, entity.posX, entity.posY, entity.posZ, stack);
            ei.setPickupDelay(200);
            ei.setNoDespawn();
            if (!world.isRemote)
                world.spawnEntity(ei);
        }
    }
}
Also used : EnumHand(net.minecraft.util.EnumHand) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 14 with EnumHand

use of net.minecraft.util.EnumHand in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callBlockPlaceEvent.

public static ChangeBlockEvent.Place callBlockPlaceEvent(Event event) {
    ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) event;
    if (spongeEvent.getCause().root() instanceof Player) {
        EntityPlayer player = (EntityPlayer) spongeEvent.getCause().first(Player.class).get();
        net.minecraft.world.World world = player.world;
        final PhaseTracker phaseTracker = PhaseTracker.getInstance();
        final PhaseContext<?> currentContext = phaseTracker.getCurrentContext();
        PhaseContext<?> target = currentContext;
        if (currentContext instanceof UnwindingPhaseContext) {
            target = ((UnwindingPhaseContext) currentContext).getUnwindingContext();
        }
        PacketContext<?> context = target instanceof PacketContext<?> ? (PacketContext<?>) target : null;
        Packet<?> contextPacket = context != null ? context.getPacket() : null;
        if (contextPacket == null) {
            return spongeEvent;
        }
        if (spongeEvent.getTransactions().size() == 1) {
            BlockPos pos = VecHelper.toBlockPos(spongeEvent.getTransactions().get(0).getOriginal().getPosition());
            IBlockState state = (IBlockState) spongeEvent.getTransactions().get(0).getOriginal().getState();
            net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
            IBlockState placedAgainst = Blocks.AIR.getDefaultState();
            EnumHand hand = EnumHand.MAIN_HAND;
            if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
                CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            BlockEvent.PlaceEvent forgeEvent = new BlockEvent.PlaceEvent(blockSnapshot, placedAgainst, player, hand);
            ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
            if (forgeEvent.isCanceled()) {
                spongeEvent.setCancelled(true);
            }
        } else {
            // multi
            Iterator<Transaction<BlockSnapshot>> iterator = spongeEvent.getTransactions().iterator();
            List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = new ArrayList<>();
            while (iterator.hasNext()) {
                Transaction<BlockSnapshot> transaction = iterator.next();
                Location<World> location = transaction.getOriginal().getLocation().get();
                IBlockState state = (IBlockState) transaction.getOriginal().getState();
                BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
                net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
                blockSnapshots.add(blockSnapshot);
            }
            IBlockState placedAgainst = Blocks.AIR.getDefaultState();
            EnumHand hand = EnumHand.MAIN_HAND;
            if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
                CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshots.get(0).getWorld().getBlockState(blockSnapshots.get(0).getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            BlockEvent.MultiPlaceEvent forgeEvent = new BlockEvent.MultiPlaceEvent(blockSnapshots, placedAgainst, player, hand);
            ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
            if (forgeEvent.isCanceled()) {
                spongeEvent.setCancelled(true);
            }
        }
    }
    return spongeEvent;
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) UnwindingPhaseContext(org.spongepowered.common.event.tracking.phase.general.UnwindingPhaseContext) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) EnumHand(net.minecraft.util.EnumHand) BlockPos(net.minecraft.util.math.BlockPos) Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IBlockState(net.minecraft.block.state.IBlockState) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Transaction(org.spongepowered.api.data.Transaction) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) BlockEvent(net.minecraftforge.event.world.BlockEvent) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) NotifyNeighborBlockEvent(org.spongepowered.api.event.block.NotifyNeighborBlockEvent)

Example 15 with EnumHand

use of net.minecraft.util.EnumHand in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callEntityInteractEvent.

private static InteractEntityEvent.Secondary callEntityInteractEvent(Event event) {
    InteractEntityEvent.Secondary spongeEvent = (InteractEntityEvent.Secondary) event;
    Optional<Player> player = spongeEvent.getCause().first(Player.class);
    if (!player.isPresent()) {
        return null;
    }
    final EntityPlayerMP entityPlayerMP = EntityUtil.toNative(player.get());
    final EnumHand hand = entityPlayerMP.getActiveHand();
    final EntityPlayer entityPlayer = (EntityPlayer) player.get();
    final Entity entity = (Entity) spongeEvent.getTargetEntity();
    final Vector3d hitVec = spongeEvent.getInteractionPoint().orElse(null);
    PlayerInteractEvent forgeEvent = null;
    if (hitVec != null) {
        forgeEvent = new PlayerInteractEvent.EntityInteractSpecific(entityPlayer, hand, entity, VecHelper.toVec3d(hitVec));
    } else {
        forgeEvent = new PlayerInteractEvent.EntityInteract(entityPlayer, hand, entity);
    }
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    if (forgeEvent.isCanceled()) {
        spongeEvent.setCancelled(true);
    }
    return spongeEvent;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(net.minecraft.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) InteractEntityEvent(org.spongepowered.api.event.entity.InteractEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) EnumHand(net.minecraft.util.EnumHand) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Aggregations

EnumHand (net.minecraft.util.EnumHand)72 ItemStack (net.minecraft.item.ItemStack)44 EntityPlayer (net.minecraft.entity.player.EntityPlayer)39 BlockPos (net.minecraft.util.math.BlockPos)31 EnumFacing (net.minecraft.util.EnumFacing)23 World (net.minecraft.world.World)18 Entity (net.minecraft.entity.Entity)16 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)16 Vec3d (net.minecraft.util.math.Vec3d)15 IBlockState (net.minecraft.block.state.IBlockState)12 Item (net.minecraft.item.Item)12 TileEntity (net.minecraft.tileentity.TileEntity)12 RayTraceResult (net.minecraft.util.math.RayTraceResult)12 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)12 Block (net.minecraft.block.Block)11 EntityLivingBase (net.minecraft.entity.EntityLivingBase)11 List (java.util.List)10 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)10 CPacketPlayerTryUseItemOnBlock (net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock)8 EntityItem (net.minecraft.entity.item.EntityItem)7