Passed
Pull Request — master (#172)
by Vincent
14:22
created

quantity()   A

Complexity

Conditions 2

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.admin.player;
21
22
import fr.quatrevieux.araknemu.common.account.Permission;
23
import fr.quatrevieux.araknemu.data.constant.Effect;
24
import fr.quatrevieux.araknemu.data.value.ItemTemplateEffectEntry;
25
import fr.quatrevieux.araknemu.data.world.transformer.ItemEffectsTransformer;
26
import fr.quatrevieux.araknemu.game.admin.AbstractCommand;
27
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
28
import fr.quatrevieux.araknemu.game.admin.exception.CommandException;
29
import fr.quatrevieux.araknemu.game.admin.formatter.Link;
30
import fr.quatrevieux.araknemu.game.item.Item;
31
import fr.quatrevieux.araknemu.game.item.ItemService;
32
import fr.quatrevieux.araknemu.game.item.effect.ItemEffect;
33
import fr.quatrevieux.araknemu.game.player.GamePlayer;
34
import org.apache.commons.lang3.StringUtils;
35
import org.kohsuke.args4j.Argument;
36
import org.kohsuke.args4j.CmdLineException;
37
import org.kohsuke.args4j.CmdLineParser;
38
import org.kohsuke.args4j.Option;
39
import org.kohsuke.args4j.OptionDef;
40
import org.kohsuke.args4j.spi.OptionHandler;
41
import org.kohsuke.args4j.spi.Parameters;
42
import org.kohsuke.args4j.spi.Setter;
43
44
import java.util.ArrayList;
45
import java.util.List;
46
47
/**
48
 * Get an item for the player
49
 */
50
public final class GetItem extends AbstractCommand<GetItem.Options> {
51
    private final GamePlayer player;
52
    private final ItemService service;
53
54 1
    public GetItem(GamePlayer player, ItemService service) {
55 1
        this.player = player;
56 1
        this.service = service;
57 1
    }
58
59
    @Override
60
    protected void build(Builder builder) {
61 1
        builder
62 1
            .description("Add an item to the player")
63 1
            .help(
64 1
                formatter -> formatter
65 1
                    .synopsis("getitem [options] item_id [quantity=1]")
66
67 1
                    .options("--max", "Generate item with maximized stats")
68 1
                    .options("--each", "Regenerate item stats for each [quantity] items instead of generate the same item with [quantity]")
69 1
                    .options("--effects",
70
                        "Set the item effects\n" +
71
                        "The effects should be a list of effects separated with comma ','. Available formats :\n" +
72
                        "Item template format : 64#b#f#0#1d5+10,7d#b#0#0#0d0+11,9a#f#0#0#0d0+15\n" +
73
                        "Simplified format    : INFLICT_DAMAGE_NEUTRAL:11:15,ADD_VITALITY:11,SUB_AGILITY:15\n" +
74
                        "This option is not compatible with --max option.\n" +
75
                        "If a range value is set for a characteristic effect, a random value will be generated\n"
76
                    )
77
78 1
                    .example("getitem 2425", "Generate a random 'Amulette du Bouftou'")
79 1
                    .example("!getitem 2425 3", "Generate 3 random 'Amulette du Bouftou', and ensure that the admin user is the target")
80 1
                    .example("${player:Robert} getitem 39", "Add to Robert the 'Petite Amulette du Hibou'")
81 1
                    .example("getitem --max 2425", "Generate an 'Amulette du Bouftou' with max stats")
82 1
                    .example("getitem --effects 5b#1#32#0#,5c#1#32#0#,5d#1#32#0#,5e#1#32#0#,5f#1#32#0# 40", "Cheated 'Petite Epée de Boisaille'")
83 1
                    .example("getitem --effects STOLEN_WATER:1:50,STOLEN_EARTH:1:50,STOLEN_WIND:1:50,STOLEN_FIRE:1:50,STOLEN_NEUTRAL:1:50 40", "Same as above")
84
85 1
                    .seeAlso("/ui itemsummoner", "Show the item picker", Link.Type.EXECUTE)
86
            )
87 1
            .requires(Permission.MANAGE_PLAYER)
88
        ;
89 1
    }
90
91
    @Override
92
    public String name() {
93 1
        return "getitem";
94
    }
95
96
    @Override
97
    public Options createArguments() {
98 1
        return new Options();
99
    }
100
101
    @Override
102
    public void execute(AdminPerformer performer, Options options) throws CommandException {
103 1
        for (int j = 0; j < options.times(); ++j) {
104 1
            final Item item = options.hasCustomEffects()
105 1
                ? service.retrieve(options.itemId(), options.effects())
106 1
                : service.create(options.itemId(), options.max())
107
            ;
108
109 1
            player.inventory().add(item, options.quantity());
110
111 1
            performer.success("Generate {} '{}'", options.quantity(), item.template().name());
112
113 1
            if (!item.effects().isEmpty()) {
114 1
                performer.success("Effects :");
115
116 1
                for (ItemEffect effect : item.effects()) {
117 1
                    performer.success("\t{}", effect.toString());
118 1
                }
119
            }
120
        }
121 1
    }
122
123 1
    public static final class Options {
124 1
        @Option(name = "--max")
125
        private boolean max = false;
126
127 1
        @Option(name = "--each")
128
        private boolean each = false;
129
130 1
        @Option(name = "--effects", handler = EffectsConverter.class)
131
        private List<ItemTemplateEffectEntry> effects = null;
132
133
        @Argument(required = true, index = 0, metaVar = "item id")
134
        private int itemId;
135
136 1
        @Argument(index = 1, metaVar = "quantity")
137
        private int quantity = 1;
138
139
        public boolean max() {
140 1
            return max;
141
        }
142
143
        public void setMax(boolean max) {
144
            this.max = max;
145
        }
146
147
        public boolean each() {
148
            return each;
149
        }
150
151
        public void setEach(boolean each) {
152
            this.each = each;
153
        }
154
155
        public List<ItemTemplateEffectEntry> effects() {
156 1
            return effects;
157
        }
158
159
        public boolean hasCustomEffects() {
160 1
            return effects != null;
161
        }
162
163
        public int itemId() {
164 1
            return itemId;
165
        }
166
167
        public int times() {
168 1
            return each ? quantity : 1;
169
        }
170
171
        public int quantity() {
172 1
            return each ? 1 : quantity;
173
        }
174
    }
175
176
    public static class EffectsConverter extends OptionHandler<ItemTemplateEffectEntry> {
177
        public EffectsConverter(CmdLineParser parser, OptionDef option, Setter<? super ItemTemplateEffectEntry> setter) {
178 1
            super(parser, option, setter);
179 1
        }
180
181
        @Override
182
        public int parseArguments(Parameters params) throws CmdLineException {
183 1
            for (ItemTemplateEffectEntry effect : parseEffects(params.getParameter(0))) {
184 1
                setter.addValue(effect);
185 1
            }
186
187 1
            return 1;
188
        }
189
190
        @Override
191
        public String getDefaultMetaVariable() {
192
            return "effects,...";
193
        }
194
195
        private List<ItemTemplateEffectEntry> parseEffects(String value) throws CmdLineException {
196 1
            if (value.contains("#")) {
197 1
                return new ItemEffectsTransformer().unserialize(value);
198
            }
199
200 1
            final List<ItemTemplateEffectEntry> effects = new ArrayList<>();
201
202 1
            for (String strEffect : StringUtils.split(value, ",")) {
203 1
                final String[] parts = StringUtils.split(strEffect, ":", 5);
204
                final Effect effect;
205
206
                try {
207 1
                    effect = Effect.valueOf(parts[0].toUpperCase());
208 1
                } catch (IllegalArgumentException e) {
209 1
                    throw new CmdLineException(owner, "Undefined effect " + parts[0], e);
210 1
                }
211
212 1
                effects.add(
213
                    new ItemTemplateEffectEntry(
214
                        effect,
215 1
                        parts.length > 1 ? Integer.parseInt(parts[1]) : 0,
216 1
                        parts.length > 2 ? Integer.parseInt(parts[2]) : 0,
217 1
                        parts.length > 3 ? Integer.parseInt(parts[3]) : 0,
218
                        parts.length > 4 ? parts[4]                   : ""
219
                    )
220
                );
221
            }
222
223 1
            return effects;
224
        }
225
    }
226
}
227