makeArguments(int,Object[])   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
dl 0
loc 16
ccs 7
cts 7
cp 1
crap 3
rs 9.85
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-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.network.game.out.game.action;
21
22
import fr.quatrevieux.araknemu.game.exploration.interaction.action.Action;
23
import fr.quatrevieux.araknemu.game.exploration.interaction.action.ActionType;
24
import fr.quatrevieux.araknemu.game.exploration.interaction.action.BlockingAction;
25
import org.checkerframework.common.value.qual.MinLen;
26
27
/**
28
 * Response for a game action
29
 *
30
 * https://github.com/Emudofus/Dofus/blob/1.29/dofus/aks/GameActions.as#L127
31
 */
32
public final class GameActionResponse {
33
    /** Nul / cancel game action */
34 1
    public static final GameActionResponse NOOP = new GameActionResponse("", ActionType.NONE);
35
36
    private final String id;
37
    private final ActionType type;
38
    private final Object[] arguments;
39
40 1
    public GameActionResponse(String id, ActionType type, Object... arguments) {
41 1
        this.id = id;
42 1
        this.type = type;
43 1
        this.arguments = arguments;
44 1
    }
45
46
    public GameActionResponse(Action action) {
47 1
        this("", action.type(), makeArguments(action.performer().id(), action.arguments()));
48 1
    }
49
50
    public GameActionResponse(BlockingAction action) {
51 1
        this(Integer.toString(action.id()), action.type(), makeArguments(action.performer().id(), action.arguments()));
52 1
    }
53
54
    @Override
55
    public String toString() {
56 1
        final StringBuilder packet = new StringBuilder("GA" + id + ";" + type.id());
57
58 1
        for (Object argument : arguments) {
59 1
            packet.append(';').append(argument.toString());
60
        }
61
62 1
        return packet.toString();
63
    }
64
65
    @SuppressWarnings("assignment") // arguments length not resolved from switch
66
    private static Object[] makeArguments(int spriteId, Object[] arguments) {
67 1
        switch (arguments.length) {
0 ignored issues
show
Code Smell introduced by
You may want to add a default case to this switch to deal with unforseen inputs.

Default branches should deal with the unexpected. At a minimum, they should log the error and if applicable, return a default value (null, empty collection). If you really do not expect the default branch to ever be use, throw a RuntimeException when it is.

Loading history...
68
            case 0:
69 1
                return new Object[] {spriteId};
70
71
            case 1:
72 1
                return new Object[] {spriteId, arguments[0]};
73
        }
74
75 1
        final Object @MinLen(2) [] newArguments = new Object[arguments.length + 1];
76
77 1
        newArguments[0] = spriteId;
78 1
        System.arraycopy(arguments, 0, newArguments, 1, arguments.length);
79
80 1
        return newArguments;
81
    }
82
}
83