Add utility method for getting a live World from WorldProperties.
Should make it a lot easier for developers, especially operating in commands, to get an actual ServerWorld without needing to perform this logic themselves.
package org.spongepowered.api.world.storage;
import org.checkerframework.checker.nullness.qual.Nullable;
+import org.spongepowered.api.Sponge;
import org.spongepowered.api.boss.BossBar;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.world.dimension.DimensionType;
import org.spongepowered.api.world.gamerule.GameRuleHolder;
import org.spongepowered.api.world.gen.GeneratorType;
+import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.api.world.teleport.PortalAgentType;
import org.spongepowered.api.world.weather.WeatherUniverse;
import org.spongepowered.math.vector.Vector3i;
*/
public interface WorldProperties extends WeatherUniverse, Identifiable, GameRuleHolder {
+ /**
+ * Gets the {@link ServerWorld} that correlates to this properties, if available.
+ *
+ * <p>The rules are that the world must be loaded and it's {@link World#getUniqueId()} matches
+ * this properties' {@link #getUniqueId()}. Lastly, the properties of that world and this properties
+ * must be reference equal.</p>
+ * @return The world or {@link Optional#empty()} otherwise
+ */
+ default Optional<ServerWorld> getWorld() {
+ final Optional<ServerWorld> potentialWorld = Sponge.getServer().getWorldManager().getWorld(this.getUniqueId());
+ if (!potentialWorld.isPresent()) {
+ return Optional.empty();
+ }
+
+ final ServerWorld serverWorld = potentialWorld.get();
+ if (serverWorld.getProperties() == this) {
+ return Optional.of(serverWorld);
+ }
+
+ return Optional.empty();
+ }
+
/**
* Gets if this has been initialized.
*
