Skip to content

Commit

Permalink
Implemented #unloadWorld on a SlimeWorld object
Browse files Browse the repository at this point in the history
  • Loading branch information
Swofty-Developments committed Nov 23, 2023
1 parent 827362d commit 4d7f860
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 43 deletions.
2 changes: 1 addition & 1 deletion SLIME_FORMAT
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
“Slime” file format
2 bytes - magic = 0xB10B
// removed from Continued-Swofty-Manager: 1 byte (ubyte) - version, 1.8.8 = 0x01
1 byte (ubyte) - world version (see version list below)
// removed from Continued-Swofty-Manager: 1 byte (ubyte) - world version (see version list below)
2 bytes (short) - xPos of chunk lowest x & lowest z
2 bytes (short) - zPos
2 bytes (ushort) - width
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>net.swofty</groupId>
<artifactId>swoftyworldmanager</artifactId>
<packaging>pom</packaging>
<version>3.0.5</version>
<version>3.0.6</version>
<modules>
<module>swoftyworldmanager-api</module>
<module>swoftyworldmanager-nms</module>
Expand Down
2 changes: 1 addition & 1 deletion swoftyworldmanager-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>swoftyworldmanager</artifactId>
<groupId>net.swofty</groupId>
<version>3.0.5</version>
<version>3.0.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ public interface SlimeWorld {
*/
Collection<CompoundTag> getWorldMaps();

/**
* Standardized method to Bukkit unload.
*
* @param save Whether or not to save the world
* @param fallBack String name of the world to fall players back to
*/
void unloadWorld(boolean save, String fallBack);

/**
* Standardized method to Bukkit unload.
*
* @param save Whether or not to save the world
*/
default void unloadWorld(boolean save) {
unloadWorld(save, null);
}

/**
* Returns the properties of the world. These properties are automatically
* kept up-to-date when the world is loaded and its properties are updated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public class SlimeProperties {
*/
public static final SlimeProperty<Boolean> ALLOW_ANIMALS = new SlimePropertyBoolean("allowAnimals", true);

/**
* Whether the dragon battle should be enabled in end worlds
*/
public static final SlimeProperty<Boolean> DRAGON_BATTLE = new SlimePropertyBoolean("dragonBattle", false);

/**
* Whether PVP combat is allowed
*/
Expand All @@ -68,12 +63,6 @@ public class SlimeProperties {
|| value.equalsIgnoreCase("debug_all_block_states") || value.equalsIgnoreCase("default_1_1")
);

/**
* The default biome generated in empty chunks
*/
public static final SlimeProperty<String> DEFAULT_BIOME = new SlimePropertyString("defaultBiome", "minecraft:plains");


/**
* The default biome generated in empty chunks
*/
Expand Down
2 changes: 1 addition & 1 deletion swoftyworldmanager-classmodifier/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>swoftyworldmanager</artifactId>
<groupId>net.swofty</groupId>
<version>3.0.5</version>
<version>3.0.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion swoftyworldmanager-importer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>swoftyworldmanager</artifactId>
<groupId>net.swofty</groupId>
<version>3.0.5</version>
<version>3.0.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>swoftyworldmanager-importer</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion swoftyworldmanager-nms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>swoftyworldmanager</artifactId>
<groupId>net.swofty</groupId>
<version>3.0.5</version>
<version>3.0.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import net.swofty.swm.api.exceptions.UnknownWorldException;
import net.swofty.swm.api.exceptions.WorldAlreadyExistsException;
import net.swofty.swm.api.loaders.SlimeLoader;
import net.swofty.swm.api.utils.SlimeFormat;
import net.swofty.swm.api.world.SlimeChunk;
import net.swofty.swm.api.world.SlimeChunkSection;
import net.swofty.swm.api.world.SlimeWorld;
import net.swofty.swm.api.world.properties.SlimePropertyMap;
import org.bukkit.Difficulty;
import org.bukkit.*;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
Expand Down Expand Up @@ -54,6 +57,42 @@ public SlimeChunk getChunk(int x, int z) {
}
}

@Override
public void unloadWorld(boolean save, String fallBack) {
World world = Bukkit.getWorld(name);

// Teleport all players outside the world before unloading it
List<Player> players = world.getPlayers();

if (!players.isEmpty()) {
World fallbackWorld = null;
if (fallBack != null) {
fallbackWorld = Bukkit.getWorld(fallBack);
} else {
fallbackWorld = Bukkit.getWorlds().get(0);
}
Location spawnLocation = fallbackWorld.getSpawnLocation();

while (spawnLocation.getBlock().getType() != Material.AIR || spawnLocation.getBlock().getRelative(BlockFace.UP).getType() != Material.AIR) {
spawnLocation.add(0, 1, 0);
}

for (Player player : players) {
player.teleport(spawnLocation);
}
}

if (!Bukkit.unloadWorld(world, save)) {
throw new IllegalStateException("Failed to unload world " + name + ".");
} else {
try {
loader.unlockWorld(name);
} catch (UnknownWorldException | IOException e) {
throw new RuntimeException(e);
}
}
}

public void updateChunk(SlimeChunk chunk) {
if (!chunk.getWorldName().equals(getName())) {
throw new IllegalArgumentException("Chunk (" + chunk.getX() + ", " + chunk.getZ() + ") belongs to world '"
Expand Down
2 changes: 1 addition & 1 deletion swoftyworldmanager-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>swoftyworldmanager</artifactId>
<groupId>net.swofty</groupId>
<version>3.0.5</version>
<version>3.0.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>swoftyworldmanager-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package net.swofty.swm.plugin.commands.subtypes;

import lombok.SneakyThrows;
import net.swofty.swm.api.loaders.SlimeLoader;
import net.swofty.swm.api.world.SlimeWorld;
import net.swofty.swm.plugin.SWMPlugin;
import net.swofty.swm.plugin.commands.CommandCooldown;
import net.swofty.swm.plugin.commands.CommandParameters;
import net.swofty.swm.plugin.commands.CommandSource;
import net.swofty.swm.plugin.commands.SWMCommand;
import net.swofty.swm.plugin.config.ConfigManager;
import net.swofty.swm.plugin.config.WorldData;
import net.swofty.swm.plugin.config.WorldsConfig;
import net.swofty.swm.plugin.loader.LoaderUtils;
import net.swofty.swm.plugin.log.Logging;
import org.bukkit.*;
import org.bukkit.block.BlockFace;
Expand All @@ -26,34 +33,16 @@ public void run(CommandSource sender, String[] args) {
return;
}

World world = Bukkit.getWorld(args[0]);
WorldsConfig config = ConfigManager.getWorldConfig();
WorldData worldData = config.getWorlds().get(args[0]);

if (world == null) {
sender.send(Logging.COMMAND_PREFIX + ChatColor.RED + "World " + args[0] + " is not loaded!");
if (worldData == null || Bukkit.getWorld(args[0]) == null) {
sender.send(Logging.COMMAND_PREFIX + ChatColor.RED + "Unknown slime world " + args[0] + "! Are you sure you've typed it correctly?");
return;
}

// Teleport all players outside the world before unloading it
List<Player> players = world.getPlayers();

if (!players.isEmpty()) {
World defaultWorld = Bukkit.getWorlds().get(0);
Location spawnLocation = defaultWorld.getSpawnLocation();

while (spawnLocation.getBlock().getType() != Material.AIR || spawnLocation.getBlock().getRelative(BlockFace.UP).getType() != Material.AIR) {
spawnLocation.add(0, 1, 0);
}

for (Player player : players) {
player.teleport(spawnLocation);
}
}

if (Bukkit.unloadWorld(world, true)) {
sender.send(Logging.COMMAND_PREFIX + ChatColor.GREEN + "World " + ChatColor.YELLOW + args[0] + ChatColor.GREEN + " unloaded correctly.");
} else {
sender.send(Logging.COMMAND_PREFIX + ChatColor.RED + "Failed to unload world " + args[0] + ".");
}
SlimeWorld world = SWMPlugin.getInstance().getNms().getSlimeWorld(Bukkit.getWorld(args[0]));
world.unloadWorld(true);
}

@Override
Expand Down

0 comments on commit 4d7f860

Please sign in to comment.