Skip to content

Commit

Permalink
コンフィグの設定を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Maru32768 committed Jan 9, 2023
1 parent 28b2d78 commit 43accf7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
10 changes: 5 additions & 5 deletions bukkit/src/main/java/net/kunmc/lab/configlib/BaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.Modifier;
import java.util.Set;
import java.util.TimerTask;
import java.util.function.Consumer;

public abstract class BaseConfig extends CommonBaseConfig implements Listener {
private static final Gson gson = new GsonBuilder().setPrettyPrinting()
Expand All @@ -41,14 +42,13 @@ public abstract class BaseConfig extends CommonBaseConfig implements Listener {
private final transient Plugin plugin;

public BaseConfig(@NotNull Plugin plugin) {
this(plugin, true);
this(plugin, option -> {
});
}

public BaseConfig(@NotNull Plugin plugin, boolean makeConfigFile) {
public BaseConfig(@NotNull Plugin plugin, Consumer<Option> options) {
this.plugin = plugin;
this.makeConfigFile = makeConfigFile;

init();
init(options);

// Pluginがenabledになっていない状態でregisterすると例外が発生するため遅延,ループさせている
timer.scheduleAtFixedRate(new TimerTask() {
Expand Down
31 changes: 27 additions & 4 deletions common/src/main/java/net/kunmc/lab/configlib/CommonBaseConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kunmc.lab.configlib;

import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import com.google.gson.Gson;
import net.kunmc.lab.configlib.util.ConfigUtil;
Expand All @@ -18,6 +19,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
Expand All @@ -27,7 +29,6 @@ public abstract class CommonBaseConfig {
protected transient boolean enableList = true;
protected transient boolean enableModify = true;
protected transient boolean enableReload = true;
transient boolean makeConfigFile = true;
private transient volatile boolean initialized = false;
private transient String entryName = "";
private final transient List<Runnable> onInitializeListeners = new ArrayList<>();
Expand All @@ -51,8 +52,11 @@ public final String entryName() {
return entryName;
}

final void init() {
if (!makeConfigFile) {
final void init(Consumer<Option> options) {
Option option = new Option();
options.accept(option);

if (!option.makeConfigFile) {
return;
}
getConfigFolder().mkdirs();
Expand Down Expand Up @@ -119,7 +123,7 @@ public void run() {
saveConfigIfPresent();
}
}
}, 0, 500);
}, 0, option.modifyDetectionTimerPeriod);
}

/**
Expand Down Expand Up @@ -298,4 +302,23 @@ private static void replaceField(Field field, Object src, Object dst) throws Ill
// 新しいフィールドが追加されるとNullPointerExceptionが発生するため握りつぶしている
}
}

public static final class Option {
boolean makeConfigFile = true;
int modifyDetectionTimerPeriod = 500;

Option() {
}

public Option makeConfigFile(boolean makeConfigFile) {
this.makeConfigFile = makeConfigFile;
return this;
}

public Option modifyDetectionTimerPeriod(int period) {
Preconditions.checkArgument(period > 0);
this.modifyDetectionTimerPeriod = period;
return this;
}
}
}
11 changes: 7 additions & 4 deletions forge/src/main/java/net/kunmc/lab/configlib/BaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.lang.reflect.Modifier;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -45,23 +46,25 @@ public abstract class BaseConfig extends CommonBaseConfig {
.create();
private final transient String modId;
private final transient Type type;
private final transient Consumer<Option> options;

public BaseConfig(@NotNull String modId, @NotNull Type type) {
this(modId, type, true);
this(modId, type, option -> {
});
}

public BaseConfig(@NotNull String modId, @NotNull Type type, boolean makeConfigFile) {
public BaseConfig(@NotNull String modId, @NotNull Type type, Consumer<Option> options) {
this.modId = modId;
this.type = type;
this.makeConfigFile = makeConfigFile;
this.options = options;

MinecraftForge.EVENT_BUS.register(this);
}

@SubscribeEvent
public void onServerStarting(FMLServerStartingEvent e) {
if (type.isCorrectSide()) {
init();
init(options);
}
}

Expand Down

0 comments on commit 43accf7

Please sign in to comment.