-
-
Save VapidLinus/da9d9bc20ccef733bbde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.uniqraft.murder.match.settings; | |
public interface ISetting<T> { | |
public T getDefault(); | |
public T value(); | |
public void set(T value); | |
public String getName(); | |
public default String getValueName() { | |
Object obj = value(); | |
if (obj instanceof Boolean) { | |
return (boolean)obj ? "Yes" : "No"; | |
} | |
return obj.toString(); | |
} | |
public default boolean isHidden() { | |
return false; | |
} | |
public default boolean isDefault() { | |
return getDefault().equals(value()); | |
} | |
@SuppressWarnings("unchecked") | |
public default void increase() { | |
// If value is a boolean | |
if (value() instanceof Boolean) { | |
ISetting<Boolean> b = (ISetting<Boolean>) this; | |
b.set(!b.value()); // Toggle | |
} | |
// If value is a float | |
if (value() instanceof Float) { | |
ISetting<Float> f = (ISetting<Float>) this; | |
f.set(Math.round((f.value() + .1f) * 10f) / 10f); // Increase by 0.1 | |
} | |
// If value is an integer | |
else if (value() instanceof Integer) { | |
ISetting<Integer> i = (ISetting<Integer>) this; | |
i.set(i.value() + 1); // Increase by 1 | |
} | |
// If value is an enum | |
else if (value() instanceof Enum<?>) { | |
ISetting<Enum<?>> e = (ISetting<Enum<?>>) this; | |
Enum<?>[] values = e.value().getClass().getEnumConstants(); | |
int next = e.value().ordinal() + 1; | |
if (next >= values.length) next = 0; | |
e.set(Enum.valueOf(e.value().getClass(), values[next].name())); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public default void decrease() { | |
// If value is a boolean | |
if (value() instanceof Boolean) { | |
ISetting<Boolean> b = (ISetting<Boolean>) this; | |
b.set(!b.value()); // Toggle | |
} | |
// If value is a float | |
if (value() instanceof Float) { | |
ISetting<Float> f = (ISetting<Float>) this; | |
f.set(Math.round((f.value() - .1f) * 10f) / 10f); // Decrease by 0.1 | |
} | |
// If value is an integer | |
else if (value() instanceof Integer) { | |
ISetting<Integer> i = (ISetting<Integer>) this; | |
i.set(i.value() - 1); // Decrease by 1 | |
} | |
// If value is an enum | |
else if (value() instanceof Enum<?>) { | |
ISetting<Enum<?>> e = (ISetting<Enum<?>>) this; | |
Enum<?>[] values = e.value().getClass().getEnumConstants(); | |
int next = e.value().ordinal() - 1; | |
if (next <= 0) next = values.length - 1; | |
e.set(Enum.valueOf(e.value().getClass(), values[next].name())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment