Search in sources :

Example 1 with SpecialMagic

use of cavern.magic.SpecialMagic 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)

Example 2 with SpecialMagic

use of cavern.magic.SpecialMagic in project Cavern2 by kegare.

the class MagicResultMessage method process.

@Override
public IMessage process(EntityPlayerMP player) {
    MagicBook book = MagicBook.get(player);
    Magic magic = book.getSpellingMagic();
    if (magic != null) {
        EnumActionResult type;
        ITextComponent message;
        if (clientResult == EnumActionResult.PASS) {
            ActionResult<ITextComponent> result = magic.fireMagic();
            type = result.getType();
            message = result.getResult();
        } else {
            type = clientResult;
            message = null;
        }
        if (type == EnumActionResult.SUCCESS) {
            ItemStack stack = book.getSpellingMagicBook();
            World world = player.getServerWorld();
            int cost = 0;
            SpecialMagic specialMagic = book.getSpecialMagic();
            if (specialMagic != null && specialMagic != magic && specialMagic.hasSpecialCost(magic)) {
                cost = specialMagic.getSpecialCost(magic);
            } else {
                cost = magic.getCost();
            }
            if (cost == 0) {
                ItemMagicBook.setLastUseTime(stack, world.getTotalWorldTime());
            } else {
                if (ItemMagicBook.consumeMana(stack, cost) > 0) {
                    ItemMagicBook.setLastUseTime(stack, world.getTotalWorldTime());
                } else {
                    player.setHeldItem(magic.getSpellingHand(), new ItemStack(Items.BOOK));
                }
            }
            SoundEvent sound = magic.getSuccessSound();
            if (sound != null) {
                world.playSound(null, player.posX, player.posY + 0.25D, player.posZ, sound, SoundCategory.PLAYERS, 1.0F, 1.0F);
            }
            if (magic.isOverload()) {
                book.setSpecialMagic(null);
            }
        }
        if (message != null) {
            player.sendStatusMessage(message, true);
        }
        magic.onCloseBook();
    }
    book.setSpellingMagic(null);
    return null;
}
Also used : SpecialMagic(cavern.magic.SpecialMagic) SoundEvent(net.minecraft.util.SoundEvent) EnumActionResult(net.minecraft.util.EnumActionResult) Magic(cavern.magic.Magic) SpecialMagic(cavern.magic.SpecialMagic) ITextComponent(net.minecraft.util.text.ITextComponent) ItemStack(net.minecraft.item.ItemStack) World(net.minecraft.world.World) ItemMagicBook(cavern.item.ItemMagicBook) MagicBook(cavern.magic.MagicBook)

Example 3 with SpecialMagic

use of cavern.magic.SpecialMagic in project Cavern2 by kegare.

the class SpecialMagicMessage method process.

@Override
public IMessage process(EntityPlayerMP player) {
    MagicBook book = MagicBook.get(player);
    SpecialMagic magic = book.getSpecialMagic();
    if (magic != null) {
        ITextComponent message = magic.finishMagic();
        if (message != null) {
            player.sendStatusMessage(message, true);
        }
        SoundEvent sound = magic.getFinishSound();
        if (sound != null) {
            player.world.playSound(null, player.posX, player.posY + 0.25D, player.posZ, sound, SoundCategory.PLAYERS, 1.0F, 1.0F);
        }
        book.setSpecialMagic(null);
    }
    return null;
}
Also used : SpecialMagic(cavern.magic.SpecialMagic) SoundEvent(net.minecraft.util.SoundEvent) ITextComponent(net.minecraft.util.text.ITextComponent) MagicBook(cavern.magic.MagicBook)

Aggregations

MagicBook (cavern.magic.MagicBook)3 SpecialMagic (cavern.magic.SpecialMagic)3 SoundEvent (net.minecraft.util.SoundEvent)3 ITextComponent (net.minecraft.util.text.ITextComponent)3 ItemMagicBook (cavern.item.ItemMagicBook)2 Magic (cavern.magic.Magic)2 ItemStack (net.minecraft.item.ItemStack)2 EnumActionResult (net.minecraft.util.EnumActionResult)2 ParticleMagicSpell (cavern.client.particle.ParticleMagicSpell)1 MagicInfinity (cavern.magic.MagicInfinity)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 EnumHand (net.minecraft.util.EnumHand)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1 World (net.minecraft.world.World)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1