mirror of
https://github.com/CivMC/Civ.git
synced 2026-07-18 08:30:59 +00:00
Adding in spawn point support. Per world set multiple named spawn points which are randomly pulled from during firstjoin or during a new players first few respawn -- configurable -- and optionally requiring that a player be nearby before using that spawn point. This supplements the other existing methods and co-exists nicely with all. Only thing missing is the commands to add and remove spawnpoints. Also added an event for this, so that conceivably other plugins could cancel the targetted spawn point events. Other spawn types do not call events, however and behave as previously.
This commit is contained in:
@@ -3,7 +3,10 @@ package me.josvth.randomspawn;
|
||||
import isaac.bastion.Bastion;
|
||||
import isaac.bastion.manager.BastionBlockManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -12,6 +15,7 @@ import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.World.Environment;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
@@ -33,7 +37,8 @@ public class RandomSpawn extends JavaPlugin{
|
||||
DamageListener damageListener;
|
||||
private boolean isWorldBorderEnabled = false;
|
||||
private boolean isBastionsEnabled = false;
|
||||
|
||||
private static List<Integer> defaultBlacklist = Arrays.asList(new Integer[]{8,9,10,11,18,51,81});
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
@@ -81,18 +86,16 @@ public class RandomSpawn extends JavaPlugin{
|
||||
public Location chooseSpawn(World world){
|
||||
String worldName = world.getName();
|
||||
|
||||
// I don't like this method
|
||||
// Nah man this method is pretty good. Any better ideas though?
|
||||
|
||||
List<Integer> blacklist = Arrays.asList(new Integer[]{8,9,10,11,18,51,81});
|
||||
List<Integer> blacklist = defaultBlacklist;
|
||||
if( yamlHandler.worlds.contains( worldName + ".spawnblacklist") )
|
||||
blacklist = yamlHandler.worlds.getIntegerList(worldName + ".spawnblacklist");
|
||||
|
||||
if(yamlHandler.worlds.getBoolean(worldName + ".spawnbyplayer")) {
|
||||
Player[] playersOnline = Bukkit.getOnlinePlayers().toArray(new Player[]{});
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Player> playersOnline = (List<Player>) Bukkit.getOnlinePlayers();
|
||||
|
||||
if(playersOnline.length > 0) {
|
||||
Player randomPlayer = playersOnline[(int)(Math.random() * playersOnline.length)];
|
||||
if(playersOnline.size() > 0) {
|
||||
Player randomPlayer = playersOnline.get((int)(Math.random() * playersOnline.size()));
|
||||
Location spawnNear = randomPlayer.getLocation();
|
||||
|
||||
double radius = yamlHandler.worlds.getDouble(worldName + ".spawnbyplayerarea.radius", 500);
|
||||
@@ -151,7 +154,7 @@ public class RandomSpawn extends JavaPlugin{
|
||||
|
||||
do {
|
||||
double r = exclusionRadius + Math.random() * (radius - exclusionRadius);
|
||||
double phi = Math.random() * 2 * Math.PI;
|
||||
double phi = Math.random() * 2d * Math.PI;
|
||||
|
||||
double x = center.getX() + Math.cos(phi) * r;
|
||||
double z = center.getZ() + Math.sin(phi) * r;
|
||||
@@ -159,7 +162,7 @@ public class RandomSpawn extends JavaPlugin{
|
||||
result.setX(x);
|
||||
result.setZ(z);
|
||||
result.setY(getValidHighestY(center.getWorld(), x, z, blacklist));
|
||||
} while (result.getY() == -1);
|
||||
} while (result.getY() == -1); // TODO this should never loop forever.
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -238,13 +241,55 @@ public class RandomSpawn extends JavaPlugin{
|
||||
|
||||
// Methods for a safe landing :)
|
||||
public void sendGround(Player player, Location location) {
|
||||
location.getChunk().load();
|
||||
if (!location.getChunk().isLoaded()) {
|
||||
location.getChunk().load();
|
||||
}
|
||||
}
|
||||
|
||||
// World world = location.getWorld();
|
||||
//
|
||||
// for(int y = 0 ; y <= location.getBlockY() + 2; y++){
|
||||
// Block block = world.getBlockAt(location.getBlockX(), y, location.getBlockZ());
|
||||
// player.sendBlockChange(block.getLocation(), block.getType(), block.getData());
|
||||
// }
|
||||
// Attempts to find a single valid spawn location against all configured spawn points for the world.
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Location> findSpawnPoints(World world) {
|
||||
String worldName = world.getName();
|
||||
|
||||
if (!yamlHandler.worlds.contains(worldName + ".spawnpoints")) {
|
||||
return new ArrayList<Location>(0);
|
||||
}
|
||||
|
||||
LinkedList<Location> spawnLocs = new LinkedList<Location>();
|
||||
|
||||
List<Integer> blacklist = defaultBlacklist;
|
||||
if( yamlHandler.worlds.contains( worldName + ".spawnblacklist") )
|
||||
blacklist = yamlHandler.worlds.getIntegerList(worldName + ".spawnblacklist");
|
||||
|
||||
// find all spawnpoint data
|
||||
List<ConfigurationSection> spawnpoints = (List<ConfigurationSection>) yamlHandler.worlds.getList(worldName + ".spawnpoints");
|
||||
|
||||
// reserve list of online players : TODO make sure this is just online players
|
||||
List<Player> playersOnline = world.getPlayers();
|
||||
|
||||
// For each potential spawn location ...
|
||||
for (ConfigurationSection spawnpoint : spawnpoints) {
|
||||
Location location = new Location(world, spawnpoint.getDouble("x"),
|
||||
spawnpoint.getDouble("y"), spawnpoint.getDouble("z"));
|
||||
boolean skip = true;
|
||||
// check if a player must be nearby and look for a player if so
|
||||
if (spawnpoint.getBoolean("nearby")) {
|
||||
for (Player player : playersOnline) {
|
||||
if (location.distance(player.getLocation()) < spawnpoint.getDouble("radius")) {
|
||||
skip = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else { // doesn't require nearby player
|
||||
skip = false;
|
||||
}
|
||||
if (!skip) {
|
||||
// Now pick a location to spawn the player.
|
||||
Location derive = chooseSpawn(spawnpoint.getDouble("radius"), spawnpoint.getDouble("exclusion"), location, blacklist);
|
||||
spawnLocs.add(derive);
|
||||
}
|
||||
}
|
||||
|
||||
return spawnLocs;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user