Mineplex2018-withcommit/Plugins/Mineplex.MapParser/src/mineplex/mapparser/MapParser.java

726 lines
19 KiB
Java
Raw Normal View History

2014-06-15 02:12:08 +02:00
package mineplex.mapparser;
import java.io.File;
2014-08-16 22:50:33 +02:00
import java.util.ArrayList;
2014-07-09 00:13:30 +02:00
import java.util.HashMap;
import java.util.HashSet;
2014-08-16 22:50:33 +02:00
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
2014-06-15 02:12:08 +02:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
2014-06-15 02:12:08 +02:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
2014-11-14 04:14:31 +01:00
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
2014-06-15 02:12:08 +02:00
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
2014-06-15 02:12:08 +02:00
import org.bukkit.event.EventHandler;
2014-10-17 12:20:04 +02:00
import org.bukkit.event.EventPriority;
2014-06-15 02:12:08 +02:00
import org.bukkit.event.Listener;
2014-11-14 04:14:31 +01:00
import org.bukkit.event.block.Action;
2014-08-15 07:28:50 +02:00
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.block.LeavesDecayEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
2014-06-15 02:12:08 +02:00
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
2014-10-17 12:20:04 +02:00
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
2014-06-15 02:12:08 +02:00
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.MapUtil;
2014-11-14 04:14:31 +01:00
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilEvent;
2014-11-14 04:14:31 +01:00
import mineplex.core.common.util.UtilEvent.ActionType;
import mineplex.core.common.util.UtilGear;
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.mapparser.command.AdminCommand;
import mineplex.mapparser.command.AuthorCommand;
import mineplex.mapparser.command.BaseCommand;
import mineplex.mapparser.command.CopyCommand;
import mineplex.mapparser.command.CopySchematicsCommand;
import mineplex.mapparser.command.CreateCommand;
import mineplex.mapparser.command.DeleteCommand;
import mineplex.mapparser.command.GameTypeCommand;
import mineplex.mapparser.command.HubCommand;
import mineplex.mapparser.command.ListCommand;
import mineplex.mapparser.command.MapCommand;
import mineplex.mapparser.command.NameCommand;
2015-01-27 03:59:28 +01:00
import mineplex.mapparser.command.ParseCommand600;
import mineplex.mapparser.command.RenameCommand;
import mineplex.mapparser.command.SaveCommand;
import mineplex.mapparser.command.WorldsCommand;
2014-06-15 02:12:08 +02:00
public class MapParser extends JavaPlugin implements Listener
{
private WorldManager _worldManager;
private Parse _curParse = null;
2014-07-09 00:13:30 +02:00
private HashMap<String, MapData> _mapData = new HashMap<String, MapData>();
private HashSet<String> _mapsBeingZipped = new HashSet<String>();
2014-08-16 22:50:33 +02:00
private List<BaseCommand> _commands = new ArrayList<BaseCommand>();
private Location _spawnLocation;
2014-07-09 00:13:30 +02:00
private HashMap<Player, Boolean> _permissionMap = new HashMap<Player, Boolean>();
2014-06-15 02:12:08 +02:00
@Override
public void onEnable()
{
_worldManager = new WorldManager(this);
getServer().getPluginManager().registerEvents(this, this);
getServer().getWorlds().get(0).setSpawnLocation(0, 106, 0);
_spawnLocation = new Location(getServer().getWorlds().get(0), 0, 106, 0);
//Updates
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Ticker(this), 1, 1);
2014-08-16 22:50:33 +02:00
_commands.add(new AuthorCommand(this));
2014-08-20 03:02:12 +02:00
_commands.add(new AdminCommand(this));
2014-08-16 22:50:33 +02:00
_commands.add(new CopySchematicsCommand(this));
_commands.add(new CreateCommand(this));
_commands.add(new DeleteCommand(this));
_commands.add(new GameTypeCommand(this));
_commands.add(new HubCommand(this));
_commands.add(new ListCommand(this));
_commands.add(new MapCommand(this));
_commands.add(new NameCommand(this));
2015-01-27 03:59:28 +01:00
_commands.add(new ParseCommand600(this));
2014-08-16 22:50:33 +02:00
_commands.add(new RenameCommand(this));
_commands.add(new SaveCommand(this));
_commands.add(new WorldsCommand(this));
_commands.add(new CopyCommand(this));
2014-06-15 02:12:08 +02:00
}
@Override
public void onDisable()
{
}
@EventHandler
public void PlayerJoin(PlayerJoinEvent event)
2014-06-15 02:12:08 +02:00
{
Player player = event.getPlayer();
player.teleport(getSpawnLocation());
ResetInventory(event.getPlayer());
DisplayHelp(player);
}
@EventHandler
public void permissionUpdate(TickEvent event)
{
for (Player player : UtilServer.getPlayers())
{
permissionSet(player);
}
}
public void permissionSet(Player player)
{
boolean hasPermission = GetData(player.getWorld().getName()).HasAccess(player);
if (!_permissionMap.containsKey(player) || _permissionMap.get(player) != hasPermission)
{
for (Plugin plugin : Bukkit.getPluginManager().getPlugins())
{
player.addAttachment(plugin, "worldedit.*", hasPermission);
player.addAttachment(plugin, "voxelsniper.sniper", hasPermission);
player.addAttachment(plugin, "voxelsniper.brush.*", hasPermission);
}
_permissionMap.put(player, hasPermission);
UtilPlayer.message(player, "Plugin Permissions: " + F.tf(hasPermission));
}
}
public void DisplayHelp(Player player)
{
UtilPlayer.message(player, F.main("Parser", "Listing Commands;"));
2014-08-16 22:50:33 +02:00
UtilPlayer.message(player, F.value("Parameters", "Parameters: <?> = Required, [?] = Optional"));
UtilPlayer.message(player, F.value("/hub", "Return to hub world"));
UtilPlayer.message(player, " ");
UtilPlayer.message(player, F.value("/name <name>", "Set name for current map"));
UtilPlayer.message(player, F.value("/author <name>", "Set author for current map"));
2014-08-16 22:50:33 +02:00
UtilPlayer.message(player, F.value("/gametype <type>", "Set gametype for current map"));
2014-07-09 03:08:37 +02:00
UtilPlayer.message(player, " ");
2014-08-20 03:02:12 +02:00
UtilPlayer.message(player, F.value("/admin <name>", "Toggle admin for player on map"));
UtilPlayer.message(player, " ");
UtilPlayer.message(player, F.value("/create <name> [gametype]", "Creates a new map"));
UtilPlayer.message(player, F.value("/delete <name> [gametype]", "Deletes an existing map"));
UtilPlayer.message(player, F.value("/copy <name> <copy name>", "Copies an existing map"));
UtilPlayer.message(player, " ");
UtilPlayer.message(player, F.value("/list", "List maps"));
UtilPlayer.message(player, F.value("/map <name> [gametype]", "Teleport to a map"));
UtilPlayer.message(player, " ");
2014-07-09 03:08:37 +02:00
UtilPlayer.message(player, C.cYellow + "Documentation: " + C.cGreen + "http://tinyurl.com/mpxmaps");
}
@EventHandler
public void Command(PlayerCommandPreprocessEvent event)
{
2014-08-16 22:50:33 +02:00
Player player = event.getPlayer();
String[] parts = event.getMessage().split(" ");
String commandLabel = parts[0].substring(1);
String[] args = new String[parts.length - 1];
System.arraycopy(parts, 1, args, 0, parts.length - 1);
if (_curParse != null)
{
2014-08-16 22:50:33 +02:00
UtilPlayer.message(player, F.main("Parser", "Cannot use commands during Map Parse!"));
return;
}
if (event.getMessage().toLowerCase().startsWith("/help"))
{
event.setCancelled(true);
2014-08-16 22:50:33 +02:00
DisplayHelp(player);
}
2014-08-16 22:50:33 +02:00
for (BaseCommand command : _commands)
{
2014-08-16 22:50:33 +02:00
for (String alias : command.getAliases())
2014-07-08 21:18:43 +02:00
{
2014-08-16 22:50:33 +02:00
if (alias.equalsIgnoreCase(commandLabel))
{
2014-08-16 22:50:33 +02:00
if (!command.execute(player, commandLabel, args))
{
UtilPlayer.message(player, F.main("Parser", "Invalid Input."));
UtilPlayer.message(player, F.elem(command.getUsage()));
2014-08-16 22:50:33 +02:00
}
2014-08-16 22:50:33 +02:00
event.setCancelled(true);
return;
}
}
2014-08-16 22:50:33 +02:00
}
}
2014-08-16 22:50:33 +02:00
public void sendValidGameTypes(Player player)
{
UtilPlayer.message(player, F.main("Parser", "Valid Game Types;"));
2014-08-16 22:50:33 +02:00
String gameTypes = "";
2014-08-16 22:50:33 +02:00
for (GameType game : GameType.values())
{
2014-08-16 22:50:33 +02:00
gameTypes += game.toString() + " ";
2014-06-15 02:12:08 +02:00
}
2014-08-16 22:50:33 +02:00
player.sendMessage(gameTypes);
}
2014-08-16 22:50:33 +02:00
@EventHandler
public void ParseUpdate(TickEvent event)
{
if (_curParse == null)
2014-06-15 02:12:08 +02:00
return;
if (_curParse.Update())
2014-06-15 02:12:08 +02:00
{
2014-07-08 21:40:28 +02:00
Announce("Parse Completed!");
2014-07-08 21:18:43 +02:00
2014-07-08 21:40:28 +02:00
Announce("Cleaning and Creating ZIP...");
2014-07-08 21:40:28 +02:00
try
{
2014-07-09 00:13:30 +02:00
_worldManager.finalizeParsedWorld(_curParse.getWorld());
2014-07-08 21:40:28 +02:00
}
catch (Exception e)
{
Announce("Creating ZIP Failed! Please Try Again!");
e.printStackTrace();
}
_curParse = null;
2014-06-15 02:12:08 +02:00
}
}
@EventHandler
public void DisableCreatures(EntitySpawnEvent event)
{
if (event.getEntityType() == EntityType.DROPPED_ITEM || event.getEntity() instanceof LivingEntity)
event.setCancelled(true);
}
2014-08-15 07:28:50 +02:00
@EventHandler
public void DisableBurn(BlockBurnEvent event)
{
event.setCancelled(true);
}
@EventHandler
public void DisableFire(BlockSpreadEvent event)
{
event.setCancelled(true);
}
@EventHandler
public void DisableFade(BlockFadeEvent event)
{
event.setCancelled(true);
}
@EventHandler
public void DisableDecay(LeavesDecayEvent event)
{
event.setCancelled(true);
}
@EventHandler
public void Updates(PlayerMoveEvent event)
{
for (World world : this.getServer().getWorlds())
2014-08-06 03:07:49 +02:00
{
if (world.getName().toLowerCase().contains("halloween"))
world.setTime(16000);
else
world.setTime(8000);
2014-08-06 03:07:49 +02:00
world.setStorm(false);
}
if (event.getPlayer().getGameMode() != GameMode.CREATIVE)
event.getPlayer().setGameMode(GameMode.CREATIVE);
}
@EventHandler
public void SaveUnloadWorlds(TickEvent event)
{
for (final World world : getServer().getWorlds())
{
if (world.getName().equalsIgnoreCase("world"))
continue;
2014-07-08 21:18:43 +02:00
if (world.getName().startsWith("parse_"))
continue;
2014-08-16 22:50:33 +02:00
if (!world.getName().startsWith("map"))
2014-07-08 21:18:43 +02:00
continue;
if (world.getPlayers().isEmpty())
{
Announce("Saving & Closing World: " + F.elem(world.getName()));
MapUtil.UnloadWorld(this, world, true);
_mapsBeingZipped.add(world.getName());
System.out.println("Starting backup of " + world);
BackupTask backupTask = new BackupTask(this, world.getName(), new Callback<Boolean>()
{
@Override
public void run(Boolean data)
{
System.out.println("Finished backup of " + world);
_mapsBeingZipped.remove(world.getName());
}
});
}
}
}
public void Announce(String msg)
{
for (Player player : UtilServer.getPlayers())
{
player.sendMessage(C.cGold + msg);
}
System.out.println("[Announce] " + msg);
}
2014-08-16 22:50:33 +02:00
public boolean DoesMapExist(String mapName, GameType gameType)
{
2014-08-16 22:50:33 +02:00
return DoesMapExist(getWorldString(mapName, gameType));
}
public boolean DoesMapExist(String worldName)
{
File file = new File(worldName);
if (file.exists() && file.isDirectory())
return true;
return false;
}
2014-08-16 22:50:33 +02:00
public String getShortWorldName(String worldName)
{
int lastIndexOfSeperator = worldName.lastIndexOf('/');
if (lastIndexOfSeperator != -1)
return worldName.substring(lastIndexOfSeperator + 1);
return worldName;
}
public World GetMapWorld(String mapName, GameType gameType)
{
return GetMapWorld(getWorldString(mapName, gameType));
}
public World GetMapWorld(String worldName)
{
for (World world : this.getServer().getWorlds())
{
2014-08-16 22:50:33 +02:00
if (world.getName().equals(worldName))
return world;
}
2014-08-16 22:50:33 +02:00
return null;
}
2014-08-16 22:50:33 +02:00
public String getWorldString(String mapName, GameType type)
2014-08-16 22:50:33 +02:00
{
return "map" + "/" + type.GetName() + "/" + mapName;
2014-08-16 22:50:33 +02:00
}
public List<String> getMapsByName(String name)
{
name = name.toLowerCase();
List<String> maps = new LinkedList<String>();
boolean matchesExact = false;
for (GameType type : GameType.values())
{
ChatColor color = ChatColor.YELLOW;
File mapsFolder = new File("map" + File.separator + type.GetName());
if (!mapsFolder.exists())
continue;
for (File file : mapsFolder.listFiles())
{
if (!file.isDirectory())
continue;
if (!file.getName().toLowerCase().contains(name))
continue;
if (file.getName().equalsIgnoreCase(name))
matchesExact = true;
maps.add(getWorldString(file.getName(), type));
}
}
if (matchesExact)
{
Iterator<String> it = maps.iterator();
while (it.hasNext())
{
String mapString = it.next();
if (!mapString.toLowerCase().endsWith(name))
{
it.remove();
}
}
}
return maps;
}
2014-07-09 00:13:30 +02:00
public MapData GetData(String mapName)
{
if (_mapData.containsKey(mapName))
return _mapData.get(mapName);
2014-07-09 00:13:30 +02:00
MapData data = new MapData(mapName);
2014-07-09 00:13:30 +02:00
_mapData.put(mapName, data);
2014-07-09 00:13:30 +02:00
return data;
}
public Location getSpawnLocation()
{
return _spawnLocation;
}
public void ResetInventory(Player player)
{
2014-08-20 03:02:12 +02:00
UtilInv.Clear(player);
player.getInventory().addItem(new ItemStack(Material.STONE_SWORD));
player.getInventory().addItem(new ItemStack(Material.STONE_SPADE));
player.getInventory().addItem(new ItemStack(Material.STONE_PICKAXE));
player.getInventory().addItem(new ItemStack(Material.STONE_AXE));
2014-08-20 02:39:41 +02:00
player.getInventory().addItem(new ItemStack(Material.WOOD_AXE));
}
2014-08-16 22:50:33 +02:00
public WorldManager getWorldManager()
{
return _worldManager;
}
public void setCurrentParse(Parse parse)
{
_curParse = parse;
}
@EventHandler
public void Chat(AsyncPlayerChatEvent event)
{
event.setCancelled(true);
2014-08-16 22:50:33 +02:00
String world = C.cDGreen + C.Bold + getShortWorldName(event.getPlayer().getWorld().getName());
String name = C.cYellow + event.getPlayer().getName();
2014-08-20 03:02:12 +02:00
if (GetData(event.getPlayer().getWorld().getName()).HasAccess(event.getPlayer()))
name = C.cGreen + event.getPlayer().getName();
String grayName = C.cBlue + event.getPlayer().getName();
String grayWorld = C.cBlue + C.Bold + event.getPlayer().getWorld().getName();
for (Player player : UtilServer.getPlayers())
{
if (player.getWorld().equals(event.getPlayer().getWorld()))
{
player.sendMessage(world + ChatColor.RESET + " " + name + ChatColor.RESET + " " + event.getMessage());
}
else
{
player.sendMessage(grayWorld + ChatColor.RESET + " " + grayName + ChatColor.RESET + " " + C.cGray + event.getMessage());
}
}
System.out.println(world + ChatColor.RESET + " " + name + ChatColor.RESET + " " + event.getMessage());
}
@EventHandler(priority = EventPriority.LOWEST)
public void InteractCancel(PlayerInteractEvent event)
{
//Permission
2014-08-20 03:02:12 +02:00
if (!GetData(event.getPlayer().getWorld().getName()).HasAccess(event.getPlayer()))
{
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void commandCancel(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().startsWith("/tp") ||
event.getMessage().startsWith("/hub") ||
event.getMessage().startsWith("/list") ||
event.getMessage().startsWith("/map") ||
event.getMessage().startsWith("/create") ||
event.getMessage().startsWith("/copy") ||
event.getMessage().startsWith("/delete"))
return;
//Permission
if (!GetData(event.getPlayer().getWorld().getName()).HasAccess(event.getPlayer()))
{
UtilPlayer.message(event.getPlayer(), F.main("Parser", "You do not have Build-Access for this Map."));
event.setCancelled(true);
}
}
public HashSet<String> getMapsBeingZipped()
{
return _mapsBeingZipped;
}
@EventHandler
public void Join(PlayerJoinEvent event)
{
event.setJoinMessage(F.sys("Player Join", event.getPlayer().getName()));
}
@EventHandler
public void Join(PlayerQuitEvent event)
{
event.setQuitMessage(F.sys("Player Quit", event.getPlayer().getName()));
}
@EventHandler(priority = EventPriority.LOWEST)
public void TeleportCommand(PlayerCommandPreprocessEvent event)
{
if (!event.getMessage().toLowerCase().startsWith("/tp"))
return;
Player player = event.getPlayer();
String[] tokens = event.getMessage().split(" ");
if (tokens.length != 2)
{
return;
}
2014-11-14 04:14:31 +01:00
event.setCancelled(true);
Player target = UtilPlayer.searchOnline(player, tokens[1], true);
if (target != null)
{
UtilPlayer.message(player, F.main("Game", "You teleported to " + F.name(target.getName()) + "."));
player.teleport(target);
}
}
2014-11-14 04:14:31 +01:00
@EventHandler(priority = EventPriority.LOWEST)
public void FlySpeed(PlayerCommandPreprocessEvent event)
{
if (!event.getMessage().toLowerCase().startsWith("/speed"))
return;
Player player = event.getPlayer();
String[] tokens = event.getMessage().split(" ");
if (tokens.length != 2)
{
return;
}
event.setCancelled(true);
try
{
float speed = Float.parseFloat(tokens[1]);
player.setFlySpeed(speed);
UtilPlayer.message(player, F.main("Game", "Fly Speed set to " + F.elem("" + speed) + "."));
}
catch (Exception e)
{
UtilPlayer.message(player, F.main("Game", "Invalid Speed Input."));
}
}
private HashMap<Player, ArrayList<HashSet<BlockData>>> treeHistory = new HashMap<Player, ArrayList<HashSet<BlockData>>>();
@EventHandler(priority = EventPriority.HIGHEST)
public void treeRemover(PlayerInteractEvent event)
{
if (event.isCancelled())
return;
//Permission
if (!GetData(event.getPlayer().getWorld().getName()).HasAccess(event.getPlayer()))
{
return;
}
Player player = event.getPlayer();
if (!UtilGear.isMat(player.getItemInHand(), Material.NETHER_STAR))
return;
event.setCancelled(true);
//Remove
if (event.getAction() == Action.LEFT_CLICK_BLOCK)
{
if (event.getClickedBlock().getType() != Material.LOG)
{
player.sendMessage(C.cRed + C.Bold + "TreeTool: " + ChatColor.RESET + "Left-Click on Log");
return;
}
HashSet<Block> toRemove = searchLog(new HashSet<Block>(), event.getClickedBlock());
if (toRemove.isEmpty())
{
player.sendMessage(C.cRed + C.Bold + "TreeTool: " + ChatColor.RESET + "Left-Click on Log");
return;
}
HashSet<BlockData> history = new HashSet<BlockData>();
for (Block block : toRemove)
{
history.add(new BlockData(block));
block.setType(Material.AIR);
}
if (!treeHistory.containsKey(player))
treeHistory.put(player, new ArrayList<HashSet<BlockData>>());
treeHistory.get(player).add(0, history);
player.sendMessage(C.cRed + C.Bold + "TreeTool: " + ChatColor.RESET + "Tree Removed");
while (treeHistory.get(player).size() > 10)
treeHistory.get(player).remove(10);
}
else if (UtilEvent.isAction(event, ActionType.R))
{
if (!treeHistory.containsKey(player) || treeHistory.get(player).isEmpty())
{
player.sendMessage(C.cGreen + C.Bold + "TreeTool: " + ChatColor.RESET + "No Tree History");
return;
}
HashSet<BlockData> datas = treeHistory.get(player).remove(0);
for (BlockData data : datas)
data.restore();
player.sendMessage(C.cGreen + C.Bold + "TreeTool: " + ChatColor.RESET + "Tree Restored");
}
}
private HashSet<Block> searchLog(HashSet<Block> blocks, Block current)
{
//Not Tree
if (current.getType() != Material.LOG && current.getType() != Material.LEAVES)
return blocks;
if (!blocks.add(current))
return blocks;
for (Block other : UtilBlock.getSurrounding(current, true))
{
if (current.getType() != Material.LOG && current.getType() != Material.LEAVES)
continue;
if (blocks.contains(other))
continue;
//Dont spread from leaves to log
if (current.getType() == Material.LEAVES && other.getType() == Material.LOG)
continue;
searchLog(blocks, other);
}
return blocks;
}
2014-06-15 02:12:08 +02:00
}