use of com.elmakers.mine.bukkit.api.maps.MapController in project MagicPlugin by elBukkit.
the class MagicMapCommandExecutor method onMapRestore.
protected void onMapRestore(CommandSender sender, World world, int mapId) {
final boolean backwards = mapId > 1;
String direction = ChatColor.YELLOW + " " + (backwards ? "moving backward" : "moving forward");
sender.sendMessage(ChatColor.AQUA + "Restoring maps, starting at id " + ChatColor.DARK_AQUA + mapId + direction);
// Getting dirty now!
MapController apiController = api.getController().getMaps();
com.elmakers.mine.bukkit.maps.MapController mapController = (com.elmakers.mine.bukkit.maps.MapController) apiController;
List<URLMap> maps = mapController.getAll();
Set<String> urls = new HashSet<>();
Set<Integer> ids = new HashSet<>();
int maxId = 0;
final String skinURL = "http://skins.minecraft.net/MinecraftSkins/";
final String alternateSkinURL = "http://s3.amazonaws.com/MinecraftSkins/";
for (URLMap map : maps) {
maxId = Math.max(maxId, map.getId());
if (map.getURL().startsWith(skinURL) && map.getName() == null) {
map.setName("Photo of " + map.getURL().replace(skinURL, "").replace(".png", ""));
sender.sendMessage("Added name to " + map.getName());
} else if (map.getURL().startsWith(alternateSkinURL) && map.getName() == null) {
map.setName("Photo of " + map.getURL().replace(alternateSkinURL, "").replace(".png", ""));
sender.sendMessage("Added name to " + map.getName());
}
urls.add(map.getURL());
ids.add((int) map.getId());
}
if (!backwards) {
mapId = maxId;
}
int addedFiles = 0;
File[] cacheFiles = mapController.getCacheFolder().listFiles();
Arrays.sort(cacheFiles, new Comparator<File>() {
@Override
public int compare(File f1, File f2) {
if (backwards) {
return Long.compare(f2.lastModified(), f1.lastModified());
}
return Long.compare(f1.lastModified(), f2.lastModified());
}
});
for (File cacheFile : cacheFiles) {
try {
while (mapId > 0 && ids.contains(mapId)) mapId--;
if (mapId <= 0)
break;
if (cacheFile.getName().startsWith(".") || cacheFile.isDirectory())
continue;
String url = URLDecoder.decode(cacheFile.getName(), "UTF-8");
if (urls.contains(url)) {
sender.sendMessage("Skipping " + url);
continue;
}
String name = null;
int x = 0;
int y = 0;
int width = 0;
int height = 0;
Integer xOverlay = null;
Integer yOverlay = null;
if (url.startsWith(skinURL) || url.startsWith(alternateSkinURL)) {
name = "Photo of " + url.replace(skinURL, "").replace(alternateSkinURL, "").replace(".png", "");
x = 8;
y = 8;
width = 8;
height = 8;
xOverlay = 40;
yOverlay = 8;
sender.sendMessage("Added " + mapId + " as player skin: " + name);
} else {
sender.sendMessage("Added " + mapId + " as: " + url);
}
mapController.get(world.getName(), (short) mapId, url, name, x, y, xOverlay, yOverlay, width, height, null);
addedFiles++;
if (backwards) {
mapId--;
} else {
mapId++;
}
} catch (UnsupportedEncodingException ex) {
sender.sendMessage("Error decoding: " + cacheFile.getName());
}
}
mapController.save();
sender.sendMessage(ChatColor.AQUA + "Restored " + ChatColor.DARK_AQUA + addedFiles);
}
use of com.elmakers.mine.bukkit.api.maps.MapController in project MagicPlugin by elBukkit.
the class MagicMapCommandExecutor method onMapName.
protected void onMapName(CommandSender sender, String mapName) {
if (!(sender instanceof Player)) {
sender.sendMessage("This command only works in-game");
return;
}
Player player = (Player) sender;
ItemStack currentMap = player.getInventory().getItemInMainHand();
if (currentMap == null || currentMap.getType() != Material.MAP) {
sender.sendMessage("You must be holding a map");
return;
}
MapController mapController = api.getController().getMaps();
URLMap map = mapController.getMap(currentMap.getDurability());
if (map == null) {
sender.sendMessage("Map id " + currentMap.getDurability() + " is not registered");
return;
}
map.setName(mapName);
mapController.save();
sender.sendMessage("Renamed map id " + map.getId() + " to " + map.getName());
player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
}
use of com.elmakers.mine.bukkit.api.maps.MapController in project MagicPlugin by elBukkit.
the class MagicMapCommandExecutor method onMapLoad.
protected void onMapLoad(CommandSender sender, World world, String url, String mapName, int width, int height, int x, int y, Integer priority) {
MapController maps = api.getController().getMaps();
ItemStack item = maps.getURLItem(world.getName(), url, mapName, x, y, width, height, priority);
if (item == null) {
sender.sendMessage("Failed to load map: " + url);
return;
}
short mapId = item.getDurability();
sender.sendMessage("Loaded map id " + mapId);
if (sender instanceof Player) {
ItemStack mapItem = maps.getMapItem(mapId);
api.giveItemToPlayer((Player) sender, mapItem);
}
}
use of com.elmakers.mine.bukkit.api.maps.MapController in project MagicPlugin by elBukkit.
the class MagicMapCommandExecutor method onMapFix.
protected void onMapFix(CommandSender sender, World world, int retries) {
sender.sendMessage(ChatColor.AQUA + "Fixing maps, using up to " + ChatColor.DARK_AQUA + retries + ChatColor.AQUA + " ids at a time.");
MapController mapController = api.getController().getMaps();
List<URLMap> maps = mapController.getAll();
int fixed = 0;
int notFixed = 0;
int skipped = 0;
for (URLMap map : maps) {
if (map.isEnabled()) {
skipped++;
} else {
if (map.fix(world, retries)) {
fixed++;
} else {
notFixed++;
}
}
}
mapController.save();
sender.sendMessage(ChatColor.AQUA + "Fixed " + ChatColor.DARK_AQUA + fixed + ChatColor.AQUA + " and skipped " + ChatColor.DARK_AQUA + skipped + ChatColor.AQUA + " maps");
if (notFixed > 0) {
sender.sendMessage(ChatColor.RED + "There are still " + ChatColor.DARK_RED + notFixed + ChatColor.RED + " maps disabled, you may want to try running this command again.");
}
}
use of com.elmakers.mine.bukkit.api.maps.MapController in project MagicPlugin by elBukkit.
the class MagicMapCommandExecutor method onCommand.
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!api.hasPermission(sender, "Magic.commands.mmap")) {
sendNoPermission(sender);
return true;
}
if (args.length == 0) {
sender.sendMessage("Usage: mmap [list|give|load|import|player|fix|restore|unnamed|name]");
return true;
}
String subCommand = args[0];
if (!api.hasPermission(sender, "Magic.commands.mmap." + subCommand)) {
sendNoPermission(sender);
return true;
}
World world = null;
if (sender instanceof Player) {
world = ((Player) sender).getWorld();
} else {
world = Bukkit.getWorlds().get(0);
}
if (subCommand.equalsIgnoreCase("list")) {
String keyword = "";
for (int i = 1; i < args.length; i++) {
if (i != 1)
keyword = keyword + " ";
keyword = keyword + args[i];
}
onMapList(sender, keyword);
} else if (subCommand.equalsIgnoreCase("give")) {
if (args.length == 1) {
sender.sendMessage("Usage: mmap give <player> [id]");
return true;
}
Player recipient = null;
String mapId = null;
if (args.length == 2) {
mapId = args[1];
} else if (args.length > 2) {
mapId = args[2];
}
if (args.length == 2 && sender instanceof Player) {
recipient = (Player) sender;
} else if (args.length > 2) {
recipient = DeprecatedUtils.getPlayer(args[1]);
}
if (recipient == null) {
if (args.length > 2) {
sender.sendMessage("Unknown player: " + args[1]);
return true;
} else {
sender.sendMessage("Console usage: mmap give [player] [id]");
return true;
}
}
Short parsedId = null;
if (mapId != null) {
try {
parsedId = Short.parseShort(mapId);
} catch (Exception ignored) {
}
}
if (parsedId == null) {
sender.sendMessage("Invalid map id, expecting integer: " + mapId);
return true;
}
onMapGive(sender, recipient, parsedId);
} else if (subCommand.equalsIgnoreCase("remove")) {
if (args.length == 1) {
sender.sendMessage("Usage: mmap delete [id]");
return true;
}
String mapId = args[1];
Short parsedId = null;
if (mapId != null) {
try {
parsedId = Short.parseShort(mapId);
} catch (Exception ignored) {
}
}
if (parsedId == null) {
sender.sendMessage("Invalid map id, expecting integer: " + mapId);
return true;
}
MapController maps = api.getController().getMaps();
if (maps.remove(parsedId)) {
sender.sendMessage("Unregistered map id: " + parsedId);
} else {
sender.sendMessage("Map id " + parsedId + " is not registered");
}
} else if (subCommand.equalsIgnoreCase("player")) {
if (args.length <= 1) {
sender.sendMessage("Usage: mmap player <name>");
return true;
}
String playerName = args[1];
onMapPlayer(sender, world, playerName);
} else if (subCommand.equalsIgnoreCase("fix")) {
int limit = 100;
if (args.length > 1) {
try {
limit = Integer.parseInt(args[1]);
} catch (Exception ignored) {
}
}
onMapFix(sender, world, limit);
} else if (subCommand.equalsIgnoreCase("name")) {
if (args.length <= 1) {
sender.sendMessage("Usage: mmap name <name>");
}
String mapName = args[1];
for (int i = 2; i < args.length; i++) {
mapName = mapName + " " + args[i];
}
onMapName(sender, mapName);
} else if (subCommand.equalsIgnoreCase("unnamed")) {
onMapUnnamed(sender);
} else if (subCommand.equalsIgnoreCase("restore")) {
int startingId = 1;
if (args.length > 1) {
try {
startingId = Integer.parseInt(args[1]);
} catch (Exception ignored) {
}
}
onMapRestore(sender, world, startingId);
} else if (subCommand.equalsIgnoreCase("load")) {
if (args.length == 1) {
sender.sendMessage("Usage: mmap load <file/url> [name] [width] [height] [x] [y]");
return true;
}
int width = 0;
int height = 0;
int x = 0;
int y = 0;
Integer priority = null;
String mapName = null;
if (args.length > 2) {
mapName = args[2];
}
if (args.length > 3) {
try {
width = Integer.parseInt(args[3]);
} catch (Exception ex) {
sender.sendMessage("Invalid width: " + args[3]);
}
}
if (args.length > 4) {
try {
height = Integer.parseInt(args[4]);
} catch (Exception ex) {
sender.sendMessage("Invalid height: " + args[4]);
}
}
if (args.length > 5) {
try {
x = Integer.parseInt(args[5]);
} catch (Exception ex) {
sender.sendMessage("Invalid x: " + args[5]);
}
}
if (args.length > 6) {
try {
y = Integer.parseInt(args[6]);
} catch (Exception ex) {
sender.sendMessage("Invalid y: " + args[6]);
}
}
onMapLoad(sender, world, args[1], mapName, width, height, x, y, priority);
} else if (subCommand.equalsIgnoreCase("import")) {
String path = "plugins/Pixelator/renderers.cache";
if (args.length > 1) {
path = args[1];
}
onMapImport(sender, world, path);
} else {
sender.sendMessage("Usage: mmap [list|remove|give|load|import]");
}
return true;
}
Aggregations