From ae9ac1273b240b9fc5209bd5c7fe59bf1072e944 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Sun, 19 Apr 2015 01:54:13 -0400 Subject: [PATCH] Mineplex - Chunk Changes diff --git a/src/main/java/com/mineplex/spigot/ChunkAddEntityEvent.java b/src/main/java/com/mineplex/spigot/ChunkAddEntityEvent.java new file mode 100644 index 0000000..3f16935 --- /dev/null +++ b/src/main/java/com/mineplex/spigot/ChunkAddEntityEvent.java @@ -0,0 +1,31 @@ +package com.mineplex.spigot; + +import org.bukkit.entity.Entity; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ChunkAddEntityEvent extends Event +{ + private static final HandlerList handlers = new HandlerList(); + private Entity _entity; + + public ChunkAddEntityEvent(Entity _entity) + { + this._entity = _entity; + } + + public Entity getEntity() + { + return _entity; + } + + public HandlerList getHandlers() + { + return handlers; + } + + public static HandlerList getHandlerList() + { + return handlers; + } +} diff --git a/src/main/java/com/mineplex/spigot/ChunkPreLoadEvent.java b/src/main/java/com/mineplex/spigot/ChunkPreLoadEvent.java new file mode 100644 index 0000000..21d75c7 --- /dev/null +++ b/src/main/java/com/mineplex/spigot/ChunkPreLoadEvent.java @@ -0,0 +1,60 @@ +package com.mineplex.spigot; + +import org.bukkit.World; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ChunkPreLoadEvent extends Event implements Cancellable +{ + private static final HandlerList handlers = new HandlerList(); + private boolean _cancelled; + private World _world; + private int _x; + private int _z; + + public ChunkPreLoadEvent(World world, int x, int z) + { + _world = world; + _x = x; + _z = z; + } + + public World getWorld() + { + return _world; + } + + public int getX() + { + return _x; + } + + public int getZ() + { + return _z; + } + + @Override + public boolean isCancelled() + { + return _cancelled; + } + + @Override + public void setCancelled(boolean cancel) + { + _cancelled = cancel; + } + + @Override + public HandlerList getHandlers() + { + return null; + } + + public static HandlerList getHandlerList() + { + return handlers; + } +} diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java index d5922fe..0a0e778 100644 --- a/src/main/java/net/minecraft/server/Chunk.java +++ b/src/main/java/net/minecraft/server/Chunk.java @@ -11,6 +11,8 @@ import java.util.Map; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentLinkedQueue; + +import com.mineplex.spigot.ChunkAddEntityEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -660,6 +662,9 @@ public class Chunk { entity.die(); } + ChunkAddEntityEvent event = new ChunkAddEntityEvent(entity.bukkitEntity); + Bukkit.getServer().getPluginManager().callEvent(event); + int k = MathHelper.floor(entity.locY / 16.0D); if (k < 0) { @@ -1317,6 +1322,10 @@ public class Chunk { return this.done; } + public void setDone(boolean done) { + this.done = done; + } + public void d(boolean flag) { this.done = flag; } diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java index 74710f9..b0d132f 100644 --- a/src/main/java/net/minecraft/server/ChunkProviderServer.java +++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java @@ -8,6 +8,8 @@ import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; + +import com.mineplex.spigot.ChunkPreLoadEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -88,6 +90,13 @@ public class ChunkProviderServer implements IChunkProvider { } + private boolean callChunkPreLoad(int i, int j) + { + ChunkPreLoadEvent event = new ChunkPreLoadEvent(world.getWorld(), i, j); + world.getServer().getPluginManager().callEvent(event); + return event.isCancelled(); + } + // CraftBukkit start - Add async variant, provide compatibility public Chunk getChunkIfLoaded(int x, int z) { return chunks.get(LongHash.toLong(x, z)); @@ -109,10 +118,30 @@ public class ChunkProviderServer implements IChunkProvider { // We can only use the queue for already generated chunks if (chunk == null && loader != null && loader.chunkExists(world, i, j)) { if (runnable != null) { - ChunkIOExecutor.queueChunkLoad(world, loader, this, i, j, runnable); + if (callChunkPreLoad(i, j)) + { + runnable.run(); + chunk = new EmptyChunk(world, i, j); + chunk.setDone(true); + chunks.put(LongHash.toLong(i, j), chunk); + } + else + { + ChunkIOExecutor.queueChunkLoad(world, loader, this, i, j, runnable); + } + return null; } else { - chunk = ChunkIOExecutor.syncChunkLoad(world, loader, this, i, j); + if (callChunkPreLoad(i, j)) + { + chunk = new EmptyChunk(world, i, j); + chunk.setDone(true); + chunks.put(LongHash.toLong(i, j), chunk); + } + else + { + chunk = ChunkIOExecutor.syncChunkLoad(world, loader, this, i, j); + } } } else if (chunk == null) { chunk = originalGetChunkAt(i, j); @@ -131,6 +160,20 @@ public class ChunkProviderServer implements IChunkProvider { boolean newChunk = false; // CraftBukkit end + Server server = world.getServer(); + + if (chunk == null && server != null) + { + if (callChunkPreLoad(i, j)) + { + chunk = new EmptyChunk(world, i, j); + chunk.setDone(true); + chunks.put(LongHash.toLong(i, j), chunk); + + return chunk; + } + } + if (chunk == null) { world.timings.syncChunkLoadTimer.startTiming(); // Spigot chunk = this.loadChunk(i, j); @@ -158,7 +201,6 @@ public class ChunkProviderServer implements IChunkProvider { chunk.addEntities(); // CraftBukkit start - Server server = world.getServer(); if (server != null) { /* * If it's a new world, the first few chunks are generated inside -- 2.3.5