Created
July 24, 2013 20:23
-
-
Save NeatMonster/6074184 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.neatmonster.playersinregion; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Logger; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.Sign; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.block.BlockBreakEvent; | |
import org.bukkit.event.block.SignChangeEvent; | |
import org.bukkit.plugin.Plugin; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.sk89q.worldguard.bukkit.WGBukkit; | |
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; | |
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | |
public class PlayersInRegion extends JavaPlugin implements Listener { | |
private final Logger logger = Logger.getLogger("CraftBukkit"); | |
private final String logTag = "[PlayersInRegion] "; | |
private final List<String> signs = new ArrayList<String>(); | |
public void save() { | |
getConfig().set("signs", signs); | |
saveConfig(); | |
} | |
@Override | |
public void onEnable() { | |
final Plugin worldGuard = Bukkit.getPluginManager().getPlugin("WorldGuard"); | |
if (worldGuard == null || !(worldGuard instanceof WorldGuardPlugin)) { | |
logger.info(logTag + "WorldGuard not found, disabling..."); | |
Bukkit.getPluginManager().disablePlugin(this); | |
return; | |
} | |
signs.clear(); | |
if (getConfig().contains("signs")) | |
signs.addAll(getConfig().getStringList("signs")); | |
Bukkit.getPluginManager().registerEvents(this, this); | |
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { | |
@Override | |
public void run() { | |
for (final String locationString : signs) { | |
final String[] locationArray = locationString.split(":"); | |
final String worldName = locationArray[0]; | |
final int x = Integer.parseInt(locationArray[1]); | |
final int y = Integer.parseInt(locationArray[2]); | |
final int z = Integer.parseInt(locationArray[3]); | |
final Block block = Bukkit.getWorld(worldName).getBlockAt(x, y, z); | |
if (block.getState() != null && block.getState() instanceof Sign) { | |
final Sign sign = (Sign) block.getState(); | |
final String[] lines = update(sign.getLines()); | |
for (int i = 0; i < 4; i++) | |
sign.setLine(i, lines[i]); | |
sign.update(); | |
} | |
} | |
} | |
}, 20L, 20L); | |
} | |
@EventHandler(priority = EventPriority.MONITOR) | |
public void onBlockBreak(final BlockBreakEvent event) { | |
if (event.getBlock().getState() != null && event.getBlock().getState() instanceof Sign) { | |
final Location location = event.getBlock().getLocation(); | |
signs.remove(location.getWorld().getName() + ":" + location.getBlockX() + ":" + location.getBlockY() + ":" + location.getBlockZ()); | |
} | |
} | |
@EventHandler | |
public void onSignChange(final SignChangeEvent event) { | |
final String[] lines = update(event.getLines()); | |
for (int i = 0; i < 4; i++) | |
event.setLine(i, lines[i]); | |
if (lines[0].equals(ChatColor.GREEN + "[Players]")) { | |
final Location location = event.getBlock().getLocation(); | |
signs.add(location.getWorld().getName() + ":" + location.getBlockX() + ":" + location.getBlockY() + ":" + location.getBlockZ()); | |
save(); | |
} | |
} | |
public String[] update(final String[] lines) { | |
if (lines[0].trim().equals("[Players]") || lines[0].equals(ChatColor.GREEN + "[Players]")) { | |
if (lines[1].trim().isEmpty() || lines[2].trim().isEmpty()) | |
return error(lines); | |
else | |
try { | |
final World world = Bukkit.getWorld(lines[2]); | |
final ProtectedRegion region = WGBukkit.getRegionManager(world).getRegion(lines[1]); | |
int count = 0; | |
for (final Player player : world.getPlayers()) { | |
final Location location = player.getLocation(); | |
if (region.contains(location.getBlockX(), location.getBlockY(), location.getBlockZ())) | |
count++; | |
} | |
lines[0] = ChatColor.GREEN + "[Players]"; | |
lines[3] = "" + count; | |
return lines; | |
} catch (final Exception e) { | |
e.printStackTrace(); | |
return error(lines); | |
} | |
} | |
return lines; | |
} | |
public String[] error(final String[] lines) { | |
lines[0] = ChatColor.RED + "[Players]"; | |
lines[1] = lines[2] = lines[3] = ""; | |
return lines; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment