From 4d40b0d15f3119905621a94dbb8ac7053bcb5e08 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 23 Oct 2015 21:21:32 +1300 Subject: [PATCH] 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..564efe6 --- /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 handlers; + } + + 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 022ba31..464d0c2 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 975d666..a55bd6c 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.7.4