feat: implemented read dialogs from files
This commit is contained in:
parent
602712f446
commit
ba3b087024
@ -34,6 +34,10 @@ dependencies {
|
||||
api 'org.apache.logging.log4j:log4j-api:2.24.0'
|
||||
api 'org.apache.logging.log4j:log4j-core:2.19.0'
|
||||
|
||||
api 'org.spongepowered:configurate-hocon:4.2.0'
|
||||
api 'org.spongepowered:configurate-gson:4.2.0'
|
||||
api 'org.spongepowered:configurate-yaml:4.2.0'
|
||||
|
||||
api 'org.sql2o:sql2o:1.8.0'
|
||||
api 'com.clickhouse:clickhouse-jdbc:0.7.1'
|
||||
api 'org.lz4:lz4-java:1.8.0'
|
||||
|
||||
@ -0,0 +1,134 @@
|
||||
package ru.dragonestia.msb3.api.dialog.provider;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import org.spongepowered.configurate.gson.GsonConfigurationLoader;
|
||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
import ru.dragonestia.msb3.api.dialog.Dialog;
|
||||
import ru.dragonestia.msb3.api.dialog.DialogButton;
|
||||
import ru.dragonestia.msb3.api.dialog.DialogCondition;
|
||||
import ru.dragonestia.msb3.api.dialog.DialogRegistry;
|
||||
import team.unnamed.creative.base.Writable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Log4j2
|
||||
@UtilityClass
|
||||
public class DialogFileProvider {
|
||||
|
||||
@SneakyThrows
|
||||
public void readFromHocon(Writable writable) {
|
||||
var root = HoconConfigurationLoader.builder()
|
||||
.buildAndLoadString(writable.toUTF8String());
|
||||
loadDialogs(Objects.requireNonNull(root.get(DialogsList.class)));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void readFromJson(Writable writable) {
|
||||
var root = GsonConfigurationLoader.builder()
|
||||
.buildAndLoadString(writable.toUTF8String());
|
||||
loadDialogs(Objects.requireNonNull(root.get(DialogsList.class)));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void readFromYaml(Writable writable) {
|
||||
var root = YamlConfigurationLoader.builder()
|
||||
.buildAndLoadString(writable.toUTF8String());
|
||||
loadDialogs(Objects.requireNonNull(root.get(DialogsList.class)));
|
||||
}
|
||||
|
||||
private void loadDialogs(DialogsList source) {
|
||||
for (var entry: source.dialogs.entrySet()) {
|
||||
var dialogId = entry.getKey();
|
||||
var dialogEntry = entry.getValue();
|
||||
var dialog = new Dialog();
|
||||
|
||||
dialog.setId(Key.key(dialogId));
|
||||
dialog.setText(Objects.requireNonNull(dialogEntry.text, "Text is null inside dialog " + dialogId));
|
||||
dialog.setRememberId(dialogEntry.remember);
|
||||
dialog.setThemeId(dialogEntry.theme);
|
||||
|
||||
var buttons = new ArrayList<DialogButton>();
|
||||
for (var buttonEntry: dialogEntry.buttons) {
|
||||
var button = new DialogButton();
|
||||
|
||||
if (buttonEntry.id != null) button.setId(Key.key(buttonEntry.id));
|
||||
button.setText(Objects.requireNonNull(buttonEntry.text));
|
||||
button.setActionId(Objects.requireNonNull(buttonEntry.action));
|
||||
button.setParams(Objects.requireNonNull(buttonEntry.params));
|
||||
|
||||
var conditions = new ArrayList<DialogCondition>();
|
||||
for (var conditionEntry: buttonEntry.conditions) {
|
||||
var condition = new DialogCondition();
|
||||
|
||||
condition.setConditionId(Objects.requireNonNull(conditionEntry.id));
|
||||
condition.setParams(Objects.requireNonNull(conditionEntry.params));
|
||||
}
|
||||
button.setConditions(conditions);
|
||||
|
||||
buttons.add(button);
|
||||
}
|
||||
dialog.setButtons(buttons);
|
||||
|
||||
DialogRegistry.storeDialog(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigSerializable
|
||||
public static class DialogsList {
|
||||
|
||||
private Map<String, DialogEntry> dialogs;
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigSerializable
|
||||
public static class DialogEntry {
|
||||
private String text;
|
||||
private boolean remember = false;
|
||||
private String theme = null;
|
||||
private List<ButtonEntry> buttons;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{text=" + text + ", remember=" + remember + ", theme=" + theme + ", buttons=" + buttons + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigSerializable
|
||||
public static class ButtonEntry {
|
||||
private String id = null;
|
||||
private String text;
|
||||
private String action;
|
||||
private Map<String, String> params = new HashMap<>();
|
||||
private List<ConditionEntry> conditions = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{id=" + id + ", text=" + text + ", action=" + action + ", params=" + params + ", conditions=" + conditions + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigSerializable
|
||||
public static class ConditionEntry {
|
||||
private String id;
|
||||
private Map<String, String> params = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{id=" + id + ", params=" + params + "}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user