1919import com .google .gson .Gson ;
2020import com .google .gson .GsonBuilder ;
2121import dev .terminalmc .clientsort .ClientSort ;
22- import dev .terminalmc .clientsort .inventory .sort .SortMode ;
22+ import dev .terminalmc .clientsort .inventory .sort .SortOrder ;
23+ import dev .terminalmc .clientsort .platform .Services ;
2324import net .minecraft .resources .ResourceLocation ;
2425import org .jetbrains .annotations .NotNull ;
2526import org .jetbrains .annotations .Nullable ;
3132import java .nio .file .StandardCopyOption ;
3233
3334public class Config {
34- private static final Path DIR_PATH = Path . of ( "config" );
35+ private static final Path CONFIG_DIR = Services . PLATFORM . getConfigDir ( );
3536 private static final String FILE_NAME = ClientSort .MOD_ID + ".json" ;
3637 private static final String BACKUP_FILE_NAME = ClientSort .MOD_ID + ".unreadable.json" ;
3738 private static final Gson GSON = new GsonBuilder ().setPrettyPrinting ().create ();
@@ -45,71 +46,63 @@ public static Options options() {
4546 }
4647
4748 public static class Options {
48- // General
49+
50+ // General options
51+
4952 public static final int interactionRateMin = 1 ;
5053 public static final int interactionRateMax = 100 ;
5154 public static final int interactionRateServerDefault = 10 ;
5255 public int interactionRateServer = interactionRateServerDefault ;
5356
5457 public static final int interactionRateClientDefault = 1 ;
5558 public int interactionRateClient = interactionRateClientDefault ;
59+
60+ public static final boolean serverAcceleratedSortingDefault = true ;
61+ public boolean serverAcceleratedSorting = serverAcceleratedSortingDefault ;
5662
57- public static final HotbarMode hotbarModeDefault = HotbarMode .HARD ;
58- public HotbarMode hotbarMode = hotbarModeDefault ;
59- public enum HotbarMode {
60- NONE ,
61- HARD ,
62- SOFT ;
63+ public static final boolean optimizedCreativeSortingDefault = true ;
64+ public boolean optimizedCreativeSorting = optimizedCreativeSortingDefault ;
6365
64- public String lowerName () {
65- return switch (this ) {
66- case NONE -> "merge" ;
67- case HARD -> "split" ;
68- case SOFT -> "off" ;
69- };
70- }
66+ public static final HotbarScope hotbarScopeDefault = HotbarScope .HOTBAR ;
67+ public HotbarScope hotbarScope = hotbarScopeDefault ;
68+ public enum HotbarScope {
69+ HOTBAR ,
70+ INVENTORY ,
71+ NONE
7172 }
7273
73- public static final ExtraSlotMode extraSlotModeDefault = ExtraSlotMode . NONE ;
74- public ExtraSlotMode extraSlotMode = extraSlotModeDefault ;
75- public enum ExtraSlotMode {
76- NONE ,
74+ public static final ExtraSlotScope extraSlotScopeDefault = ExtraSlotScope . EXTRA ;
75+ public ExtraSlotScope extraSlotScope = extraSlotScopeDefault ;
76+ public enum ExtraSlotScope {
77+ EXTRA ,
7778 HOTBAR ,
78- INVENTORY ;
79-
80- public String lowerName () {
81- return switch (this ) {
82- case NONE -> "none" ;
83- case HOTBAR -> "hotbar" ;
84- case INVENTORY -> "inventory" ;
85- };
86- }
79+ INVENTORY ,
80+ NONE
8781 }
8882
8983 public static final boolean lmbBundleDefault = false ;
9084 public boolean lmbBundle = lmbBundleDefault ;
9185
92- // Sorting
93- public static final String sortModeDefault = SortMode .CREATIVE .name ;
94- public String sortModeStr = sortModeDefault ;
95- public transient SortMode sortMode ;
96-
97- public static final String shiftSortModeDefault = SortMode .QUANTITY .name ;
98- public String shiftSortModeStr = shiftSortModeDefault ;
99- public transient SortMode shiftSortMode ;
86+ // Sort mode options
87+
88+ public static final String sortOrderDefault = SortOrder .CREATIVE .name ;
89+ public String sortOrderStr = sortOrderDefault ;
90+ public transient SortOrder sortOrder ;
10091
101- public static final String ctrlSortModeDefault = SortMode . ALPHABET .name ;
102- public String ctrlSortModeStr = ctrlSortModeDefault ;
103- public transient SortMode ctrlSortMode ;
92+ public static final String shiftSortOrderDefault = SortOrder . QUANTITY .name ;
93+ public String shiftSortOrderStr = shiftSortOrderDefault ;
94+ public transient SortOrder shiftSortOrder ;
10495
105- public static final String altSortModeDefault = SortMode . RAW_ID .name ;
106- public String altSortModeStr = altSortModeDefault ;
107- public transient SortMode altSortMode ;
96+ public static final String ctrlSortOrderDefault = SortOrder . ALPHABET .name ;
97+ public String ctrlSortOrderStr = ctrlSortOrderDefault ;
98+ public transient SortOrder ctrlSortOrder ;
10899
109- public static final boolean optimizedCreativeSortingDefault = true ;
110- public boolean optimizedCreativeSorting = optimizedCreativeSortingDefault ;
100+ public static final String altSortOrderDefault = SortOrder .RAW_ID .name ;
101+ public String altSortOrderStr = altSortOrderDefault ;
102+ public transient SortOrder altSortOrder ;
103+
104+ // Sorting sound options
111105
112- // Sounds
113106 public static final boolean soundEnabledDefault = false ;
114107 public boolean soundEnabled = soundEnabledDefault ;
115108
@@ -135,11 +128,54 @@ public String lowerName() {
135128
136129 public static final boolean soundAllowOverlapDefault = true ;
137130 public boolean soundAllowOverlap = soundAllowOverlapDefault ;
131+
132+ // Legacy from pre v1.3.0
133+
134+ public static final HotbarMode hotbarModeDefault = HotbarMode .HARD ;
135+ public HotbarMode hotbarMode = hotbarModeDefault ;
136+ public enum HotbarMode {
137+ NONE ,
138+ HARD ,
139+ SOFT ;
140+
141+ public HotbarScope update () {
142+ return switch (this ) {
143+ case NONE -> HotbarScope .INVENTORY ;
144+ case HARD -> HotbarScope .HOTBAR ;
145+ case SOFT -> HotbarScope .NONE ;
146+ };
147+ }
148+ }
149+
150+ public static final ExtraSlotMode extraSlotModeDefault = ExtraSlotMode .NONE ;
151+ public ExtraSlotMode extraSlotMode = extraSlotModeDefault ;
152+ public enum ExtraSlotMode {
153+ NONE ,
154+ HOTBAR ,
155+ INVENTORY ;
156+
157+ public ExtraSlotScope update () {
158+ return switch (this ) {
159+ case NONE -> ExtraSlotScope .NONE ;
160+ case HOTBAR -> ExtraSlotScope .HOTBAR ;
161+ case INVENTORY -> ExtraSlotScope .INVENTORY ;
162+ };
163+ }
164+ }
165+
166+ public String sortModeStr = sortOrderDefault ;
167+ public String shiftSortModeStr = shiftSortOrderDefault ;
168+ public String ctrlSortModeStr = ctrlSortOrderDefault ;
169+ public String altSortModeStr = altSortOrderDefault ;
138170 }
139171
140- // Cleanup
172+ // Validation
141173
142- private void cleanup () {
174+ /**
175+ * Ensures that all config values are valid.
176+ */
177+ private void validate () {
178+ update ();
143179 // interactionRateServer
144180 if (options .interactionRateServer < Options .interactionRateMin )
145181 options .interactionRateServer = Options .interactionRateMin ;
@@ -174,7 +210,35 @@ private void cleanup() {
174210 if (options .soundVolume > Options .soundVolumeMax )
175211 options .soundVolume = Options .soundVolumeMax ;
176212 }
177-
213+
214+
215+ private void update () {
216+ if (options .hotbarMode != Options .hotbarModeDefault ) {
217+ options .hotbarScope = options .hotbarMode .update ();
218+ options .hotbarMode = Options .hotbarModeDefault ;
219+ }
220+ if (options .extraSlotMode != Options .extraSlotModeDefault ) {
221+ options .extraSlotScope = options .extraSlotMode .update ();
222+ options .extraSlotMode = Options .extraSlotModeDefault ;
223+ }
224+ if (!Options .sortOrderDefault .equals (options .sortModeStr )) {
225+ options .sortOrderStr = options .sortModeStr ;
226+ options .sortModeStr = Options .sortOrderDefault ;
227+ }
228+ if (!Options .shiftSortOrderDefault .equals (options .shiftSortModeStr )) {
229+ options .shiftSortOrderStr = options .shiftSortModeStr ;
230+ options .shiftSortModeStr = Options .shiftSortOrderDefault ;
231+ }
232+ if (!Options .ctrlSortOrderDefault .equals (options .ctrlSortModeStr )) {
233+ options .ctrlSortOrderStr = options .ctrlSortModeStr ;
234+ options .ctrlSortModeStr = Options .ctrlSortOrderDefault ;
235+ }
236+ if (!Options .altSortOrderDefault .equals (options .altSortModeStr )) {
237+ options .altSortOrderStr = options .altSortModeStr ;
238+ options .altSortModeStr = Options .altSortOrderDefault ;
239+ }
240+ }
241+
178242 // Instance management
179243
180244 private static Config instance = null ;
@@ -201,7 +265,7 @@ public static Config resetAndSave() {
201265 // Load and save
202266
203267 public static @ NotNull Config load () {
204- Path file = DIR_PATH .resolve (FILE_NAME );
268+ Path file = CONFIG_DIR .resolve (FILE_NAME );
205269 Config config = null ;
206270 if (Files .exists (file )) {
207271 config = load (file , GSON );
@@ -228,8 +292,8 @@ public static Config resetAndSave() {
228292 private static void backup () {
229293 try {
230294 ClientSort .LOG .warn ("Copying {} to {}" , FILE_NAME , BACKUP_FILE_NAME );
231- if (!Files .isDirectory (DIR_PATH )) Files .createDirectories (DIR_PATH );
232- Path file = DIR_PATH .resolve (FILE_NAME );
295+ if (!Files .isDirectory (CONFIG_DIR )) Files .createDirectories (CONFIG_DIR );
296+ Path file = CONFIG_DIR .resolve (FILE_NAME );
233297 Path backupFile = file .resolveSibling (BACKUP_FILE_NAME );
234298 Files .move (file , backupFile , StandardCopyOption .ATOMIC_MOVE ,
235299 StandardCopyOption .REPLACE_EXISTING );
@@ -240,10 +304,10 @@ private static void backup() {
240304
241305 public static void save () {
242306 if (instance == null ) return ;
243- instance .cleanup ();
307+ instance .validate ();
244308 try {
245- if (!Files .isDirectory (DIR_PATH )) Files .createDirectories (DIR_PATH );
246- Path file = DIR_PATH .resolve (FILE_NAME );
309+ if (!Files .isDirectory (CONFIG_DIR )) Files .createDirectories (CONFIG_DIR );
310+ Path file = CONFIG_DIR .resolve (FILE_NAME );
247311 Path tempFile = file .resolveSibling (file .getFileName () + ".tmp" );
248312 try (OutputStreamWriter writer = new OutputStreamWriter (
249313 new FileOutputStream (tempFile .toFile ()), StandardCharsets .UTF_8 )) {
0 commit comments