Search in sources :

Example 21 with EnumHand

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

the class SpongeToForgeEventFactory method createAndPostBlockPlaceEvent.

@SuppressWarnings("deprecation")
private static boolean createAndPostBlockPlaceEvent(final SpongeToForgeEventData eventData) {
    final ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) eventData.getSpongeEvent();
    BlockEvent.PlaceEvent forgeEvent = (BlockEvent.PlaceEvent) eventData.getForgeEvent();
    if (!(spongeEvent.getCause().root() instanceof Player)) {
        return false;
    }
    if (forgeEvent == null) {
        final EntityPlayer player = (EntityPlayer) spongeEvent.getCause().root();
        final 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();
        }
        final PacketContext<?> context = target instanceof PacketContext<?> ? (PacketContext<?>) target : null;
        final Packet<?> contextPacket = context != null ? context.getPacket() : null;
        if (contextPacket == null) {
            return false;
        }
        if (spongeEvent.getTransactions().size() == 1) {
            final BlockPos pos = VecHelper.toBlockPos(spongeEvent.getTransactions().get(0).getOriginal().getPosition());
            final IBlockState state = (IBlockState) spongeEvent.getTransactions().get(0).getOriginal().getState();
            final 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) {
                final CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                final EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            forgeEvent = new BlockEvent.PlaceEvent(blockSnapshot, placedAgainst, player, hand);
            eventData.setForgeEvent(forgeEvent);
        } else {
            // multi
            final Iterator<Transaction<BlockSnapshot>> iterator = spongeEvent.getTransactions().iterator();
            final List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = new ArrayList<>();
            while (iterator.hasNext()) {
                final Transaction<BlockSnapshot> transaction = iterator.next();
                final Location<World> location = transaction.getOriginal().getLocation().get();
                final IBlockState state = (IBlockState) transaction.getOriginal().getState();
                final BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
                final 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) {
                final CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                final EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshots.get(0).getWorld().getBlockState(blockSnapshots.get(0).getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            forgeEvent = new BlockEvent.MultiPlaceEvent(blockSnapshots, placedAgainst, player, hand);
            eventData.setForgeEvent(forgeEvent);
        }
    }
    forgeEventBus.forgeBridge$post(eventData);
    return true;
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) UnwindingPhaseContext(org.spongepowered.common.event.tracking.UnwindingPhaseContext) World(org.spongepowered.api.world.World) 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) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) 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 22 with EnumHand

use of net.minecraft.util.EnumHand in project PizzaCraft by Tiviacz1337.

the class CommonEventHandler method onShieldBlock.

@SubscribeEvent
public static void onShieldBlock(LivingAttackEvent event) {
    ItemStack activeItemStack;
    EntityPlayer player;
    if (!(event.getEntityLiving() instanceof EntityPlayer)) {
        return;
    }
    player = (EntityPlayer) event.getEntityLiving();
    if (player.getActiveItemStack() == null) {
        return;
    }
    activeItemStack = player.getActiveItemStack();
    float damage = event.getAmount();
    if (damage > 0.0F && activeItemStack != null && activeItemStack.getItem() instanceof ItemPizzaShield) {
        int i = 1 + MathHelper.floor(damage);
        activeItemStack.damageItem(i, player);
        if (activeItemStack.getCount() <= 0) {
            EnumHand enumhand = player.getActiveHand();
            ForgeEventFactory.onPlayerDestroyItem(player, activeItemStack, enumhand);
            if (enumhand == EnumHand.MAIN_HAND) {
                player.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, ItemStack.EMPTY);
            } else {
                player.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, ItemStack.EMPTY);
            }
            activeItemStack = ItemStack.EMPTY;
            if (player.world.isRemote) {
                player.playSound(SoundEvents.ITEM_SHIELD_BREAK, 0.8F, 0.8F + player.world.rand.nextFloat() * 0.4F);
            }
        }
    }
}
Also used : EnumHand(net.minecraft.util.EnumHand) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemStack(net.minecraft.item.ItemStack) ItemPizzaShield(com.tiviacz.pizzacraft.items.ItemPizzaShield) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 23 with EnumHand

use of net.minecraft.util.EnumHand in project takumicraft by TNTModders.

the class BlockTakumiAltar method onBlockActivated.

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    boolean flg = super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
    Entity entity = null;
    if (canSpawnWither(worldIn, pos)) {
        spawnWither(worldIn, pos);
        return true;
    } else if (worldIn.getBlockState(pos.down()).getBlock() == TakumiBlockCore.CREEPER_BOMB) {
        entity = new EntityKingCreeper(worldIn);
    } else if (worldIn.getBlockState(pos.down()).getBlock() == Blocks.EMERALD_BLOCK) {
        entity = new EntityAngelCreeper(worldIn);
    } else if (worldIn.getBlockState(pos.down()).getBlock() == TakumiBlockCore.MAGIC_BLOCK) {
        entity = new EntityGemCreeper(worldIn);
    } else if (worldIn.getBlockState(pos.up()).getBlock() == Blocks.CAKE) {
        entity = new EntityAnnivCreeper(worldIn);
    } else if (worldIn.getBlockState(pos.down()).getBlock() == Blocks.DIAMOND_BLOCK) {
        entity = new EntitySuperDiamondCreeper(worldIn);
    } else {
        List<ITakumiEntity> entities = new ArrayList<>();
        TakumiEntityCore.getEntityList().forEach(iTakumiEntity -> {
            if (iTakumiEntity.takumiRank() == EnumTakumiRank.HIGH) {
                entities.add(iTakumiEntity);
            }
        });
        if (!entities.isEmpty()) {
            entities.removeIf(iTakumiEntity -> iTakumiEntity instanceof EntityAnnivCreeper || iTakumiEntity instanceof EntitySuperDiamondCreeper);
            try {
                entity = (Entity) entities.get(worldIn.rand.nextInt(entities.size())).getClass().getConstructor(World.class).newInstance(worldIn);
            } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    if (entity != null) {
        entity.setPosition(pos.getX(), pos.getY(), pos.getZ());
        worldIn.setBlockToAir(pos);
        if (worldIn.getBlockState(pos.down()).getBlockHardness(worldIn, pos.down()) > 0) {
            worldIn.setBlockToAir(pos.down());
        }
        if (!worldIn.isRemote) {
            worldIn.createExplosion(new EntityAlterDummy(worldIn), pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, 3f, true);
            worldIn.loadedEntityList.removeIf(entity1 -> entity1 instanceof EntityAlterDummy);
            if (worldIn.spawnEntity(entity)) {
                return true;
            }
        } else {
            if (entity instanceof EntityKingCreeper && !TakumiUtils.getAdvancementUnlocked(new ResourceLocation("takumicraft:kingslayer"))) {
                if (FMLCommonHandler.instance().getSide().isClient()) {
                    this.proxyShowGuiToast();
                }
            }
        }
    }
    return flg;
}
Also used : EntitySuperDiamondCreeper(com.tntmodders.takumi.entity.mobs.EntitySuperDiamondCreeper) BlockMaterialMatcher(net.minecraft.block.state.pattern.BlockMaterialMatcher) SystemToast(net.minecraft.client.gui.toasts.SystemToast) TakumiEntityCore(com.tntmodders.takumi.core.TakumiEntityCore) EntityWitherCreeper(com.tntmodders.takumi.entity.mobs.boss.EntityWitherCreeper) Blocks(net.minecraft.init.Blocks) EntityAlterDummy(com.tntmodders.takumi.entity.item.EntityAlterDummy) EnumHand(net.minecraft.util.EnumHand) EnumTakumiRank(com.tntmodders.takumi.entity.ITakumiEntity.EnumTakumiRank) EntitySuperDiamondCreeper(com.tntmodders.takumi.entity.mobs.EntitySuperDiamondCreeper) FMLCommonHandler(net.minecraftforge.fml.common.FMLCommonHandler) ITooltipFlag(net.minecraft.client.util.ITooltipFlag) ITakumiEntity(com.tntmodders.takumi.entity.ITakumiEntity) BlockPattern(net.minecraft.block.state.pattern.BlockPattern) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) Block(net.minecraft.block.Block) Minecraft(net.minecraft.client.Minecraft) Side(net.minecraftforge.fml.relauncher.Side) BlockWorldState(net.minecraft.block.state.BlockWorldState) EntityKingCreeper(com.tntmodders.takumi.entity.mobs.boss.EntityKingCreeper) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) BlockStateMatcher(net.minecraft.block.state.pattern.BlockStateMatcher) Nullable(javax.annotation.Nullable) TakumiUtils(com.tntmodders.takumi.utils.TakumiUtils) EntityAnnivCreeper(com.tntmodders.takumi.entity.mobs.EntityAnnivCreeper) EntityGemCreeper(com.tntmodders.takumi.entity.mobs.boss.EntityGemCreeper) MapColor(net.minecraft.block.material.MapColor) Entity(net.minecraft.entity.Entity) TakumiBlockCore(com.tntmodders.takumi.core.TakumiBlockCore) World(net.minecraft.world.World) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) EntityAngelCreeper(com.tntmodders.takumi.entity.mobs.boss.EntityAngelCreeper) FactoryBlockPattern(net.minecraft.block.state.pattern.FactoryBlockPattern) TakumiCraftCore(com.tntmodders.takumi.TakumiCraftCore) InvocationTargetException(java.lang.reflect.InvocationTargetException) IBlockState(net.minecraft.block.state.IBlockState) List(java.util.List) Material(net.minecraft.block.material.Material) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ResourceLocation(net.minecraft.util.ResourceLocation) EnumParticleTypes(net.minecraft.util.EnumParticleTypes) ITakumiEntity(com.tntmodders.takumi.entity.ITakumiEntity) Entity(net.minecraft.entity.Entity) EntityAnnivCreeper(com.tntmodders.takumi.entity.mobs.EntityAnnivCreeper) EntityGemCreeper(com.tntmodders.takumi.entity.mobs.boss.EntityGemCreeper) EntityAngelCreeper(com.tntmodders.takumi.entity.mobs.boss.EntityAngelCreeper) ResourceLocation(net.minecraft.util.ResourceLocation) EntityAlterDummy(com.tntmodders.takumi.entity.item.EntityAlterDummy) ArrayList(java.util.ArrayList) List(java.util.List) EntityKingCreeper(com.tntmodders.takumi.entity.mobs.boss.EntityKingCreeper)

Example 24 with EnumHand

use of net.minecraft.util.EnumHand in project takumicraft by TNTModders.

the class ItemAttackBlock method onItemUse.

@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (!worldIn.isRemote && worldIn.loadedEntityList.stream().noneMatch(entity -> entity instanceof EntityAttackBlock && !entity.isDead)) {
        EntityAttackBlock attackBlock = new EntityAttackBlock(worldIn);
        BlockPos blockPos = pos.offset(facing);
        if (worldIn.isAirBlock(blockPos) && worldIn.isAirBlock(blockPos.up())) {
            attackBlock.setPosition(blockPos.getX() + 0.5, blockPos.getY(), blockPos.getZ() + 0.5);
        }
        double theta = Math.toRadians(worldIn.rand.nextFloat() * 360);
        BlockPos bigPos = blockPos.add(Math.cos(theta) * EntityAttackBlock.DIST, 0, Math.sin(theta) * EntityAttackBlock.DIST);
        bigPos = worldIn.getHeight(bigPos);
        attackBlock.setPos(bigPos);
        double dx = bigPos.getX() - blockPos.getX();
        attackBlock.setDX(((float) (dx / EntityAttackBlock.ATTACK_TICK)));
        double dz = bigPos.getZ() - blockPos.getZ();
        attackBlock.setDZ(((float) (dz / EntityAttackBlock.ATTACK_TICK)));
        if (worldIn.spawnEntity(attackBlock)) {
            EntityBigCreeperDummy bigCreeperDummy = new EntityBigCreeperDummy(worldIn);
            bigCreeperDummy.setPosition(bigPos.getX(), bigPos.getY(), bigPos.getZ());
            worldIn.spawnEntity(bigCreeperDummy);
            worldIn.playBroadcastSound(1038, pos, 0);
            if (!player.isCreative()) {
                player.getHeldItem(hand).shrink(1);
            }
            worldIn.getPlayers(EntityPlayer.class, input -> attackBlock.getDistanceSq(input) < 10000).forEach(player1 -> {
                player1.sendMessage(new TextComponentTranslation("entity.attackblock.message.summon.01"));
                player1.sendMessage(new TextComponentTranslation("entity.attackblock.message.summon.02"));
                player1.sendMessage(new TextComponentTranslation("entity.attackblock.message.summon.03"));
            });
        }
    }
    return EnumActionResult.SUCCESS;
}
Also used : Item(net.minecraft.item.Item) World(net.minecraft.world.World) EnumHand(net.minecraft.util.EnumHand) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) EntityBigCreeperDummy(com.tntmodders.takumi.entity.item.EntityBigCreeperDummy) TakumiCraftCore(com.tntmodders.takumi.TakumiCraftCore) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) EnumRarity(net.minecraft.item.EnumRarity) ItemStack(net.minecraft.item.ItemStack) Side(net.minecraftforge.fml.relauncher.Side) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EnumActionResult(net.minecraft.util.EnumActionResult) EntityAttackBlock(com.tntmodders.takumi.entity.item.EntityAttackBlock) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) EntityAttackBlock(com.tntmodders.takumi.entity.item.EntityAttackBlock) EntityBigCreeperDummy(com.tntmodders.takumi.entity.item.EntityBigCreeperDummy) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos)

Example 25 with EnumHand

use of net.minecraft.util.EnumHand in project Spark-Client by Spark-Client-Development.

the class SwitchManager method getCalculateAction.

// gets action that needs to be done for switching to item
public ItemSwitcher.SwitchResult getCalculateAction(SwitchItem switcher, ItemSwitcher.usedHand handType, ItemSwitcher.switchType type) {
    if (type == ItemSwitcher.switchType.NoSwitch || handType == ItemSwitcher.usedHand.Offhand) {
        float main = ((handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Mainhand) ? switcher.isItemGood(mc.player.getHeldItemMainhand()) : 0);
        float off = ((handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Offhand) ? switcher.isItemGood(mc.player.getHeldItemOffhand()) : 0);
        if (main > 0 || off > 0)
            return new ItemSwitcher.NoSwitchResult(main > off ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND);
    } else if (type == ItemSwitcher.switchType.Const) {
        int id = ItemSwitcher.FindStackInInventory(switcher, handType != ItemSwitcher.usedHand.Mainhand);
        if (id == 45)
            return new ItemSwitcher.NoSwitchResult(EnumHand.OFF_HAND);
        if (id == mc.player.inventory.currentItem + 36)
            return new ItemSwitcher.InventorySwitchResult(id);
        if (id != -1 && !didInventorySwitch)
            return new ItemSwitcher.InventorySwitchResult(id);
    } else if (type == ItemSwitcher.switchType.Normal || type == ItemSwitcher.switchType.Silent) {
        float Best = 0;
        EnumHand hand = null;
        if (handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Offhand || type == ItemSwitcher.switchType.NoSwitch) {
            float offhandVal = switcher.isItemGood(mc.player.getHeldItemOffhand());
            if (offhandVal > Best) {
                Best = offhandVal;
                hand = EnumHand.OFF_HAND;
            }
        }
        int newSlot = mc.player.inventory.currentItem;
        if (handType == ItemSwitcher.usedHand.Both || handType == ItemSwitcher.usedHand.Mainhand) {
            int i = 0;
            while (i < 9) {
                float val = switcher.isItemGood(mc.player.inventory.getStackInSlot(i));
                if (i == mc.player.inventory.currentItem)
                    val *= 1.1f;
                if (val > Best) {
                    newSlot = i;
                    Best = val;
                    hand = EnumHand.MAIN_HAND;
                }
                i++;
            }
        } else if (type == ItemSwitcher.switchType.NoSwitch) {
            float val = switcher.isItemGood(mc.player.getHeldItemMainhand()) * 1.1f;
            if (val > Best) {
                Best = val;
                hand = EnumHand.MAIN_HAND;
            }
        }
        if (Best > 0)
            return new ItemSwitcher.HotbarSwitchResult(hand, newSlot);
    }
    return null;
}
Also used : EnumHand(net.minecraft.util.EnumHand) ItemSwitcher(me.wallhacks.spark.util.player.itemswitcher.ItemSwitcher)

Aggregations

EnumHand (net.minecraft.util.EnumHand)99 ItemStack (net.minecraft.item.ItemStack)56 EntityPlayer (net.minecraft.entity.player.EntityPlayer)48 BlockPos (net.minecraft.util.math.BlockPos)36 EnumFacing (net.minecraft.util.EnumFacing)25 World (net.minecraft.world.World)24 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)20 EntityLivingBase (net.minecraft.entity.EntityLivingBase)18 Entity (net.minecraft.entity.Entity)17 Item (net.minecraft.item.Item)17 Vec3d (net.minecraft.util.math.Vec3d)16 TileEntity (net.minecraft.tileentity.TileEntity)15 IBlockState (net.minecraft.block.state.IBlockState)14 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)14 RayTraceResult (net.minecraft.util.math.RayTraceResult)13 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)13 Block (net.minecraft.block.Block)12 List (java.util.List)11 EnumActionResult (net.minecraft.util.EnumActionResult)11 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)9