This commit is contained in:
Moulberry committed 2024-09-30 20:00:24 +08:00
1 parent be99313e81
commit 63535e6b3d
79 files changed
+2007 -529

No files matched your search

@@ -0,0 +1,195 @@
package com.moulberry.flashback.screen;
import com.mojang.serialization.Lifecycle;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.FlashbackGson;
import com.moulberry.flashback.exporting.AsyncFileDialogs;
import com.moulberry.flashback.io.ReplayCombiner;
import com.moulberry.flashback.playback.EmptyLevelSource;
import com.moulberry.flashback.record.FlashbackMeta;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.StringWidget;
import net.minecraft.client.gui.layouts.FrameLayout;
import net.minecraft.client.gui.layouts.GridLayout;
import net.minecraft.client.gui.screens.AlertScreen;
import net.minecraft.client.gui.screens.ConfirmScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.commands.Commands;
import net.minecraft.core.Holder;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.server.WorldLoader;
import net.minecraft.server.WorldStem;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.server.packs.repository.ServerPacksSource;
import net.minecraft.world.Difficulty;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.level.DataPackConfig;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.LevelSettings;
import net.minecraft.world.level.WorldDataConfiguration;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.dimension.BuiltinDimensionTypes;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.WorldDimensions;
import net.minecraft.world.level.levelgen.WorldOptions;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.level.storage.PrimaryLevelData;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
public class CombineReplayScreen extends Screen {
@Nullable
private final Screen lastScreen;
private String newReplayName = "Combined Replay";
private Path firstReplay;
private Path secondReplay;
private Path output;
private Button firstReplayButton;
private Button secondReplayButton;
private Button outputButton;
protected CombineReplayScreen(@Nullable Screen lastScreen, @Nullable Path firstReplay, @Nullable Path secondReplay, @Nullable Path output) {
super(Component.literal("Combine Replay"));
this.lastScreen = lastScreen;
this.firstReplay = firstReplay;
this.secondReplay = secondReplay;
this.output = output;
}
@Override
protected void init() {
super.init();
GridLayout gridLayout = new GridLayout();
gridLayout.defaultCellSetting().padding(4, 4, 4, 0);
GridLayout.RowHelper rowHelper = gridLayout.createRowHelper(2);
rowHelper.addChild(new StringWidget(204, 20, Component.literal("Combine Replay"), this.font), 2);
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal("New Replay Name"), this.font).alignLeft(), 2);
EditBox replayNameEditBox = new EditBox(this.font, 0, 0, 204, 20, Component.literal(this.newReplayName));
replayNameEditBox.setValue(this.newReplayName);
replayNameEditBox.setResponder(string -> this.newReplayName = string);
rowHelper.addChild(replayNameEditBox, 2);
Path replayFolder = Flashback.getReplayFolder();
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal("Source #1"), this.font).alignLeft(), 2);
String firstReplayPath = this.firstReplay == null ? "" : this.firstReplay.toString();
this.firstReplayButton = Button.builder(Component.literal(firstReplayPath), button -> {
CompletableFuture<String> future = AsyncFileDialogs.openFileDialog(replayFolder.toString(),
"Replay Archive", "zip");
future.thenAccept(pathStr -> {
if (pathStr != null) {
this.firstReplay = Path.of(pathStr);
this.firstReplayButton.setMessage(Component.literal(this.firstReplay.toString()));
}
});
}).width(204).build();
rowHelper.addChild(this.firstReplayButton, 2);
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal("Source #2"), this.font).alignLeft(), 2);
String secondReplayPath = this.secondReplay == null ? "" : this.secondReplay.toString();
this.secondReplayButton = Button.builder(Component.literal(secondReplayPath), button -> {
CompletableFuture<String> future = AsyncFileDialogs.openFileDialog(replayFolder.toString(),
"Replay Archive", "zip");
future.thenAccept(pathStr -> {
if (pathStr != null) {
this.secondReplay = Path.of(pathStr);
this.secondReplayButton.setMessage(Component.literal(this.secondReplay.toString()));
}
});
}).width(204).build();
rowHelper.addChild(this.secondReplayButton, 2);
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal("Output"), this.font).alignLeft(), 2);
String outputPath = this.output == null ? "" : this.output.toString();
this.outputButton = Button.builder(Component.literal(outputPath), button -> {
CompletableFuture<String> future = AsyncFileDialogs.saveFileDialog(replayFolder.toString(), "combined.zip",
"Replay Archive", "zip");
future.thenAccept(pathStr -> {
if (pathStr != null) {
this.output = Path.of(pathStr);
this.outputButton.setMessage(Component.literal(this.output.toString()));
}
});
}).width(204).build();
rowHelper.addChild(this.outputButton, 2);
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal(""), this.font), 2);
rowHelper.addChild(Button.builder(Component.literal("Combine Replays"), button -> {
try {
PackRepository packRepository = ServerPacksSource.createVanillaTrustedRepository();
packRepository.reload();
WorldDataConfiguration worldDataConfiguration = new WorldDataConfiguration(new DataPackConfig(List.of(), List.of()), FeatureFlags.DEFAULT_FLAGS);
LevelSettings levelSettings = new LevelSettings("Replay", GameType.SPECTATOR, false, Difficulty.NORMAL, true, new GameRules(), worldDataConfiguration);
WorldLoader.PackConfig packConfig = new WorldLoader.PackConfig(packRepository, worldDataConfiguration, false, true);
WorldLoader.InitConfig initConfig = new WorldLoader.InitConfig(packConfig, Commands.CommandSelection.DEDICATED, 4);
WorldStem worldStem = Util.blockUntilDone(executor -> WorldLoader.load(initConfig, dataLoadContext -> {
Registry<LevelStem> registry = new MappedRegistry<>(Registries.LEVEL_STEM, Lifecycle.stable()).freeze();
Holder.Reference<Biome> plains = dataLoadContext.datapackWorldgen().registryOrThrow(Registries.BIOME).getHolder(Biomes.PLAINS).get();
Holder.Reference<DimensionType> overworld = dataLoadContext.datapackWorldgen().registryOrThrow(Registries.DIMENSION_TYPE).getHolder(BuiltinDimensionTypes.OVERWORLD).get();
WorldDimensions worldDimensions = new WorldDimensions(Map.of(LevelStem.OVERWORLD, new LevelStem(overworld, new EmptyLevelSource(plains))));
WorldDimensions.Complete complete = worldDimensions.bake(registry);
return new WorldLoader.DataLoadOutput<>(new PrimaryLevelData(levelSettings, new WorldOptions(0L, false, false),
complete.specialWorldProperty(), complete.lifecycle()), complete.dimensionsRegistryAccess());
}, WorldStem::new, Util.backgroundExecutor(), executor)).get();
ReplayCombiner.combine(worldStem.registries().compositeAccess(), this.firstReplay, this.secondReplay, this.output);
Minecraft.getInstance().setScreen(new TitleScreen());
worldStem.close();
} catch (Exception e) {
Flashback.LOGGER.error("Error combining replays", e);
Minecraft.getInstance().setScreen(new AlertScreen(() -> Minecraft.getInstance().setScreen(this.lastScreen),
Component.literal("Unable to combine replays"), Component.literal(e.getMessage())));
}
}).width(98).build(), 1);
rowHelper.addChild(Button.builder(Component.literal("Cancel"), button -> {
Minecraft.getInstance().setScreen(this.lastScreen);
}).width(98).build(), 1);
gridLayout.arrangeElements();
FrameLayout.alignInRectangle(gridLayout, 0, 0, this.width, this.height, 0.5f, 0.5f);
gridLayout.visitWidgets(this::addRenderableWidget);
}
@Override
public void onClose() {
this.minecraft.setScreen(this.lastScreen);
}
}
@@ -63,6 +63,10 @@ public class EditReplayScreen extends Screen {
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal(""), this.font), 2);
rowHelper.addChild(Button.builder(Component.literal("Combine With Other Replay"), button -> {
this.minecraft.setScreen(new CombineReplayScreen(this, this.replayPath, null, null));
}).width(204).build(), 2);
this.saveChangesButton = rowHelper.addChild(Button.builder(Component.literal("Save Changes"), button -> {
this.applyChanges();
}).width(98).build());
@@ -19,6 +19,7 @@ import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.client.gui.components.toasts.SystemToast;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.navigation.CommonInputs;
@@ -62,6 +63,7 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
private List<ReplaySummary> currentlyDisplayedReplays;
private String filter;
private final LoadingHeader loadingHeader;
private final LoadFromDeviceHeader loadFromDeviceHeader;
private Map<UUID, Long> lastOpenTimes = null;
@@ -69,6 +71,7 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
super(minecraft, i, j, k, l);
this.screen = selectReplayScreen;
this.loadingHeader = new LoadingHeader(minecraft);
this.loadFromDeviceHeader = new LoadFromDeviceHeader(minecraft);
this.filter = string;
this.pendingReplays = replaySelectionList != null ? replaySelectionList.pendingReplays : this.loadReplays();
this.handleNewReplays(this.pollReplaysIgnoreErrors());
@@ -239,6 +242,7 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
private void fillReplays(String string, List<ReplaySummary> list) {
this.clearEntries();
this.addEntry(this.loadFromDeviceHeader);
string = string.toLowerCase(Locale.ROOT);
for (ReplaySummary ReplaySummary : list) {
if (!this.filterAccepts(string, ReplaySummary)) continue;
@@ -253,6 +257,7 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
private void fillLoadingReplays() {
this.clearEntries();
this.addEntry(this.loadFromDeviceHeader);
this.addEntry(this.loadingHeader);
this.notifyListUpdated();
}
@@ -270,6 +275,11 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
@Override
public void setSelected(@Nullable Entry entry) {
if (entry instanceof LoadFromDeviceHeader) {
Flashback.openReplayFromFileBrowser();
return;
}
super.setSelected(entry);
if (entry instanceof ReplayListEntry replayListEntry) {
@@ -326,6 +336,31 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
}
}
public static class LoadFromDeviceHeader extends Entry {
private static final Component LOAD_REPLAY_LABEL = Component.translatable("flashback.select_replay.load_replay_from_file");
private static final WidgetSprites SPRITES = new WidgetSprites(ResourceLocation.withDefaultNamespace("widget/button"), ResourceLocation.withDefaultNamespace("widget/button_disabled"),
ResourceLocation.withDefaultNamespace("widget/button_highlighted"));
private final Minecraft minecraft;
public LoadFromDeviceHeader(Minecraft minecraft) {
this.minecraft = minecraft;
}
@Override
public void render(GuiGraphics guiGraphics, int index, int y, int x, int width, int height, int mouseX, int mouseY, boolean hovered, float partialTick) {
guiGraphics.blitSprite(SPRITES.get(true, hovered), x + 4, y + 2, width - 8, height - 4);
int p = (this.minecraft.screen.width - this.minecraft.font.width(LOAD_REPLAY_LABEL)) / 2;
int q = y + (height - this.minecraft.font.lineHeight) / 2 + 1;
guiGraphics.drawString(this.minecraft.font, LOAD_REPLAY_LABEL, p, q, 0xFFFFFF, true);
}
@Override
public Component getNarration() {
return LOAD_REPLAY_LABEL;
}
}
public final class ReplayListEntry extends Entry implements AutoCloseable {
private static final int ICON_WIDTH = 32;
private static final int ICON_HEIGHT = 32;
@@ -352,7 +387,7 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
}
@Override
public void render(GuiGraphics guiGraphics, int i, int j, int k, int l, int m, int n, int o, boolean bl, float f) {
public void render(GuiGraphics guiGraphics, int index, int y, int x, int width, int height, int mouseX, int mouseY, boolean hovered, float partialTick) {
String title = this.summary.getReplayName();
String fileAndTime = this.summary.getReplayId();
long p = this.summary.getLastModified();
@@ -360,7 +395,7 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
fileAndTime = fileAndTime + " (" + DATE_FORMAT.format(Instant.ofEpochMilli(p)) + ")";
}
if (StringUtils.isEmpty(title)) {
title = I18n.get("flashback.select_replay.replay") + " " + (i + 1);
title = I18n.get("flashback.select_replay.replay") + " " + (index + 1);
}
Component info = this.summary.getInfo();
@@ -371,21 +406,21 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
titleColour = 0xFFAA55;
}
guiGraphics.drawString(this.minecraft.font, title, k + ICON_WIDTH + 3, j + 1, titleColour, false);
guiGraphics.drawString(this.minecraft.font, fileAndTime, k + ICON_WIDTH + 3, j + this.minecraft.font.lineHeight + 3, 0xFF808080, false);
guiGraphics.drawString(this.minecraft.font, info, k + ICON_WIDTH + 3, j + this.minecraft.font.lineHeight + this.minecraft.font.lineHeight + 3, 0xFF808080, false);
guiGraphics.drawString(this.minecraft.font, title, x + ICON_WIDTH + 3, y + 1, titleColour, false);
guiGraphics.drawString(this.minecraft.font, fileAndTime, x + ICON_WIDTH + 3, y + this.minecraft.font.lineHeight + 3, 0xFF808080, false);
guiGraphics.drawString(this.minecraft.font, info, x + ICON_WIDTH + 3, y + this.minecraft.font.lineHeight + this.minecraft.font.lineHeight + 3, 0xFF808080, false);
RenderSystem.enableBlend();
guiGraphics.blit(this.icon.textureLocation(), k, j, 0.0f, 0.0f, 32, 32, 32, 32);
guiGraphics.blit(this.icon.textureLocation(), x, y, 0.0f, 0.0f, 32, 32, 32, 32);
RenderSystem.disableBlend();
if (this.minecraft.options.touchscreen().get() || bl) {
guiGraphics.fill(k, j, k + ICON_WIDTH, j + ICON_HEIGHT, -1601138544);
int q = n - k;
if (this.minecraft.options.touchscreen().get() || hovered) {
guiGraphics.fill(x, y, x + ICON_WIDTH, y + ICON_HEIGHT, -1601138544);
int q = mouseX - x;
boolean hoveredIcon = q < 32;
if (bl && this.summary.getHoverInfo() != null) {
guiGraphics.renderTooltip(this.minecraft.font, this.minecraft.font.split(this.summary.getHoverInfo(), 240), n, o);
if (hovered && this.summary.getHoverInfo() != null) {
guiGraphics.renderTooltip(this.minecraft.font, this.minecraft.font.split(this.summary.getHoverInfo(), 240), mouseX, mouseY);
}
ResourceLocation iconOverlay;
@@ -396,7 +431,7 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
} else {
iconOverlay = hoveredIcon ? JOIN_HIGHLIGHTED_SPRITE : JOIN_SPRITE;
}
guiGraphics.blitSprite(iconOverlay, k, j, ICON_WIDTH, ICON_HEIGHT);
guiGraphics.blitSprite(iconOverlay, x, y, ICON_WIDTH, ICON_HEIGHT);
}
}
@@ -459,41 +494,6 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
public void editReplay() {
Minecraft.getInstance().setScreen(new EditReplayScreen(Minecraft.getInstance().screen, this.summary));
// SystemToast.add(this.minecraft.getToasts(), new SystemToast.SystemToastId(3000),
// Component.literal("Sorry"), Component.literal("Editing replay data hasn't been implemented yet"));
// todo: implement editing replay data
// EditWorldScreen editWorldScreen;
// ReplayStorageSource.ReplayStorageAccess levelStorageAccess;
// this.queueLoadScreen();
// String string = this.summary.getReplayId();
// try {
// levelStorageAccess = this.minecraft.getReplaySource().validateAndCreateAccess;
// } catch (IOException iOException) {
// SystemToast.onWorldAccessFailure(this.minecraft, string);
// LOGGER.error("Failed to access level {}", (Object)string, (Object)iOException);
// ReplaySelectionList.this.reloadWorldList();
// return;
// } catch (ContentValidationException contentValidationException) {
// LOGGER.warn("{}", (Object)contentValidationException.getMessage());
// this.minecraft.setScreen(NoticeWithLinkScreen.createWorldSymlinkWarningScreen(() -> this.minecraft.setScreen(this.screen)));
// return;
// }
// try {
// editWorldScreen = EditWorldScreen.create(this.minecraft, levelStorageAccess, bl -> {
// levelStorageAccess.safeClose();
// if (bl) {
// ReplaySelectionList.this.reloadWorldList();
// }
// this.minecraft.setScreen(this.screen);
// });
// } catch (IOException | NbtException | ReportedNbtException exception) {
// levelStorageAccess.safeClose();
// SystemToast.onWorldAccessFailure(this.minecraft, string);
// LOGGER.error("Failed to load world data {}", (Object)string, (Object)exception);
// ReplaySelectionList.this.reloadWorldList();
// return;
// }
// this.minecraft.setScreen(editWorldScreen);
}
private void loadIcon() {