Search in sources :

Example 1 with ParticleMagicSpell

use of cavern.client.particle.ParticleMagicSpell 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.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.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.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 = RANDOM.nextInt(2) * 2 - 1;
            int var2 = RANDOM.nextInt(2) * 2 - 1;
            double ptX = mc.player.posX + 0.25D * var1;
            double ptY = mc.player.posY + 0.7D + RANDOM.nextFloat();
            double ptZ = mc.player.posZ + 0.25D * var2;
            double motionX = RANDOM.nextFloat() * 1.0F * var1;
            double motionY = (RANDOM.nextFloat() - 0.25D) * 0.125D;
            double motionZ = RANDOM.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.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.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)

Aggregations

ParticleMagicSpell (cavern.client.particle.ParticleMagicSpell)1 ItemMagicBook (cavern.item.ItemMagicBook)1 Magic (cavern.magic.Magic)1 MagicBook (cavern.magic.MagicBook)1 MagicInfinity (cavern.magic.MagicInfinity)1 SpecialMagic (cavern.magic.SpecialMagic)1 MagicBookMessage (cavern.network.server.MagicBookMessage)1 MagicResultMessage (cavern.network.server.MagicResultMessage)1 SpecialMagicMessage (cavern.network.server.SpecialMagicMessage)1 Minecraft (net.minecraft.client.Minecraft)1 ItemStack (net.minecraft.item.ItemStack)1 EnumActionResult (net.minecraft.util.EnumActionResult)1 EnumHand (net.minecraft.util.EnumHand)1 SoundEvent (net.minecraft.util.SoundEvent)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1