Search in sources :

Example 81 with EnumHand

use of net.minecraft.util.EnumHand in project EnderIO by SleepyTrousers.

the class ItemLocationPrintout method getGuiElement.

@Override
@Nullable
public Object getGuiElement(boolean server, @Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nullable EnumFacing facing, int ID, int handID, int param3) {
    if (server) {
        return null;
    } else if (GUI_ID_LOCATION_PRINTOUT_CREATE == ID) {
        int foundPaper = -1;
        for (int paperIndex = 0; paperIndex < player.inventoryContainer.inventorySlots.size() && foundPaper < 0; paperIndex++) {
            ItemStack invItem = player.inventoryContainer.inventorySlots.get(paperIndex).getStack();
            if (invItem.getItem() == Items.PAPER) {
                foundPaper = paperIndex;
            }
        }
        if (foundPaper < 0) {
            player.sendMessage(Lang.PRINTOUT_NOPAPER.toChat());
            return null;
        }
        TelepadTarget target = new TelepadTarget(pos, world.provider.getDimension());
        ItemStack stack = new ItemStack(this);
        target.writeToNBT(stack);
        return new GuiLocationPrintout(target, stack, foundPaper);
    } else if (GUI_ID_LOCATION_PRINTOUT == ID) {
        EnumHand hand = handID == 0 ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND;
        EntityEquipmentSlot slot = hand == EnumHand.MAIN_HAND ? EntityEquipmentSlot.MAINHAND : EntityEquipmentSlot.OFFHAND;
        TelepadTarget target = TelepadTarget.readFromNBT(player.getItemStackFromSlot(slot));
        if (target != null) {
            return new GuiLocationPrintout(target, player, slot);
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : EntityEquipmentSlot(net.minecraft.inventory.EntityEquipmentSlot) EnumHand(net.minecraft.util.EnumHand) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable)

Example 82 with EnumHand

use of net.minecraft.util.EnumHand in project BaseMetals by MinecraftModDevelopmentMods.

the class EventHandler method attackEvent.

/**
 * @param event
 */
@SubscribeEvent
public static void attackEvent(final LivingAttackEvent event) {
    final float damage = event.getAmount();
    if (!(event.getEntityLiving() instanceof EntityPlayer)) {
        return;
    }
    final EntityPlayer player = (EntityPlayer) event.getEntityLiving();
    final ItemStack activeItemStack = player.getActiveItemStack();
    if (activeItemStack.isEmpty()) {
        return;
    }
    if ((damage > 0.0F) && (activeItemStack.getItem() instanceof ItemMMDShield)) {
        final int i = 1 + MathHelper.floor(damage);
        activeItemStack.damageItem(i, player);
        if (activeItemStack.getCount() <= 0) {
            final 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);
            }
            if (FMLCommonHandler.instance().getSide() == Side.CLIENT) {
                player.playSound(SoundEvents.BLOCK_ANVIL_BREAK, 0.8F, 0.8F + (player.world.rand.nextFloat() * 0.4F));
            }
        }
    }
}
Also used : EnumHand(net.minecraft.util.EnumHand) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemMMDShield(com.mcmoddev.lib.item.ItemMMDShield) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 83 with EnumHand

use of net.minecraft.util.EnumHand in project Wizardry by TeamWizardry.

the class ItemBook method onScroll.

@SubscribeEvent
@SideOnly(Side.CLIENT)
public static void onScroll(MouseEvent event) {
    EntityPlayer player = Minecraft.getMinecraft().player;
    if (player == null)
        return;
    if (Keyboard.isCreated() && event.getDwheel() != 0 && player.isSneaking()) {
        for (EnumHand hand : EnumHand.values()) {
            ItemStack stack = player.getHeldItem(hand);
            if (stack.getItem() != ModItems.BOOK)
                continue;
            if (!NBTHelper.getBoolean(stack, "has_spell", false))
                return;
            NBTTagList moduleList = NBTHelper.getList(stack, NBTConstants.NBT.SPELL, net.minecraftforge.common.util.Constants.NBT.TAG_STRING);
            if (moduleList == null)
                return;
            if (event.getDwheel() > 0) {
                List<List<ModuleInstance>> spellModules = SpellUtils.deserializeModuleList(moduleList);
                List<ItemStack> spellItems = SpellUtils.getSpellItems(spellModules);
                int page = NBTHelper.getInt(stack, "page", 0);
                int maxPages = 0;
                int row = 0;
                int column = 0;
                for (int i = 0; i < spellItems.size(); i++) {
                    if (++column >= 3) {
                        column = 0;
                        row++;
                    }
                    if (row >= 9) {
                        row = 0;
                        maxPages++;
                        if (maxPages > page) {
                            NBTHelper.setInt(stack, "page", page + 1);
                        }
                    }
                }
            } else {
                int page = NBTHelper.getInt(stack, "page", 0);
                NBTHelper.setInt(stack, "page", Math.max(page - 1, 0));
            }
            event.setCanceled(true);
        }
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) EnumHand(net.minecraft.util.EnumHand) EntityPlayer(net.minecraft.entity.player.EntityPlayer) NBTTagList(net.minecraft.nbt.NBTTagList) List(java.util.List) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 84 with EnumHand

use of net.minecraft.util.EnumHand in project Cavern2 by kegare.

the class MagicEventHooks method onTick.

@SubscribeEvent
public void onTick(ClientTickEvent event) {
    if (event.phase != Phase.END) {
        return;
    }
    Minecraft mc = FMLClientHandler.instance().getClient();
    if (mc.world == null || mc.player == null || mc.currentScreen != null) {
        return;
    }
    MagicBook book = MagicBook.get(mc.player);
    boolean isMagicKeyDown = CaveKeyBindings.KEY_MAGIC_BOOK.isKeyDown();
    if (isMagicKeyDown || mc.gameSettings.keyBindUseItem.isKeyDown()) {
        ItemStack stack = book.getSpellingMagicBook();
        Magic magic = book.getSpellingMagic();
        if (spellingResult != null && spellingResult != EnumActionResult.PASS) {
            if (!sendResult) {
                if (magic != null) {
                    if (spellingResult == EnumActionResult.SUCCESS) {
                        ActionResult<ITextComponent> result = magic.fireMagic();
                        CaveNetworkRegistry.NETWORK.sendToServer(new MagicResultMessage(result.getType()));
                        ITextComponent message = result.getResult();
                        if (message != null) {
                            mc.ingameGUI.setOverlayMessage(message, true);
                        }
                        if (magic.isOverload()) {
                            book.setSpecialMagic(null);
                        }
                    } else {
                        CaveNetworkRegistry.NETWORK.sendToServer(new MagicResultMessage(EnumActionResult.FAIL));
                    }
                    magic.onCloseBook();
                    SoundEvent sound = magic.getCloseSound();
                    if (sound != null) {
                        mc.getSoundHandler().playDelayedSound(PositionedSoundRecord.getMasterRecord(sound, 1.0F), 5);
                    }
                }
                book.setSpellingMagic(null);
                sendResult = true;
            }
            return;
        }
        if (book.isSpellingCanceled()) {
            spellingResult = EnumActionResult.FAIL;
            return;
        }
        if (magic == null) {
            Pair<EnumHand, ItemStack> magicBook = getMagicBook(mc.player);
            if (magicBook == null) {
                spellingResult = EnumActionResult.FAIL;
                return;
            }
            stack = magicBook.getRight();
            magic = EnumType.byItemStack(stack).createMagic(mc.world, mc.player, magicBook.getLeft());
            if (magic == null) {
                spellingResult = EnumActionResult.FAIL;
                return;
            }
            book.setSpellingMagic(magic);
            if (magic.getSpellingHand() == EnumHand.MAIN_HAND) {
                spellingSlot = mc.player.inventory.currentItem;
            }
            CaveNetworkRegistry.NETWORK.sendToServer(new MagicBookMessage(magic.getSpellingHand()));
        }
        boolean hasSpecialMagic = book.getSpecialMagic() != null;
        boolean infinity = hasSpecialMagic && book.getSpecialMagic() instanceof MagicInfinity;
        if (hasSpecialMagic && magic instanceof SpecialMagic) {
            spellingResult = EnumActionResult.FAIL;
            return;
        }
        if (!mc.player.capabilities.isCreativeMode && ItemMagicBook.isInCoolTime(mc.player, stack)) {
            mc.ingameGUI.setOverlayMessage(new TextComponentTranslation("item.magicBook.fail.time"), false);
            spellingResult = EnumActionResult.FAIL;
            return;
        }
        if (magic.getSpellingHand() == EnumHand.MAIN_HAND) {
            mc.player.inventory.currentItem = spellingSlot;
        }
        double progress = magic.getSpellingProgress();
        if (infinity) {
            progress = MathHelper.clamp(progress * 2.5D, 0.0D, 1.0D);
        }
        if (progress >= 1.0D) {
            spellingResult = EnumActionResult.SUCCESS;
            return;
        }
        EnumActionResult result = magic.onSpelling();
        if (result != EnumActionResult.PASS) {
            spellingResult = result;
            return;
        }
        if (++spellingSoundTicks >= (infinity ? 8 : 12)) {
            SoundEvent sound = magic.getSpellingSound();
            if (sound != null) {
                mc.getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(sound, 1.0F));
            }
            spellingSoundTicks = 0;
        }
        for (int i = 0; i < 2; ++i) {
            int var1 = rand.nextInt(2) * 2 - 1;
            int var2 = rand.nextInt(2) * 2 - 1;
            double ptX = mc.player.posX + 0.25D * var1;
            double ptY = mc.player.posY + 0.7D + rand.nextFloat();
            double ptZ = mc.player.posZ + 0.25D * var2;
            double motionX = rand.nextFloat() * 1.0F * var1;
            double motionY = (rand.nextFloat() - 0.25D) * 0.125D;
            double motionZ = rand.nextFloat() * 1.0F * var2;
            ParticleMagicSpell particle = new ParticleMagicSpell(mc.world, ptX, ptY, ptZ, motionX, motionY, motionZ);
            mc.effectRenderer.addEffect(particle);
        }
    } else {
        Magic magic = book.getSpellingMagic();
        if (magic != null) {
            CaveNetworkRegistry.NETWORK.sendToServer(new MagicResultMessage(EnumActionResult.FAIL));
            magic.onCloseBook();
            SoundEvent sound = magic.getCloseSound();
            if (sound != null) {
                mc.getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(sound, 1.0F));
            }
        }
        book.setSpellingMagic(null);
        spellingResult = null;
        spellingSlot = 0;
        spellingSoundTicks = 0;
        sendResult = false;
    }
    SpecialMagic magic = book.getSpecialMagic();
    if (magic != null && magic.getEffectProgress() >= 1.0D) {
        ITextComponent message = magic.finishMagic();
        if (message != null) {
            mc.ingameGUI.setOverlayMessage(message, false);
        }
        CaveNetworkRegistry.NETWORK.sendToServer(new SpecialMagicMessage());
        book.setSpecialMagic(null);
    }
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) MagicResultMessage(cavern.network.server.MagicResultMessage) Magic(cavern.magic.Magic) SpecialMagic(cavern.magic.SpecialMagic) ParticleMagicSpell(cavern.client.particle.ParticleMagicSpell) ITextComponent(net.minecraft.util.text.ITextComponent) MagicInfinity(cavern.magic.MagicInfinity) SpecialMagicMessage(cavern.network.server.SpecialMagicMessage) Minecraft(net.minecraft.client.Minecraft) SoundEvent(net.minecraft.util.SoundEvent) SpecialMagic(cavern.magic.SpecialMagic) EnumActionResult(net.minecraft.util.EnumActionResult) EnumHand(net.minecraft.util.EnumHand) MagicBookMessage(cavern.network.server.MagicBookMessage) ItemStack(net.minecraft.item.ItemStack) ItemMagicBook(cavern.item.ItemMagicBook) MagicBook(cavern.magic.MagicBook) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 85 with EnumHand

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

the class SpongeToForgeEventFactory method createAndPostPlayerInteractItemEvent.

private static boolean createAndPostPlayerInteractItemEvent(final SpongeToForgeEventData eventData) {
    final InteractItemEvent.Secondary spongeEvent = (InteractItemEvent.Secondary) eventData.getSpongeEvent();
    PlayerInteractEvent.RightClickItem forgeEvent = (PlayerInteractEvent.RightClickItem) eventData.getForgeEvent();
    if (forgeEvent == null) {
        final Player player = spongeEvent.getCause().first(Player.class).orElse(null);
        if (player == null) {
            return false;
        }
        final EnumHand hand = spongeEvent instanceof InteractItemEvent.Secondary.MainHand ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND;
        forgeEvent = new PlayerInteractEvent.RightClickItem((EntityPlayerMP) player, hand);
        eventData.setForgeEvent(forgeEvent);
    }
    forgeEvent.setCanceled(spongeEvent.isCancelled());
    forgeEventBus.forgeBridge$post(eventData);
    return true;
}
Also used : InteractItemEvent(org.spongepowered.api.event.item.inventory.InteractItemEvent) Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) EnumHand(net.minecraft.util.EnumHand) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Aggregations

EnumHand (net.minecraft.util.EnumHand)117 ItemStack (net.minecraft.item.ItemStack)62 EntityPlayer (net.minecraft.entity.player.EntityPlayer)55 BlockPos (net.minecraft.util.math.BlockPos)45 EnumFacing (net.minecraft.util.EnumFacing)32 Vec3d (net.minecraft.util.math.Vec3d)24 World (net.minecraft.world.World)24 Entity (net.minecraft.entity.Entity)23 RayTraceResult (net.minecraft.util.math.RayTraceResult)21 EntityLivingBase (net.minecraft.entity.EntityLivingBase)19 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)19 Item (net.minecraft.item.Item)18 CPacketPlayerTryUseItemOnBlock (net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock)18 IBlockState (net.minecraft.block.state.IBlockState)17 TileEntity (net.minecraft.tileentity.TileEntity)16 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)15 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)14 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)13 List (java.util.List)12 Block (net.minecraft.block.Block)11