Skip to content

Commit

Permalink
Specify a config option order, instead of the Beans alphabetical orde…
Browse files Browse the repository at this point in the history
…r (which didn't match the translations)
  • Loading branch information
Gaming32 committed May 22, 2024
1 parent 5e89d6a commit e8bd454
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ConfigProperty {
int order();

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface EnumFallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void setServerIp(String serverIp) {
this.serverIp = serverIp;
}

@ConfigProperty
@ConfigProperty(order = 1)
@ConfigProperty.EnumFallback("right")
public OnlineStatusLocation getOnlineStatusLocation() {
return onlineStatusLocation;
Expand All @@ -131,7 +131,7 @@ public void setOnlineStatusLocation(OnlineStatusLocation onlineStatusLocation) {
this.onlineStatusLocation = onlineStatusLocation;
}

@ConfigProperty
@ConfigProperty(order = 2)
public boolean isEnableFriends() {
return enableFriends;
}
Expand All @@ -140,7 +140,7 @@ public void setEnableFriends(boolean enableFriends) {
this.enableFriends = enableFriends;
}

@ConfigProperty
@ConfigProperty(order = 3)
public boolean isEnableReconnectionToasts() {
return enableReconnectionToasts;
}
Expand All @@ -149,7 +149,7 @@ public void setEnableReconnectionToasts(boolean enableReconnectionToasts) {
this.enableReconnectionToasts = enableReconnectionToasts;
}

@ConfigProperty
@ConfigProperty(order = 4)
public boolean isNoUPnP() {
return noUPnP;
}
Expand All @@ -158,7 +158,7 @@ public void setNoUPnP(boolean noUPnP) {
this.noUPnP = noUPnP;
}

@ConfigProperty
@ConfigProperty(order = 5)
public boolean isUseShortIp() {
return useShortIp;
}
Expand All @@ -167,7 +167,7 @@ public void setUseShortIp(boolean useShortIp) {
this.useShortIp = useShortIp;
}

@ConfigProperty
@ConfigProperty(order = 6)
public boolean isShowOutdatedWorldHost() {
return showOutdatedWorldHost;
}
Expand All @@ -176,7 +176,7 @@ public void setShowOutdatedWorldHost(boolean showOutdatedWorldHost) {
this.showOutdatedWorldHost = showOutdatedWorldHost;
}

@ConfigProperty
@ConfigProperty(order = 7)
public boolean isShareButton() {
return shareButton;
}
Expand All @@ -185,7 +185,7 @@ public void setShareButton(boolean shareButton) {
this.shareButton = shareButton;
}

@ConfigProperty
@ConfigProperty(order = 8)
public boolean isAllowFriendRequests() {
return allowFriendRequests;
}
Expand All @@ -194,7 +194,7 @@ public void setAllowFriendRequests(boolean allowFriendRequests) {
this.allowFriendRequests = allowFriendRequests;
}

@ConfigProperty
@ConfigProperty(order = 9)
public boolean isAnnounceFriendsOnline() {
return announceFriendsOnline;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.gaming32.worldhost.config.option;

import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.config.ConfigProperty;
import io.github.gaming32.worldhost.config.WorldHostConfig;
import net.minecraft.client.gui.components.Button;
import org.quiltmc.parsers.json.JsonReader;
Expand All @@ -11,16 +12,24 @@

public abstract sealed class ConfigOption<T> permits EnumOption, YesNoOption {
protected final PropertyDescriptor property;
private final int order;
private Runnable onSet = null;

protected ConfigOption(PropertyDescriptor property) {
this.property = property;

final ConfigProperty info = property.getReadMethod().getAnnotation(ConfigProperty.class);
order = info.order();
}

public String getName() {
return property.getName();
}

public int getOrder() {
return order;
}

public void onSet(Runnable onSet) {
if (this.onSet == null) {
this.onSet = onSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
Expand All @@ -29,6 +30,7 @@ private ConfigOptions() {
return Arrays.stream(Introspector.getBeanInfo(WorldHostConfig.class).getPropertyDescriptors())
.map(ConfigOptions::createConfigOption)
.filter(Objects::nonNull)
.sorted(Comparator.comparingInt(ConfigOption::getOrder))
.collect(ImmutableMap.toImmutableMap(ConfigOption::getName, Function.identity()));
} catch (IntrospectionException e) {
throw new IllegalStateException(e);
Expand Down

0 comments on commit e8bd454

Please sign in to comment.