Implemented a world generator. The generator is currently not working! Unsolved problem: How to force bukkit to populate a chunk.

This commit is contained in:
Tobias Ottenweller 2013-06-08 16:18:09 +02:00
parent de145b3a59
commit 5b75b83c5b
15 changed files with 430 additions and 22 deletions

View File

@ -84,7 +84,8 @@ public class Messages
ChatColor.WHITE + "Commands are always related to the current world." + NEWLINE + ChatColor.WHITE + "Commands are always related to the current world." + NEWLINE +
makeCmd("help", "shows this help") + makeCmd("help", "shows this help") +
makeCmd("get | info", "Shows information about the border.") + makeCmd("get | info", "Shows information about the border.") +
makeCmd("generate", "Generate all chunks inside the border", "on | off") + makeCmd("generate", "Generate all not existing chunks inside the border.") +
makeCmd("cancelgenerate", "Cancels the generation of chunks.") +
makeCmd("on | off", "Enables/disables the border.") + makeCmd("on | off", "Enables/disables the border.") +
makeCmd("set", "Square border with distance (d) from 0,0.", "r", "<d>") + makeCmd("set", "Square border with distance (d) from 0,0.", "r", "<d>") +
makeCmd("set", "Rectangle defined by two points. Point=x,z.", "r", "<p1>", "<p2>") + makeCmd("set", "Rectangle defined by two points. Point=x,z.", "r", "<p1>", "<p2>") +

View File

@ -19,9 +19,7 @@ package de.craftinc.borderprotection;
import de.craftinc.borderprotection.borders.CircBorder; import de.craftinc.borderprotection.borders.CircBorder;
import de.craftinc.borderprotection.borders.RectBorder; import de.craftinc.borderprotection.borders.RectBorder;
import de.craftinc.borderprotection.commands.CommandSwitch; import de.craftinc.borderprotection.commands.CommandSwitch;
import de.craftinc.borderprotection.events.PlayerLoginListener; import de.craftinc.borderprotection.events.*;
import de.craftinc.borderprotection.events.PlayerMoveListener;
import de.craftinc.borderprotection.events.PlayerTeleportListener;
import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -51,6 +49,8 @@ public class Plugin extends JavaPlugin
PlayerMoveListener playerMoveListener = new PlayerMoveListener(); PlayerMoveListener playerMoveListener = new PlayerMoveListener();
PlayerTeleportListener playerTeleportListener = new PlayerTeleportListener(); PlayerTeleportListener playerTeleportListener = new PlayerTeleportListener();
PlayerLoginListener playerLoginListener = new PlayerLoginListener(); PlayerLoginListener playerLoginListener = new PlayerLoginListener();
ChunkLoadingListener chunkLoadingListener = new ChunkLoadingListener();
PlayerQuitListener playerQuitListener = new PlayerQuitListener();
// commands // commands
CommandSwitch commandExecutor = new CommandSwitch(); CommandSwitch commandExecutor = new CommandSwitch();
@ -61,5 +61,7 @@ public class Plugin extends JavaPlugin
pm.registerEvents(playerMoveListener, this); pm.registerEvents(playerMoveListener, this);
pm.registerEvents(playerTeleportListener, this); pm.registerEvents(playerTeleportListener, this);
pm.registerEvents(playerLoginListener, this); pm.registerEvents(playerLoginListener, this);
pm.registerEvents(chunkLoadingListener, this);
pm.registerEvents(playerQuitListener, this);
} }
} }

View File

@ -73,6 +73,12 @@ public abstract class Border
*/ */
public abstract Location checkBorder( Location l ); public abstract Location checkBorder( Location l );
/**
* Returns an array of two Location objects defining a rectangle bigger or at size of the border. There are no
* locations outside the rectangle which are inside the border.
*/
public abstract Location[] getSurroundingRect();
public Boolean isActive() public Boolean isActive()
{ {
return isActive; return isActive;

View File

@ -120,4 +120,13 @@ public class CircBorder extends Border implements ConfigurationSerializable
return newLocation; return newLocation;
} }
@Override
public Location[] getSurroundingRect()
{
Location l1 = new Location(center.getWorld(), center.getX()+radius, center.getY(), center.getX()+radius);
Location l2 = new Location(center.getWorld(), center.getX()-radius, center.getY(), center.getX()-radius);
return new Location[]{ l1, l2 };
}
} }

View File

@ -180,4 +180,10 @@ public class RectBorder extends Border implements ConfigurationSerializable
} }
} }
} }
@Override
public Location[] getSurroundingRect()
{
return new Location[]{ rectPoint1, rectPoint2 };
}
} }

View File

@ -0,0 +1,63 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
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.commands;
import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.util.ChunkGenerator;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
public class CancelGenerateCommand implements SubCommand
{
@Override
public boolean execute(CommandSender sender, String[] parameters)
{
if ( !sender.hasPermission("craftinc.borderprotection.set") ) // TODO: a new/different permission?
{
sender.sendMessage(Messages.noPermissionSet);
return false;
}
World world = ( (Player) sender ).getWorld();
if (!ChunkGenerator.isGenerating(world))
{
sender.sendMessage("nothing to cancel"); // TODO: put better message into Message class
}
else
{
ChunkGenerator.cancelRender(world);
sender.sendMessage("generation canceled"); // TODO: put better message into Message class
}
return true;
}
@Override
public List<String> commandNames()
{
ArrayList<String> names = new ArrayList<String>();
names.add("cancelgenerate");
return names;
}
}

View File

@ -17,19 +17,11 @@
package de.craftinc.borderprotection.commands; package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages; import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.borders.Border;
import de.craftinc.borderprotection.borders.CircBorder;
import de.craftinc.borderprotection.borders.RectBorder;
import de.craftinc.borderprotection.util.UpdateHelper;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -39,6 +31,7 @@ public class CommandSwitch implements CommandExecutor
public CommandSwitch() public CommandSwitch()
{ {
registerCommand(new CancelGenerateCommand());
registerCommand(new CheckVersionCommand()); registerCommand(new CheckVersionCommand());
registerCommand(new GenerateCommand()); registerCommand(new GenerateCommand());
registerCommand(new GetCommand()); registerCommand(new GetCommand());

View File

@ -17,12 +17,15 @@
package de.craftinc.borderprotection.commands; package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages; import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.util.ChunkGenerator;
import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GenerateCommand implements SubCommand public class GenerateCommand implements SubCommand
{ {
@Override @Override
public boolean execute(CommandSender sender, String[] parameters) public boolean execute(CommandSender sender, String[] parameters)
@ -33,9 +36,26 @@ public class GenerateCommand implements SubCommand
return false; return false;
} }
// TODO: implement me! World world = ( (Player) sender ).getWorld();
return false; if (ChunkGenerator.isGenerating(world))
{
sender.sendMessage("already generating! will resume on logout"); // TODO: put better message into Message class
}
else
{
if (ChunkGenerator.generate(world))
{
sender.sendMessage("world marked for generation! will start on logout"); // TODO: put better message into Message class
}
else
{
sender.sendMessage("could not start generation. is there a border?"); // TODO: put better message into Message class
}
}
return true;
} }
@Override @Override

View File

@ -17,7 +17,6 @@
package de.craftinc.borderprotection.commands; package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages; import de.craftinc.borderprotection.Messages;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -20,7 +20,6 @@ package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages; import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.borders.Border; import de.craftinc.borderprotection.borders.Border;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View File

@ -26,15 +26,14 @@ public interface SubCommand
{ {
/** /**
* *
* @param sender will contain the command name at index 0. * @param sender The sender (player) who executed the command.
* @param parameters * @param parameters All parameters for executing this subcommand. Includes the subcommand name at index 0.
* @return * @return Returns an boolean indicating if the subcommand got could be executed.
*/ */
public boolean execute(CommandSender sender, String[] parameters); public boolean execute(CommandSender sender, String[] parameters);
/** /**
* * Returns a list of names of the command. All strings should be lowercase!
* @return a list of names of the command. All strings should be lowercase!
*/ */
public List<String> commandNames(); public List<String> commandNames();
} }

View File

@ -0,0 +1,44 @@
/* 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 )
{
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);
}
}

View File

@ -18,6 +18,7 @@ package de.craftinc.borderprotection.events;
import de.craftinc.borderprotection.Messages; import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.Plugin; import de.craftinc.borderprotection.Plugin;
import de.craftinc.borderprotection.util.ChunkGenerator;
import de.craftinc.borderprotection.util.UpdateHelper; import de.craftinc.borderprotection.util.UpdateHelper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -50,5 +51,8 @@ public class PlayerLoginListener implements Listener
}, 20L); }, 20L);
} }
} }
System.out.println("pausing generation");
ChunkGenerator.pause();
} }
} }

View File

@ -0,0 +1,37 @@
/* 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.player.PlayerQuitEvent;
public class PlayerQuitListener implements Listener
{
@SuppressWarnings("unused")
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerQuit( PlayerQuitEvent e )
{
if (Plugin.instance.getServer().getOnlinePlayers().length == 1)
{
ChunkGenerator.resume();
}
}
}

View File

@ -0,0 +1,226 @@
/* 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.util;
import de.craftinc.borderprotection.Plugin;
import de.craftinc.borderprotection.borders.Border;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
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 boolean isPaused = true;
protected static ArrayList<Chunk> loadedChunks = new ArrayList<Chunk>();
public static void pause()
{
isPaused = true;
}
public static void resume()
{
if (isPaused)
{
isPaused = false;
for (World w : chunkGenerationStatus.keySet())
{
loadNextChunk(w);
}
}
}
public static void cancelRender(World w)
{
if (w == null)
{
throw new IllegalArgumentException("World 'w' must not be null!");
}
chunkGenerationStatus.remove(w);
}
public static boolean isGenerating(World w)
{
if (w == null)
{
throw new IllegalArgumentException("World 'w' must not be null!");
}
return chunkGenerationStatus.containsKey(w);
}
/**
* Starts the generation of all chunks inside a border.
* @param w The world in which chunks will be generated. Must not be 'null'. An exception will be thrown otherwise!
* @return A boolean indicating if the generation was successfully started. Will return false if no border exists
* for a given world. Will return true if the generation was already running but will not restart the generation.
*/
public static boolean generate(World w)
{
if (w == null)
{
throw new IllegalArgumentException("World 'w' must not be null!");
}
Border border = Border.getBorders().get(w);
if (border == null)
{
return false;
}
Location[] borderRect = border.getSurroundingRect();
int firstChunkX = Math.min(borderRect[0].getBlockX(), borderRect[1].getBlockX()) >> 4;
int firstChunkZ = Math.min(borderRect[0].getBlockZ(), borderRect[1].getBlockZ()) >> 4;
firstChunkX--;
chunkGenerationStatus.put(w, new Integer[]{firstChunkX, firstChunkZ});
// the actual generation will start when resume is called!
return true;
}
public static void handleChunkLoad(Chunk c, boolean isPopulated)
{
if (c == null)
{
throw new IllegalArgumentException("Chunk 'c' 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");
// loadSurroundingChunks(c.getX(), c.getZ(), w);
}
else
{
unloadLoadedChunks();
loadNextChunk(c.getWorld());
}
}
}
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.
*/
protected static void loadNextChunk(World w)
{
Border border = Border.getBorders().get(w);
if (border == null)
{
return;
}
Integer[] lastGeneratedChunk = chunkGenerationStatus.get(w);
if (lastGeneratedChunk == null)
{
return; // the generation got most likely canceled
}
int chunkX = lastGeneratedChunk[0];
int chunkZ = lastGeneratedChunk[1];
Location[] borderRect = border.getSurroundingRect();
int minChunkX = Math.min(borderRect[0].getBlockX(), borderRect[1].getBlockX()) >> 4;
int maxChunkX = Math.max(borderRect[0].getBlockX(), borderRect[1].getBlockX()) >> 4;
int maxChunkZ = Math.max(borderRect[0].getBlockZ(), borderRect[1].getBlockZ()) >> 4;
chunkX++;
while (!chunkIsInsideBorder(chunkX, chunkZ, w, border)
&& chunkZ <= maxChunkZ
/*&& !w.isChunkLoaded(chunkX, chunkZ)*/)
{
chunkX++;
if (chunkX > maxChunkX)
{
chunkZ++;
chunkX = minChunkX;
}
}
if (chunkZ <= maxChunkZ)
{
chunkGenerationStatus.put(w, new Integer[]{chunkX, chunkZ});
Plugin.instance.getLogger().info("Loading/Generating Chunk ( x=" + chunkX + " z=" + chunkZ + " world=" + w.getName() + " )");
// loadSurroundingChunks(chunkX, chunkZ, w);
w.loadChunk(chunkX, chunkZ, true);
}
else
{
Plugin.instance.getLogger().info("Finished generating Chunks for world " + w.getName());
chunkGenerationStatus.remove(w);
}
}
protected static boolean chunkIsInsideBorder(int x, int z, World w, Border b)
{
// Location chunkLocation = new Location(w, (double)(x << 4), (double)100, (double)(z << 4));
// return b.checkBorder(chunkLocation) == null;
return true;
}
}