|
17 | 17 |
|
18 | 18 | package lol.hyper.velocityblockversion.tools; |
19 | 19 |
|
| 20 | +import com.google.common.collect.ImmutableSet; |
20 | 21 | import com.google.inject.Inject; |
21 | 22 | import com.google.inject.Singleton; |
22 | 23 | import com.moandjiezana.toml.Toml; |
23 | 24 | import com.velocitypowered.api.network.ProtocolVersion; |
24 | 25 | import com.velocitypowered.api.plugin.annotation.DataDirectory; |
| 26 | + |
| 27 | +import net.kyori.adventure.text.Component; |
| 28 | +import net.kyori.adventure.text.minimessage.MiniMessage; |
| 29 | +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; |
| 30 | + |
25 | 31 | import org.slf4j.Logger; |
26 | 32 |
|
27 | 33 | import java.io.IOException; |
|
30 | 36 | import java.nio.file.Path; |
31 | 37 | import java.util.ArrayList; |
32 | 38 | import java.util.List; |
| 39 | +import java.util.Set; |
33 | 40 | import java.util.stream.Collectors; |
34 | 41 |
|
35 | 42 | @Singleton |
36 | 43 | public final class ConfigHandler { |
| 44 | + public enum OperationMode { |
| 45 | + BLACKLIST, WHITELIST; |
| 46 | + |
| 47 | + public static OperationMode fromString(String mode) { |
| 48 | + switch (mode) { |
| 49 | + case "blacklist": |
| 50 | + return BLACKLIST; |
| 51 | + case "whitelist": |
| 52 | + return WHITELIST; |
| 53 | + default: |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + }; |
| 58 | + |
37 | 59 | @Inject |
38 | 60 | private Logger logger; |
39 | 61 | @Inject |
40 | 62 | @DataDirectory |
41 | 63 | private Path folderPath; |
42 | 64 |
|
43 | 65 | private Toml config; |
44 | | - private final List<Integer> blockVersions = new ArrayList<>(); |
45 | | - public final long CONFIG_VERSION = 5; |
| 66 | + private OperationMode operationMode = OperationMode.BLACKLIST; |
| 67 | + private Set<Integer> versionsSet = ImmutableSet.of(); |
| 68 | + private Component deniedMessage = MiniMessage.miniMessage().deserialize(""); |
| 69 | + public final long CONFIG_VERSION = 6; |
46 | 70 |
|
47 | 71 | @Inject |
48 | 72 | public ConfigHandler() {} |
49 | 73 |
|
| 74 | + /** |
| 75 | + * Load the plugin settings from the configuration file. |
| 76 | + * @return True if the configuration was loaded, false if there was an error. |
| 77 | + */ |
50 | 78 | public boolean loadConfig() { |
51 | 79 | if (Files.notExists(folderPath)) { |
52 | 80 | try { |
@@ -83,41 +111,76 @@ public boolean loadConfig() { |
83 | 111 | logger.warn( |
84 | 112 | "To fix this, delete your current config and let the server remake it."); |
85 | 113 | } |
86 | | - blockVersions.clear(); |
87 | 114 |
|
| 115 | + operationMode = OperationMode.fromString(config.getString("mode", "blacklist")); |
| 116 | + if (operationMode == null) { |
| 117 | + logger.error("Unexpected mode option found. Using default value."); |
| 118 | + operationMode = OperationMode.BLACKLIST; |
| 119 | + } |
| 120 | + logger.info("Operation mode: {}", operationMode.toString()); |
| 121 | + |
| 122 | + List<Integer> versionsList = new ArrayList<>(); |
88 | 123 | // for some reason, the config loads the versions as longs |
89 | 124 | // we have to convert them this ugly way |
90 | 125 | for (Object obj : config.getList("versions", List.of())) { |
91 | 126 | if (obj instanceof Number) { |
92 | 127 | int t = ((Number) obj).intValue(); |
93 | | - blockVersions.add(t); |
| 128 | + versionsList.add(t); |
94 | 129 | } else { |
95 | 130 | logger.error("Unexpected versions configuration input {}", obj); |
96 | 131 | } |
97 | 132 | } |
98 | 133 |
|
99 | | - if (blockVersions.isEmpty()) { |
| 134 | + if (versionsList.isEmpty()) { |
100 | 135 | logger.warn("There are no versions listed in the config!"); |
101 | 136 | } else { |
102 | | - blockVersions.removeIf(protocol -> { |
103 | | - if (ProtocolVersion.ID_TO_PROTOCOL_CONSTANT.containsKey(protocol)) { |
104 | | - return false; |
105 | | - } else { |
106 | | - logger.warn("Version {} is NOT a valid version number! Ignoring this version.", protocol); |
107 | | - return true; |
108 | | - } |
109 | | - }); |
110 | | - logger.info("Loaded {} versions!", blockVersions.size()); |
| 137 | + versionsList.sort((a, b) -> a.compareTo(b)); |
| 138 | + versionsList = versionsList.stream().filter(elm -> ProtocolVersion.ID_TO_PROTOCOL_CONSTANT.containsKey(elm)) |
| 139 | + .distinct().sorted().toList(); |
| 140 | + logger.info("Loaded {} versions!", versionsList.size()); |
111 | 141 | } |
112 | | - logger.info("Loaded versions: {}", blockVersions.stream().map(String::valueOf).collect(Collectors.joining(", "))); |
| 142 | + logger.info("Loaded versions: {}", versionsList.stream().map(String::valueOf).collect(Collectors.joining(", "))); |
113 | 143 |
|
| 144 | + String versionString = VersionToStrings.versionRange(versionsList); |
| 145 | + String disconnectMessage = config.getString("disconnect_message"); |
| 146 | + |
| 147 | + deniedMessage = MiniMessage.miniMessage().deserialize( |
| 148 | + disconnectMessage, |
| 149 | + Placeholder.unparsed("versions", versionString) |
| 150 | + ); |
| 151 | + |
| 152 | + versionsSet = ImmutableSet.copyOf(versionsList); |
114 | 153 | return true; |
115 | 154 | } |
116 | 155 |
|
117 | | - public List<Integer> getBlockVersions() { |
118 | | - return blockVersions; |
| 156 | + /** |
| 157 | + * Get the set of version protocol included in the configuration by the user |
| 158 | + * @return The version set |
| 159 | + */ |
| 160 | + public Set<Integer> getVersionsSet() { |
| 161 | + return versionsSet; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get the mode on which the plugin operates. |
| 166 | + * @return A value from the enum describing the mode. |
| 167 | + */ |
| 168 | + public OperationMode getOperationMode() { |
| 169 | + return operationMode; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get the computed message sent to players that are denied access. |
| 174 | + * @return miniMessage Component containing the message. |
| 175 | + */ |
| 176 | + public Component getDeniedMessage() { |
| 177 | + return deniedMessage; |
119 | 178 | } |
120 | 179 |
|
| 180 | + /** |
| 181 | + * Get the config parse instance. |
| 182 | + * @return The Toml parser instance. |
| 183 | + */ |
121 | 184 | public Toml getConfig() { |
122 | 185 | return config; |
123 | 186 | } |
|
0 commit comments