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

494 lines
13 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;
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.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;
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.event.EventHandler;
import org.bukkit.event.Listener;
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.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.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
2014-06-15 02:12:08 +02:00
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.mapparser.command.AuthorCommand;
import mineplex.mapparser.command.BaseCommand;
2014-08-20 03:02:12 +02:00
import mineplex.mapparser.command.AdminCommand;
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;
import mineplex.mapparser.command.ParseCommand;
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>();
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
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));
_commands.add(new ParseCommand(this));
_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);
}
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
{
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 (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);
}
}
}
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);
MapData data = new MapData(mapName);
_mapData.put(mapName, data);
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
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
public void WorldeditCancel(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().startsWith("//"))
{
//Permission
2014-08-20 03:02:12 +02:00
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);
}
}
}
@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()));
}
2014-06-15 02:12:08 +02:00
}