Skip to content

Commit 35e2536

Browse files
committed
Refactored flag system and introduced specialized flags
Redesigned `Flag` as an abstract class and removed `ProtectionFlag`. Added specialized flag implementations such as `BooleanFlag`, `NumberFlag`, and its derivatives including `IntegerFlag`, `DoubleFlag`, and others for enhanced modularity.
1 parent de6bf07 commit 35e2536

File tree

11 files changed

+204
-43
lines changed

11 files changed

+204
-43
lines changed
Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,56 @@
11
package net.thenextlvl.protect.flag;
22

3+
import com.google.common.base.Preconditions;
34
import net.kyori.adventure.key.Key;
5+
import net.kyori.adventure.key.Keyed;
46
import org.jspecify.annotations.NonNull;
7+
import org.jspecify.annotations.Nullable;
58

6-
/**
7-
* Represents a flag with a specific value type.
8-
*
9-
* @param <T> The type of the flag value.
10-
*/
11-
public interface Flag<T> extends Comparable<@NonNull Flag<?>> {
12-
/**
13-
* Retrieves the {@link Key} associated with this flag.
14-
*
15-
* @return the Key of the flag
16-
*/
17-
@NonNull
18-
Key key();
19-
20-
/**
21-
* Retrieves the type of the flag value.
22-
*
23-
* @return the type of the flag value
24-
*/
25-
@NonNull
26-
Class<? extends T> type();
27-
28-
/**
29-
* Retrieves the default value for a flag.
30-
*
31-
* @return The default value for the flag.
32-
*/
33-
T defaultValue();
9+
public abstract class Flag<T> implements Keyed, Comparable<@NonNull Flag<?>> {
10+
private final @NonNull Key key;
11+
private final T defaultValue;
12+
private final T protectedValue;
13+
private final boolean nullable;
14+
15+
protected Flag(@NonNull Key key, @Nullable T defaultValue, @Nullable T protectedValue, boolean nullable) {
16+
Preconditions.checkArgument(nullable || defaultValue != null, "Default value must be non-null if flag is not nullable");
17+
this.key = key;
18+
this.defaultValue = defaultValue;
19+
this.protectedValue = protectedValue;
20+
this.nullable = nullable;
21+
}
22+
23+
protected Flag(@NonNull Key key, @NonNull T defaultValue, @Nullable T protectedValue) {
24+
this(key, defaultValue, protectedValue, false);
25+
}
26+
27+
protected Flag(@NonNull Key key, @Nullable T defaultValue, boolean nullable) {
28+
this(key, defaultValue, null, nullable);
29+
}
30+
31+
protected Flag(@NonNull Key key) {
32+
this(key, null, false);
33+
}
34+
35+
@Override
36+
public @NonNull Key key() {
37+
return key;
38+
}
39+
40+
public T getDefaultValue() {
41+
return defaultValue;
42+
}
43+
44+
public T getProtectedValue() {
45+
return protectedValue;
46+
}
47+
48+
public boolean isNullable() {
49+
return nullable;
50+
}
51+
52+
@Override
53+
public int compareTo(@NonNull Flag<?> flag) {
54+
return key.compareTo(flag.key());
55+
}
3456
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.thenextlvl.protect.flag;
2+
3+
import org.jspecify.annotations.NonNull;
4+
5+
import java.util.List;
6+
7+
public interface ListFlag<T> extends Flag<@NonNull List<T>> {
8+
9+
}

api/src/main/java/net/thenextlvl/protect/flag/ProtectionFlag.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.thenextlvl.protect.flag.number;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
public final class ByteFlag extends NumberFlag<Byte> {
8+
public ByteFlag(@NonNull Key key, @Nullable Byte defaultValue, boolean nullable) {
9+
super(key, defaultValue, nullable);
10+
}
11+
12+
public ByteFlag(@NonNull Key key, @NonNull Byte defaultValue) {
13+
super(key, defaultValue);
14+
}
15+
16+
public ByteFlag(@NonNull Key key) {
17+
super(key);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.thenextlvl.protect.flag.number;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
public final class DoubleFlag extends NumberFlag<Double> {
8+
public DoubleFlag(@NonNull Key key, @Nullable Double defaultValue, boolean nullable) {
9+
super(key, defaultValue, nullable);
10+
}
11+
12+
public DoubleFlag(@NonNull Key key, @NonNull Double defaultValue) {
13+
super(key, defaultValue);
14+
}
15+
16+
public DoubleFlag(@NonNull Key key) {
17+
super(key);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.thenextlvl.protect.flag.number;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
public final class FloatFlag extends NumberFlag<Float> {
8+
public FloatFlag(@NonNull Key key, @Nullable Float defaultValue, boolean nullable) {
9+
super(key, defaultValue, nullable);
10+
}
11+
12+
public FloatFlag(@NonNull Key key, @NonNull Float defaultValue) {
13+
super(key, defaultValue);
14+
}
15+
16+
public FloatFlag(@NonNull Key key) {
17+
super(key);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.thenextlvl.protect.flag.number;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
public final class IntegerFlag extends NumberFlag<Integer> {
8+
public IntegerFlag(@NonNull Key key, @Nullable Integer defaultValue, boolean nullable) {
9+
super(key, defaultValue, nullable);
10+
}
11+
12+
public IntegerFlag(@NonNull Key key, @NonNull Integer defaultValue) {
13+
super(key, defaultValue);
14+
}
15+
16+
public IntegerFlag(@NonNull Key key) {
17+
super(key);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.thenextlvl.protect.flag.number;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
public final class LongFlag extends NumberFlag<Long> {
8+
public LongFlag(@NonNull Key key, @Nullable Long defaultValue, boolean nullable) {
9+
super(key, defaultValue, nullable);
10+
}
11+
12+
public LongFlag(@NonNull Key key, @NonNull Long defaultValue) {
13+
super(key, defaultValue);
14+
}
15+
16+
public LongFlag(@NonNull Key key) {
17+
super(key);
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.thenextlvl.protect.flag.number;
2+
3+
import net.kyori.adventure.key.Key;
4+
import net.thenextlvl.protect.flag.Flag;
5+
import org.jspecify.annotations.NonNull;
6+
import org.jspecify.annotations.Nullable;
7+
8+
public abstract class NumberFlag<T extends Number> extends Flag<T> {
9+
protected NumberFlag(@NonNull Key key, @Nullable T defaultValue, boolean nullable) {
10+
super(key, defaultValue, nullable);
11+
}
12+
13+
protected NumberFlag(@NonNull Key key, @NonNull T defaultValue) {
14+
super(key, defaultValue);
15+
}
16+
17+
protected NumberFlag(@NonNull Key key) {
18+
super(key);
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.thenextlvl.protect.flag.number;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
public final class ShortFlag extends NumberFlag<Short> {
8+
public ShortFlag(@NonNull Key key, @Nullable Short defaultValue, boolean nullable) {
9+
super(key, defaultValue, nullable);
10+
}
11+
12+
public ShortFlag(@NonNull Key key, @NonNull Short defaultValue) {
13+
super(key, defaultValue);
14+
}
15+
16+
public ShortFlag(@NonNull Key key) {
17+
super(key);
18+
}
19+
}

0 commit comments

Comments
 (0)