Skip to content

Commit

Permalink
added more api stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Apr 27, 2024
1 parent 89e5f03 commit 9abc681
Show file tree
Hide file tree
Showing 17 changed files with 624 additions and 309 deletions.
11 changes: 8 additions & 3 deletions api/src/main/java/net/thenextlvl/worlds/image/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
public record Generator(String plugin, @Nullable String id) {

@Nullable
@SuppressWarnings("UnstableApiUsage")
public static Generator of(World world) {
var plugin = getGeneratorPlugin(world);
return plugin != null ? new Generator(plugin.getName(), null) : null;
}

@Nullable
@SuppressWarnings("UnstableApiUsage")
public static Plugin getGeneratorPlugin(World world) {
if (world.getGenerator() == null) return null;
var loader = world.getGenerator().getClass().getClassLoader();
if (!(loader instanceof PluginClassLoader pluginLoader)) return null;
if (pluginLoader.getPlugin() == null) return null;
return new Generator(pluginLoader.getPlugin().getName(), null);
return pluginLoader.getPlugin();
}

public static boolean hasChunkGenerator(Class<? extends Plugin> clazz) {
Expand Down
146 changes: 11 additions & 135 deletions api/src/main/java/net/thenextlvl/worlds/image/Image.java
Original file line number Diff line number Diff line change
@@ -1,156 +1,32 @@
package net.thenextlvl.worlds.image;

import core.file.FileIO;
import core.io.IO;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.*;
public interface Image {

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Image {
private static final Map<UUID, Image> images = new HashMap<>();
private final FileIO<WorldImage> file;
private final World world;
Image save();

private Image(World world, WorldImage image) {
this(WorldImage.loadFile(IO.of(Bukkit.getWorldContainer(), image.name() + ".image"), image), world);
}

private Image(World world) {
this(world, WorldImage.of(world));
}

private Image register() {
images.put(getWorld().getUID(), this);
return this;
}

public Image save() {
file.save();
return this;
}

public WorldImage getWorldImage() {
return getFile().getRoot();
}

public boolean canUnload() {
return !Bukkit.isTickingWorlds() && world.getPlayers().isEmpty();
}

public boolean unload() {
return canUnload() && Bukkit.unloadWorld(world, world.isAutoSave());
}

public DeleteResult delete(boolean keepImage, boolean keepWorld, boolean schedule) {
return schedule ? deleteOnShutdown(keepImage, keepWorld) : deleteImmediately(keepImage, keepWorld);
}

public DeleteResult deleteOnShutdown(boolean keepImage, boolean keepWorld) {
if (keepWorld && (keepImage || !getFile().getIO().exists()))
return DeleteResult.WORLD_DELETE_NOTHING;

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (!keepWorld) delete(getWorld().getWorldFolder());
if (!keepImage) try {
getFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
}));

return DeleteResult.WORLD_DELETE_SCHEDULED;
}

public DeleteResult deleteImmediately(boolean keepImage, boolean keepWorld) {
if (getWorld().getKey().toString().equals("minecraft:overworld"))
return DeleteResult.WORLD_DELETE_ILLEGAL;

var fallback = Bukkit.getWorlds().get(0).getSpawnLocation();
getWorld().getPlayers().forEach(player -> player.teleport(fallback));

try {
if (!keepImage && file.getIO().exists() && !file.delete())
return DeleteResult.IMAGE_DELETE_FAILED;
} catch (IOException e) {
return DeleteResult.IMAGE_DELETE_FAILED;
}

if (keepImage && keepWorld)
return Bukkit.unloadWorld(world, world.isAutoSave())
? DeleteResult.WORLD_UNLOADED
: DeleteResult.WORLD_UNLOAD_FAILED;

if (!Bukkit.unloadWorld(world, world.isAutoSave() && keepWorld))
return DeleteResult.WORLD_UNLOAD_FAILED;

if (!keepWorld && !delete(world.getWorldFolder()))
return DeleteResult.WORLD_DELETE_FAILED;

return DeleteResult.WORLD_DELETED;
}
World getWorld();

private boolean delete(File file) {
if (file.isFile()) return file.delete();
var files = file.listFiles();
if (files == null) return false;
for (var file1 : files) delete(file1);
return file.delete();
}
WorldImage getWorldImage();

@Nullable
public static Image load(@Nullable WorldImage image) {
if (image == null) return null;
if (Bukkit.getWorld(image.name()) != null) return null;
var build = image.build();
return build != null ? new Image(build, image).save().register() : null;
}
boolean unload();

@Nullable
public static Image get(World world) {
return images.get(world.getUID());
}
boolean canUnload();

public static Image getOrCreate(World world) {
if (!images.containsKey(world.getUID()))
return new Image(world).save().register();
return images.get(world.getUID());
}
boolean canDelete();

public static Image getOrDefault(World world) {
return images.getOrDefault(world.getUID(), new Image(world));
}
DeleteResult delete(boolean keepImage, boolean keepWorld, boolean schedule);

public static List<File> findWorlds() {
var files = Bukkit.getWorldContainer().listFiles(file ->
file.isDirectory() && new File(file, "level.dat").isFile());
return files != null ? Arrays.asList(files) : Collections.emptyList();
}

public static List<File> findImageFiles() {
var files = Bukkit.getWorldContainer().listFiles(file ->
file.isFile() && file.getName().endsWith(".image"));
return files != null ? Arrays.asList(files) : Collections.emptyList();
}
DeleteResult deleteNow(boolean keepImage, boolean keepWorld);

public static List<WorldImage> findImages() {
return findImageFiles().stream()
.map(WorldImage::of)
.filter(Objects::nonNull)
.toList();
}
DeleteResult scheduleDeletion(boolean keepImage, boolean keepWorld);

@Getter
@RequiredArgsConstructor
public enum DeleteResult {
enum DeleteResult {
WORLD_DELETE_SCHEDULED("world.delete.scheduled"),
WORLD_DELETE_ILLEGAL("world.delete.disallowed"),
WORLD_DELETE_NOTHING("world.delete.nothing"),
Expand Down
35 changes: 35 additions & 0 deletions api/src/main/java/net/thenextlvl/worlds/image/ImageProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.thenextlvl.worlds.image;

import org.bukkit.World;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.List;

public interface ImageProvider {

@Nullable
Image load(@Nullable WorldImage image);

@Nullable
Image get(World world);

void register(Image image);

Image getOrCreate(World world);

Image getOrDefault(World world);

List<File> findImageFiles();

List<File> findWorldFiles();

List<WorldImage> findImages();

WorldImage createWorldImage();

WorldImage of(World world);

@Nullable
WorldImage of(File file);
}
Loading

0 comments on commit 9abc681

Please sign in to comment.