Skip to content

Commit

Permalink
Deferred default
Browse files Browse the repository at this point in the history
- BackendManager.defaultBackend now dynamically computes the result every time it is invoked
- Forge backend config value now uses a supplier to provide the default backend
- Fix Fabric backend config being saved to "flw_backends" but loaded from "flw_backend"
  • Loading branch information
PepperCode1 committed Nov 13, 2024
1 parent 46fb592 commit ac54424
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public final class BackendManagerImpl {
.supported(() -> true)
.register(Flywheel.rl("off"));

public static final Backend DEFAULT_BACKEND = findDefaultBackend();

private static Backend backend = OFF_BACKEND;

private BackendManagerImpl() {
Expand All @@ -41,7 +39,7 @@ private static ArrayList<Backend> backendsByPriority() {
return backends;
}

private static Backend findDefaultBackend() {
public static Backend defaultBackend() {
var backendsByPriority = backendsByPriority();
if (backendsByPriority.isEmpty()) {
// This probably shouldn't happen, but fail gracefully.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Backend getOffBackend() {

@Override
public Backend getDefaultBackend() {
return BackendManagerImpl.DEFAULT_BACKEND;
return BackendManagerImpl.defaultBackend();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,7 @@ public void fromJson(JsonElement json) {
readBackend(object);
readLimitUpdates(object);
readWorkerThreads(object);
readFlwBackend(object);
}

private void readFlwBackend(JsonObject object) {
var flwBackendJson = object.get("flw_backend");

if (flwBackendJson instanceof JsonObject flwBackendObject) {
backendConfig.fromJson(flwBackendObject);
} else {
FlwImpl.CONFIG_LOGGER.warn("'flw_backend' value must be an object");
}
readFlwBackends(object);
}

private void readBackend(JsonObject object) {
Expand Down Expand Up @@ -174,6 +164,16 @@ private void readWorkerThreads(JsonObject object) {
workerThreads = WORKER_THREADS_DEFAULT;
}

private void readFlwBackends(JsonObject object) {
var flwBackendsJson = object.get("flw_backends");

if (flwBackendsJson instanceof JsonObject flwBackendsObject) {
backendConfig.fromJson(flwBackendsObject);
} else {
FlwImpl.CONFIG_LOGGER.warn("'flw_backends' value must be an object");
}
}

public JsonObject toJson() {
JsonObject object = new JsonObject();
object.addProperty("backend", Backend.REGISTRY.getIdOrThrow(backend).toString());
Expand Down Expand Up @@ -206,10 +206,10 @@ public void fromJson(JsonObject object) {
}

private void readLightSmoothness(JsonObject object) {
var backendJson = object.get("lightSmoothness");
var lightSmoothnessJson = object.get("lightSmoothness");
String msg = null;

if (backendJson instanceof JsonPrimitive primitive && primitive.isString()) {
if (lightSmoothnessJson instanceof JsonPrimitive primitive && primitive.isString()) {
var value = primitive.getAsString();

for (var item : LightSmoothness.values()) {
Expand All @@ -221,7 +221,7 @@ private void readLightSmoothness(JsonObject object) {
}

msg = "Unknown 'lightSmoothness' value: " + value;
} else if (backendJson != null) {
} else if (lightSmoothnessJson != null) {
msg = "'lightSmoothness' value must be a string";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static class ClientConfig {

private ClientConfig(ForgeConfigSpec.Builder builder) {
backend = builder.comment("Select the backend to use.")
.define("backend", Backend.REGISTRY.getIdOrThrow(BackendManager.defaultBackend()).toString());
.define("backend", () -> Backend.REGISTRY.getIdOrThrow(BackendManager.defaultBackend()).toString(), o -> o != null && String.class.isAssignableFrom(o.getClass()));

limitUpdates = builder.comment("Enable or disable instance update limiting with distance.")
.define("limitUpdates", true);
Expand Down

0 comments on commit ac54424

Please sign in to comment.