Skip to content
Merged
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 @@ -24,6 +24,7 @@
import org.mvplugins.multiverse.core.config.node.NodeGroup;
import org.mvplugins.multiverse.core.economy.MVEconomist;
import org.mvplugins.multiverse.core.utils.MaterialConverter;
import org.mvplugins.multiverse.core.utils.text.ChatTextFormatter;
import org.mvplugins.multiverse.core.world.helpers.EnforcementHandler;
import org.mvplugins.multiverse.core.world.location.NullSpawnLocation;
import org.mvplugins.multiverse.core.world.location.SpawnLocation;
Expand Down Expand Up @@ -90,6 +91,8 @@ private <T> ConfigNode<T> node(ConfigNode.Builder<T, ?> nodeBuilder) {
.onLoadAndChange((oldValue, newValue) -> {
if (world == null) return;
world.updateColourlessAlias();
worldManager.getWorldStore().changeAlias(
ChatTextFormatter.removeColor(oldValue), ChatTextFormatter.removeColor(newValue), world);
}));

final ConfigNode<Boolean> allowAdvancementGrant = node(ConfigNode.builder("allow-advancement-grant", Boolean.class)
Expand Down
112 changes: 46 additions & 66 deletions src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import org.jvnet.hk2.annotations.Service;

import org.mvplugins.multiverse.core.config.CoreConfig;
Expand All @@ -47,15 +48,13 @@
import org.mvplugins.multiverse.core.permissions.CorePermissions;
import org.mvplugins.multiverse.core.teleportation.BlockSafety;
import org.mvplugins.multiverse.core.teleportation.LocationManipulation;
import org.mvplugins.multiverse.core.utils.CaseInsensitiveStringMap;
import org.mvplugins.multiverse.core.utils.ServerProperties;
import org.mvplugins.multiverse.core.utils.compatibility.BukkitCompatibility;
import org.mvplugins.multiverse.core.utils.compatibility.WorldCompatibility;
import org.mvplugins.multiverse.core.utils.compatibility.WorldCreatorCompatibility;
import org.mvplugins.multiverse.core.utils.result.Attempt;
import org.mvplugins.multiverse.core.utils.result.FailureReason;
import org.mvplugins.multiverse.core.utils.FileUtils;
import org.mvplugins.multiverse.core.utils.text.ChatTextFormatter;
import org.mvplugins.multiverse.core.world.biomeprovider.BiomeProviderFactory;
import org.mvplugins.multiverse.core.world.entity.EntityPurger;
import org.mvplugins.multiverse.core.world.generators.GeneratorProvider;
Expand Down Expand Up @@ -96,8 +95,7 @@
"data/paper/metadata.dat" // New papermc format for 26.1+
);

private final CaseInsensitiveStringMap<MultiverseWorld> worldsMap;
private final CaseInsensitiveStringMap<LoadedMultiverseWorld> loadedWorldsMap;
private final WorldStore worldStore;
private final List<String> unloadTracker;
private final List<String> loadTracker;
private final WorldsConfigManager worldsConfigManager;
Expand All @@ -115,6 +113,7 @@

@Inject
WorldManager(
@NotNull WorldStore worldStore,
@NotNull WorldsConfigManager worldsConfigManager,
@NotNull WorldNameChecker worldNameChecker,
@NotNull BiomeProviderFactory biomeProviderFactory,
Expand All @@ -127,6 +126,7 @@
@NotNull ServerProperties serverProperties,
@NotNull CoreConfig config,
@NotNull EntityPurger entityPurger) {
this.worldStore = worldStore;
this.worldsConfigManager = worldsConfigManager;
this.worldNameChecker = worldNameChecker;
this.biomeProviderFactory = biomeProviderFactory;
Expand All @@ -140,8 +140,6 @@
this.config = config;
this.entityPurger = entityPurger;

this.worldsMap = new CaseInsensitiveStringMap<>();
this.loadedWorldsMap = new CaseInsensitiveStringMap<>();
this.unloadTracker = new ArrayList<>();
this.loadTracker = new ArrayList<>();
}
Expand Down Expand Up @@ -173,9 +171,13 @@
}

private void loadNewWorldConfigs(Collection<WorldConfig> newWorldConfigs) {
newWorldConfigs.forEach(worldConfig -> Option.of(worldsMap.get(worldConfig.getWorldKeyOrName().usableName()))
.peek(unloadedWorld -> unloadedWorld.setWorldConfig(worldConfig))
.onEmpty(() -> newMultiverseWorld(worldConfig)));
newWorldConfigs.forEach(worldConfig -> {
worldStore.getUnloadedWorldRef(worldConfig.getWorldKeyOrName().usableName())
.peek(unloadedWorld -> unloadedWorld.setWorldConfig(worldConfig))
.onEmpty(() -> newMultiverseWorld(worldConfig));
worldStore.getLoadedWorld(worldConfig.getWorldKeyOrName().usableName())
.peek(loadedWorld -> loadedWorld.setWorldConfig(worldConfig));
});
}

private void removeWorldsNotInConfigs(Collection<WorldKeyOrName> removedWorlds) {
Expand Down Expand Up @@ -387,7 +389,7 @@

private MultiverseWorld newMultiverseWorld(WorldConfig worldConfig) {
MultiverseWorld mvWorld = new MultiverseWorld(worldConfig, config);
worldsMap.put(mvWorld.getName(), mvWorld);
worldStore.putUnloadedWorld(mvWorld);
corePermissions.addWorldPermissions(mvWorld);
return mvWorld;
}
Expand Down Expand Up @@ -425,7 +427,7 @@
locationManipulation,
entityPurger
);
loadedWorldsMap.put(loadedWorld.getName(), loadedWorld);
worldStore.putLoadedWorld(loadedWorld);
saveWorldsConfig();
pluginManager.callEvent(new MVWorldLoadedEvent(loadedWorld));
return loadedWorld;
Expand Down Expand Up @@ -568,7 +570,7 @@
locationManipulation,
entityPurger
);
loadedWorldsMap.put(loadedWorld.getName(), loadedWorld);
worldStore.putLoadedWorld(loadedWorld);

Check warning on line 573 in src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Return count is 4 (max allowed for non-void methods/lambdas is 2). Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java:573:5: info: Return count is 4 (max allowed for non-void methods/lambdas is 2). (com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck)
saveWorldsConfig();
pluginManager.callEvent(new MVWorldLoadedEvent(loadedWorld));
return Attempt.success(loadedWorld);
Expand Down Expand Up @@ -602,12 +604,10 @@
success -> removeLoadedMultiverseWorld(world));
}

private Attempt<MultiverseWorld, UnloadFailureReason> removeLoadedMultiverseWorld(@NotNull LoadedMultiverseWorld world) {
MultiverseWorld mvWorld = Objects.requireNonNull(loadedWorldsMap.remove(world.getName()),
"For some reason, the loaded world isn't in the map... BUGGG");
Logging.fine("Removed MultiverseWorld from map: " + world.getName());
var unloadedWorld = Objects.requireNonNull(worldsMap.get(world.getName()),
"For some reason, the unloaded world isn't in the map... BUGGG");
private Attempt<MultiverseWorld, UnloadFailureReason> removeLoadedMultiverseWorld(@NotNull LoadedMultiverseWorld mvWorld) {
MultiverseWorld unloadedWorld = worldStore.getUnloadedWorldRef(mvWorld.getKey().toString()).getOrElseThrow(
() -> new IllegalStateException("Unloaded ref of world not found: " + mvWorld));
worldStore.removeLoadedWorld(mvWorld);
mvWorld.getWorldConfig().setMVWorld(unloadedWorld);
pluginManager.callEvent(new MVWorldUnloadedEvent(mvWorld));
return worldActionResult(unloadedWorld);
Expand Down Expand Up @@ -697,7 +697,7 @@
*/
private Attempt<String, RemoveFailureReason> removeWorldFromConfig(@NotNull MultiverseWorld world) {
// Remove world from config
worldsMap.remove(world.getName());
worldStore.removeWorld(world);
world.getWorldConfig().deferenceMVWorld();
worldsConfigManager.deleteWorldConfig(world.getKey());
saveWorldsConfig();
Expand Down Expand Up @@ -1054,9 +1054,7 @@
* @return The world if it exists.
*/
public Option<MultiverseWorld> getWorld(@Nullable String worldName) {
return getLoadedWorld(worldName)
.map(world -> (MultiverseWorld) world)
.orElse(() -> getUnloadedWorld(worldName));
return worldStore.getWorld(worldName);
}

/**
Expand All @@ -1067,9 +1065,7 @@
* @return The world if it exists.
*/
public Option<MultiverseWorld> getWorldByNameOrAlias(@Nullable String worldNameOrAlias) {
return getLoadedWorldByNameOrAlias(worldNameOrAlias)
.map(world -> (MultiverseWorld) world)
.orElse(() -> getUnloadedWorldByNameOrAlias(worldNameOrAlias));
return getWorld(worldStore.translateAlias(worldNameOrAlias));
}

/**
Expand All @@ -1079,14 +1075,15 @@
* <p>If you want only unloaded worlds, use {@link #getUnloadedWorlds()}. If you want only loaded worlds, use
* {@link #getLoadedWorlds()}.</p>
*
* <p>Note that this is an unmodifiable copy of the current worlds. It will not update as worlds are added/removed.
* Call it everytime you need the most updated list of worlds.</p>
*
* @return A list of all worlds that may or may not be loaded.
*/
@Unmodifiable
@NotNull
public Collection<MultiverseWorld> getWorlds() {
return worldsMap.values().stream()
.map(world -> getLoadedWorld(world)
.map(loadedWorld -> (MultiverseWorld) loadedWorld)
.getOrElse(world))
.toList();
return worldStore.getWorlds();
}

/**
Expand All @@ -1096,7 +1093,7 @@
* @return True if the world is a world is known to multiverse, but may or may not be loaded.
*/
public boolean isWorld(@Nullable String worldName) {
return worldName != null && worldsMap.containsKey(worldName);
return worldName != null && worldStore.getWorld(worldName).isDefined();
}

/**
Expand All @@ -1106,9 +1103,7 @@
* @return The world if it exists.
*/
public Option<MultiverseWorld> getUnloadedWorld(@Nullable String worldName) {
return isLoadedWorld(worldName)
? Option.none()
: Option.of(worldName).flatMap(name -> Option.of(worldsMap.get(name)));
return worldStore.getUnloadedWorld(worldName);
}

/**
Expand All @@ -1118,29 +1113,18 @@
* @return The world if it exists.
*/
public Option<MultiverseWorld> getUnloadedWorldByNameOrAlias(@Nullable String worldNameOrAlias) {
return getUnloadedWorld(worldNameOrAlias).orElse(() -> getUnloadedWorldByAlias(worldNameOrAlias));
}

private Option<MultiverseWorld> getUnloadedWorldByAlias(@Nullable String alias) {
if (alias == null || alias.isEmpty()) {
return Option.none();
}
String colourlessAlias = ChatTextFormatter.removeColor(alias);
return Option.ofOptional(worldsMap.values().stream()
.filter(world -> !world.isLoaded())
.filter(world -> world.getColourlessAlias().equalsIgnoreCase(colourlessAlias))
.findFirst());
return getUnloadedWorld(worldStore.translateAlias(worldNameOrAlias));
}

/**
* Get a list of all worlds that are not loaded.
*
* @return A list of all worlds that are not loaded.
*/
@Unmodifiable
@NotNull
public Collection<MultiverseWorld> getUnloadedWorlds() {
return worldsMap.values().stream()
.filter(world -> !world.isLoaded())
.toList();
return worldStore.getUnloadedWorlds();
}

/**
Expand Down Expand Up @@ -1180,8 +1164,7 @@
* @return The multiverse world if it exists.
*/
public Option<LoadedMultiverseWorld> getLoadedWorld(@Nullable String worldName) {
return Option.of(worldName)
.flatMap(name -> Option.of(loadedWorldsMap.get(name)));
return Option.of(worldName).flatMap(worldStore::getLoadedWorld);
}

/**
Expand All @@ -1191,28 +1174,21 @@
* @return The multiverse world if it exists.
*/
public Option<LoadedMultiverseWorld> getLoadedWorldByNameOrAlias(@Nullable String worldNameOrAlias) {
return getLoadedWorld(worldNameOrAlias)
.orElse(() -> getLoadedWorldByAlias(worldNameOrAlias));
}

private Option<LoadedMultiverseWorld> getLoadedWorldByAlias(@Nullable String alias) {
if (alias == null || alias.isEmpty()) {
return Option.none();
}
return Option.ofOptional(loadedWorldsMap.values().stream()
.filter(world -> world.getColourlessAlias()
.equalsIgnoreCase(ChatTextFormatter.removeColor(alias)))
.findFirst());
return getLoadedWorld(worldStore.translateAlias(worldNameOrAlias));
}

/**
* Get a read-only list of all multiverse worlds that are loaded.
*
* <p>Note that this is an unmodifiable copy of the current worlds. It will not update as worlds are added/removed.
* Call it everytime you need the most updated list of worlds.</p>
*
* @return A list of all multiverse worlds that are loaded.
*/
@Unmodifiable
@NotNull
public Collection<LoadedMultiverseWorld> getLoadedWorlds() {
return loadedWorldsMap.values().stream()
.toList();
return worldStore.getLoadedWorlds();
}

/**
Expand Down Expand Up @@ -1242,7 +1218,7 @@
* @return True if the world is a multiverse world that is loaded.
*/
public boolean isLoadedWorld(@Nullable String worldName) {
return worldName != null && loadedWorldsMap.containsKey(worldName);
return worldName != null && worldStore.getLoadedWorld(worldName).isDefined();
}

/**
Expand Down Expand Up @@ -1271,6 +1247,10 @@
});
}

WorldStore getWorldStore() {

Check warning on line 1250 in src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 '}' at column 83 should be alone on a line. Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java:1250:83: warning: '}' at column 83 should be alone on a line. (RightCurlyAlone)
return worldStore;
}

/**
* A simple pair wrapper for convenience to pass both the world key or name and the options together between methods
* when parsing world options.
Expand Down
Loading
Loading