Add support for home regions, city regions, and region posts.

This commit is contained in:
Trever Fischer 2012-04-24 16:40:52 -04:00
parent 491aad7eac
commit c3ff978e99
8 changed files with 267 additions and 9 deletions

View File

@ -0,0 +1,54 @@
package us.camin.regions;
/**
* This file is part of Regions
*
* Regions 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.
*
* Regions 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 Regions. If not, see <http://www.gnu.org/licenses/>.
*
*/
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
public class CityRegionCommand implements CommandExecutor {
Plugin m_plugin;
public CityRegionCommand(Plugin p) {
m_plugin = p;
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] split) {
if (!(sender instanceof Player)) {
sender.sendMessage("Region command is only available to players.");
return true;
}
Player p = (Player)sender;
Region city = m_plugin.regionManager().cityRegion(p.getLocation().getWorld().getName());
Region nearest = m_plugin.regionManager().nearestRegion(p.getLocation());
if (city != null) {
if (p.getLocation().distance(nearest.teleportLocation()) <= 5) {
p.teleport(city.teleportLocation(), TeleportCause.COMMAND);
} else {
sender.sendMessage("You must be within 5 blocks of a region center.");
}
} else {
sender.sendMessage("There is no city region defined.");
}
return true;
}
}

View File

@ -0,0 +1,55 @@
package us.camin.regions;
/**
* This file is part of Regions
*
* Regions 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.
*
* Regions 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 Regions. If not, see <http://www.gnu.org/licenses/>.
*
*/
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
public class HomeRegionCommand implements CommandExecutor {
Plugin m_plugin;
public HomeRegionCommand(Plugin p) {
m_plugin = p;
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] split) {
if (!(sender instanceof Player)) {
sender.sendMessage("Region command is only available to players.");
return true;
}
Player p = (Player)sender;
Region home = m_plugin.regionManager().homeRegion(p.getName());
Region nearest = m_plugin.regionManager().nearestRegion(p.getLocation());
if (home != null) {
if (p.getLocation().distance(nearest.teleportLocation()) <= 5) {
p.teleport(home.teleportLocation(), TeleportCause.COMMAND);
} else {
sender.sendMessage("You must be within 5 blocks of a region center.");
}
} else {
sender.sendMessage("You have no home region.");
}
return true;
}
}

View File

@ -0,0 +1,50 @@
package us.camin.regions;
/**
* This file is part of Regions
*
* Regions 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.
*
* Regions 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 Regions. If not, see <http://www.gnu.org/licenses/>.
*
*/
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
public class MoveinRegionCommand implements CommandExecutor {
Plugin m_plugin;
public MoveinRegionCommand(Plugin p) {
m_plugin = p;
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] split) {
if (!(sender instanceof Player)) {
sender.sendMessage("Region command is only available to players.");
return true;
}
Player p = (Player)sender;
Region nearest = m_plugin.regionManager().nearestRegion(p.getLocation());
if (nearest != null) {
m_plugin.regionManager().setHomeRegion(p.getName(), nearest);
sender.sendMessage("Your home region has been set to "+nearest.name());
} else {
sender.sendMessage("There are no regions in this world.");
}
return true;
}
}

View File

@ -22,6 +22,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.dynmap.markers.MarkerAPI; import org.dynmap.markers.MarkerAPI;
@ -50,13 +52,20 @@ public class Plugin extends JavaPlugin {
CommandExecutor regionCommand = new RegionCommand(this); CommandExecutor regionCommand = new RegionCommand(this);
getCommand("region").setExecutor(regionCommand); getCommand("region").setExecutor(regionCommand);
getCommand("cityregion").setExecutor(new CityRegionCommand(this));
getCommand("homeregion").setExecutor(new HomeRegionCommand(this));
getCommand("movein").setExecutor(new MoveinRegionCommand(this));
org.bukkit.plugin.Plugin mapPlugin = getServer().getPluginManager().getPlugin("dynmap"); org.bukkit.plugin.Plugin mapPlugin = getServer().getPluginManager().getPlugin("dynmap");
if (mapPlugin instanceof DynmapCommonAPI) { if (mapPlugin instanceof DynmapCommonAPI) {
DynmapCommonAPI mapAPI = (DynmapCommonAPI)mapPlugin; DynmapCommonAPI mapAPI = (DynmapCommonAPI)mapPlugin;
MarkerAPI markerAPI = mapAPI.getMarkerAPI(); MarkerAPI markerAPI = mapAPI.getMarkerAPI();
if (markerAPI != null) {
RegionEventHandler regionHandler = new RegionEventHandler(markerAPI); RegionEventHandler regionHandler = new RegionEventHandler(markerAPI);
getServer().getPluginManager().registerEvents(regionHandler, this); getServer().getPluginManager().registerEvents(regionHandler, this);
} else {
log.info("[Regions] Dynmap marker API not found. Disabling map support.");
}
} else { } else {
log.info("[Regions] Dynmap not found. Disabling map support."); log.info("[Regions] Dynmap not found. Disabling map support.");
} }
@ -79,13 +88,13 @@ public class Plugin extends JavaPlugin {
public void loadRegions() { public void loadRegions() {
reloadConfig(); reloadConfig();
ConfigurationSection section = getConfig().getConfigurationSection("regions"); ConfigurationSection section = getConfig().getConfigurationSection("worlds");
if (section != null) if (section != null)
m_regions.loadRegions(section, getServer()); m_regions.loadRegions(section, getServer());
} }
public void saveRegions() { public void saveRegions() {
m_regions.saveRegions(getConfig().createSection("regions")); m_regions.saveRegions(getConfig().createSection("worlds"));
saveConfig(); saveConfig();
} }
@ -93,4 +102,20 @@ public class Plugin extends JavaPlugin {
saveRegions(); saveRegions();
log.info("[Regions] Plugin disabled"); log.info("[Regions] Plugin disabled");
} }
public void regenRegionPost(Region r) {
World world = r.location().getWorld();
Location center = world.getHighestBlockAt(r.location()).getLocation();
for(int x = center.getBlockX()-1;x <= center.getBlockX()+1;x++) {
for(int z = center.getBlockZ()-1;z <= center.getBlockZ()+1;z++) {
Block b = world.getBlockAt(x, center.getBlockY()-1, z);
b.setType(Material.COBBLESTONE);
}
}
for(int y = center.getBlockY()-2;y < center.getBlockY()+2;y++) {
Block b = world.getBlockAt(center.getBlockX(), y, center.getBlockZ());
b.setType(Material.GLOWSTONE);
}
}
} }

View File

@ -33,6 +33,10 @@ public class Region {
return m_location; return m_location;
} }
public Location teleportLocation() {
return m_location.getWorld().getHighestBlockAt(m_location).getLocation();
}
public String name() { public String name() {
return m_name; return m_name;
} }

View File

@ -56,6 +56,8 @@ public class RegionCommand implements CommandExecutor {
regionName.append(split[split.length-1]); regionName.append(split[split.length-1]);
Region r = new Region(regionName.toString(), p.getLocation()); Region r = new Region(regionName.toString(), p.getLocation());
m_plugin.regionManager().addRegion(r); m_plugin.regionManager().addRegion(r);
m_plugin.regenRegionPost(r);
p.teleport(r.teleportLocation());
} else if (subCommand.equals("remove") && p.hasPermission("regions.remove")) { } else if (subCommand.equals("remove") && p.hasPermission("regions.remove")) {
Region r = m_plugin.regionManager().nearestRegion(p.getLocation()); Region r = m_plugin.regionManager().nearestRegion(p.getLocation());
if (r == null) { if (r == null) {
@ -63,8 +65,20 @@ public class RegionCommand implements CommandExecutor {
return true; return true;
} }
m_plugin.regionManager().removeRegion(r); m_plugin.regionManager().removeRegion(r);
} else if (subCommand.equals("city") && p.hasPermission("regions.setCity")) {
Region r = m_plugin.regionManager().nearestRegion(p.getLocation());
if (r == null) {
p.sendMessage("There are no regions in this world.");
return true;
}
m_plugin.regionManager().setCityRegion(p.getLocation().getWorld().getName(), r);
p.sendMessage("City region set to "+r.name());
} else if (subCommand.equals("regen") && p.hasPermission("regions.create")) {
Region r = m_plugin.regionManager().nearestRegion(p.getLocation());
m_plugin.regenRegionPost(r);
p.sendMessage("Region post regenerated.");
} else { } else {
p.sendMessage("Unknown operation. Options are create and remove."); p.sendMessage("Unknown operation. Options are create, remove, city.");
} }
return true; return true;
} }

View File

@ -38,20 +38,33 @@ import java.util.Set;
public class RegionManager { public class RegionManager {
Logger log = Logger.getLogger("Regions.RegionManager"); Logger log = Logger.getLogger("Regions.RegionManager");
private Map<String, Collection<Region>> m_regions; private Map<String, Collection<Region>> m_regions;
private Map<String, Region> m_cityRegions;
private Map<String, Region> m_homeRegions;
private PluginManager m_pm; private PluginManager m_pm;
public RegionManager(PluginManager pm) { public RegionManager(PluginManager pm) {
m_pm = pm; m_pm = pm;
m_regions = new HashMap<String, Collection<Region>>(); clear();
} }
public void clear() { public void clear() {
m_regions = new HashMap<String, Collection<Region>>(); m_regions = new HashMap<String, Collection<Region>>();
m_cityRegions = new HashMap<String, Region>();
m_homeRegions = new HashMap<String, Region>();
} }
public void renameWorld(String oldName, String newName) { public void renameWorld(String oldName, String newName) {
log.fine("Renaming "+oldName+" to "+newName); log.fine("Renaming "+oldName+" to "+newName);
m_regions.put(newName, m_regions.remove(oldName)); m_regions.put(newName, m_regions.remove(oldName));
m_cityRegions.put(newName, m_cityRegions.remove(oldName));
}
public Region cityRegion(String worldName) {
return m_cityRegions.get(worldName);
}
public void setCityRegion(String worldName, Region region) {
m_cityRegions.put(worldName, region);
} }
public boolean addRegion(Region r) { public boolean addRegion(Region r) {
@ -118,31 +131,61 @@ public class RegionManager {
public void saveRegions(ConfigurationSection section) { public void saveRegions(ConfigurationSection section) {
for(String worldName : m_regions.keySet()) { for(String worldName : m_regions.keySet()) {
ConfigurationSection worldSection = section.createSection(worldName); ConfigurationSection worldSection = section.createSection(worldName);
Region cityRegion = cityRegion(worldName);
if (cityRegion != null)
worldSection.set("city", cityRegion.name());
ConfigurationSection worldRegionSection = worldSection.createSection("regions");
for(Region r : regionsForWorld(worldName)) { for(Region r : regionsForWorld(worldName)) {
ConfigurationSection regionSection = worldSection.createSection(r.name()); ConfigurationSection regionSection = worldRegionSection.createSection(r.name());
regionSection.set("x", r.location().getBlockX()); regionSection.set("x", r.location().getBlockX());
regionSection.set("z", r.location().getBlockZ()); regionSection.set("z", r.location().getBlockZ());
ArrayList<String> homePlayers = new ArrayList<String>();
for(String player : m_homeRegions.keySet()) {
if (m_homeRegions.get(player) == r) {
homePlayers.add(player);
} }
} }
regionSection.set("players", homePlayers);
}
}
}
public Region homeRegion(String playerName) {
return m_homeRegions.get(playerName);
}
public void setHomeRegion(String player, Region r) {
m_homeRegions.put(player, r);
} }
public void loadRegions(ConfigurationSection section, Server server) { public void loadRegions(ConfigurationSection section, Server server) {
Set<String> worldNames = section.getKeys(false); Set<String> worldNames = section.getKeys(false);
for(String worldName : worldNames) { for(String worldName : worldNames) {
ConfigurationSection worldSection = section.getConfigurationSection(worldName); ConfigurationSection worldSection = section.getConfigurationSection(worldName);
Set<String> regionNames = worldSection.getKeys(false); String cityName = worldSection.getString("city");
ConfigurationSection worldRegionSection = worldSection.getConfigurationSection("regions");
Set<String> regionNames = worldRegionSection.getKeys(false);
World world = server.getWorld(worldName); World world = server.getWorld(worldName);
if (world == null) { if (world == null) {
log.warning("Could not find world: "+worldName); log.warning("Could not find world: "+worldName);
continue; continue;
} }
for(String regionName : regionNames) { for(String regionName : regionNames) {
ConfigurationSection regionSection = worldSection.getConfigurationSection(regionName); ConfigurationSection regionSection = worldRegionSection.getConfigurationSection(regionName);
int x = regionSection.getInt("x"); int x = regionSection.getInt("x");
int z = regionSection.getInt("z"); int z = regionSection.getInt("z");
Location loc = new Location(world, x, 64, z); Location loc = new Location(world, x, 64, z);
Region r = new Region(regionName, loc); Region r = new Region(regionName, loc);
addRegion(r); addRegion(r);
if (regionName.equals(cityName)) {
m_cityRegions.put(worldName, r);
}
List<String> regionPlayers = regionSection.getStringList("players");
for(String player : regionPlayers) {
m_homeRegions.put(player, r);
}
} }
} }
} }

View File

@ -8,6 +8,15 @@ commands:
region: region:
description: Interface to the region system description: Interface to the region system
usage: /<command> [args...] usage: /<command> [args...]
movein:
description: "Sets your home region."
usage: /<command>
cityregion:
description: "Teleports you to the world's city region."
usage: /<command>
homeregion:
description: "Teleports you to your home region."
usage: /<command>
permissions: permissions:
regions.*: regions.*:
default: op default: op
@ -15,9 +24,13 @@ permissions:
children: children:
regions.create: true regions.create: true
regions.remove: true regions.remove: true
regions.city: true
regions.create: regions.create:
default: op default: op
description: Create a region description: Create a region
regions.remove: regions.remove:
default: op default: op
description: Remove a region description: Remove a region
regions.city:
default: op
description: Defines the city region