diff --git a/common/src/main/java/xyz/jpenilla/squaremap/common/command/commands/ResetMapCommand.java b/common/src/main/java/xyz/jpenilla/squaremap/common/command/commands/ResetMapCommand.java index 771647d1..cfaa6a1c 100644 --- a/common/src/main/java/xyz/jpenilla/squaremap/common/command/commands/ResetMapCommand.java +++ b/common/src/main/java/xyz/jpenilla/squaremap/common/command/commands/ResetMapCommand.java @@ -9,26 +9,31 @@ import net.minecraft.server.level.ServerLevel; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; +import xyz.jpenilla.squaremap.common.WorldManager; import xyz.jpenilla.squaremap.common.command.Commander; import xyz.jpenilla.squaremap.common.command.Commands; import xyz.jpenilla.squaremap.common.command.SquaremapCommand; import xyz.jpenilla.squaremap.common.command.argument.LevelArgument; import xyz.jpenilla.squaremap.common.config.Messages; import xyz.jpenilla.squaremap.common.data.DirectoryProvider; +import xyz.jpenilla.squaremap.common.data.MapWorldInternal; import xyz.jpenilla.squaremap.common.util.Components; import xyz.jpenilla.squaremap.common.util.FileUtil; @DefaultQualifier(NonNull.class) public final class ResetMapCommand extends SquaremapCommand { private final DirectoryProvider directoryProvider; + private final WorldManager worldManager; @Inject private ResetMapCommand( final Commands commands, - final DirectoryProvider directoryProvider + final DirectoryProvider directoryProvider, + final WorldManager worldManager ) { super(commands); this.directoryProvider = directoryProvider; + this.worldManager = worldManager; } @Override @@ -51,6 +56,7 @@ private void executeResetMap(final CommandContext context) { } catch (final IOException ex) { throw new RuntimeException("Could not reset map for level '" + world.dimension().location() + "'", ex); } + this.worldManager.getWorldIfEnabled(world).ifPresent(MapWorldInternal::didReset); sender.sendMessage(Messages.SUCCESSFULLY_RESET_MAP.withPlaceholders(Components.worldPlaceholder(world))); } } diff --git a/common/src/main/java/xyz/jpenilla/squaremap/common/data/MapWorldInternal.java b/common/src/main/java/xyz/jpenilla/squaremap/common/data/MapWorldInternal.java index 73a63cca..72c8d5bf 100644 --- a/common/src/main/java/xyz/jpenilla/squaremap/common/data/MapWorldInternal.java +++ b/common/src/main/java/xyz/jpenilla/squaremap/common/data/MapWorldInternal.java @@ -50,6 +50,7 @@ public abstract class MapWorldInternal implements MapWorld { private final BlockColors blockColors; private final LevelBiomeColorData levelBiomeColorData; private final VisibilityLimitImpl visibilityLimit; + private volatile long lastReset = -1; protected MapWorldInternal( final ServerLevel level, @@ -213,6 +214,14 @@ private void deserializeDirtyChunks() { this.modifiedChunks.addAll(deserialized); } + public void didReset() { + this.lastReset = System.currentTimeMillis(); + } + + public long lastReset() { + return this.lastReset; + } + public interface Factory { MapWorldInternal create(ServerLevel level); } diff --git a/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdateWorldData.java b/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdateWorldData.java index 2d353e24..f2831f1c 100644 --- a/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdateWorldData.java +++ b/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdateWorldData.java @@ -28,8 +28,7 @@ public final class UpdateWorldData implements Runnable { private final WorldManager worldManager; private final DirectoryProvider directoryProvider; - // spawn position is the only thing that needs updating until a /map reload - if this changes the value needs to be updated - private @MonotonicNonNull Map lastUpdateWorlds = null; + private @MonotonicNonNull Map lastUpdate = null; @Inject private UpdateWorldData( @@ -42,22 +41,23 @@ private UpdateWorldData( @Override public void run() { - if (this.lastUpdateWorlds != null) { - final Map current = this.worldManager.worlds().stream() - .collect(Collectors.toMap(MapWorld::identifier, w -> w.serverLevel().getSharedSpawnPos())); - if (this.lastUpdateWorlds.equals(current)) { + if (this.lastUpdate != null) { + final Map current = this.worldManager.worlds().stream() + .collect(Collectors.toMap(MapWorld::identifier, ChangingData::snapshot)); + if (this.lastUpdate.equals(current)) { return; } - this.lastUpdateWorlds.clear(); + this.lastUpdate.clear(); } else { - this.lastUpdateWorlds = new HashMap<>(); + this.lastUpdate = new HashMap<>(); } List worlds = new ArrayList<>(); this.worldManager.worlds().forEach(mapWorld -> { + this.lastUpdate.put(mapWorld.identifier(), ChangingData.snapshot(mapWorld)); + final ServerLevel level = mapWorld.serverLevel(); - this.lastUpdateWorlds.put(mapWorld.identifier(), level.getSharedSpawnPos()); final WorldConfig worldConfig = mapWorld.config(); this.writeWorldSettings(mapWorld, level, worldConfig); @@ -154,4 +154,10 @@ private static String environment(final ServerLevel level) { } return "custom"; } + + private record ChangingData(BlockPos spawn, long lastReset) { + static ChangingData snapshot(final MapWorldInternal world) { + return new ChangingData(world.serverLevel().getSharedSpawnPos(), world.lastReset()); + } + } }