package mineplex.mapparser; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import mineplex.core.common.util.C; import mineplex.core.common.util.Callback; import mineplex.core.common.util.F; import mineplex.core.common.util.MapUtil; import mineplex.core.common.util.UtilBlock; import mineplex.core.common.util.UtilEvent; import mineplex.core.common.util.UtilEvent.ActionType; import mineplex.core.common.util.UtilGear; import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilTime; import mineplex.core.common.util.UtilWorld; import mineplex.mapparser.command.*; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.block.BlockFadeEvent; import org.bukkit.event.block.BlockFormEvent; import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockIgniteEvent.IgniteCause; import org.bukkit.event.block.BlockSpreadEvent; import org.bukkit.event.block.LeavesDecayEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; 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.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.Vector; public class MapParser extends JavaPlugin implements Listener { private WorldManager _worldManager; private Parse _curParse = null; private HashMap _mapData = new HashMap(); private HashSet _mapsBeingZipped = new HashSet(); private List _commands = new ArrayList(); private Location _spawnLocation; private HashMap _permissionMap = new HashMap(); @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); _commands.add(new AuthorCommand(this)); _commands.add(new AdminCommand(this)); _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 ParseCommand200(this)); _commands.add(new ParseCommand400(this)); _commands.add(new ParseCommand600(this)); _commands.add(new RenameCommand(this)); _commands.add(new SaveCommand(this)); _commands.add(new WorldsCommand(this)); _commands.add(new CopyCommand(this)); _commands.add(new SpawnCommand(this)); _commands.add(new SetSpawnCommand(this)); _commands.add(new ItemNameCommand(this)); _commands.add(new AddLoreCommand(this)); _commands.add(new ClearLoreCommand(this)); } @Override public void onDisable() { } @EventHandler public void PlayerJoin(PlayerJoinEvent event) { 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); player.addAttachment(plugin, "coloredsigns.format", 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;")); 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 ", "Set name for current map")); UtilPlayer.message(player, F.value("/author ", "Set author for current map")); UtilPlayer.message(player, F.value("/gametype ", "Set gametype for current map")); UtilPlayer.message(player, " "); UtilPlayer.message(player, F.value("/admin ", "Toggle admin for player on map")); UtilPlayer.message(player, " "); UtilPlayer.message(player, F.value("/create [gametype]", "Creates a new map")); UtilPlayer.message(player, F.value("/delete [gametype]", "Deletes an existing map")); UtilPlayer.message(player, F.value("/copy ", "Copies an existing map")); UtilPlayer.message(player, " "); UtilPlayer.message(player, F.value("/list", "List maps")); UtilPlayer.message(player, F.value("/map [gametype]", "Teleport to a map")); UtilPlayer.message(player, " "); UtilPlayer.message(player, C.cYellow + "Documentation: " + C.cGreen + "http://tinyurl.com/mpxmaps"); } @EventHandler public void Command(PlayerCommandPreprocessEvent event) { 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) { UtilPlayer.message(player, F.main("Parser", "Cannot use commands during Map Parse!")); return; } if (event.getMessage().toLowerCase().startsWith("/help")) { event.setCancelled(true); DisplayHelp(player); } for (BaseCommand command : _commands) { for (String alias : command.getAliases()) { if (alias.equalsIgnoreCase(commandLabel)) { event.setCancelled(true); if (!command.execute(player, commandLabel, args)) { UtilPlayer.message(player, F.main("Parser", "Invalid Input.")); UtilPlayer.message(player, F.elem(command.getUsage())); } return; } } } } public void sendValidGameTypes(Player player) { UtilPlayer.message(player, F.main("Parser", "Valid Game Types;")); String gameTypes = ""; for (GameType game : GameType.values()) { gameTypes += game.toString() + " "; } player.sendMessage(gameTypes); } @EventHandler public void ParseUpdate(TickEvent event) { if (_curParse == null) return; if (_curParse.Update()) { Announce("Parse Completed!"); Announce("Cleaning and Creating ZIP..."); try { _worldManager.finalizeParsedWorld(_curParse.getWorld()); } catch (Exception e) { Announce("Creating ZIP Failed! Please Try Again!"); e.printStackTrace(); } _curParse = null; } } @EventHandler public void DisableCreatures(EntitySpawnEvent event) { if (event.getEntityType() == EntityType.DROPPED_ITEM || event.getEntity() instanceof LivingEntity) event.setCancelled(true); } @EventHandler public void DisableBurn(BlockBurnEvent event) { event.setCancelled(true); } @EventHandler public void DisableIgnite(BlockIgniteEvent event) { if (event.getCause() == IgniteCause.LAVA || event.getCause() == IgniteCause.SPREAD) 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 DisableIceForm(BlockFormEvent event) { event.setCancelled(true); } @EventHandler public void Updates(PlayerMoveEvent event) { for (World world : this.getServer().getWorlds()) { if (world.getName().toLowerCase().contains("halloween")) world.setTime(16000); else world.setTime(8000); 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; if (world.getName().startsWith("parse_")) continue; if (!world.getName().startsWith("map")) 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() { @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); } public boolean DoesMapExist(String mapName, GameType gameType) { return DoesMapExist(getWorldString(mapName, gameType)); } public boolean DoesMapExist(String worldName) { File file = new File(worldName); if (file.exists() && file.isDirectory()) return true; return false; } 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()) { if (world.getName().equals(worldName)) return world; } return null; } public String getWorldString(String mapName, GameType type) { return "map" + "/" + type.GetName() + "/" + mapName; } public List getMapsByName(String name) { name = name.toLowerCase(); List maps = new LinkedList(); boolean matchesExact = false; for (GameType type : GameType.values()) { 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 it = maps.iterator(); while (it.hasNext()) { String mapString = it.next(); if (!mapString.toLowerCase().endsWith(name)) { it.remove(); } } } return maps; } 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) { // 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)); // player.getInventory().addItem(new ItemStack(Material.WOOD_AXE)); } public WorldManager getWorldManager() { return _worldManager; } public void setCurrentParse(Parse parse) { _curParse = parse; } @EventHandler public void Chat(AsyncPlayerChatEvent event) { event.setCancelled(true); String world = C.cDGreen + C.Bold + getShortWorldName(event.getPlayer().getWorld().getName()); String name = C.cYellow + event.getPlayer().getName(); 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 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 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; } 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); } } @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>> treeHistory = new HashMap>>(); @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 toRemove = searchLog(new HashSet(), event.getClickedBlock()); if (toRemove.isEmpty()) { player.sendMessage(C.cRed + C.Bold + "TreeTool: " + ChatColor.RESET + "Left-Click on Log"); return; } HashSet history = new HashSet(); for (Block block : toRemove) { history.add(new BlockData(block)); block.setType(Material.AIR); } if (!treeHistory.containsKey(player)) treeHistory.put(player, new ArrayList>()); 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 datas = treeHistory.get(player).remove(0); for (BlockData data : datas) data.restore(); player.sendMessage(C.cGreen + C.Bold + "TreeTool: " + ChatColor.RESET + "Tree Restored"); } } @EventHandler public void mmMazeParser(PlayerInteractEvent event) { if (event.isCancelled()) return; if (event.getAction() != Action.LEFT_CLICK_BLOCK) return; //Permission if (!GetData(event.getPlayer().getWorld().getName()).HasAccess(event.getPlayer())) { return; } Player player = event.getPlayer(); if (!UtilGear.isMat(player.getItemInHand(), Material.WEB)) return; event.setCancelled(true); // parse Block clicked = event.getClickedBlock(); Location center = clicked.getLocation(); Location lowestCorner = center.clone().subtract(49, 0, 49); // 0 = air or other // 1 = path - quartz // 2 = mob spawn - gold // 3 = safe spawn - stone int[][] maze = new int[99][99]; /*Iterator iter = blocks.iterator(); while(iter.hasNext()) { Block b = iter.next(); switch (b.getType()) { case QUARTZ_BLOCK: maze[x][y] = 1; break; case GOLD_BLOCK: maze[x][y] = 2; break; case STONE: maze[x][y] = 3; break; default: maze[x][y] = 0; break; } x++; if(x > 99) { y++; x = 0; } if(y > 99) { System.out.println("y > 99"); break; } }*/ for (int i = 0; i < 99; i++) for (int j = 0; j < 99; j++) maze[i][j] = getMMParseValue(lowestCorner.clone().add(j, 0, i).getBlock().getType()); //Save try { FileWriter fstream = new FileWriter(GetData(player.getWorld().getName()).MapFolder + File.separator + "Maze.dat"); BufferedWriter out = new BufferedWriter(fstream); out.write("private static final int[][] PARSED_MAZE = {" + System.lineSeparator()); for (int j[] : maze) { out.write("{"); boolean first = true; for (int k : j) { if(!first) out.write(","); out.write(k + ""); first = false; } out.write("}," + System.lineSeparator()); } out.write("};" + System.lineSeparator()); out.close(); } catch (Exception e) { player.sendMessage(C.cRed + C.Bold + "MMMazeParse: " + ChatColor.RESET + "An error has occured, see console."); e.printStackTrace(); } player.sendMessage(C.cGreen + C.Bold + "MMMazeParse: " + ChatColor.RESET + "Maze parsed."); } private int getMMParseValue(Material m) { switch (m) { case QUARTZ_BLOCK: return 1; case GOLD_BLOCK: return 2; case STONE: return 3; case DIRT: return 4; case COBBLESTONE: return 5; case BRICK: return 6; default: return 0; } } private HashSet searchLog(HashSet 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; } long lastUse = 0; @EventHandler(priority = EventPriority.LOWEST) public void leaptest(PlayerInteractEvent event) { if (event.getPlayer().getItemInHand() == null) return; if (event.getPlayer().getItemInHand().getType() != Material.FEATHER) return; if (System.currentTimeMillis() - lastUse < 800) { Bukkit.broadcastMessage("Cooldown"); return; } lastUse = System.currentTimeMillis(); Vector vel = event.getPlayer().getLocation().getDirection().multiply(1.2); vel.add(new Vector(0,0.6,0)); event.getPlayer().setVelocity(vel); } @EventHandler(priority = EventPriority.LOWEST) public void signChangeLog(SignChangeEvent event) { if (GetData(event.getPlayer().getWorld().getName()).HasAccess(event.getPlayer())) { ArrayList text = new ArrayList(); text.add("Date: " + UtilTime.now()); text.add("Player: " + event.getPlayer().getName()); text.add("Location: " + UtilWorld.locToStrClean(event.getBlock().getLocation())); for (int i=0 ; i text = new ArrayList(); text.add("Date: " + UtilTime.now()); text.add("Player: " + event.getPlayer().getName()); text.add("Location: " + UtilWorld.locToStrClean(event.getPlayer().getLocation())); text.add("Message: " + event.getMessage()); writeSignCommandLog(text, event.getPlayer().getWorld()); } } public void writeSignCommandLog(ArrayList text, World world) { try { File file = new File(world.getName() + "/" + "command_sign_log.txt"); if (!file.exists()) file.createNewFile(); FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); bw.write("\n\n"); for (String line : text) bw.write("\n" + line); bw.close(); } catch (Exception e) { e.printStackTrace(); } } public void writeSignLog(ArrayList text, World world) { try { File file = new File(world.getName() + "/" + "sign_log.txt"); if (!file.exists()) file.createNewFile(); FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); bw.write("\n\n"); for (String line : text) bw.write("\n" + line); bw.close(); } catch (Exception e) { e.printStackTrace(); } } }