use of com.electronwill.nightconfig.core.UnmodifiableConfig in project Create by Creators-of-Create.
the class SubMenuConfigScreen method saveChanges.
protected void saveChanges() {
UnmodifiableConfig values = spec.getValues();
ConfigHelper.changes.forEach((path, change) -> {
ForgeConfigSpec.ConfigValue<Object> configValue = values.get(path);
configValue.set(change.value);
if (type == ModConfig.Type.SERVER) {
AllPackets.channel.sendToServer(new CConfigureConfigPacket<>(ConfigScreen.modID, path, change.value));
}
String command = change.annotations.get("Execute");
if (minecraft.player != null && command != null && command.startsWith("/")) {
minecraft.player.chat(command);
// AllPackets.channel.sendToServer(new CChatMessagePacket(command));
}
});
clearChanges();
}
use of com.electronwill.nightconfig.core.UnmodifiableConfig in project Create by Creators-of-Create.
the class SubMenuConfigScreen method init.
@Override
protected void init() {
super.init();
listWidth = Math.min(width - 80, 300);
int yCenter = height / 2;
int listL = this.width / 2 - listWidth / 2;
int listR = this.width / 2 + listWidth / 2;
resetAll = new BoxWidget(listR + 10, yCenter - 25, 20, 20).withPadding(2, 2).withCallback((x, y) -> new ConfirmationScreen().centered().withText(FormattedText.of("Resetting all settings of the " + type.toString() + " config. Are you sure?")).withAction(success -> {
if (success)
resetConfig(spec.getValues());
}).open(this));
resetAll.showingElement(AllIcons.I_CONFIG_RESET.asStencil().withElementRenderer(BoxWidget.gradientFactory.apply(resetAll)));
resetAll.getToolTip().add(new TextComponent("Reset All"));
resetAll.getToolTip().addAll(TooltipHelper.cutStringTextComponent("Click here to reset all settings to their default value.", ChatFormatting.GRAY, ChatFormatting.GRAY));
saveChanges = new BoxWidget(listL - 30, yCenter - 25, 20, 20).withPadding(2, 2).withCallback((x, y) -> {
if (ConfigHelper.changes.isEmpty())
return;
ConfirmationScreen confirm = new ConfirmationScreen().centered().withText(FormattedText.of("Saving " + ConfigHelper.changes.size() + " changed value" + (ConfigHelper.changes.size() != 1 ? "s" : "") + "")).withAction(success -> {
if (success)
saveChanges();
});
addAnnotationsToConfirm(confirm).open(this);
});
saveChanges.showingElement(AllIcons.I_CONFIG_SAVE.asStencil().withElementRenderer(BoxWidget.gradientFactory.apply(saveChanges)));
saveChanges.getToolTip().add(new TextComponent("Save Changes"));
saveChanges.getToolTip().addAll(TooltipHelper.cutStringTextComponent("Click here to save your current changes.", ChatFormatting.GRAY, ChatFormatting.GRAY));
discardChanges = new BoxWidget(listL - 30, yCenter + 5, 20, 20).withPadding(2, 2).withCallback((x, y) -> {
if (ConfigHelper.changes.isEmpty())
return;
new ConfirmationScreen().centered().withText(FormattedText.of("Discarding " + ConfigHelper.changes.size() + " unsaved change" + (ConfigHelper.changes.size() != 1 ? "s" : "") + "")).withAction(success -> {
if (success)
clearChanges();
}).open(this);
});
discardChanges.showingElement(AllIcons.I_CONFIG_DISCARD.asStencil().withElementRenderer(BoxWidget.gradientFactory.apply(discardChanges)));
discardChanges.getToolTip().add(new TextComponent("Discard Changes"));
discardChanges.getToolTip().addAll(TooltipHelper.cutStringTextComponent("Click here to discard all the changes you made.", ChatFormatting.GRAY, ChatFormatting.GRAY));
goBack = new BoxWidget(listL - 30, yCenter + 65, 20, 20).withPadding(2, 2).withCallback(this::attemptBackstep);
goBack.showingElement(AllIcons.I_CONFIG_BACK.asStencil().withElementRenderer(BoxWidget.gradientFactory.apply(goBack)));
goBack.getToolTip().add(new TextComponent("Go Back"));
addRenderableWidget(resetAll);
addRenderableWidget(saveChanges);
addRenderableWidget(discardChanges);
addRenderableWidget(goBack);
list = new ConfigScreenList(minecraft, listWidth, height - 80, 35, height - 45, 40);
list.setLeftPos(this.width / 2 - list.getWidth() / 2);
addRenderableWidget(list);
search = new ConfigTextField(font, width / 2 - listWidth / 2, height - 35, listWidth, 20);
search.setResponder(this::updateFilter);
search.setHint("Search...");
search.moveCursorToStart();
addRenderableWidget(search);
configGroup.valueMap().forEach((key, obj) -> {
String humanKey = toHumanReadable(key);
if (obj instanceof AbstractConfig) {
SubMenuEntry entry = new SubMenuEntry(this, humanKey, spec, (UnmodifiableConfig) obj);
entry.path = key;
list.children().add(entry);
if (configGroup.valueMap().size() == 1)
ScreenOpener.open(new SubMenuConfigScreen(parent, humanKey, type, spec, (UnmodifiableConfig) obj));
} else if (obj instanceof ForgeConfigSpec.ConfigValue<?>) {
ForgeConfigSpec.ConfigValue<?> configValue = (ForgeConfigSpec.ConfigValue<?>) obj;
ForgeConfigSpec.ValueSpec valueSpec = spec.getRaw(configValue.getPath());
Object value = configValue.get();
ConfigScreenList.Entry entry = null;
if (value instanceof Boolean) {
entry = new BooleanEntry(humanKey, (ForgeConfigSpec.ConfigValue<Boolean>) configValue, valueSpec);
} else if (value instanceof Enum) {
entry = new EnumEntry(humanKey, (ForgeConfigSpec.ConfigValue<Enum<?>>) configValue, valueSpec);
} else if (value instanceof Number) {
entry = NumberEntry.create(value, humanKey, configValue, valueSpec);
}
if (entry == null)
entry = new LabeledEntry("Impl missing - " + configValue.get().getClass().getSimpleName() + " " + humanKey + " : " + value);
if (highlights.contains(key))
entry.annotations.put("highlight", ":)");
list.children().add(entry);
}
});
Collections.sort(list.children(), (e, e2) -> {
int group = (e2 instanceof SubMenuEntry ? 1 : 0) - (e instanceof SubMenuEntry ? 1 : 0);
if (group == 0 && e instanceof LabeledEntry && e2 instanceof LabeledEntry) {
LabeledEntry le = (LabeledEntry) e;
LabeledEntry le2 = (LabeledEntry) e2;
return le.label.getComponent().getString().compareTo(le2.label.getComponent().getString());
}
return group;
});
list.search(highlights.stream().findFirst().orElse(""));
// extras for server configs
if (type != ModConfig.Type.SERVER)
return;
if (minecraft.hasSingleplayerServer())
return;
boolean canEdit = minecraft != null && minecraft.player != null && minecraft.player.hasPermissions(2);
Couple<Color> red = Theme.p(Theme.Key.BUTTON_FAIL);
Couple<Color> green = Theme.p(Theme.Key.BUTTON_SUCCESS);
DelegatedStencilElement stencil = new DelegatedStencilElement();
serverLocked = new BoxWidget(listR + 10, yCenter + 5, 20, 20).withPadding(2, 2).showingElement(stencil);
if (!canEdit) {
list.children().forEach(e -> e.setEditable(false));
resetAll.active = false;
stencil.withStencilRenderer((ms, w, h, alpha) -> AllIcons.I_CONFIG_LOCKED.render(ms, 0, 0));
stencil.withElementRenderer((ms, w, h, alpha) -> UIRenderHelper.angledGradient(ms, 90, 8, 0, 16, 16, red));
serverLocked.withBorderColors(red);
serverLocked.getToolTip().add(new TextComponent("Locked").withStyle(ChatFormatting.BOLD));
serverLocked.getToolTip().addAll(TooltipHelper.cutStringTextComponent("You do not have enough permissions to edit the server config. You can still look at the current values here though.", ChatFormatting.GRAY, ChatFormatting.GRAY));
} else {
stencil.withStencilRenderer((ms, w, h, alpha) -> AllIcons.I_CONFIG_UNLOCKED.render(ms, 0, 0));
stencil.withElementRenderer((ms, w, h, alpha) -> UIRenderHelper.angledGradient(ms, 90, 8, 0, 16, 16, green));
serverLocked.withBorderColors(green);
serverLocked.getToolTip().add(new TextComponent("Unlocked").withStyle(ChatFormatting.BOLD));
serverLocked.getToolTip().addAll(TooltipHelper.cutStringTextComponent("You have enough permissions to edit the server config. Changes you make here will be synced with the server when you save them.", ChatFormatting.GRAY, ChatFormatting.GRAY));
}
addRenderableWidget(serverLocked);
}
use of com.electronwill.nightconfig.core.UnmodifiableConfig in project Mohist by MohistMC.
the class ForgeConfigSpec method correct.
private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
int count = 0;
Map<String, Object> specMap = spec.valueMap();
Map<String, Object> configMap = config.valueMap();
for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
final String key = specEntry.getKey();
final Object specValue = specEntry.getValue();
final Object configValue = configMap.get(key);
final CorrectionAction action = configValue == null ? ADD : REPLACE;
parentPath.addLast(key);
if (specValue instanceof Config) {
if (configValue instanceof CommentedConfig) {
count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
if (count > 0 && dryRun)
return count;
} else if (dryRun) {
return 1;
} else {
CommentedConfig newValue = config.createSubConfig();
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
}
String newComment = levelComments.get(parentPath);
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
if (dryRun)
return 1;
config.setComment(key, newComment);
}
} else {
ValueSpec valueSpec = (ValueSpec) specValue;
if (!valueSpec.test(configValue)) {
if (dryRun)
return 1;
Object newValue = valueSpec.correct(configValue);
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
}
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
if (dryRun)
return 1;
config.setComment(key, valueSpec.getComment());
}
}
parentPath.removeLast();
}
// Second step: removes the unspecified values
for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
Map.Entry<String, Object> entry = ittr.next();
if (!specMap.containsKey(entry.getKey())) {
if (dryRun)
return 1;
ittr.remove();
parentPath.addLast(entry.getKey());
listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
parentPath.removeLast();
count++;
}
}
return count;
}
use of com.electronwill.nightconfig.core.UnmodifiableConfig in project Magma-1.16.x by magmafoundation.
the class ForgeConfigSpec method correct.
private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
int count = 0;
Map<String, Object> specMap = spec.valueMap();
Map<String, Object> configMap = config.valueMap();
for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
final String key = specEntry.getKey();
final Object specValue = specEntry.getValue();
final Object configValue = configMap.get(key);
final CorrectionAction action = configValue == null ? ADD : REPLACE;
parentPath.addLast(key);
if (specValue instanceof Config) {
if (configValue instanceof CommentedConfig) {
count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
if (count > 0 && dryRun)
return count;
} else if (dryRun) {
return 1;
} else {
CommentedConfig newValue = config.createSubConfig();
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
}
String newComment = levelComments.get(parentPath);
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
if (dryRun)
return 1;
config.setComment(key, newComment);
}
} else {
ValueSpec valueSpec = (ValueSpec) specValue;
if (!valueSpec.test(configValue)) {
if (dryRun)
return 1;
Object newValue = valueSpec.correct(configValue);
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
}
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
if (dryRun)
return 1;
config.setComment(key, valueSpec.getComment());
}
}
parentPath.removeLast();
}
// Second step: removes the unspecified values
for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
Map.Entry<String, Object> entry = ittr.next();
if (!specMap.containsKey(entry.getKey())) {
if (dryRun)
return 1;
ittr.remove();
parentPath.addLast(entry.getKey());
listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
parentPath.removeLast();
count++;
}
}
return count;
}
use of com.electronwill.nightconfig.core.UnmodifiableConfig in project MinecraftForge by MinecraftForge.
the class ForgeConfigSpec method correct.
private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
int count = 0;
Map<String, Object> specMap = spec.valueMap();
Map<String, Object> configMap = config.valueMap();
for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
final String key = specEntry.getKey();
final Object specValue = specEntry.getValue();
final Object configValue = configMap.get(key);
final CorrectionAction action = configValue == null ? ADD : REPLACE;
parentPath.addLast(key);
if (specValue instanceof Config) {
if (configValue instanceof CommentedConfig) {
count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
if (count > 0 && dryRun)
return count;
} else if (dryRun) {
return 1;
} else {
CommentedConfig newValue = config.createSubConfig();
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
}
String newComment = levelComments.get(parentPath);
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
if (dryRun)
return 1;
config.setComment(key, newComment);
}
} else {
ValueSpec valueSpec = (ValueSpec) specValue;
if (!valueSpec.test(configValue)) {
if (dryRun)
return 1;
Object newValue = valueSpec.correct(configValue);
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
}
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
if (dryRun)
return 1;
config.setComment(key, valueSpec.getComment());
}
}
parentPath.removeLast();
}
// Second step: removes the unspecified values
for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
Map.Entry<String, Object> entry = ittr.next();
if (!specMap.containsKey(entry.getKey())) {
if (dryRun)
return 1;
ittr.remove();
parentPath.addLast(entry.getKey());
listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
parentPath.removeLast();
count++;
}
}
return count;
}
Aggregations