summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandfirework.java
blob: aa1e608808ae75999cb44f185654ab7b39d75e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package net.ess3.commands;

import static net.ess3.I18n._;
import java.util.regex.Pattern;

import net.ess3.MetaItemStack;
import net.ess3.api.ISettings;
import net.ess3.api.IUser;
import net.ess3.permissions.Permissions;
import net.ess3.utils.Util;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.util.Vector;


//This command has quite a complicated syntax, in theory it has 4 separate syntaxes which are all variable:
//
//1: /firework clear             - This clears all of the effects on a firework stack
//
//2: /firework power <int>       - This changes the base power of a firework
//
//3: /firework fire              - This 'fires' a copy of the firework held.
//3: /firework fire <int>        - This 'fires' a number of copies of the firework held.
//3: /firework fire <other>      - This 'fires' a copy of the firework held, in the direction you are looking, #easteregg
//
//4: /firework [meta]            - This will add an effect to the firework stack held
//4: /firework color:<color>     - The minimum you need to set an effect is 'color'
//4: Full Syntax:                  color:<color[,color,..]> [fade:<color[,color,..]>] [shape:<shape>] [effect:<effect[,effect]>]
//4: Possible Shapes:              star, ball, large, creeper, burst
//4: Possible Effects              trail, twinkle

public class Commandfirework extends EssentialsCommand
{
    private final transient Pattern splitPattern = Pattern.compile("[:+',;.]");

    @Override
    protected void run(final IUser user, final String commandLabel, final String[] args) throws Exception
    {
        final Player player = user.getPlayer();
        final ItemStack stack = user.getPlayer().getItemInHand();
        if (stack.getType() == Material.FIREWORK)
        {
            if (args.length > 0)
            {
                if (args[0].equalsIgnoreCase("clear"))
                {
                    FireworkMeta fmeta = (FireworkMeta)stack.getItemMeta();
                    fmeta.clearEffects();
                    stack.setItemMeta(fmeta);
                    user.sendMessage(_("Removed all effects from held stack."));
                }
                else if (args.length > 1 && (args[0].equalsIgnoreCase("power") || (args[0].equalsIgnoreCase("p"))))
                {
                    FireworkMeta fmeta = (FireworkMeta)stack.getItemMeta();
                    try
                    {
                        int power = Integer.parseInt(args[1]);
                        fmeta.setPower(power > 3 ? 4 : power);
                    }
                    catch (NumberFormatException e)
                    {
                        throw new Exception(_("The option {0} is not a valid value for {1}.", args[1], args[0]));
                    }
                    stack.setItemMeta(fmeta);
                }
                else if ((args[0].equalsIgnoreCase("fire") || (args[0].equalsIgnoreCase("f")))
                        && Permissions.FIREWORK_FIRE.isAuthorized(user))
                {
                    int amount = 1;
                    boolean direction = false;
                    if (args.length > 1)
                    {
                        if (Util.isInt(args[1]))
                        {
                            ISettings settings = ess.getSettings();
                            int serverLimit = settings.getData().getCommands().getSpawnmob().getLimit();
                            amount = Integer.parseInt(args[1]);
                            if (amount > serverLimit)
                            {
                                amount = serverLimit;
                                user.sendMessage(_("Mob quantity limited to server limit."));
                            }
                        }
                        else
                        {
                            direction = true;
                        }
                    }
                    for (int i = 0; i < amount; i++)
                    {
                        Firework firework = (Firework)player.getWorld().spawnEntity(player.getLocation(), EntityType.FIREWORK);
                        FireworkMeta fmeta = (FireworkMeta)stack.getItemMeta();
                        if (direction)
                        {
                            final Vector vector = player.getEyeLocation().getDirection().multiply(0.070);
                            if (fmeta.getPower() > 1)
                            {
                                fmeta.setPower(1);
                            }
                            firework.setVelocity(vector);
                        }
                        firework.setFireworkMeta(fmeta);
                    }
                }
                else
                {
                    final MetaItemStack mStack = new MetaItemStack(stack);
                    for (String arg : args)
                    {
                        try
                        {
                            mStack.addFireworkMeta(user, true, arg, ess);
                        }
                        catch (Exception e)
                        {
                            user.sendMessage(_("Firework parameters: color:<color> [fade:<color>] [shape:<shape>] [effect:<effect>]nTo use multiple colors/effects, separate values with commas: red,blue,pinknShapes: star, ball, large, creeper, burst Effects: trail, twinkle."));
                            throw e;
                        }
                    }

                    if (mStack.isValidFirework())
                    {
                        FireworkMeta fmeta = (FireworkMeta)mStack.getItemStack().getItemMeta();
                        FireworkEffect effect = mStack.getFireworkBuilder().build();
                        if (fmeta.getEffects().size() > 0 && !Permissions.FIREWORK_MULTIPLE.isAuthorized(user))
                        {
                            throw new Exception(_("You cannot apply more than one charge to this firework."));
                        }
                        fmeta.addEffect(effect);
                        stack.setItemMeta(fmeta);
                    }
                    else
                    {
                        user.sendMessage(_("Firework parameters: color:<color> [fade:<color>] [shape:<shape>] [effect:<effect>]nTo use multiple colors/effects, separate values with commas: red,blue,pinknShapes: star, ball, large, creeper, burst Effects: trail, twinkle."));
                        throw new Exception(_("Invalid firework charge parameters inserted, must set a color first."));
                    }
                }
            }
            else
            {
                throw new NotEnoughArgumentsException();
            }
        }
        else
        {
            throw new Exception(_("You must be holding a firework to add effects."));
        }
    }
}