|
1 | 1 | package net.thenextlvl.protect.flag;
|
2 | 2 |
|
3 | 3 | import net.kyori.adventure.key.Key;
|
4 |
| -import net.kyori.adventure.key.KeyPattern; |
5 | 4 | import org.bukkit.plugin.Plugin;
|
| 5 | +import org.bukkit.plugin.java.JavaPlugin; |
| 6 | +import org.jetbrains.annotations.Contract; |
| 7 | +import org.jetbrains.annotations.Unmodifiable; |
6 | 8 | import org.jspecify.annotations.NonNull;
|
7 |
| -import org.jspecify.annotations.NullMarked; |
8 | 9 |
|
9 | 10 | import java.util.Optional;
|
10 | 11 | import java.util.Set;
|
|
13 | 14 | * FlagRegistry is an interface that provides methods for managing registered flags.
|
14 | 15 | */
|
15 | 16 | public interface FlagRegistry {
|
| 17 | + @NonNull |
| 18 | + @Unmodifiable |
| 19 | + @Contract(pure = true) |
| 20 | + Set<@NonNull Flag<?>> getFlags(); |
16 | 21 |
|
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); |
24 | 26 |
|
25 | 27 | /**
|
26 |
| - * Retrieves the set of flags associated with the given plugin. |
| 28 | + * Retrieves the flag associated with the given key. |
27 | 29 | *
|
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 |
30 | 33 | */
|
31 |
| - @NullMarked |
32 |
| - Set<Flag<?>> getFlags(Plugin plugin); |
| 34 | + @Contract(pure = true) |
| 35 | + <T extends Flag<?>> @NonNull Optional<@NonNull T> getFlag(Key key); |
33 | 36 |
|
34 | 37 | /**
|
35 |
| - * Retrieves the flag associated with the given Key. |
| 38 | + * Registers a new flag with the specified plugin. |
36 | 39 | *
|
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 |
40 | 42 | */
|
41 |
| - @NullMarked |
42 |
| - <T> Optional<Flag<T>> getFlag(Key key); |
| 43 | + boolean register(@NonNull Plugin plugin, @NonNull Flag<?> flag); |
43 | 44 |
|
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); |
59 | 47 | }
|
60 | 48 |
|
61 | 49 | /**
|
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. |
78 | 51 | *
|
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 |
87 | 54 | */
|
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()); |
92 | 57 | }
|
93 | 58 |
|
94 | 59 | /**
|
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. |
117 | 61 | *
|
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 |
120 | 64 | */
|
121 |
| - boolean unregister(@NonNull Key flag); |
| 65 | + boolean unregister(@NonNull Key key); |
122 | 66 |
|
123 | 67 | /**
|
124 | 68 | * Unregisters all flags associated with the specified plugin.
|
125 | 69 | *
|
126 | 70 | * @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 |
128 | 72 | */
|
129 | 73 | boolean unregisterAll(@NonNull Plugin plugin);
|
130 | 74 | }
|
0 commit comments