From 64492523a761fb37d2b73f4aad8b8041f7a30d83 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 3 Dec 2014 14:35:33 -0500
Subject: [PATCH] Move WorldSaveEvent to proper location

calling CraftWorld.save() currently does not call WorldSaveEvent, and WorldSaveEvent could fire on worlds that have saving disabled.

New location will always fire during a world save and only during an actual save.
---
 nms-patches/MinecraftServer.patch             | 60 +++++++++----------
 nms-patches/WorldServer.patch                 | 26 ++++----
 .../org/bukkit/craftbukkit/CraftServer.java   |  2 -
 3 files changed, 42 insertions(+), 46 deletions(-)

diff --git a/nms-patches/MinecraftServer.patch b/nms-patches/MinecraftServer.patch
index ea9982db97..e560580510 100644
--- a/nms-patches/MinecraftServer.patch
+++ b/nms-patches/MinecraftServer.patch
@@ -1,5 +1,5 @@
---- ../work/decompile-8eb82bde//net/minecraft/server/MinecraftServer.java	Sat Nov 29 19:31:45 2014
-+++ src/main/java/net/minecraft/server/MinecraftServer.java	Sat Nov 29 19:27:57 2014
+--- ../decompile-8eb82bde//net/minecraft/server/MinecraftServer.java	2014-12-03 14:33:55.037564649 -0500
++++ src/main/java/net/minecraft/server/MinecraftServer.java	2014-12-03 14:33:47.061564766 -0500
 @@ -37,6 +37,18 @@
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
@@ -276,7 +276,7 @@
          this.q();
      }
  
-@@ -247,35 +390,42 @@
+@@ -247,35 +390,38 @@
      protected void q() {
          this.e = null;
          this.f = 0;
@@ -309,10 +309,6 @@
 -                    }
 +                    worldserver.save(true, (IProgressUpdate) null);
 +                    worldserver.saveLevel();
-+
-+                    WorldSaveEvent event = new WorldSaveEvent(worldserver.getWorld());
-+                    this.server.getPluginManager().callEvent(event);
-+                    // CraftBukkit end
                  }
              }
  
@@ -331,7 +327,7 @@
              if (this.ao() != null) {
                  this.ao().b();
              }
-@@ -290,11 +440,13 @@
+@@ -290,11 +436,13 @@
                  MinecraftServer.LOGGER.info("Saving worlds");
                  this.saveChunks(false);
  
@@ -345,7 +341,7 @@
              }
  
              if (this.m.d()) {
-@@ -335,6 +487,7 @@
+@@ -335,6 +483,7 @@
                      long k = j - this.ab;
  
                      if (k > 2000L && this.ab - this.R >= 15000L) {
@@ -353,7 +349,7 @@
                          MinecraftServer.LOGGER.warn("Can\'t keep up! Did the system time change, or is the server overloaded? Running {}ms behind, skipping {} tick(s)", new Object[] { Long.valueOf(k), Long.valueOf(k / 50L)});
                          k = 2000L;
                          this.R = this.ab;
-@@ -347,11 +500,12 @@
+@@ -347,11 +496,12 @@
  
                      i += k;
                      this.ab = j;
@@ -367,7 +363,7 @@
                              i -= 50L;
                              this.y();
                          }
-@@ -389,6 +543,12 @@
+@@ -389,6 +539,12 @@
              } catch (Throwable throwable1) {
                  MinecraftServer.LOGGER.error("Exception stopping the server", throwable1);
              } finally {
@@ -380,7 +376,7 @@
                  this.x();
              }
  
-@@ -428,7 +588,7 @@
+@@ -428,7 +584,7 @@
  
      protected void x() {}
  
@@ -389,7 +385,7 @@
          long i = System.nanoTime();
  
          ++this.ticks;
-@@ -454,7 +614,7 @@
+@@ -454,7 +610,7 @@
              this.r.b().a(agameprofile);
          }
  
@@ -398,7 +394,7 @@
              this.methodProfiler.a("save");
              this.v.savePlayers();
              this.saveChunks(true);
-@@ -493,20 +653,40 @@
+@@ -493,20 +649,40 @@
  
          this.methodProfiler.c("levels");
  
@@ -442,7 +438,7 @@
  
                  this.methodProfiler.a("tick");
  
-@@ -533,9 +713,9 @@
+@@ -533,9 +709,9 @@
                  worldserver.getTracker().updatePlayers();
                  this.methodProfiler.b();
                  this.methodProfiler.b();
@@ -454,7 +450,7 @@
          }
  
          this.methodProfiler.c("connection");
-@@ -559,10 +739,11 @@
+@@ -559,10 +735,11 @@
          this.o.add(iupdateplayerlistbox);
      }
  
@@ -467,7 +463,7 @@
              boolean flag = true;
              String s = null;
              String s1 = ".";
-@@ -636,6 +817,27 @@
+@@ -636,6 +813,27 @@
  
              dedicatedserver.B();
              Runtime.getRuntime().addShutdownHook(new ThreadShutdown("Server Shutdown Thread", dedicatedserver));
@@ -495,7 +491,7 @@
          } catch (Exception exception) {
              MinecraftServer.LOGGER.fatal("Failed to start the minecraft server", exception);
          }
-@@ -643,8 +845,10 @@
+@@ -643,8 +841,10 @@
      }
  
      public void B() {
@@ -506,7 +502,7 @@
      }
  
      public File d(String s) {
-@@ -660,7 +864,14 @@
+@@ -660,7 +860,14 @@
      }
  
      public WorldServer getWorldServer(int i) {
@@ -522,7 +518,7 @@
      }
  
      public String C() {
-@@ -696,17 +907,62 @@
+@@ -696,17 +903,62 @@
      }
  
      public String getPlugins() {
@@ -592,7 +588,7 @@
      }
  
      public void h(String s) {
-@@ -721,7 +977,7 @@
+@@ -721,7 +973,7 @@
      }
  
      public String getServerModName() {
@@ -601,7 +597,7 @@
      }
  
      public CrashReport b(CrashReport crashreport) {
-@@ -734,6 +990,7 @@
+@@ -734,6 +986,7 @@
      }
  
      public List tabCompleteCommand(ICommandListener icommandlistener, String s, BlockPosition blockposition) {
@@ -609,7 +605,7 @@
          ArrayList arraylist = Lists.newArrayList();
  
          if (s.startsWith("/")) {
-@@ -772,6 +1029,9 @@
+@@ -772,6 +1025,9 @@
  
              return arraylist;
          }
@@ -619,7 +615,7 @@
      }
  
      public static MinecraftServer getServer() {
-@@ -779,7 +1039,7 @@
+@@ -779,7 +1035,7 @@
      }
  
      public boolean N() {
@@ -628,7 +624,7 @@
      }
  
      public String getName() {
-@@ -835,8 +1095,10 @@
+@@ -835,8 +1091,10 @@
      }
  
      public void a(EnumDifficulty enumdifficulty) {
@@ -641,7 +637,7 @@
  
              if (worldserver != null) {
                  if (worldserver.getWorldData().isHardcore()) {
-@@ -878,15 +1140,17 @@
+@@ -878,15 +1136,17 @@
          this.N = true;
          this.getConvertable().d();
  
@@ -662,7 +658,7 @@
          this.safeShutdown();
      }
  
-@@ -919,9 +1183,11 @@
+@@ -919,9 +1179,11 @@
          int i = 0;
  
          if (this.worldServer != null) {
@@ -677,7 +673,7 @@
                      WorldData worlddata = worldserver.getWorldData();
  
                      mojangstatisticsgenerator.a("world[" + i + "][dimension]", Integer.valueOf(worldserver.worldProvider.getDimension()));
-@@ -954,7 +1220,7 @@
+@@ -954,7 +1216,7 @@
      public abstract boolean ad();
  
      public boolean getOnlineMode() {
@@ -686,7 +682,7 @@
      }
  
      public void setOnlineMode(boolean flag) {
-@@ -1024,8 +1290,10 @@
+@@ -1024,8 +1286,10 @@
      }
  
      public void setGamemode(EnumGamemode enumgamemode) {
@@ -699,7 +695,7 @@
          }
  
      }
-@@ -1057,7 +1325,7 @@
+@@ -1057,7 +1321,7 @@
      }
  
      public World getWorld() {
@@ -708,7 +704,7 @@
      }
  
      public Entity f() {
-@@ -1125,11 +1393,10 @@
+@@ -1125,11 +1389,10 @@
      }
  
      public Entity a(UUID uuid) {
@@ -724,7 +720,7 @@
  
              if (worldserver != null) {
                  Entity entity = worldserver.getEntity(uuid);
-@@ -1144,7 +1411,7 @@
+@@ -1144,7 +1407,7 @@
      }
  
      public boolean getSendCommandFeedback() {
diff --git a/nms-patches/WorldServer.patch b/nms-patches/WorldServer.patch
index d7463e8a9d..579f5f93f8 100644
--- a/nms-patches/WorldServer.patch
+++ b/nms-patches/WorldServer.patch
@@ -1,5 +1,5 @@
---- ../work/decompile-8eb82bde//net/minecraft/server/WorldServer.java	2014-12-02 15:12:18.246036227 +0000
-+++ src/main/java/net/minecraft/server/WorldServer.java	2014-12-02 15:02:48.310048877 +0000
+--- ../decompile-8eb82bde//net/minecraft/server/WorldServer.java	2014-12-03 14:34:52.705563806 -0500
++++ src/main/java/net/minecraft/server/WorldServer.java	2014-12-03 14:34:43.665563938 -0500
 @@ -16,6 +16,20 @@
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
@@ -415,16 +415,18 @@
                  i += random.nextInt(64) - random.nextInt(64);
                  k += random.nextInt(64) - random.nextInt(64);
                  ++l;
-@@ -648,7 +859,7 @@
+@@ -648,8 +859,9 @@
          return this.worldProvider.h();
      }
  
 -    public void save(boolean flag, IProgressUpdate iprogressupdate) {
 +    public void save(boolean flag, IProgressUpdate iprogressupdate) throws ExceptionWorldConflict { // CraftBukkit - added throws
          if (this.chunkProvider.canSave()) {
++            org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit
              if (iprogressupdate != null) {
                  iprogressupdate.a("Saving level");
-@@ -660,7 +871,8 @@
+             }
+@@ -660,7 +872,8 @@
              }
  
              this.chunkProvider.saveChunks(flag, iprogressupdate);
@@ -434,7 +436,7 @@
              Iterator iterator = list.iterator();
  
              while (iterator.hasNext()) {
-@@ -680,7 +892,7 @@
+@@ -680,7 +893,7 @@
          }
      }
  
@@ -443,7 +445,7 @@
          this.checkSession();
          this.worldData.a(this.af().h());
          this.worldData.d(this.af().f());
-@@ -692,7 +904,11 @@
+@@ -692,7 +905,11 @@
          this.worldData.b(this.af().j());
          this.worldData.e(this.af().i());
          this.dataManager.saveWorldData(this.worldData, this.server.getPlayerList().u());
@@ -456,7 +458,7 @@
      }
  
      protected void a(Entity entity) {
-@@ -724,8 +940,16 @@
+@@ -724,8 +941,16 @@
      }
  
      public boolean strikeLightning(Entity entity) {
@@ -474,7 +476,7 @@
              return true;
          } else {
              return false;
-@@ -737,10 +961,20 @@
+@@ -737,10 +962,20 @@
      }
  
      public Explosion createExplosion(Entity entity, double d0, double d1, double d2, float f, boolean flag, boolean flag1) {
@@ -495,7 +497,7 @@
          if (!flag1) {
              explosion.clearBlocks();
          }
-@@ -786,7 +1020,8 @@
+@@ -786,7 +1021,8 @@
                  BlockActionData blockactiondata = (BlockActionData) iterator.next();
  
                  if (this.a(blockactiondata)) {
@@ -505,7 +507,7 @@
                  }
              }
  
-@@ -809,6 +1044,7 @@
+@@ -809,6 +1045,7 @@
          boolean flag = this.S();
  
          super.p();
@@ -513,7 +515,7 @@
          if (this.o != this.p) {
              this.server.getPlayerList().a(new PacketPlayOutGameStateChange(7, this.p), this.worldProvider.getDimension());
          }
-@@ -827,7 +1063,21 @@
+@@ -827,7 +1064,21 @@
              this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(7, this.p));
              this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(8, this.r));
          }
@@ -536,7 +538,7 @@
      }
  
      protected int q() {
-@@ -855,10 +1105,17 @@
+@@ -855,10 +1106,17 @@
      }
  
      public void a(EnumParticle enumparticle, boolean flag, double d0, double d1, double d2, int i, double d3, double d4, double d5, double d6, int... aint) {
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index fa1fe206a1..64b2961b05 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -935,8 +935,6 @@ public final class CraftServer implements Server {
             try {
                 handle.save(true, null);
                 handle.saveLevel();
-                WorldSaveEvent event = new WorldSaveEvent(handle.getWorld());
-                getPluginManager().callEvent(event);
             } catch (ExceptionWorldConflict ex) {
                 getLogger().log(Level.SEVERE, null, ex);
             }