Passed
Pull Request — master (#172)
by Vincent
13:00
created

EffectsConverter(CmdLineParser,OptionDef,Setter)   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
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.Arguments> {
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
            .help(
63 1
                formatter -> formatter
64 1
                    .description("Add an item to the player")
65 1
                    .example("getitem 2425", "Generate a random 'Amulette du Bouftou'")
66 1
                    .example("!getitem 2425 3", "Generate 3 random 'Amulette du Bouftou', and ensure that the admin user is the target")
67 1
                    .example("${player:Robert} getitem 39", "Add to Robert the 'Petite Amulette du Hibou'")
68 1
                    .example("getitem --max 2425", "Generate an 'Amulette du Bouftou' with max stats")
69 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'")
70 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")
71
72 1
                    .seeAlso("/ui itemsummoner", "Show the item picker", Link.Type.EXECUTE)
73
            )
74 1
            .requires(Permission.MANAGE_PLAYER)
75 1
            .arguments(Arguments::new)
76
        ;
77 1
    }
78
79
    @Override
80
    public String name() {
81 1
        return "getitem";
82
    }
83
84
    @Override
85
    public void execute(AdminPerformer performer, Arguments arguments) throws CommandException {
86 1
        for (int j = 0; j < arguments.times(); ++j) {
87 1
            final Item item = arguments.hasCustomEffects()
88 1
                ? service.retrieve(arguments.itemId(), arguments.effects())
89 1
                : service.create(arguments.itemId(), arguments.max())
90
            ;
91
92 1
            player.inventory().add(item, arguments.quantity());
93
94 1
            performer.success("Generate {} '{}'", arguments.quantity(), item.template().name());
95
96 1
            if (!item.effects().isEmpty()) {
97 1
                performer.success("Effects :");
98
99 1
                for (ItemEffect effect : item.effects()) {
100 1
                    performer.success("\t{}", effect.toString());
101 1
                }
102
            }
103
        }
104 1
    }
105
106 1
    public static final class Arguments {
107 1
        @Option(name = "--max", usage = "Generate item with maximized characteristics")
108
        private boolean max = false;
109
110 1
        @Option(name = "--each", usage = "Regenerate item stats for each QUANTITY items instead of generate the same item with QUANTITY")
111
        private boolean each = false;
112
113 1
        @Option(
114
            name = "--effects", handler = EffectsConverter.class,
115
            usage = "Set the item effects\n" +
116
                "The effects should be a list of effects separated with comma ','. Available formats :\n" +
117
                "Item template format : 64#b#f#0#1d5+10,7d#b#0#0#0d0+11,9a#f#0#0#0d0+15\n" +
118
                "Simplified format    : INFLICT_DAMAGE_NEUTRAL:11:15,ADD_VITALITY:11,SUB_AGILITY:15\n" +
119
                "This option is not compatible with --max option.\n" +
120
                "If a range value is set for a characteristic effect, a random value will be generated"
121
        )
122
        private List<ItemTemplateEffectEntry> effects = null;
123
124
        @Argument(
125
            required = true, index = 0, metaVar = "ITEM_ID",
126
            usage = "The id of the item to generate. It can be found using /ui itemsummoner command"
127
        )
128
        private int itemId;
129
130 1
        @Argument(
131
            index = 1, metaVar = "QUANTITY",
132
            usage = "The quantity of item to generate. By default all generated items will gets the same characteristics unless --each option is used."
133
        )
134
        private int quantity = 1;
135
136
        public boolean max() {
137 1
            return max;
138
        }
139
140
        public void setMax(boolean max) {
141
            this.max = max;
142
        }
143
144
        public boolean each() {
145
            return each;
146
        }
147
148
        public void setEach(boolean each) {
149
            this.each = each;
150
        }
151
152
        public List<ItemTemplateEffectEntry> effects() {
153 1
            return effects;
154
        }
155
156
        public boolean hasCustomEffects() {
157 1
            return effects != null;
158
        }
159
160
        public int itemId() {
161 1
            return itemId;
162
        }
163
164
        public int times() {
165 1
            return each ? quantity : 1;
166
        }
167
168
        public int quantity() {
169 1
            return each ? 1 : quantity;
170
        }
171
    }
172
173
    public static class EffectsConverter extends OptionHandler<ItemTemplateEffectEntry> {
174
        public EffectsConverter(CmdLineParser parser, OptionDef option, Setter<? super ItemTemplateEffectEntry> setter) {
175 1
            super(parser, option, setter);
176 1
        }
177
178
        @Override
179
        public int parseArguments(Parameters params) throws CmdLineException {
180 1
            for (ItemTemplateEffectEntry effect : parseEffects(params.getParameter(0))) {
181 1
                setter.addValue(effect);
182 1
            }
183
184 1
            return 1;
185
        }
186
187
        @Override
188
        public String getDefaultMetaVariable() {
189 1
            return "effects,...";
190
        }
191
192
        private List<ItemTemplateEffectEntry> parseEffects(String value) throws CmdLineException {
193 1
            if (value.contains("#")) {
194 1
                return new ItemEffectsTransformer().unserialize(value);
195
            }
196
197 1
            final List<ItemTemplateEffectEntry> effects = new ArrayList<>();
198
199 1
            for (String strEffect : StringUtils.split(value, ",")) {
200 1
                final String[] parts = StringUtils.split(strEffect, ":", 5);
201
                final Effect effect;
202
203
                try {
204 1
                    effect = Effect.valueOf(parts[0].toUpperCase());
205 1
                } catch (IllegalArgumentException e) {
206 1
                    throw new CmdLineException(owner, "Undefined effect " + parts[0], e);
207 1
                }
208
209 1
                effects.add(
210
                    new ItemTemplateEffectEntry(
211
                        effect,
212 1
                        parts.length > 1 ? Integer.parseInt(parts[1]) : 0,
213 1
                        parts.length > 2 ? Integer.parseInt(parts[2]) : 0,
214 1
                        parts.length > 3 ? Integer.parseInt(parts[3]) : 0,
215
                        parts.length > 4 ? parts[4]                   : ""
216
                    )
217
                );
218
            }
219
220 1
            return effects;
221
        }
222
    }
223
}
224