Skip to content

Commit

Permalink
Refactored the proxy system.
Browse files Browse the repository at this point in the history
BREAKING CHANGES
- Changed: IConfigProxy now returns a Path instead of a list of Paths.
- Changed: IConfixProxy gets the relative config file to test against.
- Removed: The StackSystem now is deleted since it wasn't useful to
begin with.
- Updated: Examples
  • Loading branch information
Speiger committed Feb 15, 2024
1 parent ef3b44e commit 9a94f52
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
8 changes: 5 additions & 3 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,18 @@ Another way to do this is like this:
}

public static class MyCustomProxy implements IConfigProxy {
public List<Path> getBasePath() {
return Arrays.asList(Paths.get("MyBaseFolder/newInstance"), Paths.get("MyBaseFolder"));
public Path getBasePath(Path configFile) {
Path path = Paths.get("MyBaseFolder/newInstance");
return Files.exist(path.resolve(configFile) ? path : Paths.get("MyBaseFolder");
}

public boolean isDynamicProxy() {
return true;
}

public List<? extends IPotentialTarget> getPotentialConfigs() {
return getBasePath().stream().map(path -> new SimpleTarget(path, Helpers.firstLetterUppercase(path.getFileName().toString()))).toList();
List<Path> potentialPaths = Arrays.asList(Paths.get("MyBaseFolder/newInstance"), Paths.get("MyBaseFolder"));
return potentialPaths.stream().map(path -> new SimpleTarget(path, Helpers.firstLetterUppercase(path.getFileName().toString()))).toList();
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carbonconfiglib/api/IConfigProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public interface IConfigProxy
{
public List<Path> getBasePaths();
public Path getBasePaths(Path relativeFile);
public List<? extends IPotentialTarget> getPotentialConfigs();
public boolean isDynamicProxy();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/carbonconfiglib/api/SimpleConfigProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public boolean isDynamicProxy() {
}

@Override
public List<Path> getBasePaths() {
return ObjectLists.singleton(path);
public Path getBasePaths(Path relativeFile) {
return path;
}

@Override
Expand Down
26 changes: 7 additions & 19 deletions src/main/java/carbonconfiglib/config/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.EnumSet;
import java.util.List;

Expand Down Expand Up @@ -166,8 +167,8 @@ public void register() {
public void createDefaultConfig() {
if(!registered) return;
if(proxy.isDynamicProxy() && setting.contains(AutomationType.AUTO_LOAD)) {
List<Path> baseFolders = proxy.getBasePaths();
Path file = createConfigFile(baseFolders.get(baseFolders.size()-1));
Path baseFolder = proxy.getBasePaths(createConfigFile(Paths.get("")));
Path file = createConfigFile(baseFolder);
if(Files.notExists(file)) {
save(file);
wasSaving--;
Expand Down Expand Up @@ -209,24 +210,11 @@ public void unload() {
}

private void findConfigFile() {
List<Path> baseFolders = proxy.getBasePaths();
if(baseFolders.isEmpty()) throw new IllegalStateException("Proxy has no Folders");
if(baseFolders.size() == 1) {
configFile = createConfigFile(baseFolders.get(0));
cfgDir = configFile.getParent();
Helpers.ensureFolder(cfgDir);
return;
}

for(int i = baseFolders.size()-1,m=i;i>=0;i--) {
Path file = createConfigFile(baseFolders.get(i));
if(Files.notExists(file)) {
if(i == m) save(file);
else Helpers.copyFile(createConfigFile(baseFolders.get(i+1)), file);
}
}
configFile = createConfigFile(baseFolders.get(0));
Path baseFolder = proxy.getBasePaths(createConfigFile(Paths.get("")));
if(baseFolder == null) throw new IllegalStateException("Proxy has no Folder");
configFile = createConfigFile(baseFolder);
cfgDir = configFile.getParent();
Helpers.ensureFolder(cfgDir);
}

public void addLoadedListener(Runnable listener) {
Expand Down

0 comments on commit 9a94f52

Please sign in to comment.