Skip to content

Commit c2b7576

Browse files
committed
Refactored flags and removed unused implementations
Dropped redundant `ProtectionFlag` and `EnumArgumentType`. Simplified flag registration and introduced streamlined constructors for specialized flags.
1 parent c5197fe commit c2b7576

33 files changed

+594
-438
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.thenextlvl.protect.flag;
22

33
import com.google.common.base.Preconditions;
4+
import com.mojang.brigadier.arguments.ArgumentType;
45
import net.kyori.adventure.key.Key;
56
import net.kyori.adventure.key.Keyed;
7+
import org.jetbrains.annotations.Contract;
68
import org.jspecify.annotations.NonNull;
79
import org.jspecify.annotations.Nullable;
810

@@ -28,28 +30,43 @@ protected Flag(@NonNull Key key, @Nullable T defaultValue, boolean nullable) {
2830
this(key, defaultValue, null, nullable);
2931
}
3032

33+
protected Flag(@NonNull Key key, @NonNull T defaultValue) {
34+
this(key, defaultValue, null, false);
35+
}
36+
3137
protected Flag(@NonNull Key key) {
32-
this(key, null, false);
38+
this(key, null, true);
3339
}
3440

41+
@Contract(pure = true)
42+
public abstract @NonNull Class<T> getValueType();
43+
44+
@Contract(value = " -> new", pure = true)
45+
public abstract @NonNull ArgumentType<T> getArgumentType();
46+
3547
@Override
48+
@Contract(pure = true)
3649
public @NonNull Key key() {
3750
return key;
3851
}
3952

53+
@Contract(pure = true)
4054
public T getDefaultValue() {
4155
return defaultValue;
4256
}
4357

58+
@Contract(pure = true)
4459
public T getProtectedValue() {
4560
return protectedValue;
4661
}
4762

63+
@Contract(pure = true)
4864
public boolean isNullable() {
4965
return nullable;
5066
}
5167

5268
@Override
69+
@Contract(pure = true)
5370
public int compareTo(@NonNull Flag<?> flag) {
5471
return key.compareTo(flag.key());
5572
}
Lines changed: 33 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package net.thenextlvl.protect.flag;
22

33
import net.kyori.adventure.key.Key;
4-
import net.kyori.adventure.key.KeyPattern;
54
import org.bukkit.plugin.Plugin;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
import org.jetbrains.annotations.Contract;
7+
import org.jetbrains.annotations.Unmodifiable;
68
import org.jspecify.annotations.NonNull;
7-
import org.jspecify.annotations.NullMarked;
89

910
import java.util.Optional;
1011
import java.util.Set;
@@ -13,118 +14,61 @@
1314
* FlagRegistry is an interface that provides methods for managing registered flags.
1415
*/
1516
public interface FlagRegistry {
17+
@NonNull
18+
@Unmodifiable
19+
@Contract(pure = true)
20+
Set<@NonNull Flag<?>> getFlags();
1621

17-
/**
18-
* Retrieves the set of flags associated with this FlagRegistry.
19-
*
20-
* @return a Set of flags associated with the FlagRegistry
21-
*/
22-
@NullMarked
23-
Set<Flag<?>> getFlags();
22+
@NonNull
23+
@Unmodifiable
24+
@Contract(pure = true)
25+
Set<@NonNull Flag<?>> getFlags(Plugin plugin);
2426

2527
/**
26-
* Retrieves the set of flags associated with the given plugin.
28+
* Retrieves the flag associated with the given key.
2729
*
28-
* @param plugin the plugin for which to retrieve the flags
29-
* @return a set of flags associated with the plugin
30+
* @param key the key of the flag to retrieve
31+
* @param <T> the type of the flag
32+
* @return an {@code Optional} containing the flag, or empty if no flag was found
3033
*/
31-
@NullMarked
32-
Set<Flag<?>> getFlags(Plugin plugin);
34+
@Contract(pure = true)
35+
<T extends Flag<?>> @NonNull Optional<@NonNull T> getFlag(Key key);
3336

3437
/**
35-
* Retrieves the flag associated with the given Key.
38+
* Registers a new flag with the specified plugin.
3639
*
37-
* @param key the Key of the flag to retrieve
38-
* @param <T> the type of the flag value
39-
* @return an Optional containing the flag, or an empty Optional if no flag was found
40+
* @param plugin the plugin registering the flag
41+
* @return {@code true} if the flag was registered, {@code false} otherwise
4042
*/
41-
@NullMarked
42-
<T> Optional<Flag<T>> getFlag(Key key);
43+
boolean register(@NonNull Plugin plugin, @NonNull Flag<?> flag);
4344

44-
/**
45-
* Registers a new flag with the specified plugin, name, and default value.
46-
*
47-
* @param <T> the type of the flag value
48-
* @param plugin the plugin registering the flag
49-
* @param name the name of the flag
50-
* @param defaultValue the default value of the flag
51-
* @return the registered flag
52-
* @throws IllegalStateException if a flag by the same plugin with the same name is already registered
53-
* @see #register(Plugin, Class, String, Object)
54-
*/
55-
@NullMarked
56-
@SuppressWarnings("unchecked")
57-
default <T> Flag<T> register(Plugin plugin, @KeyPattern.Value String name, T defaultValue) throws IllegalStateException {
58-
return register(plugin, (Class<T>) defaultValue.getClass(), name, defaultValue);
45+
default boolean register(@NonNull Flag<?> flag) {
46+
return register(JavaPlugin.getProvidingPlugin(flag.getClass()), flag);
5947
}
6048

6149
/**
62-
* Registers a new flag with the specified plugin, type, name, and default value.
63-
*
64-
* @param <T> the type of the flag value
65-
* @param plugin the plugin registering the flag
66-
* @param type the class type of the flag value
67-
* @param name the name of the flag
68-
* @param defaultValue the default value of the flag
69-
* @return the registered flag
70-
* @throws IllegalStateException if a flag by the same plugin with the same name is already registered
71-
*/
72-
<T> @NonNull Flag<T> register(@NonNull Plugin plugin, @NonNull Class<? extends T> type,
73-
@KeyPattern.Value String name, T defaultValue
74-
) throws IllegalStateException;
75-
76-
/**
77-
* Registers a new protection flag with the specified plugin, name, default value, and protected value.
50+
* Unregisters the given flag.
7851
*
79-
* @param <T> the type of the flag value
80-
* @param plugin the plugin registering the flag
81-
* @param name the name of the flag
82-
* @param defaultValue the default value of the flag
83-
* @param protectedValue the protected value of the flag, which is typically opposite to the default value
84-
* @return the registered protection flag
85-
* @throws IllegalStateException if a flag by the same plugin with the same name is already registered
86-
* @see #register(Plugin, Class, String, Object, Object)
52+
* @param flag the flag to unregister
53+
* @return {@code true} if the flag was unregistered, {@code false} otherwise
8754
*/
88-
@NullMarked
89-
@SuppressWarnings("unchecked")
90-
default <T> ProtectionFlag<T> register(Plugin plugin, @KeyPattern.Value String name, T defaultValue, T protectedValue) throws IllegalStateException {
91-
return register(plugin, (Class<T>) defaultValue.getClass(), name, defaultValue, protectedValue);
55+
default boolean unregister(@NonNull Flag<?> flag) {
56+
return unregister(flag.key());
9257
}
9358

9459
/**
95-
* Registers a new protection flag with the specified plugin, name, default value, and protected value.
96-
* <p>
97-
* protectedValue defines (generally the opposite of defaultValue) what the flag value is to protect against it,
98-
* for example, taking the flag 'explosions', protectedValue would be false and defaultValue true
99-
* <p>
100-
* {@code var explosions = register(plugin, Boolean.class, "explosions", true, false);}
101-
*
102-
* @param <T> the type of the flag value
103-
* @param plugin the plugin registering the flag
104-
* @param type the class type of the flag value
105-
* @param name the name of the flag
106-
* @param defaultValue the default value of the flag
107-
* @param protectedValue the protected value of the flag, which is typically opposite to the default value
108-
* @return the registered protection flag
109-
* @throws IllegalStateException if a flag by the same plugin with the same name is already registered
110-
*/
111-
<T> @NonNull ProtectionFlag<T> register(@NonNull Plugin plugin, @NonNull Class<? extends T> type,
112-
@KeyPattern.Value @NonNull String name, T defaultValue, T protectedValue
113-
) throws IllegalStateException;
114-
115-
/**
116-
* Unregisters a flag identified by the given Key.
60+
* Unregisters the flag by the given key.
11761
*
118-
* @param flag the Key of the flag to unregister
119-
* @return true if the flag was unregistered, false otherwise
62+
* @param key the key of the flag to unregister
63+
* @return {@code true} if the flag was unregistered, {@code false} otherwise
12064
*/
121-
boolean unregister(@NonNull Key flag);
65+
boolean unregister(@NonNull Key key);
12266

12367
/**
12468
* Unregisters all flags associated with the specified plugin.
12569
*
12670
* @param plugin the plugin for which to unregister flags
127-
* @return true if any flag was unregistered, false otherwise
71+
* @return {@code true} if any flag was unregistered, {@code false} otherwise
12872
*/
12973
boolean unregisterAll(@NonNull Plugin plugin);
13074
}

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

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.thenextlvl.protect.flag.collection;
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+
import java.util.Collection;
9+
10+
public abstract class CollectionFlag<E, T extends Collection<E>> extends Flag<T> {
11+
protected CollectionFlag(@NonNull Key key, @NonNull T defaultValue, @Nullable T protectedValue) {
12+
super(key, defaultValue, protectedValue, false);
13+
}
14+
15+
protected CollectionFlag(@NonNull Key key, @NonNull T defaultValue) {
16+
this(key, defaultValue, null);
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.thenextlvl.protect.flag.collection;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
import java.util.List;
8+
9+
public abstract class ListFlag<T> extends CollectionFlag<T, List<T>> {
10+
public ListFlag(@NonNull Key key, @NonNull List<T> defaultValue, @Nullable List<T> protectedValue) {
11+
super(key, defaultValue, protectedValue);
12+
}
13+
14+
public ListFlag(@NonNull Key key, @NonNull List<T> defaultValue) {
15+
super(key, defaultValue);
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.thenextlvl.protect.flag.collection;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
6+
7+
import java.util.Set;
8+
9+
public abstract class SetFlag<T> extends CollectionFlag<T, Set<T>> {
10+
public SetFlag(@NonNull Key key, @NonNull Set<T> defaultValue, @Nullable Set<T> protectedValue) {
11+
super(key, defaultValue, protectedValue);
12+
}
13+
14+
public SetFlag(@NonNull Key key, @NonNull Set<T> defaultValue) {
15+
super(key, defaultValue);
16+
}
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.thenextlvl.protect.flag.location;
2+
3+
import com.mojang.brigadier.arguments.ArgumentType;
4+
import core.paper.command.WrappedArgumentType;
5+
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
6+
import net.kyori.adventure.key.Key;
7+
import net.thenextlvl.protect.flag.Flag;
8+
import org.bukkit.Location;
9+
import org.jspecify.annotations.NonNull;
10+
import org.jspecify.annotations.Nullable;
11+
12+
public final class LocationFlag extends Flag<Location> {
13+
public LocationFlag(@NonNull Key key, @Nullable Location defaultValue, @Nullable Location protectedValue, boolean nullable) {
14+
super(key, defaultValue, protectedValue, nullable);
15+
}
16+
17+
public LocationFlag(@NonNull Key key, @NonNull Location defaultValue, @Nullable Location protectedValue) {
18+
super(key, defaultValue, protectedValue);
19+
}
20+
21+
public LocationFlag(@NonNull Key key, @Nullable Location defaultValue, boolean nullable) {
22+
super(key, defaultValue, nullable);
23+
}
24+
25+
public LocationFlag(@NonNull Key key) {
26+
super(key);
27+
}
28+
29+
@Override
30+
public @NonNull Class<Location> getValueType() {
31+
return Location.class;
32+
}
33+
34+
@Override
35+
public @NonNull ArgumentType<Location> getArgumentType() {
36+
return new WrappedArgumentType<>(
37+
ArgumentTypes.finePosition(),
38+
(reader, type) -> {
39+
// var resolver = context.getArgument("value", FinePositionResolver.class);
40+
// var area = context.getArgument("area", Area.class);
41+
// return resolver.resolve(context.getSource()).toLocation(area.getWorld());
42+
return null; // fixme
43+
},
44+
(context, builder) -> builder.buildFuture());
45+
}
46+
}

api/src/main/java/net/thenextlvl/protect/flag/number/ByteFlag.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

api/src/main/java/net/thenextlvl/protect/flag/number/DoubleFlag.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

api/src/main/java/net/thenextlvl/protect/flag/number/FloatFlag.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)