RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
CommandParser.java
1package com.osroyale.game.world.entity.mob.player.command;
2
3import java.util.Arrays;
4
37
38public final class CommandParser {
39
40 private final String command;
41 private final String[] arguments;
42
43 private int pointer;
44
45 private CommandParser(String command, String input, String splitter) {
46 this.command = command;
47 this.arguments = Arrays.stream(input.split(splitter)).toArray(String[]::new);
48 }
49
50 public static CommandParser split(CommandParser parser, String splitter) {
51 return new CommandParser(parser.getCommand(), parser.nextLine(), splitter);
52 }
53
54 public static CommandParser split(String input, String splitter) {
55 final int index = input.indexOf(splitter);
56
57 final String command = index == -1 ? input : input.substring(0, index);
58
59 return split(command, input, splitter);
60 }
61
62 public static CommandParser split(String command, String input, String splitter) {
63
64 int position = input.indexOf(command);
65
66 if (position == -1) {
67 position = 0;
68 }
69
70 final String string = input.substring(position + command.length()).trim();
71
72 return new CommandParser(command, string, splitter);
73 }
74
75 public String getCommand() {
76 return command;
77 }
78
79 public boolean hasNext() {
80 if (arguments.length == 1) {
81 if (arguments[0].isEmpty()) {
82 return false;
83 }
84 }
85 return hasNext(1);
86 }
87
88 public boolean hasNext(int length) {
89 return pointer + length <= arguments.length;
90 }
91
92 public boolean nextBoolean() {
93 return Boolean.parseBoolean(nextString());
94 }
95
96 public byte nextByte() throws NumberFormatException {
97 return Byte.parseByte(nextString());
98 }
99
100 public double nextDouble() throws NumberFormatException {
101 return Double.parseDouble(nextString());
102 }
103
104 public int nextInt() throws NumberFormatException {
105 return Integer.parseInt(nextString());
106 }
107
108 public long nextLong() throws NumberFormatException {
109 return Long.parseLong(nextString());
110 }
111
112 public short nextShort() throws NumberFormatException {
113 return Short.parseShort(nextString());
114 }
115
116 public String nextString() throws ArrayIndexOutOfBoundsException {
117 if (pointer >= arguments.length) {
118 throw new ArrayIndexOutOfBoundsException("The next argument does not exist. [Size: " + arguments.length + ", Attempted: " + pointer + "]");
119 }
120
121 return arguments[pointer++];
122 }
123
124 public String nextLine() {
125 final StringBuilder builder = new StringBuilder();
126
127 while (hasNext()) {
128 builder.append(" ").append(nextString());
129 }
130 return builder.toString().trim();
131 }
132
133 public int argumentCount() {
134 return arguments.length;
135 }
136
137 public String[] getArguments() {
138 return arguments;
139 }
140
141 @Override
142 public String toString() {
143 return String.format("command=[%s] arguments=%s", command, arguments == null ? "none" : Arrays.toString(arguments));
144 }
145
146}