2013-08-12 02:02:18 +00:00
|
|
|
package me.StevenLawson.TotalFreedomMod;
|
|
|
|
|
2013-08-12 14:03:12 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2013-08-22 00:07:14 +00:00
|
|
|
import org.bukkit.*;
|
2013-08-12 02:02:18 +00:00
|
|
|
import org.bukkit.block.Block;
|
2013-08-12 15:58:28 +00:00
|
|
|
import org.bukkit.block.BlockFace;
|
2013-08-12 14:03:12 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
2013-08-12 02:02:18 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2013-08-12 14:03:12 +00:00
|
|
|
import org.bukkit.event.player.PlayerMoveEvent;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2013-08-12 02:02:18 +00:00
|
|
|
|
2013-08-22 00:07:14 +00:00
|
|
|
public final class TFM_AdminWorld extends TFM_CustomWorld
|
2013-08-12 02:02:18 +00:00
|
|
|
{
|
2013-08-13 01:33:21 +00:00
|
|
|
private static final long CACHE_CLEAR_FREQUENCY = 30L * 1000L; //30 seconds, milliseconds
|
|
|
|
private static final long TP_COOLDOWN_TIME = 500L; //0.5 seconds, milliseconds
|
2013-08-18 19:52:32 +00:00
|
|
|
private static final String GENERATION_PARAMETERS = TFM_ConfigEntry.FLATLANDS_GENERATION_PARAMS.getString();
|
2013-08-22 00:07:14 +00:00
|
|
|
private static final String WORLD_NAME = "adminworld";
|
2013-08-13 01:33:21 +00:00
|
|
|
//
|
|
|
|
private final Map<Player, Long> teleportCooldown = new HashMap<Player, Long>();
|
2013-08-22 00:07:14 +00:00
|
|
|
private final Map<CommandSender, Boolean> accessCache = new HashMap<CommandSender, Boolean>();
|
2013-08-13 01:33:21 +00:00
|
|
|
//
|
2013-08-12 16:38:46 +00:00
|
|
|
private Long cacheLastCleared = null;
|
2013-08-22 19:26:12 +00:00
|
|
|
private Map<Player, Player> guestList = new HashMap<Player, Player>();
|
2013-08-12 02:02:18 +00:00
|
|
|
|
|
|
|
private TFM_AdminWorld()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-22 00:07:14 +00:00
|
|
|
@Override
|
|
|
|
public void sendToWorld(Player player)
|
2013-08-12 02:02:18 +00:00
|
|
|
{
|
2013-08-22 00:07:14 +00:00
|
|
|
if (!canAccessWorld(player))
|
2013-08-12 02:02:18 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-22 00:07:14 +00:00
|
|
|
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;
|
2013-08-12 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 19:26:12 +00:00
|
|
|
public void addGuest(Player guest, Player supervisor)
|
|
|
|
{
|
|
|
|
if (TFM_SuperadminList.isUserSuperadmin(supervisor))
|
|
|
|
{
|
|
|
|
guestList.put(guest, supervisor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeGuest(Player guest)
|
|
|
|
{
|
|
|
|
guestList.remove(guest);
|
|
|
|
}
|
|
|
|
|
2013-08-12 14:03:12 +00:00
|
|
|
public boolean validateMovement(PlayerMoveEvent event)
|
2013-08-12 02:02:18 +00:00
|
|
|
{
|
2013-08-22 00:07:14 +00:00
|
|
|
World world;
|
|
|
|
try
|
2013-08-12 02:02:18 +00:00
|
|
|
{
|
2013-08-22 00:07:14 +00:00
|
|
|
world = getWorld();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (world != null && event.getTo().getWorld() == world)
|
|
|
|
{
|
|
|
|
final Player player = event.getPlayer();
|
|
|
|
if (!canAccessWorld(player))
|
2013-08-12 14:03:12 +00:00
|
|
|
{
|
2013-08-22 00:07:14 +00:00
|
|
|
Long lastTP = teleportCooldown.get(player);
|
|
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
if (lastTP == null || lastTP.longValue() + TP_COOLDOWN_TIME <= currentTimeMillis)
|
2013-08-12 14:03:12 +00:00
|
|
|
{
|
2013-08-22 00:07:14 +00:00
|
|
|
teleportCooldown.put(player, currentTimeMillis);
|
|
|
|
TFM_Log.info(player.getName() + " attempted to access the AdminWorld.");
|
|
|
|
new BukkitRunnable()
|
2013-08-12 14:03:12 +00:00
|
|
|
{
|
2013-08-22 00:07:14 +00:00
|
|
|
@Override
|
|
|
|
public void run()
|
2013-08-12 14:03:12 +00:00
|
|
|
{
|
2013-08-22 00:07:14 +00:00
|
|
|
player.teleport(Bukkit.getWorlds().get(0).getSpawnLocation());
|
|
|
|
}
|
|
|
|
}.runTaskLater(TotalFreedomMod.plugin, 1L);
|
2013-08-12 14:03:12 +00:00
|
|
|
}
|
2013-08-22 00:07:14 +00:00
|
|
|
event.setCancelled(true);
|
|
|
|
return false;
|
2013-08-12 14:03:12 +00:00
|
|
|
}
|
2013-08-12 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 00:07:14 +00:00
|
|
|
return true;
|
2013-08-12 16:38:46 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 00:07:14 +00:00
|
|
|
public void wipeAccessCache()
|
2013-08-12 16:51:30 +00:00
|
|
|
{
|
2013-08-13 01:33:21 +00:00
|
|
|
cacheLastCleared = System.currentTimeMillis();
|
2013-08-22 00:07:14 +00:00
|
|
|
accessCache.clear();
|
2013-08-12 16:51:30 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 19:26:12 +00:00
|
|
|
public boolean canAccessWorld(Player player)
|
2013-08-12 14:03:12 +00:00
|
|
|
{
|
2013-08-12 16:38:46 +00:00
|
|
|
long currentTimeMillis = System.currentTimeMillis();
|
2013-08-13 01:33:21 +00:00
|
|
|
if (cacheLastCleared == null || cacheLastCleared.longValue() + CACHE_CLEAR_FREQUENCY <= currentTimeMillis)
|
2013-08-12 16:38:46 +00:00
|
|
|
{
|
|
|
|
cacheLastCleared = currentTimeMillis;
|
2013-08-22 00:07:14 +00:00
|
|
|
accessCache.clear();
|
2013-08-12 16:38:46 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 19:26:12 +00:00
|
|
|
Boolean cached = accessCache.get(player);
|
2013-08-12 14:03:12 +00:00
|
|
|
if (cached == null)
|
2013-08-12 02:02:18 +00:00
|
|
|
{
|
2013-08-22 19:26:12 +00:00
|
|
|
boolean canAccess = TFM_SuperadminList.isUserSuperadmin(player);
|
|
|
|
if (!canAccess)
|
|
|
|
{
|
|
|
|
Player supervisor = guestList.get(player);
|
|
|
|
canAccess = supervisor != null && supervisor.isOnline() && TFM_SuperadminList.isUserSuperadmin(supervisor);
|
|
|
|
}
|
|
|
|
cached = canAccess;
|
|
|
|
accessCache.put(player, cached);
|
2013-08-12 02:02:18 +00:00
|
|
|
}
|
2013-08-12 14:03:12 +00:00
|
|
|
return cached;
|
2013-08-12 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static TFM_AdminWorld getInstance()
|
|
|
|
{
|
|
|
|
return TFM_AdminWorldHolder.INSTANCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class TFM_AdminWorldHolder
|
|
|
|
{
|
|
|
|
private static final TFM_AdminWorld INSTANCE = new TFM_AdminWorld();
|
|
|
|
}
|
|
|
|
}
|