added world loader

This commit is contained in:
Elias Müller 2025-04-17 12:06:43 +02:00
parent 3301a6d9dd
commit d37af4555f
7 changed files with 71 additions and 1 deletions

1
.gitignore vendored
View File

@ -70,3 +70,4 @@ replay_pid*
# End of https://www.toptal.com/developers/gitignore/api/java
local.gradle
config.yml
resources

View File

@ -4,6 +4,7 @@ import eu.mhsl.craftattack.teamLobby.data.Team;
import eu.mhsl.craftattack.teamLobby.util.CommonHandler;
import eu.mhsl.craftattack.teamLobby.util.PluginMessageUtil;
import eu.mhsl.craftattack.teamLobby.util.PosSerializer;
import eu.mhsl.craftattack.teamLobby.util.ResourceExtractor;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
@ -18,6 +19,7 @@ import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
import net.minestom.server.event.player.PlayerBlockBreakEvent;
import net.minestom.server.event.player.PlayerBlockInteractEvent;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.anvil.AnvilLoader;
import net.minestom.server.instance.block.Block;
import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle;
@ -30,6 +32,7 @@ import net.minestom.server.utils.NamespaceID;
import net.minestom.server.world.DimensionType;
import org.spongepowered.configurate.ConfigurationNode;
import java.nio.file.Path;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@ -86,7 +89,9 @@ public class Lobby extends InstanceContainer {
TaskSchedule.millis(100)
);
this.setGenerator(unit -> unit.modifier().fillHeight(0, 10, Block.BAMBOO_BLOCK));
Path worldPath = Path.of(ResourceExtractor.commonRessourcePath.toString(), "world");
ResourceExtractor.extractResourceFolder("/world", worldPath);
this.setChunkLoader(new AnvilLoader(worldPath));
this.setBlock(this.spawnPoint.sub(0, 1 ,0), Block.DIAMOND_BLOCK);
}

View File

@ -0,0 +1,64 @@
package eu.mhsl.craftattack.teamLobby.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.*;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ResourceExtractor {
public static final Path commonRessourcePath = Path.of("resources");
public static void extractResourceFolder(String resourcePath, Path outputDir) {
try {
handle(resourcePath, outputDir);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
private static void handle(String resourcePath, Path outputDir) throws URISyntaxException, IOException {
URL resourceURL = ResourceExtractor.class.getResource(resourcePath);
if (resourceURL == null) {
throw new IllegalArgumentException("Resource path not found: " + resourcePath);
}
if (resourceURL.getProtocol().equals("jar")) {
String jarPath = resourceURL.getPath().substring(5, resourceURL.getPath().indexOf("!"));
try (JarFile jar = new JarFile(Paths.get(jarPath).toFile())) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.startsWith(resourcePath.substring(1) + "/") && !entry.isDirectory()) {
InputStream is = jar.getInputStream(entry);
Path outPath = outputDir.resolve(name.substring(resourcePath.length()));
Files.createDirectories(outPath.getParent());
Files.copy(is, outPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
} else {
Path resourceDir = Paths.get(resourceURL.toURI());
try(var files = Files.walk(resourceDir)) {
files.forEach(source -> {
try {
Path destination = outputDir.resolve(resourceDir.relativize(source).toString());
if (Files.isDirectory(source)) {
Files.createDirectories(destination);
} else {
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.