Added TFM_CustomWorld superclass.

This commit is contained in:
Steven Lawson 2013-08-21 20:07:14 -04:00
parent 467d1d2d3a
commit e64fd42855
10 changed files with 254 additions and 152 deletions

View File

@ -1,5 +1,5 @@
#Tue, 20 Aug 2013 20:43:47 -0400 #Wed, 21 Aug 2013 19:57:49 -0400
program.VERSION=3.1 program.VERSION=3.1
program.BUILDNUM=472 program.BUILDNUM=473
program.BUILDDATE=08/20/2013 08\:43 PM program.BUILDDATE=08/21/2013 07\:57 PM

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Tue Aug 20 20:43:47 EDT 2013 #Wed Aug 21 19:57:49 EDT 2013
build.number=473 build.number=474

View File

@ -2,6 +2,7 @@ package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_AdminWorld; import me.StevenLawson.TotalFreedomMod.TFM_AdminWorld;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Util;
import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -50,7 +51,16 @@ public class Command_adminworld extends TFM_Command
case TELEPORT: case TELEPORT:
{ {
// /adminworld // /adminworld
if (sender_p.getWorld() == TFM_AdminWorld.getInstance().getAdminWorld()) World adminWorld = null;
try
{
adminWorld = TFM_AdminWorld.getInstance().getWorld();
}
catch (Exception ex)
{
}
if (adminWorld == null || sender_p.getWorld() == adminWorld)
{ {
playerMsg("Going to the main world."); playerMsg("Going to the main world.");
sender_p.teleport(server.getWorlds().get(0).getSpawnLocation()); sender_p.teleport(server.getWorlds().get(0).getSpawnLocation());
@ -58,7 +68,7 @@ public class Command_adminworld extends TFM_Command
else else
{ {
playerMsg("Going to the AdminWorld."); playerMsg("Going to the AdminWorld.");
TFM_AdminWorld.getInstance().sendToAdminWorld(sender_p); TFM_AdminWorld.getInstance().sendToWorld(sender_p);
} }
break; break;

View File

@ -1,12 +1,12 @@
package me.StevenLawson.TotalFreedomMod.Commands; package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_ConfigEntry; import me.StevenLawson.TotalFreedomMod.TFM_ConfigEntry;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Flatlands;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@CommandPermissions(level = AdminLevel.ALL, source = SourceType.BOTH) @CommandPermissions(level = AdminLevel.ALL, source = SourceType.ONLY_IN_GAME)
@CommandParameters(description = "Goto the flatlands.", usage = "/<command>") @CommandParameters(description = "Goto the flatlands.", usage = "/<command>")
public class Command_flatlands extends TFM_Command public class Command_flatlands extends TFM_Command
{ {
@ -15,7 +15,7 @@ public class Command_flatlands extends TFM_Command
{ {
if (TFM_ConfigEntry.GENERATE_FLATLANDS.getBoolean()) if (TFM_ConfigEntry.GENERATE_FLATLANDS.getBoolean())
{ {
TFM_Util.gotoWorld(sender, "flatlands"); TFM_Flatlands.getInstance().sendToWorld(sender_p);
} }
else else
{ {

View File

@ -2,12 +2,7 @@ package me.StevenLawson.TotalFreedomMod;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.bukkit.Bukkit; import org.bukkit.*;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -15,41 +10,81 @@ import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
public class TFM_AdminWorld public final class TFM_AdminWorld extends TFM_CustomWorld
{ {
private static final long CACHE_CLEAR_FREQUENCY = 30L * 1000L; //30 seconds, milliseconds private static final long CACHE_CLEAR_FREQUENCY = 30L * 1000L; //30 seconds, milliseconds
private static final long TP_COOLDOWN_TIME = 500L; //0.5 seconds, milliseconds private static final long TP_COOLDOWN_TIME = 500L; //0.5 seconds, milliseconds
private static final String GENERATION_PARAMETERS = TFM_ConfigEntry.FLATLANDS_GENERATION_PARAMS.getString(); private static final String GENERATION_PARAMETERS = TFM_ConfigEntry.FLATLANDS_GENERATION_PARAMS.getString();
private static final String ADMINWORLD_NAME = "adminworld"; private static final String WORLD_NAME = "adminworld";
// //
private final Map<Player, Long> teleportCooldown = new HashMap<Player, Long>(); private final Map<Player, Long> teleportCooldown = new HashMap<Player, Long>();
private final Map<CommandSender, Boolean> superadminCache = new HashMap<CommandSender, Boolean>(); private final Map<CommandSender, Boolean> accessCache = new HashMap<CommandSender, Boolean>();
// //
private Long cacheLastCleared = null; private Long cacheLastCleared = null;
private World adminWorld = null;
private TFM_AdminWorld() private TFM_AdminWorld()
{ {
} }
public void sendToAdminWorld(Player player) @Override
public void sendToWorld(Player player)
{ {
if (!TFM_SuperadminList.isUserSuperadmin(player)) if (!canAccessWorld(player))
{ {
return; return;
} }
player.teleport(getAdminWorld().getSpawnLocation()); super.sendToWorld(player);
}
@Override
protected World generateWorld()
{
WorldCreator worldCreator = new WorldCreator(WORLD_NAME);
worldCreator.generateStructures(false);
worldCreator.type(WorldType.NORMAL);
worldCreator.environment(World.Environment.NORMAL);
worldCreator.generator(new CleanroomChunkGenerator(GENERATION_PARAMETERS));
World world = Bukkit.getServer().createWorld(worldCreator);
world.setSpawnFlags(false, false);
world.setSpawnLocation(0, 50, 0);
Block welcomeSignBlock = world.getBlockAt(0, 50, 0);
welcomeSignBlock.setType(Material.SIGN_POST);
org.bukkit.block.Sign welcomeSign = (org.bukkit.block.Sign) welcomeSignBlock.getState();
org.bukkit.material.Sign signData = (org.bukkit.material.Sign) welcomeSign.getData();
signData.setFacingDirection(BlockFace.NORTH);
welcomeSign.setLine(0, ChatColor.GREEN + "AdminWorld");
welcomeSign.setLine(1, ChatColor.DARK_GRAY + "---");
welcomeSign.setLine(2, ChatColor.YELLOW + "Spawn Point");
welcomeSign.setLine(3, ChatColor.DARK_GRAY + "---");
welcomeSign.update();
TFM_GameRuleHandler.commitGameRules();
return world;
} }
public boolean validateMovement(PlayerMoveEvent event) public boolean validateMovement(PlayerMoveEvent event)
{ {
if (adminWorld != null) World world;
try
{ {
if (event.getTo().getWorld() == adminWorld) world = getWorld();
}
catch (Exception ex)
{
return true;
}
if (world != null && event.getTo().getWorld() == world)
{ {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (!cachedIsUserSuperadmin(player)) if (!canAccessWorld(player))
{ {
Long lastTP = teleportCooldown.get(player); Long lastTP = teleportCooldown.get(player);
long currentTimeMillis = System.currentTimeMillis(); long currentTimeMillis = System.currentTimeMillis();
@ -70,73 +105,34 @@ public class TFM_AdminWorld
return false; return false;
} }
} }
}
return true; return true;
} }
public World getAdminWorld() public void wipeAccessCache()
{
if (adminWorld == null || !Bukkit.getWorlds().contains(adminWorld))
{
generateWorld();
}
return adminWorld;
}
public void wipeSuperadminCache()
{ {
cacheLastCleared = System.currentTimeMillis(); cacheLastCleared = System.currentTimeMillis();
superadminCache.clear(); accessCache.clear();
} }
private boolean cachedIsUserSuperadmin(CommandSender user) public boolean canAccessWorld(CommandSender user)
{ {
long currentTimeMillis = System.currentTimeMillis(); long currentTimeMillis = System.currentTimeMillis();
if (cacheLastCleared == null || cacheLastCleared.longValue() + CACHE_CLEAR_FREQUENCY <= currentTimeMillis) if (cacheLastCleared == null || cacheLastCleared.longValue() + CACHE_CLEAR_FREQUENCY <= currentTimeMillis)
{ {
cacheLastCleared = currentTimeMillis; cacheLastCleared = currentTimeMillis;
superadminCache.clear(); accessCache.clear();
} }
Boolean cached = superadminCache.get(user); Boolean cached = accessCache.get(user);
if (cached == null) if (cached == null)
{ {
cached = TFM_SuperadminList.isUserSuperadmin(user); cached = TFM_SuperadminList.isUserSuperadmin(user);
superadminCache.put(user, cached); accessCache.put(user, cached);
} }
return cached; return cached;
} }
private void generateWorld()
{
WorldCreator adminWorldCreator = new WorldCreator(ADMINWORLD_NAME);
adminWorldCreator.generateStructures(false);
adminWorldCreator.type(WorldType.NORMAL);
adminWorldCreator.environment(World.Environment.NORMAL);
adminWorldCreator.generator(new CleanroomChunkGenerator(GENERATION_PARAMETERS));
adminWorld = Bukkit.getServer().createWorld(adminWorldCreator);
adminWorld.setSpawnFlags(false, false);
adminWorld.setSpawnLocation(0, 50, 0);
Block welcomeSignBlock = adminWorld.getBlockAt(0, 50, 0);
welcomeSignBlock.setType(Material.SIGN_POST);
org.bukkit.block.Sign welcomeSign = (org.bukkit.block.Sign) welcomeSignBlock.getState();
org.bukkit.material.Sign signData = (org.bukkit.material.Sign) welcomeSign.getData();
signData.setFacingDirection(BlockFace.NORTH);
welcomeSign.setLine(0, ChatColor.GREEN + "AdminWorld");
welcomeSign.setLine(1, ChatColor.DARK_GRAY + "---");
welcomeSign.setLine(2, ChatColor.YELLOW + "Spawn Point");
welcomeSign.setLine(3, ChatColor.DARK_GRAY + "---");
welcomeSign.update();
TFM_GameRuleHandler.commitGameRules();
}
public static TFM_AdminWorld getInstance() public static TFM_AdminWorld getInstance()
{ {
return TFM_AdminWorldHolder.INSTANCE; return TFM_AdminWorldHolder.INSTANCE;

View File

@ -0,0 +1,39 @@
package me.StevenLawson.TotalFreedomMod;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
public abstract class TFM_CustomWorld
{
private World world;
protected abstract World generateWorld();
public void sendToWorld(Player player)
{
try
{
player.teleport(getWorld().getSpawnLocation());
}
catch (Exception ex)
{
player.sendMessage(ex.getMessage());
}
}
public final World getWorld() throws Exception
{
if (world == null || !Bukkit.getWorlds().contains(world))
{
world = generateWorld();
}
if (world == null)
{
throw new Exception("World not loaded.");
}
return world;
}
}

View File

@ -0,0 +1,97 @@
package me.StevenLawson.TotalFreedomMod;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
public class TFM_Flatlands extends TFM_CustomWorld
{
private static final String GENERATION_PARAMETERS = TFM_ConfigEntry.FLATLANDS_GENERATION_PARAMS.getString();
private static final String WORLD_NAME = "flatlands";
private TFM_Flatlands()
{
}
@Override
protected World generateWorld()
{
if (!TFM_ConfigEntry.GENERATE_FLATLANDS.getBoolean())
{
return null;
}
wipeFlatlandsIfFlagged();
WorldCreator worldCreator = new WorldCreator(WORLD_NAME);
worldCreator.generateStructures(false);
worldCreator.type(WorldType.NORMAL);
worldCreator.environment(World.Environment.NORMAL);
worldCreator.generator(new CleanroomChunkGenerator(GENERATION_PARAMETERS));
World world = Bukkit.getServer().createWorld(worldCreator);
world.setSpawnFlags(false, false);
world.setSpawnLocation(0, 50, 0);
Block welcomeSignBlock = world.getBlockAt(0, 50, 0);
welcomeSignBlock.setType(Material.SIGN_POST);
org.bukkit.block.Sign welcomeSign = (org.bukkit.block.Sign) welcomeSignBlock.getState();
org.bukkit.material.Sign signData = (org.bukkit.material.Sign) welcomeSign.getData();
signData.setFacingDirection(BlockFace.NORTH);
welcomeSign.setLine(0, ChatColor.GREEN + "Flatlands");
welcomeSign.setLine(1, ChatColor.DARK_GRAY + "---");
welcomeSign.setLine(2, ChatColor.YELLOW + "Spawn Point");
welcomeSign.setLine(3, ChatColor.DARK_GRAY + "---");
welcomeSign.update();
TFM_GameRuleHandler.commitGameRules();
return world;
}
public static void wipeFlatlandsIfFlagged()
{
boolean doFlatlandsWipe = false;
try
{
doFlatlandsWipe = TFM_Util.getSavedFlag("do_wipe_flatlands");
}
catch (Exception ex)
{
}
if (doFlatlandsWipe)
{
if (Bukkit.getServer().getWorld("flatlands") == null)
{
TFM_Log.info("Wiping flatlands.");
TFM_Util.setSavedFlag("do_wipe_flatlands", false);
FileUtils.deleteQuietly(new File("./flatlands"));
}
else
{
TFM_Log.severe("Can't wipe flatlands, it is already loaded.");
}
}
}
public static TFM_Flatlands getInstance()
{
return TFM_FlatlandsHolder.INSTANCE;
}
private static class TFM_FlatlandsHolder
{
private static final TFM_Flatlands INSTANCE = new TFM_Flatlands();
}
}

View File

@ -122,7 +122,7 @@ public class TFM_SuperadminList
superadminIPs = TFM_Util.removeDuplicates(superadminIPs); superadminIPs = TFM_Util.removeDuplicates(superadminIPs);
seniorAdminNames = TFM_Util.removeDuplicates(seniorAdminNames); seniorAdminNames = TFM_Util.removeDuplicates(seniorAdminNames);
TFM_AdminWorld.getInstance().wipeSuperadminCache(); TFM_AdminWorld.getInstance().wipeAccessCache();
} }
public static void saveSuperadminList() public static void saveSuperadminList()

View File

@ -494,21 +494,6 @@ public class TFM_Util
} }
} }
public static void generateFlatlands()
{
generateFlatlands(TFM_ConfigEntry.FLATLANDS_GENERATION_PARAMS.getString());
}
public static void generateFlatlands(String genParams)
{
WorldCreator flatlands = new WorldCreator("flatlands");
flatlands.generateStructures(false);
flatlands.type(WorldType.NORMAL);
flatlands.environment(World.Environment.NORMAL);
flatlands.generator(new CleanroomChunkGenerator(genParams));
Bukkit.getServer().createWorld(flatlands);
}
public static String getRank(CommandSender sender) public static String getRank(CommandSender sender)
{ {
if (TFM_SuperadminList.isSuperadminImpostor(sender)) if (TFM_SuperadminList.isSuperadminImpostor(sender))
@ -739,39 +724,6 @@ public class TFM_Util
TFM_Log.severe(ex); TFM_Log.severe(ex);
} }
} }
public static void wipeFlatlandsIfFlagged()
{
boolean do_wipe_flatlands = false;
try
{
do_wipe_flatlands = TFM_Util.getSavedFlag("do_wipe_flatlands");
}
catch (Exception ex)
{
}
if (do_wipe_flatlands)
{
if (Bukkit.getServer().getWorld("flatlands") == null)
{
TFM_Log.info("Wiping flaglands.");
TFM_Util.setSavedFlag("do_wipe_flatlands", false);
File flatlands_folder = new File("./flatlands");
if (flatlands_folder.exists())
{
TFM_Util.deleteFolder(flatlands_folder);
}
}
else
{
TFM_Log.severe("Can't wipe flatlands, it is already loaded.");
}
}
}
public static String DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; public static String DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
public static String dateToString(Date date) public static String dateToString(Date date)

View File

@ -86,13 +86,21 @@ public class TotalFreedomMod extends JavaPlugin
registerEventHandlers(); registerEventHandlers();
if (TFM_ConfigEntry.GENERATE_FLATLANDS.getBoolean()) try
{
TFM_Flatlands.getInstance().getWorld();
}
catch (Exception ex)
{ {
TFM_Util.wipeFlatlandsIfFlagged();
TFM_Util.generateFlatlands(TFM_ConfigEntry.FLATLANDS_GENERATION_PARAMS.getString());
} }
TFM_AdminWorld.getInstance().getAdminWorld(); try
{
TFM_AdminWorld.getInstance().getWorld();
}
catch (Exception ex)
{
}
if (TFM_ConfigEntry.DISABLE_WEATHER.getBoolean()) if (TFM_ConfigEntry.DISABLE_WEATHER.getBoolean())
{ {