Skip to content

Commit

Permalink
Update world settings after resetting map
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Sep 7, 2023
1 parent 4f01da3 commit 5557cb1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,6 +56,7 @@ private void executeResetMap(final CommandContext<Commander> 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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<WorldIdentifier, BlockPos> lastUpdateWorlds = null;
private @MonotonicNonNull Map<WorldIdentifier, ChangingData> lastUpdate = null;

@Inject
private UpdateWorldData(
Expand All @@ -42,22 +41,23 @@ private UpdateWorldData(

@Override
public void run() {
if (this.lastUpdateWorlds != null) {
final Map<WorldIdentifier, BlockPos> 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<WorldIdentifier, ChangingData> 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<Object> 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);
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 5557cb1

Please sign in to comment.