Created ArgumentRelativeVec to prevent code duplication

This commit is contained in:
themode 2020-11-11 03:31:15 +01:00
parent 6cbe656b15
commit d520a0ebc4
3 changed files with 56 additions and 79 deletions

View File

@ -0,0 +1,54 @@
package net.minestom.server.command.builder.arguments.relative;
import net.minestom.server.utils.location.RelativeVec;
import org.jetbrains.annotations.NotNull;
/**
* Common super class for {@link ArgumentRelativeVec2} and {@link ArgumentRelativeVec3}.
*/
public abstract class ArgumentRelativeVec extends ArgumentRelative<RelativeVec> {
public ArgumentRelativeVec(@NotNull String id, int numberCount) {
super(id, numberCount);
}
@Override
public int getCorrectionResult(@NotNull String value) {
final String[] split = value.split(" ");
// Check if the value has enough element to be correct
if (split.length != getNumberCount()) {
return INVALID_NUMBER_COUNT_ERROR;
}
// Check if each element is correct
for (String element : split) {
if (!element.startsWith(RELATIVE_CHAR)) {
try {
// Will throw the exception if not a float
Float.parseFloat(element);
} catch (NumberFormatException e) {
return INVALID_NUMBER_ERROR;
}
} else {
if (element.length() > RELATIVE_CHAR.length()) {
try {
final String potentialNumber = element.substring(1);
// Will throw the exception if not a float
Float.parseFloat(potentialNumber);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
return INVALID_NUMBER_ERROR;
}
}
}
}
return SUCCESS;
}
@Override
public int getConditionResult(@NotNull RelativeVec value) {
return SUCCESS;
}
}

View File

@ -9,46 +9,12 @@ import org.jetbrains.annotations.NotNull;
* <p>
* Example: -1.2 ~
*/
public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
public class ArgumentRelativeVec2 extends ArgumentRelativeVec {
public ArgumentRelativeVec2(@NotNull String id) {
super(id, 2);
}
@Override
public int getCorrectionResult(@NotNull String value) {
final String[] split = value.split(" ");
// Check if the value has enough element to be correct
if (split.length != getNumberCount()) {
return INVALID_NUMBER_COUNT_ERROR;
}
// Check if each element is correct
for (String element : split) {
if (!element.startsWith(RELATIVE_CHAR)) {
try {
// Will throw the exception if not a float
Float.parseFloat(element);
} catch (NumberFormatException e) {
return INVALID_NUMBER_ERROR;
}
} else {
if (element.length() > RELATIVE_CHAR.length()) {
try {
final String potentialNumber = element.substring(1);
// Will throw the exception if not a float
Float.parseFloat(potentialNumber);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
return INVALID_NUMBER_ERROR;
}
}
}
}
return SUCCESS;
}
@NotNull
@Override
public RelativeVec parse(@NotNull String value) {
@ -90,8 +56,4 @@ public class ArgumentRelativeVec2 extends ArgumentRelative<RelativeVec> {
return new RelativeVec(vector, relativeX, false, relativeZ);
}
@Override
public int getConditionResult(@NotNull RelativeVec value) {
return SUCCESS;
}
}

View File

@ -9,46 +9,12 @@ import org.jetbrains.annotations.NotNull;
* <p>
* Example: -1.2 ~ 5
*/
public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
public class ArgumentRelativeVec3 extends ArgumentRelativeVec {
public ArgumentRelativeVec3(@NotNull String id) {
super(id, 3);
}
@Override
public int getCorrectionResult(@NotNull String value) {
final String[] split = value.split(" ");
// Check if the value has enough element to be correct
if (split.length != getNumberCount()) {
return INVALID_NUMBER_COUNT_ERROR;
}
// Check if each element is correct
for (String element : split) {
if (!element.startsWith(RELATIVE_CHAR)) {
try {
// Will throw the exception if not a float
Float.parseFloat(element);
} catch (NumberFormatException e) {
return INVALID_NUMBER_ERROR;
}
} else {
if (element.length() > RELATIVE_CHAR.length()) {
try {
final String potentialNumber = element.substring(1);
// Will throw the exception if not a float
Float.parseFloat(potentialNumber);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
return INVALID_NUMBER_ERROR;
}
}
}
}
return SUCCESS;
}
@NotNull
@Override
public RelativeVec parse(@NotNull String value) {
@ -97,9 +63,4 @@ public class ArgumentRelativeVec3 extends ArgumentRelative<RelativeVec> {
return new RelativeVec(vector, relativeX, relativeY, relativeZ);
}
@Override
public int getConditionResult(@NotNull RelativeVec value) {
return SUCCESS;
}
}