itemId()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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