Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private Effect effect(GameState state, PackCatalog.ConfigType type, PackCatalog.
return Effect.RESTART;
}
if (location == Location.WORLD && !state.isOpen(world)) return Effect.WORLD_OPENS;
if (restart == PackCatalog.Restart.UNKNOWN) return Effect.UNDESCRIBED;
if (restart == PackCatalog.Restart.WORLD) return state.openWorld() == null ? Effect.NOW : Effect.REJOIN;
return Effect.NOW;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,28 @@ private static BigDecimal number(String text) {
}

private static void checkRange(BigDecimal number, String range) {
BigDecimal minimum = null;
BigDecimal maximum = null;
String lower = null;
String upper = null;
if (range.startsWith("> ")) {
minimum = number(range.substring(2));
lower = range.substring(2);
} else if (range.startsWith("< ")) {
maximum = number(range.substring(2));
upper = range.substring(2);
} else {
Matcher bounds = RANGE.matcher(range);
if (bounds.matches()) {
minimum = number(bounds.group(1));
maximum = number(bounds.group(2));
lower = bounds.group(1);
upper = bounds.group(2);
}
}
if (minimum != null && number.compareTo(minimum) < 0 || maximum != null && number.compareTo(maximum) > 0) {
throw new IllegalArgumentException("Accepts " + readableRange(range));
BigDecimal minimum = lower == null ? null : number(lower);
BigDecimal maximum = upper == null ? null : number(upper);
// A bound at an int's limit reads as none, as NeoForge writes it for an int; the catalog does not know the type,
// so for a long it is a real bound, and the value refused for it names it.
if (minimum != null && number.compareTo(minimum) < 0) {
throw new IllegalArgumentException(unbounded(lower, false) ? "Accepts at least " + trimmed(lower) : "Accepts " + readableRange(range));
}
if (maximum != null && number.compareTo(maximum) > 0) {
throw new IllegalArgumentException(unbounded(upper, true) ? "Accepts at most " + trimmed(upper) : "Accepts " + readableRange(range));
}
}

Expand Down Expand Up @@ -314,8 +321,8 @@ public static List<TextChange> changes(String before, String after, List<PackCat
for (Map.Entry<String, String> entry : afterLiterals.entrySet()) {
String previous = beforeLiterals.get(entry.getKey());
if (previous == null || sameValue(previous, entry.getValue())) continue;
PackCatalog.ConfigSetting setting = described.getOrDefault(entry.getKey(), new PackCatalog.ConfigSetting(
entry.getKey(), "", "", "", List.of(), PackCatalog.Restart.NONE));
PackCatalog.ConfigSetting setting = described.getOrDefault(entry.getKey(),
PackCatalog.ConfigSetting.undescribed(entry.getKey(), ""));
changes.add(new TextChange(setting, previous, entry.getValue()));
}
return changes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static PackCatalog.ConfigSetting described(CatalogIndex index, ChangeRec
if (setting.path().equals(target.setting())) return setting;
}
}
return new PackCatalog.ConfigSetting(target.setting(), "", "", "", List.of(), PackCatalog.Restart.NONE);
return PackCatalog.ConfigSetting.undescribed(target.setting(), "");
}

private static PackCatalog.ConfigType type(CatalogIndex index, ChangeRecord.Setting target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.github.minecraft_ta.totaldebug.storage.PackCatalog;

import java.io.IOException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
Expand All @@ -28,6 +30,13 @@ public String toString() {
}
}

/** The copies of a configuration found, and why worlds that could not be read are missing, or empty. */
public record Listed(List<Source> sources, String problem) {
public Listed {
sources = List.copyOf(sources);
}
}

/** A mod's configuration file and the mod it belongs to. */
public record Owner(PackCatalog.Mod mod, PackCatalog.ConfigFile file) {
}
Expand Down Expand Up @@ -79,35 +88,43 @@ private static boolean copyOf(Path workspace, PackCatalog.ConfigFile config, Pat
/**
* A configuration's loaded file, or for a server configuration the file of each world, the open world's first and
* then the most recently changed, and then the defaults for new worlds. A server configuration's loaded file belongs to the world open when
* the catalog was captured, so every world is listed. Blocking for a server configuration; it lists the worlds.
* the catalog was captured, so every world is listed. A world that cannot be read is named in the problem and the
* others are listed. Blocking for a server configuration; it lists the worlds.
*/
public static List<Source> of(GameLocation location, PackCatalog.ConfigFile file) {
public static Listed of(GameLocation location, PackCatalog.ConfigFile file) {
Path workspace = location.workspace();
if (file.type() != PackCatalog.ConfigType.SERVER) {
Path loaded = loaded(workspace, file);
return loaded == null ? List.of() : List.of(new Source(file.fileName(), loaded));
return new Listed(loaded == null ? List.of() : List.of(new Source(file.fileName(), loaded)), "");
}
List<Source> worlds = new ArrayList<>();
Map<Source, FileTime> modified = new HashMap<>();
Set<Source> open = new HashSet<>();
List<String> unread = new ArrayList<>();
GameState game = location.read();
try (DirectoryStream<Path> saves = Files.newDirectoryStream(workspace.resolve("saves"), Files::isDirectory)) {
for (Path world : saves) {
Path path = world.resolve("serverconfig").resolve(file.fileName());
if (!Files.isRegularFile(path)) continue;
Source source = new Source(world.getFileName().toString(), path);
worlds.add(source);
modified.put(source, Files.getLastModifiedTime(path));
if (game.isOpen(world)) open.add(source);
try {
Source source = new Source(world.getFileName().toString(), path);
modified.put(source, Files.getLastModifiedTime(path));
worlds.add(source);
if (game.isOpen(world)) open.add(source);
} catch (IOException exception) {
unread.add(world.getFileName() + " (" + exception.getMessage() + ")");
}
}
} catch (IOException noSaves) {
} catch (NoSuchFileException noSaves) {
// A pack that never created a world has no server configuration yet.
} catch (IOException | DirectoryIteratorException exception) {
unread.add("the saves folder (" + (exception.getCause() != null ? exception.getCause() : exception).getMessage() + ")");
}
// The world the game has open is the one an edit is meant for.
worlds.sort(Comparator.comparing((Source source) -> !open.contains(source))
.thenComparing(Comparator.comparing((Source source) -> modified.get(source)).reversed()));
Path defaults = workspace.resolve("defaultconfigs").resolve(file.fileName());
if (Files.isRegularFile(defaults)) worlds.add(new Source("New worlds", defaults));
return worlds;
return new Listed(worlds, unread.isEmpty() ? "" : "Could not read " + String.join(", ", unread));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static void walk(UnmodifiableCommentedConfig level, String prefix, Map<S
} else {
String display = PackCatalog.ConfigSetting.display(value);
values.put(key, display);
settings.add(new PackCatalog.ConfigSetting(key, comment, "", "", List.of(), PackCatalog.Restart.NONE));
settings.add(PackCatalog.ConfigSetting.undescribed(key, comment));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

/**
* When a change takes effect in the game (see {@code docs/CHANGE_PIPELINE.md}): at once, after a rejoin or a restart,
* when the game starts or the world opens, or for worlds created later.
* when the game starts or the world opens, or for worlds created later; or unknown, for a setting no specification
* describes.
*/
public enum Effect {
NOW("the game reloaded it"),
REJOIN("takes effect after rejoining the world"),
RESTART("takes effect after restarting the game"),
GAME_STARTS("applies when the game starts"),
WORLD_OPENS("applies when the world opens"),
NEW_WORLDS("applies to worlds created from now on");
NEW_WORLDS("applies to worlds created from now on"),
UNDESCRIBED("the catalog does not describe when the game uses it");

private final String description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -38,6 +39,8 @@ public final class CatalogSearch {
private final String[] ids;
private List<ModResources.Resource> resources;
private List<Path> directories;
/** The mods each listed file backs: one, several sharing an archive, or Minecraft for its own assets. */
private Map<Path, List<PackCatalog.Mod>> owners;
/** Where the game keeps its keys, or null without a game directory. */
private final GameLocation location;
private final Path options;
Expand Down Expand Up @@ -116,10 +119,12 @@ List<Result> search(String query, Category category, int limit, Set<String> modu
if (category == Category.RESOURCES) {
for (ModResources.Resource resource : resources()) {
if (!resource.path().toLowerCase(Locale.ROOT).contains(folded)) continue;
// A mod's file owns what it holds, whatever namespace that is under.
List<PackCatalog.Mod> owners = this.owners.getOrDefault(resource.file(), List.of());
if (moduleIds != null && owners.stream().noneMatch(owner -> moduleIds.contains(owner.module()))) continue;
String namespace = namespace(resource.path());
PackCatalog.Mod owner = this.index.mod(namespace).orElse(null);
if (moduleIds != null && (owner == null || !moduleIds.contains(owner.module()))) continue;
results.add(new ResourceResult(resource, this.index.ownerName(namespace)));
results.add(new ResourceResult(resource, owners.stream().filter(owner -> owner.id().equals(namespace)).findFirst()
.or(() -> owners.stream().findFirst()).map(PackCatalog.Mod::title).orElse(this.index.ownerName(namespace))));
}
}
// Every match is ranked before the limit applies, so a closer match late in the registry is kept.
Expand Down Expand Up @@ -179,9 +184,14 @@ private static String namespace(String path) {
private synchronized List<ModResources.Resource> resources() {
if (this.resources == null) {
List<Path> files = new ArrayList<>();
Map<Path, List<PackCatalog.Mod>> owners = new HashMap<>();
for (PackCatalog.Mod mod : this.index.mods()) {
for (Path file : this.index.resourceFiles(mod.id())) if (!files.contains(file)) files.add(file);
for (Path file : this.index.resourceFiles(mod.id())) {
if (!files.contains(file)) files.add(file);
owners.computeIfAbsent(file, ignored -> new ArrayList<>()).add(mod);
}
}
this.owners = Map.copyOf(owners);
this.directories = files.stream().filter(Files::isDirectory).toList();
this.resources = List.copyOf(listed(files.stream().filter(file -> !Files.isDirectory(file)).toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.github.minecraft_ta.totalDebugCompanion.ui.components.FlatIconButton;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.function.Consumer;

/** A readable value and an explicit copy action with local feedback. */
/** A readable value, or a component showing one, and an explicit copy action with local feedback. */
public final class CopyValue extends JPanel {
private final JLabel label = PopupElements.label("");
private final JButton copy = new FlatIconButton(Icons.COPY, false);
Expand All @@ -24,6 +25,15 @@ public CopyValue(String actionName) {
}

CopyValue(String actionName, Consumer<String> copyAction) {
this(actionName, copyAction, null);
}

/** Copy beside {@code shown}, which its owner keeps up to date; {@link #setValue(String)} sets what is copied. */
public CopyValue(String actionName, JComponent shown) {
this(actionName, PopupElements::copy, shown);
}

private CopyValue(String actionName, Consumer<String> copyAction, JComponent shown) {
super(new BorderLayout(12, 0));
this.actionName = actionName;
setOpaque(false);
Expand All @@ -38,20 +48,29 @@ public CopyValue(String actionName) {
});
feedback.setRepeats(false);
label.setMinimumSize(new Dimension(0, label.getPreferredSize().height));
add(label, BorderLayout.CENTER);
add(shown == null ? label : shown, BorderLayout.CENTER);
add(copy, BorderLayout.EAST);
setValue("", "");
}

public void setValue(String display, String value) {
if (!this.value.equals(value)) resetFeedback();
this.value = value;
label.setText(display);
label.setToolTipText(Tooltip.of("").text(value).html());
setValue(value);
}

/** What the copy action copies; hidden while it is empty. */
public void setValue(String value) {
if (!this.value.equals(value)) resetFeedback();
this.value = value;
copy.setEnabled(!value.isEmpty());
setVisible(!value.isEmpty());
}

public String value() {
return this.value;
}

@Override public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
// Fractional display scales can round the label's measured and painted widths differently.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public final class ConfigPanel extends JPanel {
private final Consumer<NavigationTarget> navigator;
/** What the file could not show, such as a read error; it takes the place of {@link #status}. */
private String problem = "";
/** The worlds whose copies of the file could not be listed, or empty; shown before the problem or status. */
private String unlisted = "";
/** The outcome of the last edit, or why a typed value was refused. */
private String status = "";

Expand Down Expand Up @@ -150,7 +152,7 @@ private record ValuesRead(PackCatalog.ConfigFile file, ConfigValues values, Stri
}

/** The copies of a server configuration in the worlds. */
private record SourcesRead(PackCatalog.ConfigFile file, List<ConfigSources.Source> found) {
private record SourcesRead(PackCatalog.ConfigFile file, ConfigSources.Listed found) {
}

private void configureFiles() {
Expand Down Expand Up @@ -324,13 +326,13 @@ private void showFile() {
this.sources.cancel();
this.modifiedOnly.setVisible(file != null && !file.settings().isEmpty());
if (file == null || file.type() != PackCatalog.ConfigType.SERVER) {
showSources(file, file == null ? List.of() : ConfigSources.of(this.location, file), true);
showSources(file, file == null ? new ConfigSources.Listed(List.of(), "") : ConfigSources.of(this.location, file), true);
return;
}
// A server configuration's copies are found by listing the worlds, which reads the disk. Until they are known,
// the previous file's copies and values leave the page, so nothing can be edited into the wrong file.
this.values.cancel();
showSources(file, List.of(), false);
showSources(file, new ConfigSources.Listed(List.of(), ""), false);
this.sources.load();
}

Expand All @@ -342,12 +344,13 @@ private Callable<SourcesRead> prepareSources() {
}

/** Shows {@code file}'s copies; {@code known} tells whether they are all listed, so its values can be read. */
private void showSources(PackCatalog.ConfigFile file, List<ConfigSources.Source> found, boolean known) {
private void showSources(PackCatalog.ConfigFile file, ConfigSources.Listed found, boolean known) {
this.unlisted = found.problem();
this.updating = true;
try {
this.sourceModel.removeAllElements();
found.forEach(this.sourceModel::addElement);
this.source.setVisible(file != null && file.type() == PackCatalog.ConfigType.SERVER && !found.isEmpty());
found.sources().forEach(this.sourceModel::addElement);
this.source.setVisible(file != null && file.type() == PackCatalog.ConfigType.SERVER && !found.sources().isEmpty());
this.shownSource = this.source.getSelectedItem();
} finally {
this.updating = false;
Expand Down Expand Up @@ -380,7 +383,8 @@ private Callable<ValuesRead> prepareValues() {
if (file == null) return null;
if (path == null) {
this.values.cancel();
show(file, null, "", file.type() == PackCatalog.ConfigType.SERVER
// Where worlds could not be read, which of them has the file is not known.
show(file, null, "", !this.unlisted.isEmpty() ? "" : file.type() == PackCatalog.ConfigType.SERVER
? "No world has created this file yet. A world creates it with these defaults."
: "This file does not exist yet. The game creates it with these defaults.");
return null;
Expand Down Expand Up @@ -418,7 +422,8 @@ private void setStatus(String status) {
}

private void showNotice() {
String text = this.problem.isEmpty() ? this.status : this.problem;
String own = this.problem.isEmpty() ? this.status : this.problem;
String text = this.unlisted.isEmpty() ? own : own.isEmpty() ? this.unlisted : this.unlisted + "; " + own;
Comment on lines +425 to +426

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add a render scenario for the unread-world notice

This introduces a new configuration-page failure state, including the combined unlisted; edit problem layout, but the commit adds no UiRenderScenario or driver setup that displays it. The UI guide requires every new page or state to be rendered in both themes, so add a scenario that makes saves unreadable and exercises this notice composition.

AGENTS.md reference: AGENTS.md:L13-L13

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in d2e8b28: configuration-unlisted puts a file where the saves folder belongs and opens the test mod's new server configuration, whose defaults copy is shown under the notice. The harness catalog gains that server configuration and a world copy.

this.notice.setText(text);
this.notice.setVisible(!text.isEmpty());
}
Expand Down
Loading