RuneHive-Game
Loading...
Searching...
No Matches
CommandParser.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.player.command;
2
3import java.util.Arrays;
4
5/**
6 * @author Chex | Michael
7 * Modified by nshusa
8 */
9public final class CommandParser {
10
11 private final String command;
12 private final String[] arguments;
13
14 private int pointer;
15
16 private CommandParser(String command, String input, String splitter) {
17 this.command = command;
18 this.arguments = Arrays.stream(input.split(splitter)).toArray(String[]::new);
19 }
20
21 public static CommandParser split(CommandParser parser, String splitter) {
22 return new CommandParser(parser.getCommand(), parser.nextLine(), splitter);
23 }
24
25 public static CommandParser split(String input, String splitter) {
26 final int index = input.indexOf(splitter);
27
28 final String command = index == -1 ? input : input.substring(0, index);
29
30 return split(command, input, splitter);
31 }
32
33 public static CommandParser split(String command, String input, String splitter) {
34
35 int position = input.indexOf(command);
36
37 if (position == -1) {
38 position = 0;
39 }
40
41 final String string = input.substring(position + command.length()).trim();
42
43 return new CommandParser(command, string, splitter);
44 }
45
46 public String getCommand() {
47 return command;
48 }
49
50 public boolean hasNext() {
51 if (arguments.length == 1) {
52 if (arguments[0].isEmpty()) {
53 return false;
54 }
55 }
56 return hasNext(1);
57 }
58
59 public boolean hasNext(int length) {
60 return pointer + length <= arguments.length;
61 }
62
63 public boolean nextBoolean() {
64 return Boolean.parseBoolean(nextString());
65 }
66
67 public byte nextByte() throws NumberFormatException {
68 return Byte.parseByte(nextString());
69 }
70
71 public double nextDouble() throws NumberFormatException {
72 return Double.parseDouble(nextString());
73 }
74
75 public int nextInt() throws NumberFormatException {
76 return Integer.parseInt(nextString());
77 }
78
79 public long nextLong() throws NumberFormatException {
80 return Long.parseLong(nextString());
81 }
82
83 public short nextShort() throws NumberFormatException {
84 return Short.parseShort(nextString());
85 }
86
87 public String nextString() throws ArrayIndexOutOfBoundsException {
88 if (pointer >= arguments.length) {
89 throw new ArrayIndexOutOfBoundsException("The next argument does not exist. [Size: " + arguments.length + ", Attempted: " + pointer + "]");
90 }
91
92 return arguments[pointer++];
93 }
94
95 public String nextLine() {
96 final StringBuilder builder = new StringBuilder();
97
98 while (hasNext()) {
99 builder.append(" ").append(nextString());
100 }
101 return builder.toString().trim();
102 }
103
104 public int argumentCount() {
105 return arguments.length;
106 }
107
108 public String[] getArguments() {
109 return arguments;
110 }
111
112 @Override
113 public String toString() {
114 return String.format("command=[%s] arguments=%s", command, arguments == null ? "none" : Arrays.toString(arguments));
115 }
116
117}
static CommandParser split(String input, String splitter)
static CommandParser split(String command, String input, String splitter)
static CommandParser split(CommandParser parser, String splitter)
CommandParser(String command, String input, String splitter)