Skip to content

Commit

Permalink
Refactor mch viewer to support viewing multiple dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvinn8 committed Apr 4, 2024
1 parent 6edcbd1 commit be9f1fc
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ca.bkaw.mch.viewer.fabric;

import ca.bkaw.mch.fs.MchFileSystem;
import ca.bkaw.mch.fs.MchPath;
import ca.bkaw.mch.object.dimension.Dimension;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import xyz.nucleoid.fantasy.RuntimeWorldHandle;

import java.io.IOException;
import java.nio.file.Path;

public final class DimensionView {
private final HistoryView parent;
private final RuntimeWorldHandle worldHandle;
private final MchFileSystem fileSystem;
private final Path rootPath;
private final ResourceLocation dimensionKey;
private Dimension dimensionObject;

public DimensionView(HistoryView parent, RuntimeWorldHandle worldHandle, Dimension dimensionObject,
ResourceLocation dimensionKey, MchFileSystem fileSystem, Path rootPath) {
this.parent = parent;
this.worldHandle = worldHandle;
this.dimensionObject = dimensionObject;
this.dimensionKey = dimensionKey;
this.fileSystem = fileSystem;
this.rootPath = rootPath;
}

public HistoryView getParent() {
return this.parent;
}

public Path wrapPath(Path original) {
// We basically want to check of the "original" path starts with the root path.
// First, the "original" path needs to be made absolute since the root path is
// also absolute.
// The paths are also from different file system providers (default and mch),
// so we unwrap the root path (mch provider) to the default file system, so
// that they can be compared with the regular startsWith method.
//
Path normalized = original.toAbsolutePath().normalize();
Path unwrapped = MchPath.unwrap(this.rootPath);
if (normalized.startsWith(unwrapped)) {
// The original path should be wrapped
return new MchPath(this.fileSystem, original);
}
return original;
}

public void setDimension(Dimension dimensionObject) throws IOException {
this.dimensionObject = dimensionObject;
this.update();
}

private void update() throws IOException {
if (this.fileSystem == null) {
return;
}
this.fileSystem.setWorld(this.parent.getTrackedWorld(), this.dimensionKey.toString(), this.dimensionObject);

// Clear chunk caches
ServerLevel level = this.worldHandle.asWorld();
level.save(null, true, true); // flush, disable saving
ServerChunkCache chunkCache = level.getChunkSource();
chunkCache.save(true); // save and flush
((ClearableChunkCache) chunkCache).mch$clearChunkCache();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
public static HistoryView getHistoryView(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
MchViewerFabric mchViewer = MchViewerFabric.getInstance();
ResourceKey<Level> levelKey = ctx.getSource().getLevel().dimension();
HistoryView historyView = mchViewer.getHistoryView(levelKey);
DimensionView historyView = mchViewer.getDimensionView(levelKey);
if (historyView == null) {
throw NOT_VIEWING_HISTORY.create();
}
return historyView;
return historyView.getParent();
}

public static int test(CommandContext<CommandSourceStack> ctx) throws IOException {
Expand All @@ -96,11 +96,17 @@ public static int test(CommandContext<CommandSourceStack> ctx) throws IOExceptio
repository.readConfiguration();
TrackedWorld trackedWorld = repository.getConfiguration().getTrackedWorld(Sha1.fromString("dde930208d9c6dd54e13ef13ad5be79a476b27bf"));

HistoryView view = mchViewer.view(server, repository, trackedWorld);
Reference20<Commit> headCommitRef = repository.getHeadCommit();
if (headCommitRef == null) {
throw new IllegalArgumentException("Repository is empty");
}

Commit commit = headCommitRef.resolve(repository);
CommitInfo commitInfo = new CommitInfo(commit, headCommitRef.getSha1());

ctx.getSource().sendSuccess(Component.text(
"Created dimension with key " + view.getWorldHandle().getRegistryKey()
), true);
HistoryView view = mchViewer.view(server, repository, trackedWorld, commitInfo);

// TODO send player to view

return 1;
}
Expand All @@ -112,10 +118,7 @@ private static int log(CommandContext<CommandSourceStack> ctx) throws CommandSyn
HistoryView historyView = getHistoryView(ctx);
CachedCommits cachedCommits = historyView.getCachedCommits();

Sha1 currentCommitHash = historyView.getCommitHash();
Commit currentCommit = historyView.getCommit();
CommitInfo current = new CommitInfo(currentCommit, currentCommitHash);

CommitInfo current = historyView.getCommit();
CommitInfo[] commits = new CommitInfo[11];
commits[5] = current;

Expand Down Expand Up @@ -152,7 +155,7 @@ private static int log(CommandContext<CommandSourceStack> ctx) throws CommandSyn
}
Commit commit = commitInfo.commit();
Sha1 hash = commitInfo.hash();
boolean isCurrentCommit = hash.equals(currentCommitHash);
boolean isCurrentCommit = commitInfo.equals(current);
TextComponent.Builder row = Component.text();
row.clickEvent(ClickEvent.runCommand("/history commit " + hash.asHex()));
if (isCurrentCommit) {
Expand Down Expand Up @@ -216,7 +219,7 @@ private static int commit(CommandContext<CommandSourceStack> ctx, Sha1 sha1) thr
Reference20<Commit> ref = new Reference20<>(ObjectStorageTypes.COMMIT, sha1);
Commit commit = ref.resolve(repository);

historyView.setCommit(commit, ref.getSha1());
historyView.setCommit(new CommitInfo(commit, ref.getSha1()));

log(ctx);

Expand Down
Loading

0 comments on commit be9f1fc

Please sign in to comment.