Skip to content

Commit

Permalink
Update SectionConfiguration to use optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass authored Nov 15, 2023
1 parent 7478d32 commit adf15f5
Showing 1 changed file with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* The SectionConfiguration class can be used to create simple and efficient data sections
Expand All @@ -34,12 +35,14 @@
* </ul>
*/
public class SectionConfiguration {

private final Map<String, Object> data = new HashMap<>();
private final List<EntryLoader> entries;

@Nullable
private CodeSection parent;
private final List<EntryLoader> entries;
private final Map<String, Object> data = new HashMap<>();
private boolean loaded = false;

private boolean loaded;

private SectionConfiguration(List<EntryLoader> entries) {
this.entries = entries;
}
Expand All @@ -49,6 +52,7 @@ private SectionConfiguration(List<EntryLoader> entries) {
* a FileSection instance. This method should be called only once and will throw an error if attempting
* to load the configuration multiple times. The default use-case would be to load the configuration inside
* CodeSection's {@link CodeSection#loadSection(FileSection, ParserState, SkriptLogger) load} method.
*
* @param parent the parent section
* @param section the file section
* @param parserState the parse state
Expand All @@ -64,9 +68,8 @@ public boolean loadConfiguration(@Nullable CodeSection parent, FileSection secti

outer:
for (var entry : entries) {
for (var el : section.getElements()) {
logger.setLine(el.getLine() - 1);

for (var element : section.getElements()) {
logger.setLine(element.getLine() - 1);
if (logger.hasError()) {
/*
* If the execution of 'loadEntry' caused errors, it means that we
Expand All @@ -78,9 +81,9 @@ public boolean loadConfiguration(@Nullable CodeSection parent, FileSection secti
successful = false;
continue outer;
}
if (el instanceof VoidElement)
if (element instanceof VoidElement)
continue;
if (entry.loadEntry(this, el, parserState, logger))
if (entry.loadEntry(this, element, parserState, logger))
continue outer;
}
if (entry.isOptional())
Expand Down Expand Up @@ -115,6 +118,7 @@ public Map<String, Object> getData() {
return data;
}

@Nullable
public Object getValue(String key) {
return data.get(key);
}
Expand All @@ -124,32 +128,37 @@ public Object getValue(String key) {
* This can only be used when you register your option as a {@link Builder#addLiteral(String, Class) literal},
* otherwise, the option will likely be parsed as a String and throw an exception.
* Options that allow literal lists are saved as an array.
* @param key the key
* @param cls the class to cast to
* @return the value cast to the given class
* @param <T> the type of the value
*
* @param key the key of the node.
* @param cls the class to cast to.
* @return the value cast to the given class.
* @param <T> the type of the return value.
*/
@SuppressWarnings("unchecked")
public <T> T getValue(String key, Class<T> cls) {
public <T> Optional<T> getValue(String key, Class<T> cls) {
var result = data.get(key);
if (result == null)
return Optional.empty();
if (result.getClass() == String.class && result.getClass() != cls)
throw new UnsupportedOperationException("The key '" + key + "' was not registered as a literal, was parsed as a String and can, therefore, not be cast to " + cls.getName());
return (T) result;
return Optional.of((T) result);
}

public String getString(String key) {
@Nullable
public Optional<String> getString(String key) {
return getValue(key, String.class);
}

public String[] getStringList(String key) {
public Optional<String[]> getStringList(String key) {
return getValue(key, String[].class);
}

public CodeSection getSection(String key) {
public Optional<CodeSection> getSection(String key) {
return getValue(key, CodeSection.class);
}

public static class Builder {

private final List<EntryLoader> entries = new ArrayList<>();

public Builder addKey(String key) {
Expand Down Expand Up @@ -195,5 +204,7 @@ public Builder addLoader(EntryLoader loader) {
public SectionConfiguration build() {
return new SectionConfiguration(entries);
}

}
}

}

0 comments on commit adf15f5

Please sign in to comment.