From 47e6a466f2358bda5dc1e93c255450ebe059fa50 Mon Sep 17 00:00:00 2001 From: zontreck Date: Fri, 15 Mar 2024 00:23:50 -0700 Subject: [PATCH] Fix up compile errors --- .../dev/zontreck/ariaslib/args/Argument.java | 77 +++++++++++++++---- .../WrongArgumentTypeException.java | 7 ++ .../zontreck/ariaslib/util/ProgressBar.java | 4 +- 3 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 src/main/java/dev/zontreck/ariaslib/exceptions/WrongArgumentTypeException.java diff --git a/src/main/java/dev/zontreck/ariaslib/args/Argument.java b/src/main/java/dev/zontreck/ariaslib/args/Argument.java index 1f16d6e..64463d8 100644 --- a/src/main/java/dev/zontreck/ariaslib/args/Argument.java +++ b/src/main/java/dev/zontreck/ariaslib/args/Argument.java @@ -1,5 +1,7 @@ package dev.zontreck.ariaslib.args; +import dev.zontreck.ariaslib.exceptions.WrongArgumentTypeException; + public abstract class Argument implements Cloneable { public boolean hasValue = false; @@ -33,27 +35,70 @@ public abstract class Argument implements Cloneable throw new IllegalArgumentException("No value"); } + /** + * Directly cast to the Argument type Long + * @return + * @throws WrongArgumentTypeException Throws when type does not match + */ + public LongArgument getAsLong() throws WrongArgumentTypeException { + if(this instanceof LongArgument) + { + return (LongArgument) this; + } + + throw new WrongArgumentTypeException(); + } + + + /** + * Directly cast to the Argument type String + * @return + * @throws WrongArgumentTypeException Throws when type does not match + */ + public StringArgument getAsString() throws WrongArgumentTypeException { + if(this instanceof StringArgument) + { + return (StringArgument) this; + } + + throw new WrongArgumentTypeException(); + } + + + /** + * Directly cast to the Argument type Integer + * @return + * @throws WrongArgumentTypeException Throws when type does not match + */ + public IntegerArgument getAsInteger() throws WrongArgumentTypeException + { + if(this instanceof IntegerArgument) + { + return (IntegerArgument) this; + } + + throw new WrongArgumentTypeException(); + } + @Override public Argument clone() { Argument arg = null; - switch (getType()) - { - case LONG -> { - arg = (Argument) new LongArgument(name, (long)getValue()); - break; - } - case STRING -> { - arg = (Argument) new StringArgument(name, (String)getValue()); - break; - } - case BOOLEAN -> { + try{ + + if(getType() == ArgumentType.LONG) + { + arg = (Argument) new LongArgument(name, getAsLong().getValue()); + } else if(getType() == ArgumentType.STRING) + { + arg = (Argument) new StringArgument(name, getAsString().getValue()); + } else if(getType() == ArgumentType.BOOLEAN) { arg = (Argument) new BooleanArgument(name); - break; - } - case INTEGER -> { - arg = (Argument) new IntegerArgument(name, (int)getValue()); - break; + } else if(getType() == ArgumentType.INTEGER){ + arg = (Argument) new IntegerArgument(name, getAsInteger().getValue()); } + }catch (WrongArgumentTypeException ex) + { + ex.printStackTrace(); } return arg; diff --git a/src/main/java/dev/zontreck/ariaslib/exceptions/WrongArgumentTypeException.java b/src/main/java/dev/zontreck/ariaslib/exceptions/WrongArgumentTypeException.java new file mode 100644 index 0000000..4ab8f8a --- /dev/null +++ b/src/main/java/dev/zontreck/ariaslib/exceptions/WrongArgumentTypeException.java @@ -0,0 +1,7 @@ +package dev.zontreck.ariaslib.exceptions; + + +public class WrongArgumentTypeException extends Exception +{ + +} diff --git a/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java b/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java index 25935a8..fc7da66 100644 --- a/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java +++ b/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java @@ -25,8 +25,8 @@ public class ProgressBar /** * Build a progress bar - *

- * your text here [=========> ] 40% your text here + *

+ * your text here [========= ] 40% your text here * @param percent The percentage * @param beforeText * @param afterText