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

560 lines
14 KiB
Java
Raw Normal View History

2014-06-15 02:12:08 +02:00
package mineplex.mapparser;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
2014-06-15 02:12:08 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import mineplex.core.common.util.UtilWorld;
import net.minecraft.util.org.apache.commons.io.FileUtils;
2014-06-15 02:12:08 +02:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
2014-06-15 02:12:08 +02:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
2014-06-15 02:12:08 +02:00
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.material.Wool;
import org.bukkit.plugin.java.JavaPlugin;
import org.fusesource.jansi.Ansi.Color;
2014-06-15 02:12:08 +02:00
public class MapParser extends JavaPlugin implements Listener
{
private WorldManager _worldManager;
2014-06-15 02:12:08 +02:00
@Override
public void onEnable()
{
_worldManager = new WorldManager(this);
getServer().getPluginManager().registerEvents(this, this);
2014-06-15 02:12:08 +02:00
}
@Override
public void onDisable()
{
}
@EventHandler
public void Command(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().startsWith("/copyschematics"))
{
event.setCancelled(true);
try
{
FileUtils.copyDirectory(new File(".." + File.separator + "Build-1" + File.separator + "plugins" + File.separator + "WorldEdit" + File.separator + "schematics"),
new File("plugins" + File.separator + "WorldEdit" + File.separator + "schematics"));
event.getPlayer().sendMessage("Schematics copied.");
}
catch (IOException e)
{
e.printStackTrace();
event.getPlayer().sendMessage("Schematics copy failed!");
}
}
else if (event.getMessage().startsWith("/createMap"))
{
event.setCancelled(true);
String[] args = event.getMessage().substring(event.getMessage().indexOf(' ') + 1).split(" ");
WorldCreator worldCreator = new WorldCreator(args[0]);
worldCreator.environment(Environment.NORMAL);
worldCreator.type(WorldType.FLAT);
worldCreator.generateStructures(false);
World world = Bukkit.getServer().createWorld(worldCreator);
2014-06-15 02:12:08 +02:00
event.getPlayer().teleport(world.getSpawnLocation());
}
else if (event.getMessage().startsWith("/loadMap"))
{
event.setCancelled(true);
String[] args = event.getMessage().substring(event.getMessage().indexOf(' ') + 1).split(" ");
World world = _worldManager.loadPendingParse(event.getPlayer(), args[0]);
event.getPlayer().teleport(world.getSpawnLocation());
}
else if (event.getMessage().startsWith("/listMaps"))
{
event.setCancelled(true);
_worldManager.listPendingParses(event.getPlayer());
}
else if (event.getMessage().startsWith("/parse"))
{
event.setCancelled(true);
String[] args = event.getMessage().substring(event.getMessage().indexOf(' ') + 1).split(" ");
String gameType = args[0];
try
{
GameType type = GameType.valueOf(gameType);
}
catch (Exception ex)
{
event.getPlayer().sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Invalid gametype!");
event.getPlayer().sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "Available games:");
for (GameType game : GameType.values())
{
event.getPlayer().sendMessage(game.toString());
}
return;
}
String msg = event.getMessage().replace(gameType, "").toLowerCase();
Player player = event.getPlayer();
World world = player.getWorld();
// TODO teleport player to main world
player.teleport(new Location(Bukkit.getWorlds().get(0), 0, 100, 0));
String worldName = _worldManager.prepMapParse(world);
World parseableWorld = Bukkit.getWorld(worldName);
Parse(event.getPlayer(), parseableWorld, msg.split(" "));
_worldManager.finalizeParsedWorld(gameType, parseableWorld);
}
2014-06-15 02:12:08 +02:00
}
public void Parse(Player caller, World world, String[] args)
2014-06-15 02:12:08 +02:00
{
HashSet<Integer> dataId = new HashSet<Integer>();
for (String arg : args)
{
if (arg.equals("/parse"))
continue;
try
{
dataId.add(Integer.parseInt(arg));
}
catch (Exception e)
{
caller.sendMessage("Invalid Data ID: " + arg);
}
}
HashMap<String, ArrayList<Location>> TeamLocs = new HashMap<String, ArrayList<Location>>();
HashMap<String, ArrayList<Location>> DataLocs = new HashMap<String, ArrayList<Location>>();
HashMap<String, ArrayList<Location>> CustomLocs = new HashMap<String, ArrayList<Location>>();
Location cornerA = null;
Location cornerB = null;
int processed = 0;
caller.sendMessage("Scanning for Blocks...");
for (int x=-600 ; x < 600 ; x++)
for (int z=-600 ; z < 600 ; z++)
for (int y=0 ; y < 256 ; y++)
{
processed++;
if (processed % 20000000 == 0)
caller.sendMessage("Processed: " + processed + " of " + (1200*1200*256));
Block block = world.getBlockAt(caller.getLocation().getBlockX()+x, caller.getLocation().getBlockY()+y, caller.getLocation().getBlockZ()+z);
2014-06-15 02:12:08 +02:00
//ID DATA
if (dataId.contains(block.getTypeId()))
{
String key = ""+block.getTypeId();
if (!CustomLocs.containsKey(key))
CustomLocs.put(key, new ArrayList<Location>());
CustomLocs.get(key).add(block.getLocation());
continue;
}
//Signs
if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN)
{
if (block.getRelative(BlockFace.DOWN).getType() == Material.SPONGE)
{
Sign s = (Sign) block.getState();
String name = "";
try
{
name = s.getLine(0);
}
catch (Exception e)
{
caller.sendMessage("Invalid Sign Data: " + UtilWorld.locToStr(block.getLocation()));
}
//Add
if (!CustomLocs.containsKey(name))
CustomLocs.put(name, new ArrayList<Location>());
CustomLocs.get(name).add(block.getRelative(BlockFace.DOWN).getLocation());
//Remove Blocks
block.setTypeId(0);
block.getRelative(BlockFace.DOWN).setTypeId(0);
}
}
//Spawns + Borders
if (block.getTypeId() == 147)
{
Block wool = block.getRelative(BlockFace.DOWN);
if (wool == null)
continue;
if (wool.getType() == Material.WOOL)
{
if (wool.getData() == 0)
{
if (cornerA == null) cornerA = wool.getLocation();
else if (cornerB == null) cornerB = wool.getLocation();
else
{
caller.sendMessage("More than 2 Corner Markers:");
caller.sendMessage("Corner A: " + cornerA);
caller.sendMessage("Corner B: " + cornerB);
caller.sendMessage("Excess: " + UtilWorld.locToStrClean(wool.getLocation()));
}
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 1)
{
if (!TeamLocs.containsKey("Orange"))
TeamLocs.put("Orange", new ArrayList<Location>());
TeamLocs.get("Orange").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 2)
{
if (!TeamLocs.containsKey("Magenta"))
TeamLocs.put("Magenta", new ArrayList<Location>());
TeamLocs.get("Magenta").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 3)
{
if (!TeamLocs.containsKey("Sky"))
TeamLocs.put("Sky", new ArrayList<Location>());
TeamLocs.get("Sky").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 4)
{
if (!TeamLocs.containsKey("Yellow"))
TeamLocs.put("Yellow", new ArrayList<Location>());
TeamLocs.get("Yellow").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 5)
{
if (!TeamLocs.containsKey("Lime"))
TeamLocs.put("Lime", new ArrayList<Location>());
TeamLocs.get("Lime").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 6)
{
if (!TeamLocs.containsKey("Pink"))
TeamLocs.put("Pink", new ArrayList<Location>());
TeamLocs.get("Pink").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 7)
{
if (!TeamLocs.containsKey("Gray"))
TeamLocs.put("Gray", new ArrayList<Location>());
TeamLocs.get("Gray").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 8)
{
if (!TeamLocs.containsKey("LGray"))
TeamLocs.put("LGray", new ArrayList<Location>());
TeamLocs.get("LGray").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 9)
{
if (!TeamLocs.containsKey("Cyan"))
TeamLocs.put("Cyan", new ArrayList<Location>());
TeamLocs.get("Cyan").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 10)
{
if (!TeamLocs.containsKey("Purple"))
TeamLocs.put("Purple", new ArrayList<Location>());
TeamLocs.get("Purple").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 11)
{
if (!TeamLocs.containsKey("DBlue"))
TeamLocs.put("DBlue", new ArrayList<Location>());
TeamLocs.get("DBlue").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 12)
{
if (!TeamLocs.containsKey("Brown"))
TeamLocs.put("Brown", new ArrayList<Location>());
TeamLocs.get("Brown").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 13)
{
if (!TeamLocs.containsKey("Green"))
TeamLocs.put("Green", new ArrayList<Location>());
TeamLocs.get("Green").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 14)
{
if (!TeamLocs.containsKey("Red"))
TeamLocs.put("Red", new ArrayList<Location>());
TeamLocs.get("Red").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (wool.getData() == 15)
{
if (!TeamLocs.containsKey("Black"))
TeamLocs.put("Black", new ArrayList<Location>());
TeamLocs.get("Black").add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
}
}
if (block.getTypeId() != 148)
continue;
Block wool = block.getRelative(BlockFace.DOWN);
if (wool == null)
continue;
if (wool.getType() != Material.WOOL)
continue;
Wool woolData = new Wool(wool.getType(), wool.getData());
String dataType = woolData.getColor().name();
if (!DataLocs.containsKey(dataType))
DataLocs.put(dataType, new ArrayList<Location>());
DataLocs.get(dataType).add(wool.getLocation());
//Remove Blocks
block.setTypeId(0);
wool.setTypeId(0);
}
if (cornerA == null || cornerB == null)
{
caller.sendMessage("Missing Corner Locations!");
return;
}
//Save
try
{
FileWriter fstream = new FileWriter(world.getName() + File.separator + "WorldConfig.dat");
2014-06-15 02:12:08 +02:00
BufferedWriter out = new BufferedWriter(fstream);
out.write("MAP_NAME:");
out.write("\n");
out.write("MAP_AUTHOR:");
out.write("\n");
out.write("\n");
out.write("MIN_X:"+Math.min(cornerA.getBlockX(), cornerB.getBlockX()));
out.write("\n");
out.write("MAX_X:"+Math.max(cornerA.getBlockX(), cornerB.getBlockX()));
out.write("\n");
out.write("MIN_Z:"+Math.min(cornerA.getBlockZ(), cornerB.getBlockZ()));
out.write("\n");
out.write("MAX_Z:"+Math.max(cornerA.getBlockZ(), cornerB.getBlockZ()));
out.write("\n");
out.write("\n");
if (cornerA.getBlockY() == cornerB.getBlockY())
{
out.write("MIN_Y:0");
out.write("\n");
out.write("MAX_Y:256");
}
else
{
out.write("MIN_Y:"+Math.min(cornerA.getBlockY(), cornerB.getBlockY()));
out.write("\n");
out.write("MAX_Y:"+Math.max(cornerA.getBlockY(), cornerB.getBlockY()));
}
//Teams
for (String team : TeamLocs.keySet())
{
out.write("\n");
out.write("\n");
out.write("TEAM_NAME:" + team);
out.write("\n");
out.write("TEAM_SPAWNS:" + LocationsToString(TeamLocs.get(team)));
}
//Data
for (String data : DataLocs.keySet())
{
out.write("\n");
out.write("\n");
out.write("DATA_NAME:" + data);
out.write("\n");
out.write("DATA_LOCS:" + LocationsToString(DataLocs.get(data)));
}
//Custom
for (String data : CustomLocs.keySet())
{
out.write("\n");
out.write("\n");
out.write("CUSTOM_NAME:" + data);
out.write("\n");
out.write("CUSTOM_LOCS:" + LocationsToString(CustomLocs.get(data)));
}
out.close();
}
catch (Exception e)
{
caller.sendMessage("Error: File Write Error");
}
caller.sendMessage("World Data Saved.");
}
public String LocationsToString(ArrayList<Location> locs)
{
String out = "";
for (Location loc : locs)
out += loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ() + ":";
return out;
}
public String LocationSignsToString(HashMap<Location, String> locs)
{
String out = "";
for (Location loc : locs.keySet())
out += locs.get(loc) + "@" + loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ() + ":";
return out;
}
}