added world loader
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -70,3 +70,4 @@ replay_pid* | |||||||
| # End of https://www.toptal.com/developers/gitignore/api/java | # End of https://www.toptal.com/developers/gitignore/api/java | ||||||
| local.gradle | local.gradle | ||||||
| config.yml | config.yml | ||||||
|  | resources | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ import eu.mhsl.craftattack.teamLobby.data.Team; | |||||||
| import eu.mhsl.craftattack.teamLobby.util.CommonHandler; | import eu.mhsl.craftattack.teamLobby.util.CommonHandler; | ||||||
| import eu.mhsl.craftattack.teamLobby.util.PluginMessageUtil; | import eu.mhsl.craftattack.teamLobby.util.PluginMessageUtil; | ||||||
| import eu.mhsl.craftattack.teamLobby.util.PosSerializer; | import eu.mhsl.craftattack.teamLobby.util.PosSerializer; | ||||||
|  | import eu.mhsl.craftattack.teamLobby.util.ResourceExtractor; | ||||||
| import net.kyori.adventure.sound.Sound; | import net.kyori.adventure.sound.Sound; | ||||||
| import net.kyori.adventure.text.Component; | import net.kyori.adventure.text.Component; | ||||||
| import net.kyori.adventure.text.format.NamedTextColor; | 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.PlayerBlockBreakEvent; | ||||||
| import net.minestom.server.event.player.PlayerBlockInteractEvent; | import net.minestom.server.event.player.PlayerBlockInteractEvent; | ||||||
| import net.minestom.server.instance.InstanceContainer; | import net.minestom.server.instance.InstanceContainer; | ||||||
|  | import net.minestom.server.instance.anvil.AnvilLoader; | ||||||
| import net.minestom.server.instance.block.Block; | import net.minestom.server.instance.block.Block; | ||||||
| import net.minestom.server.network.packet.server.play.ParticlePacket; | import net.minestom.server.network.packet.server.play.ParticlePacket; | ||||||
| import net.minestom.server.particle.Particle; | import net.minestom.server.particle.Particle; | ||||||
| @@ -30,6 +32,7 @@ import net.minestom.server.utils.NamespaceID; | |||||||
| import net.minestom.server.world.DimensionType; | import net.minestom.server.world.DimensionType; | ||||||
| import org.spongepowered.configurate.ConfigurationNode; | import org.spongepowered.configurate.ConfigurationNode; | ||||||
|  |  | ||||||
|  | import java.nio.file.Path; | ||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
| import java.util.function.Consumer; | import java.util.function.Consumer; | ||||||
| import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||||
| @@ -86,7 +89,9 @@ public class Lobby extends InstanceContainer { | |||||||
|             TaskSchedule.millis(100) |             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); |         this.setBlock(this.spawnPoint.sub(0, 1 ,0), Block.DIAMOND_BLOCK); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -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); | ||||||
|  |                     } | ||||||
|  |                 }); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.-1.-1.mca
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.-1.-1.mca
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.-1.0.mca
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.-1.0.mca
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.0.-1.mca
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.0.-1.mca
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.0.0.mca
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main/resources/world/region/r.0.0.mca
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user