Skip to content

Commit 4d1e68c

Browse files
committed
Convert to use core's CommentedConfiguration
1 parent e41641c commit 4d1e68c

File tree

6 files changed

+34
-305
lines changed

6 files changed

+34
-305
lines changed

src/main/java/org/mvplugins/multiverse/inventories/MultiverseInventories.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.mvplugins.multiverse.core.submodules.MVCore;
1111
import org.mvplugins.multiverse.core.submodules.MVPlugin;
1212
import org.mvplugins.multiverse.inventories.commands.InventoriesCommand;
13+
import org.mvplugins.multiverse.inventories.config.InventoriesConfig;
1314
import org.mvplugins.multiverse.inventories.locale.Message;
1415
import org.mvplugins.multiverse.inventories.locale.Messager;
1516
import org.mvplugins.multiverse.inventories.locale.Messaging;
@@ -52,6 +53,8 @@ public static MultiverseInventories getPlugin() {
5253

5354
private PluginServiceLocator serviceLocator;
5455

56+
@Inject
57+
private Provider<InventoriesConfig> configProvider;
5558
@Inject
5659
private Provider<MVCommandManager> commandManager;
5760
@Inject
@@ -66,7 +69,6 @@ public static MultiverseInventories getPlugin() {
6669
private ImportManager importManager = new ImportManager(this);
6770

6871
private MVCore core = null;
69-
private InventoriesConfig config = null;
7072
private FlatFileProfileDataSource data = null;
7173

7274
private InventoriesDupingPatch dupingPatch;
@@ -319,7 +321,7 @@ private String logAndAddToPasteBinBuffer(String string) {
319321
* @return the Config object which contains settings for this plugin.
320322
*/
321323
public InventoriesConfig getMVIConfig() {
322-
return this.config;
324+
return this.configProvider.get();
323325
}
324326

325327
/**
@@ -328,8 +330,7 @@ public InventoriesConfig getMVIConfig() {
328330
@Override
329331
public void reloadConfig() {
330332
try {
331-
this.config = new InventoriesConfig(this, mvCoreConfig.get());
332-
this.worldGroupManager = new YamlWorldGroupManager(this, this.config.getConfig());
333+
this.worldGroupManager = new YamlWorldGroupManager(this, this.configProvider.get().getConfig());
333334
this.worldProfileContainerStore = new WeakProfileContainerStore(this, ContainerType.WORLD);
334335
this.groupProfileContainerStore = new WeakProfileContainerStore(this, ContainerType.GROUP);
335336

@@ -357,6 +358,7 @@ public void run() {
357358
}
358359

359360
getMVIConfig().setFirstRun(false);
361+
getMVIConfig().save();
360362
}
361363
getGroupManager().checkForConflicts(null);
362364
}

src/main/java/org/mvplugins/multiverse/inventories/YamlWorldGroupManager.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.dumptruckman.minecraft.util.Logging;
44
import com.google.common.collect.Lists;
5+
import org.mvplugins.multiverse.external.commentedconfiguration.CommentedConfiguration;
56
import org.mvplugins.multiverse.inventories.share.Sharables;
6-
import org.mvplugins.multiverse.inventories.util.CommentedYamlConfiguration;
77
import org.mvplugins.multiverse.inventories.util.DeserializationException;
88
import io.papermc.lib.PaperLib;
99
import org.bukkit.Bukkit;
@@ -24,12 +24,14 @@
2424

2525
final class YamlWorldGroupManager extends AbstractWorldGroupManager {
2626

27-
private final List<String> groupSectionComments = Collections.unmodifiableList(new ArrayList<String>() {{
28-
add("# To ADD, DELETE, and EDIT groups use the command /mvinv group.");
29-
add("# No support will be given for those who manually edit these groups.");
30-
}});
27+
private final String[] groupSectionComments = {
28+
"# Multiverse-Inventories Groups",
29+
"",
30+
"# To ADD, DELETE, and EDIT groups use the command /mvinv group.",
31+
"# No support will be given for those who manually edit these groups."
32+
};
3133

32-
private final CommentedYamlConfiguration groupsConfig;
34+
private final CommentedConfiguration groupsConfig;
3335

3436
YamlWorldGroupManager(final MultiverseInventories inventories, final Configuration config) throws IOException {
3537
super(inventories);
@@ -44,19 +46,18 @@ final class YamlWorldGroupManager extends AbstractWorldGroupManager {
4446
migrateGroups = true;
4547
}
4648
// Load the configuration file into memory
47-
boolean supportsCommentsNatively = PaperLib.getMinecraftVersion() > 17;
48-
groupsConfig = new CommentedYamlConfiguration(groupsConfigFile, !groupsConfigFileExists || !supportsCommentsNatively);
49+
groupsConfig = new CommentedConfiguration(groupsConfigFile.toPath());
50+
groupsConfig.load();
4951

5052
if (migrateGroups) {
5153
migrateGroups(config);
5254
}
5355

5456
groupsConfig.addComment("groups", groupSectionComments);
55-
if (groupsConfig.getConfig().get("groups") == null) {
57+
if (groupsConfig.get("groups") == null) {
5658
this.getConfig().createSection("groups");
5759
}
5860

59-
groupsConfig.getConfig().options().header("Multiverse-Inventories Groups");
6061
// Saves the configuration from memory to file
6162
groupsConfig.save();
6263

@@ -86,7 +87,7 @@ private void migrateGroups(final Configuration config) {
8687
}
8788

8889
private FileConfiguration getConfig() {
89-
return this.groupsConfig.getConfig();
90+
return this.groupsConfig;
9091
}
9192

9293
private List<WorldGroup> getGroupsFromConfig() {

src/main/java/org/mvplugins/multiverse/inventories/InventoriesConfig.java renamed to src/main/java/org/mvplugins/multiverse/inventories/config/InventoriesConfig.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
package org.mvplugins.multiverse.inventories;
1+
package org.mvplugins.multiverse.inventories.config;
22

33
import com.dumptruckman.minecraft.util.Logging;
44
import org.mvplugins.multiverse.core.api.config.MVCoreConfig;
5+
import org.mvplugins.multiverse.external.commentedconfiguration.CommentedConfiguration;
6+
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
7+
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
8+
import org.mvplugins.multiverse.inventories.MultiverseInventories;
59
import org.mvplugins.multiverse.inventories.share.Sharable;
610
import org.mvplugins.multiverse.inventories.share.Sharables;
711
import org.mvplugins.multiverse.inventories.share.Shares;
8-
import org.mvplugins.multiverse.inventories.util.CommentedYamlConfiguration;
912
import io.papermc.lib.PaperLib;
1013
import org.bukkit.configuration.file.FileConfiguration;
1114

@@ -18,6 +21,7 @@
1821
/**
1922
* Provides methods for interacting with the configuration of Multiverse-Inventories.
2023
*/
24+
@Service
2125
public final class InventoriesConfig {
2226

2327
/**
@@ -107,10 +111,11 @@ private List<String> getComments() {
107111
}
108112
}
109113

110-
private final CommentedYamlConfiguration config;
114+
private final CommentedConfiguration config;
111115
private final MultiverseInventories plugin;
112116
private final MVCoreConfig mvCoreConfig;
113117

118+
@Inject
114119
InventoriesConfig(MultiverseInventories plugin, MVCoreConfig mvCoreConfig) throws IOException {
115120
this.plugin = plugin;
116121
this.mvCoreConfig = mvCoreConfig;
@@ -128,13 +133,13 @@ private List<String> getComments() {
128133
}
129134

130135
// Load the configuration file into memory
131-
boolean supportsCommentsNatively = PaperLib.getMinecraftVersion() > 17;
132-
config = new CommentedYamlConfiguration(configFile, !configFileExists || !supportsCommentsNatively);
136+
config = new CommentedConfiguration(configFile.toPath());
137+
config.load();
133138

134139
// Sets defaults config values
135140
this.setDefaults();
136141

137-
config.getConfig().options().header("Multiverse-Inventories Settings");
142+
config.addComment("settings", "# Multiverse-Inventories Settings", "");
138143

139144
// Saves the configuration from memory to file
140145
config.save();
@@ -148,7 +153,7 @@ private List<String> getComments() {
148153
*/
149154
private void setDefaults() {
150155
for (InventoriesConfig.Path path : InventoriesConfig.Path.values()) {
151-
config.addComment(path.getPath(), path.getComments());
156+
config.addComment(path.getPath(), path.getComments().toArray(new String[0]));
152157
if (this.getConfig().get(path.getPath()) == null) {
153158
if (path.getDefault() != null) {
154159
Logging.fine("Config: Defaulting '" + path.getPath() + "' to " + path.getDefault());
@@ -173,8 +178,8 @@ private String getString(Path path) {
173178
return this.getConfig().getString(path.getPath(), (String) path.getDefault());
174179
}
175180

176-
FileConfiguration getConfig() {
177-
return this.config.getConfig();
181+
public FileConfiguration getConfig() {
182+
return this.config;
178183
}
179184

180185
/**
@@ -218,7 +223,7 @@ public boolean isFirstRun() {
218223
*
219224
* @param firstRun What to set the flag to in the config.
220225
*/
221-
void setFirstRun(boolean firstRun) {
226+
public void setFirstRun(boolean firstRun) {
222227
this.getConfig().set(Path.FIRST_RUN.getPath(), firstRun);
223228
}
224229

src/main/java/org/mvplugins/multiverse/inventories/share/Sharable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.mvplugins.multiverse.inventories.share;
22

3-
import org.mvplugins.multiverse.inventories.InventoriesConfig;
3+
import org.mvplugins.multiverse.inventories.config.InventoriesConfig;
44

55
import java.util.ArrayList;
66
import java.util.List;

0 commit comments

Comments
 (0)