Solved Ideas on how to reset a map?

Discussion in 'Plugin Development' started by Tim_M, Aug 11, 2020.

  1. Offline

    Tim_M

    I'm making a skywars plugin, and I have been stuck for a while now on a feature that resets the map after people had played there. And I know I could create a schematic and put it in a folder, so it would get pasted with WorldEdit API. Or I could create a world with the map and use that.

    But I want an automatic system that doesnt lag on small servers and only needs a pos1 and pos2.
    I'm not here for spoon feeding, only ideas and possibly simple examples.

    If you have any ideas please leave an answer.
    Thank you in advance :p
     
  2. Offline

    pietermantel

    What you can do is have a template folder on the server (named something like 'template') which has the non-used map and when resetting, delete the old world folder, copy the one named 'template' and rename it to the name of the old world. This has the downside of resetting the entire map and not just a portion.
    Code:
    public void resetMap() {
      String path = Bukkit.getServer().getWorldContainer().getAbsolutePath();
      deleteFolder(new File(path + "\\world")); // If your world name is not 'world', change it to something else here
      copyDir(new File(path + "\\template").toPath(), new File(path + "\\world").toPath());
      // Again, maybe you need to rename "\\template" and "\\world"
    }
    
    public boolean deleteFolder(File dir) {
      File[] allContents = dir.listFiles();
      if (allContents != null) {
        for (File file : allContents) {
          deleteFolder(file);
        }
      }
      return dir.delete();
    }
    
    public void copyDir(Path src, Path dest) throws IOException {
      Files.walk(src).forEach(source -> {
        try {
          Files.copy(source, dest.resolve(src.relativize(source)), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
          e.printStackTrace();
        }
      });
    }
    
    I haven't tried it and found some things on the internet, so I don't know if it will work.
     
  3. Offline

    Tim_M

    I'll try that soon, thank you for answering! I didn't think of this method because another plugin that uses a method similiar to this made the server freeze while it was copying the world.

    Ok I tried it, and it doesn't seem to be working. When I create the template with copyDir() and when I reset the map with the same method, that you provided, it gave me a bunch of warnings about not empty directories or something?

    Here are the warnings if you're wondering:
    Show Spoiler

    Code:
    java.nio.file.DirectoryNotEmptyException: plugins/MysticalSkyWars/maps/Map1
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixCopyFile.copy(UnixCopyFile.java:564)
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixFileSystemProvider.copy(UnixFileSystemProvider.java:253)
    
    [11:12:33 WARN]:     at java.nio.file.Files.copy(Files.java:1274)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.lambda$0(Utilities.java:146)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    
    [11:12:33 WARN]:     at java.util.Iterator.forEachRemaining(Iterator.java:116)
    
    [11:12:33 WARN]:     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.copyDir(Utilities.java:142)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.saveMap(Utilities.java:123)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.MatchUtilities.doneCountdown01(MatchUtilities.java:267)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Counter$2.run(Counter.java:103)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    
    [11:12:33 WARN]:     at java.lang.Thread.run(Thread.java:748)
    
    [11:12:33 WARN]: java.nio.file.DirectoryNotEmptyException: plugins/MysticalSkyWars/maps/Map1/playerdata
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixCopyFile.copy(UnixCopyFile.java:564)
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixFileSystemProvider.copy(UnixFileSystemProvider.java:253)
    
    [11:12:33 WARN]:     at java.nio.file.Files.copy(Files.java:1274)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.lambda$0(Utilities.java:146)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    
    [11:12:33 WARN]:     at java.util.Iterator.forEachRemaining(Iterator.java:116)
    
    [11:12:33 WARN]:     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.copyDir(Utilities.java:142)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.saveMap(Utilities.java:123)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.MatchUtilities.doneCountdown01(MatchUtilities.java:267)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Counter$2.run(Counter.java:103)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    
    [11:12:33 WARN]:     at java.lang.Thread.run(Thread.java:748)
    
    [11:12:33 WARN]: java.nio.file.DirectoryNotEmptyException: plugins/MysticalSkyWars/maps/Map1/region
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixCopyFile.copy(UnixCopyFile.java:564)
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixFileSystemProvider.copy(UnixFileSystemProvider.java:253)
    
    [11:12:33 WARN]:     at java.nio.file.Files.copy(Files.java:1274)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.lambda$0(Utilities.java:146)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    
    [11:12:33 WARN]:     at java.util.Iterator.forEachRemaining(Iterator.java:116)
    
    [11:12:33 WARN]:     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.copyDir(Utilities.java:142)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.saveMap(Utilities.java:123)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.MatchUtilities.doneCountdown01(MatchUtilities.java:267)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Counter$2.run(Counter.java:103)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    
    [11:12:33 WARN]:     at java.lang.Thread.run(Thread.java:748)
    
    [11:12:33 WARN]: java.nio.file.DirectoryNotEmptyException: plugins/MysticalSkyWars/maps/Map1/data
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixCopyFile.copy(UnixCopyFile.java:564)
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixFileSystemProvider.copy(UnixFileSystemProvider.java:253)
    
    [11:12:33 WARN]:     at java.nio.file.Files.copy(Files.java:1274)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.lambda$0(Utilities.java:146)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    
    [11:12:33 WARN]:     at java.util.Iterator.forEachRemaining(Iterator.java:116)
    
    [11:12:33 WARN]:     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.copyDir(Utilities.java:142)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.saveMap(Utilities.java:123)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.MatchUtilities.doneCountdown01(MatchUtilities.java:267)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Counter$2.run(Counter.java:103)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    
    [11:12:33 WARN]:     at java.lang.Thread.run(Thread.java:748)
    
    [11:12:33 WARN]: java.nio.file.DirectoryNotEmptyException: plugins/MysticalSkyWars/maps/Map1/stats
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixCopyFile.copy(UnixCopyFile.java:564)
    
    [11:12:33 WARN]:     at sun.nio.fs.UnixFileSystemProvider.copy(UnixFileSystemProvider.java:253)
    
    [11:12:33 WARN]:     at java.nio.file.Files.copy(Files.java:1274)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.lambda$0(Utilities.java:146)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    
    [11:12:33 WARN]:     at java.util.Iterator.forEachRemaining(Iterator.java:116)
    
    [11:12:33 WARN]:     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    
    [11:12:33 WARN]:     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    
    [11:12:33 WARN]:     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    
    [11:12:33 WARN]:     at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.copyDir(Utilities.java:142)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Utilities.saveMap(Utilities.java:123)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.MatchUtilities.doneCountdown01(MatchUtilities.java:267)
    
    [11:12:33 WARN]:     at me.Tim_M.MysticalSkyWars.Counter$2.run(Counter.java:103)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
    
    [11:12:33 WARN]:     at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    
    [11:12:33 WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    
    [11:12:33 WARN]:     at java.lang.Thread.run(Thread.java:748)
    


    After some research I found that this error is caused when attempting a non empty folder, meaning that the deleteFolder() method is somehow malfunctioning...
     
    Last edited: Aug 16, 2020
  4. Offline

    Tim_M

    bump :p
     
  5. Offline

    Tim_M

    bump 2.0
     
  6. Offline

    pietermantel

    Yes, the deleteFolder and copyFolder functions I found on the internet, and they may be faulty (because the internet lies sometimes :(). Maybe try searching for a different way to delete and copy folders than I gave.
    This is another one I found:
    Code:
    void deleteDirectoryStream(Path path) throws IOException {
     Files.walk(path)
     .sorted(Comparator.reverseOrder())
     .map(Path::toFile)
     .forEach(File::delete);
    }
    
     
  7. Offline

    Tim_M

    Its ok!
    I got it figured out, though I have a new problem. When resetting the map, I first UNLOAD it, then i copy over the files, and the i load with WorldCreator. But for some reason, instead of a void world with a map, it creates a normal world (with terrain). Any ideas what is happening?
     
  8. Offline

    pietermantel

    My guess is that loading a world with WorldCreator actually generates a world (though I have no experience with WorldCreator). Is it necessary to unload the world first?
     
  9. Offline

    Tim_M

    I finally got everything to work! Marking thread as solved.
     

Share This Page