Skip to content
Open
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
31 changes: 25 additions & 6 deletions Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@
import net.countercraft.movecraft.util.Tags;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -187,6 +190,8 @@ public void onEnable() {

// Startup procedure
boolean datapackInitialized = isDatapackEnabled() || initializeDatapack();
if(!isEnabled())
return;
asyncManager = new AsyncManager();
asyncManager.runTaskTimer(this, 0, 1);
MapUpdateManager.getInstance().runTaskTimer(this, 0, 1);
Expand Down Expand Up @@ -253,12 +258,7 @@ public void onLoad() {
}

private boolean initializeDatapack() {
File datapackDirectory = null;
for(var world : getServer().getWorlds()) {
datapackDirectory = new File(world.getWorldFolder(), "datapacks");
if(datapackDirectory.exists())
break;
}
File datapackDirectory = getDatapackDirectory();
if(datapackDirectory == null) {
logger.severe("Failed to initialize Movecraft data pack due to first time world initialization.");
return false;
Expand Down Expand Up @@ -313,6 +313,25 @@ else if(new File(datapackDirectory, "movecraft-data.zip").exists()) {
return true;
}

private @Nullable File getDatapackDirectory() {
// Since 26.1, World#getWorldFolder() is the dimension folder, so ask for the level directory instead
try {
Path levelDirectory = (Path) Server.class.getMethod("getLevelDirectory").invoke(getServer());
return levelDirectory.resolve("datapacks").toFile();
}
catch (final NoSuchMethodException e) {
// Before 26.1, the main world's folder is the level directory
}
catch (final ReflectiveOperationException e) {
e.printStackTrace();
return null;
}
var worlds = getServer().getWorlds();
if(worlds.isEmpty())
return null;
return new File(worlds.get(0).getWorldFolder(), "datapacks");
}

private boolean isDatapackEnabled() {
getServer().dispatchCommand(getServer().createCommandSender(response -> {}), "datapack list"); // list datapacks to trigger the server to check
for (Datapack datapack : getServer().getDatapackManager().getPacks()) {
Expand Down