Simplified the Chunk Generator code.
This commit is contained in:
parent
8592f4ada1
commit
550db367c5
@ -49,7 +49,6 @@ public class Plugin extends JavaPlugin
|
||||
PlayerMoveListener playerMoveListener = new PlayerMoveListener();
|
||||
PlayerTeleportListener playerTeleportListener = new PlayerTeleportListener();
|
||||
PlayerLoginListener playerLoginListener = new PlayerLoginListener();
|
||||
ChunkLoadingListener chunkLoadingListener = new ChunkLoadingListener();
|
||||
PlayerQuitListener playerQuitListener = new PlayerQuitListener();
|
||||
|
||||
// commands
|
||||
@ -61,7 +60,6 @@ public class Plugin extends JavaPlugin
|
||||
pm.registerEvents(playerMoveListener, this);
|
||||
pm.registerEvents(playerTeleportListener, this);
|
||||
pm.registerEvents(playerLoginListener, this);
|
||||
pm.registerEvents(chunkLoadingListener, this);
|
||||
pm.registerEvents(playerQuitListener, this);
|
||||
}
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package de.craftinc.borderprotection.events;
|
||||
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import de.craftinc.borderprotection.util.ChunkGenerator;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.event.world.ChunkPopulateEvent;
|
||||
|
||||
public class ChunkLoadingListener implements Listener
|
||||
{
|
||||
// @SuppressWarnings("unused")
|
||||
// @EventHandler(priority = EventPriority.NORMAL)
|
||||
// public void onChunkPopulate( ChunkPopulateEvent e )
|
||||
// {
|
||||
// System.out.println("populate: " + e.getChunk());
|
||||
//
|
||||
// ChunkGenerator.handleChunkLoad(e.getChunk(), true);
|
||||
// }
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onChunkLoad( ChunkLoadEvent e )
|
||||
{
|
||||
boolean populated = !e.isNewChunk();
|
||||
ChunkGenerator.handleChunkLoad(e.getChunk(), populated);
|
||||
}
|
||||
}
|
@ -16,25 +16,21 @@
|
||||
*/
|
||||
package de.craftinc.borderprotection.util;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import net.minecraft.server.v1_5_R3.ChunkProviderServer; // NOTE: this will break with any new Bukkit/Minecraft version!
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.v1_5_R3.CraftWorld; // NOTE: this will break with any new Bukkit/Minecraft version!
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ChunkGenerator
|
||||
{
|
||||
protected static Map<World, Integer[]> chunkGenerationStatus = new HashMap<World, Integer[]>();
|
||||
protected static HashMap<World, Integer[]> chunkGenerationStatus = new HashMap<World, Integer[]>();
|
||||
protected static boolean isPaused = true;
|
||||
protected static ArrayList<Chunk> loadedChunks = new ArrayList<Chunk>();
|
||||
|
||||
public static long waitTicks = 5; // TODO: make adjustable via config file
|
||||
|
||||
public static void pause()
|
||||
{
|
||||
@ -49,7 +45,7 @@ public class ChunkGenerator
|
||||
|
||||
for (World w : chunkGenerationStatus.keySet())
|
||||
{
|
||||
loadNextChunk(w);
|
||||
slowLoadNextChunk(w);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -107,74 +103,28 @@ public class ChunkGenerator
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void handleChunkLoad(Chunk c, boolean isPopulated)
|
||||
protected static void slowLoadNextChunk(World w)
|
||||
{
|
||||
if (c == null)
|
||||
if (w == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Chunk 'c' must not be null!");
|
||||
throw new IllegalArgumentException("World 'w' must not be null!");
|
||||
}
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
World w = c.getWorld();
|
||||
Integer[] currentGenerationChunk = chunkGenerationStatus.get(w);
|
||||
|
||||
if ((currentGenerationChunk != null) && c.getX() == currentGenerationChunk[0] && c.getZ() == currentGenerationChunk[1])
|
||||
{
|
||||
if (!isPopulated)
|
||||
{
|
||||
// Plugin.instance.getLogger().info("Trying to get the chunk to get populated");
|
||||
|
||||
ChunkProviderServer cps = ((CraftWorld) w).getHandle().chunkProviderServer;
|
||||
cps.chunkProvider.getOrCreateChunk(c.getX(), c.getZ());
|
||||
|
||||
|
||||
}
|
||||
|
||||
DelayedCall delayedCall = new DelayedCall();
|
||||
delayedCall.w = w;
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, delayedCall, 10L);
|
||||
|
||||
|
||||
// loadNextChunk(w);
|
||||
}
|
||||
DelayedCall delayedCall = new DelayedCall();
|
||||
delayedCall.w = w;
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, delayedCall, waitTicks);
|
||||
}
|
||||
|
||||
// protected static void loadSurroundingChunks(int x, int z, World w)
|
||||
// {
|
||||
// int radius = 2;
|
||||
//
|
||||
// for (int i=-radius; i<radius; i++)
|
||||
// {
|
||||
// for (int j=-radius; j<radius; j++)
|
||||
// {
|
||||
// if (j == 0 && i == 0)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// w.loadChunk(i+x, j+z, false);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// protected static void unloadLoadedChunks()
|
||||
// {
|
||||
// for (Chunk c : loadedChunks)
|
||||
// {
|
||||
// c.getWorld().unloadChunk(c);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Will only load chunks inside the border of the given world. Will stop if no border exists.
|
||||
* Will only load/generate the next chunks inside the border of the given world. Will stop if no border exists.
|
||||
*/
|
||||
protected static void loadNextChunk(World w)
|
||||
{
|
||||
if (w == null)
|
||||
{
|
||||
throw new IllegalArgumentException("World 'w' must not be null!");
|
||||
}
|
||||
|
||||
Border border = Border.getBorders().get(w);
|
||||
|
||||
if (border == null)
|
||||
@ -197,15 +147,10 @@ public class ChunkGenerator
|
||||
int maxChunkX = Math.max(borderRect[0].getBlockX(), borderRect[1].getBlockX()) >> 4;
|
||||
int maxChunkZ = Math.max(borderRect[0].getBlockZ(), borderRect[1].getBlockZ()) >> 4;
|
||||
|
||||
// System.out.println("minChunkX: " + minChunkX);
|
||||
// System.out.println("maxChunkX: " + maxChunkX);
|
||||
// System.out.println("maxChunkZ: " + maxChunkZ);
|
||||
|
||||
chunkX++;
|
||||
|
||||
while (!chunkIsInsideBorder(chunkX, chunkZ, w, border)
|
||||
&& chunkZ <= maxChunkZ
|
||||
/*&& !w.isChunkLoaded(chunkX, chunkZ)*/)
|
||||
&& chunkZ <= maxChunkZ)
|
||||
{
|
||||
chunkX++;
|
||||
|
||||
@ -220,7 +165,11 @@ public class ChunkGenerator
|
||||
{
|
||||
chunkGenerationStatus.put(w, new Integer[]{chunkX, chunkZ});
|
||||
Plugin.instance.getLogger().info("Loading/Generating Chunk ( x=" + chunkX + " z=" + chunkZ + " world=" + w.getName() + " )");
|
||||
w.loadChunk(chunkX, chunkZ, true);
|
||||
|
||||
Chunk chunk = w.getChunkAt(chunkX, chunkZ);
|
||||
chunk.load(true);
|
||||
|
||||
slowLoadNextChunk(w);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user