-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/info/itsthesky/disky/api/skript/entries/MutexEntryData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package info.itsthesky.disky.api.skript.entries; | ||
|
||
import ch.njol.skript.ScriptLoader; | ||
import ch.njol.skript.config.Node; | ||
import ch.njol.skript.config.SectionNode; | ||
import ch.njol.skript.config.SimpleNode; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.entry.EntryContainer; | ||
import org.skriptlang.skript.lang.entry.EntryData; | ||
import org.skriptlang.skript.lang.entry.EntryValidator; | ||
import org.skriptlang.skript.lang.entry.KeyValueEntryData; | ||
|
||
import java.util.function.Function; | ||
|
||
public class MutexEntryData<T> extends EntryData<MutexEntryData.MutexEntry<T>> { | ||
|
||
private EntryValidator entryValidator; | ||
private KeyValueEntryData<T> valueEntryData; | ||
|
||
public MutexEntryData(String key, @Nullable T defaultValue, boolean optional, | ||
EntryValidator entryValidator, Function<String, T> parser) { | ||
super(key, MutexEntryData.MutexEntry.ofValue(defaultValue), optional); | ||
this.entryValidator = entryValidator; | ||
this.valueEntryData = new KeyValueEntryData<T>(key, | ||
defaultValue, false) { | ||
|
||
@Override | ||
protected T getValue(@NotNull String value) { | ||
return parser.apply(value); | ||
} | ||
|
||
@Override | ||
public @NotNull String getSeparator() { | ||
return ":"; | ||
} | ||
|
||
}; | ||
} | ||
|
||
@Override | ||
public MutexEntryData.MutexEntry<T> getValue(@NotNull Node node) { | ||
if (node instanceof SimpleNode) { | ||
final T value = valueEntryData.getValue(node); | ||
return MutexEntryData.MutexEntry.ofValue(value); | ||
} else if (node instanceof SectionNode) { | ||
final @Nullable EntryContainer entry = entryValidator.validate((SectionNode) node); | ||
if (entry == null) | ||
return MutexEntryData.MutexEntry.empty(); | ||
|
||
return MutexEntryData.MutexEntry.ofEntry(entry); | ||
} | ||
|
||
return MutexEntryData.MutexEntry.empty(); | ||
} | ||
|
||
@Override | ||
public boolean canCreateWith(@NotNull Node node) { | ||
if (node instanceof SectionNode) { | ||
String key = node.getKey(); | ||
if (key == null) | ||
return false; | ||
key = ScriptLoader.replaceOptions(key); | ||
return getKey().equalsIgnoreCase(key); | ||
} | ||
|
||
return valueEntryData.canCreateWith(node); | ||
} | ||
|
||
public static class MutexEntry<T> { | ||
|
||
public static <T> MutexEntry<T> ofEntry(@NotNull EntryContainer entry) { | ||
return new MutexEntry<>(null, entry); | ||
} | ||
|
||
public static <T> MutexEntry<T> ofValue(@NotNull T value) { | ||
return new MutexEntry<>(value, null); | ||
} | ||
|
||
public static <T> MutexEntry<T> empty() { | ||
return new MutexEntry<>(null, null); | ||
} | ||
|
||
private final @Nullable T value; | ||
private final @Nullable EntryContainer entry; | ||
|
||
private MutexEntry(@Nullable T value, | ||
@Nullable EntryContainer entry) { | ||
this.value = value; | ||
this.entry = entry; | ||
} | ||
|
||
public @Nullable T getValue() { | ||
return value; | ||
} | ||
|
||
public @Nullable EntryContainer getEntryContainer() { | ||
return entry; | ||
} | ||
|
||
public boolean isComplex() { | ||
return entry != null; | ||
} | ||
|
||
public boolean isValid() { | ||
return value != null || entry != null; | ||
} | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/info/itsthesky/disky/api/skript/entries/SimpleKeyValueEntries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package info.itsthesky.disky.api.skript.entries; | ||
|
||
import ch.njol.skript.Skript; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.entry.KeyValueEntryData; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.regex.Pattern; | ||
|
||
public final class SimpleKeyValueEntries { | ||
private static final Pattern LIST = Pattern.compile("\\s*,\\s*/?"); | ||
|
||
public static KeyValueEntryData<Boolean> createBooleanEntry(String key, boolean def, boolean optional) { | ||
return new KeyValueEntryData<Boolean>(key, def, optional) { | ||
@Override | ||
protected @Nullable Boolean getValue(@NotNull String value) { | ||
try { | ||
return Boolean.parseBoolean(value); | ||
} catch (Exception e) { | ||
Skript.error("Cannot parse the value as a boolean! (got " + value + ")"); | ||
return null; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public static <T> KeyValueEntryData<List<T>> createList(String key, List<T> def, boolean optional, | ||
Function<String, T> singleParser) { | ||
return new KeyValueEntryData<List<T>>(key, def, optional) { | ||
@Override | ||
protected @Nullable List<T> getValue(@NotNull String value) { | ||
final String[] values = LIST.split(value); | ||
final List<T> result = new ArrayList<>(); | ||
for (String val : values) { | ||
try { | ||
final T parsed = singleParser.apply(val); | ||
if (parsed == null) { | ||
return null; | ||
} | ||
result.add(parsed); | ||
} catch (Exception e) { | ||
Skript.error("Cannot parse the value as a list! (got " + value + ")"); | ||
return null; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
}; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/info/itsthesky/disky/api/skript/entries/SubEntryData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package info.itsthesky.disky.api.skript.entries; | ||
|
||
import ch.njol.skript.ScriptLoader; | ||
import ch.njol.skript.config.Node; | ||
import ch.njol.skript.config.SectionNode; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.entry.EntryContainer; | ||
import org.skriptlang.skript.lang.entry.EntryData; | ||
import org.skriptlang.skript.lang.entry.EntryValidator; | ||
|
||
public class SubEntryData extends EntryData<EntryContainer> { | ||
|
||
private final EntryValidator validator; | ||
|
||
public SubEntryData(String key, boolean optional, EntryValidator validator) { | ||
super(key, null, optional); | ||
this.validator = validator; | ||
} | ||
|
||
@Override | ||
public @Nullable EntryContainer getValue(@NotNull Node node) { | ||
assert node instanceof SectionNode; | ||
return validator.validate((SectionNode) node); | ||
} | ||
|
||
@Override | ||
public boolean canCreateWith(@NotNull Node node) { | ||
if (!(node instanceof SectionNode)) | ||
return false; | ||
String key = node.getKey(); | ||
if (key == null) | ||
return false; | ||
key = ScriptLoader.replaceOptions(key); | ||
return getKey().equalsIgnoreCase(key); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
15f8adc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.