From b01f133639ce34255ca6eb2ecde25b19aee88594 Mon Sep 17 00:00:00 2001 From: Vlammar Date: Thu, 16 Jul 2020 02:50:17 +0200 Subject: [PATCH] Update working --- .../fr/moribus/imageonmap/ImageOnMap.java | 3 +- .../fr/moribus/imageonmap/Permissions.java | 1 + .../imageonmap/commands/IoMCommand.java | 1 - .../commands/maptool/UpdateCommand.java | 234 ++++++++++++++++++ .../image/ImageRendererExecutor.java | 123 +++++++-- .../fr/moribus/imageonmap/map/ImageMap.java | 32 ++- .../fr/moribus/imageonmap/map/MapManager.java | 8 +- .../imageonmap/map/PlayerMapStore.java | 8 +- 8 files changed, 376 insertions(+), 34 deletions(-) create mode 100644 src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java diff --git a/src/main/java/fr/moribus/imageonmap/ImageOnMap.java b/src/main/java/fr/moribus/imageonmap/ImageOnMap.java index 2c1361d..f545743 100644 --- a/src/main/java/fr/moribus/imageonmap/ImageOnMap.java +++ b/src/main/java/fr/moribus/imageonmap/ImageOnMap.java @@ -119,7 +119,8 @@ public final class ImageOnMap extends ZPlugin DeleteCommand.class, GetRemainingCommand.class, ExploreCommand.class, - MigrateCommand.class + MigrateCommand.class, + UpdateCommand.class ); Commands.registerShortcut("maptool", NewCommand.class, "tomap"); diff --git a/src/main/java/fr/moribus/imageonmap/Permissions.java b/src/main/java/fr/moribus/imageonmap/Permissions.java index 6667243..4ca6a8d 100644 --- a/src/main/java/fr/moribus/imageonmap/Permissions.java +++ b/src/main/java/fr/moribus/imageonmap/Permissions.java @@ -45,6 +45,7 @@ public enum Permissions GET("imageonmap.get"), RENAME("imageonmap.rename"), DELETE("imageonmap.delete"), + UPDATE("imageonmap.update"), ADMINISTRATIVE("imageonmap.administrative"), BYPASS_SIZE("imageonmap.bypasssize") diff --git a/src/main/java/fr/moribus/imageonmap/commands/IoMCommand.java b/src/main/java/fr/moribus/imageonmap/commands/IoMCommand.java index b3203b4..25efd58 100644 --- a/src/main/java/fr/moribus/imageonmap/commands/IoMCommand.java +++ b/src/main/java/fr/moribus/imageonmap/commands/IoMCommand.java @@ -69,7 +69,6 @@ public abstract class IoMCommand extends Command } mapName = mapName.trim(); - map = MapManager.getMap(player.getUniqueId(), mapName); if(map == null) error(I.t("This map does not exist.")); diff --git a/src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java b/src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java new file mode 100644 index 0000000..1be5743 --- /dev/null +++ b/src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java @@ -0,0 +1,234 @@ +/* + * Copyright or © or Copr. Moribus (2013) + * Copyright or © or Copr. ProkopyL (2015) + * Copyright or © or Copr. Amaury Carrade (2016 – 2020) + * Copyright or © or Copr. Vlammar (2019 – 2020) + * + * This software is a computer program whose purpose is to allow insertion of + * custom images in a Minecraft world. + * + * This software is governed by the CeCILL-B license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL-B + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL-B license and that you accept its terms. + */ + +package fr.moribus.imageonmap.commands.maptool; + +import fr.moribus.imageonmap.Permissions; +import fr.moribus.imageonmap.commands.IoMCommand; +import fr.moribus.imageonmap.image.ImageRendererExecutor; +import fr.moribus.imageonmap.image.ImageUtils; +import fr.moribus.imageonmap.map.ImageMap; +import fr.moribus.imageonmap.map.MapManager; +import fr.zcraft.zlib.components.commands.CommandException; +import fr.zcraft.zlib.components.commands.CommandInfo; +import fr.zcraft.zlib.components.commands.WithFlags; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.worker.WorkerCallback; +import fr.zcraft.zlib.tools.PluginLogger; +import fr.zcraft.zlib.tools.text.ActionBar; +import fr.zcraft.zlib.tools.text.MessageSender; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +@CommandInfo (name = "update", usageParameters = " [--confirm]") +@WithFlags ({"confirm"}) +public class UpdateCommand extends IoMCommand +{ + @Override + protected void run() throws CommandException + { + final Player player = playerSender(); + ImageUtils.ScalingType scaling = ImageUtils.ScalingType.NONE; + URL url; + + + if(args.length < 1) throwInvalidArgument(I.t("You must give an URL and a map name to update.")); + if(args.length < 2) throwInvalidArgument(I.t("You must give a map name to update.")); + + ImageMap map=getMapFromArgs(player,1,true); + + try + { + url = new URL(args[0]); + MapManager.load(); + Integer[] size={1,1}; + if(map.getType()== ImageMap.Type.POSTER) + size=map.getSize( new HashMap(),map.getUserUUID(),map.getId()); + + int width=size[0],height=size[1]; + + try { + + + ActionBar.sendPermanentMessage(player, ChatColor.DARK_GREEN + I.t("Updating...")); + ImageRendererExecutor.update(url, scaling, player.getUniqueId(), map, width, height, new WorkerCallback() { + @Override + public void finished(ImageMap result) { + ActionBar.removeMessage(player); + MessageSender.sendActionBarMessage(player, ChatColor.DARK_GREEN + I.t("Updating finished!")); + + + } + + @Override + public void errored(Throwable exception) { + player.sendMessage(I.t("{ce}Map rendering failed: {0}", exception.getMessage())); + + PluginLogger.warning("Rendering from {0} failed: {1}: {2}", + player.getName(), + exception.getClass().getCanonicalName(), + exception.getMessage()); + } + }); + } + //Added to fix bug with rendering displaying after error + finally { + ActionBar.removeMessage(player); + } + + } + catch(MalformedURLException ex) + { + throwInvalidArgument(I.t("Invalid URL.")); + return; + } + /*catch (MapManagerException ex){ + throwInvalidArgument(I.t("Update failed.")); + return; + }*/ + + + + + + /* if(args.length >= 2) + { + if(args.length >= 4) { + width = Integer.parseInt(args[2]); + height = Integer.parseInt(args[3]); + } + + switch(args[1]) { + case "resize": scaling = ImageUtils.ScalingType.CONTAINED; break; + case "resize-stretched": scaling = ImageUtils.ScalingType.STRETCHED; break; + case "resize-covered": scaling = ImageUtils.ScalingType.COVERED; break; + default: throwInvalidArgument(I.t("Invalid Stretching mode.")); break; + } + }*/ + /* try { + + + ActionBar.sendPermanentMessage(player, ChatColor.DARK_GREEN + I.t("Updating map...")); + //ImageRendererExecutor.render(url, scaling, player.getUniqueId(), width, height, new WorkerCallback() { + @Override + public void finished(ImageMap result) { + ActionBar.removeMessage(player); + MessageSender.sendActionBarMessage(player, ChatColor.DARK_GREEN + I.t("Rendering finished!")); + + if (result.give(player) && (result instanceof PosterMap && !((PosterMap) result).hasColumnData())) { + info(I.t("The rendered map was too big to fit in your inventory.")); + info(I.t("Use '/maptool getremaining' to get the remaining maps.")); + } + } + + @Override + public void errored(Throwable exception) { + player.sendMessage(I.t("{ce}Map rendering failed: {0}", exception.getMessage())); + + PluginLogger.warning("Rendering from {0} failed: {1}: {2}", + player.getName(), + exception.getClass().getCanonicalName(), + exception.getMessage()); + } + }); + } + //Added to fix bug with rendering displaying after error + finally { + ActionBar.removeMessage(player); + }*/ + } + + +/* + + @Override + protected void run() throws CommandException + { + ImageMap map = getMapFromArgs(); + + if (!hasFlag("confirm")) + { + RawText msg = new RawText(I.t("You are going to update") + " ") + .then(map.getId()) + .color(ChatColor.GOLD) + .then(". " + I.t("Are you sure ? ")) + .color(ChatColor.WHITE) + .then(I.t("[Confirm]")) + .color(ChatColor.GREEN) + .hover(new RawText(I.t("{red}This map will be updated {bold}forever{red}!"))) + .command(getClass(), map.getId(), "--confirm") + .build(); + + send(msg); + } + else + { + Player player = playerSender(); + MapManager.clear(player.getInventory(), map); + + try + { + MapManager.updateMap(map); + info(I.t("Map successfully updated.")); + } + catch (MapManagerException ex) + { + PluginLogger.warning("A non-existent map was requested to be updated", ex); + warning(I.t("This map does not exist.")); + } + } + } + + @Override + protected List complete() throws CommandException + { + if(args.length == 1) + return getMatchingMapNames(playerSender(), args[0]); + + return null; + } +*/ + @Override + public boolean canExecute(CommandSender sender) + { + return Permissions.UPDATE.grantedTo(sender); + } +} diff --git a/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java b/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java index 3811c8f..b52da7b 100644 --- a/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java +++ b/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java @@ -61,6 +61,39 @@ import java.util.concurrent.Future; @WorkerAttributes(name = "Image Renderer", queriesMainThread = true) public class ImageRendererExecutor extends Worker { + static private URLConnection HTTPconnection(final URL url) throws IOException { + final URLConnection connection = url.openConnection(); + connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); + connection.connect(); + + if (connection instanceof HttpURLConnection) + { + final HttpURLConnection httpConnection = (HttpURLConnection) connection; + final int httpCode = httpConnection.getResponseCode(); + if ((httpCode / 100) != 2) + { + throw new IOException(I.t("HTTP error: {0} {1}", httpCode, httpConnection.getResponseMessage())); + } + } + return connection; + } + + static private void checkSizeLimit(final UUID playerUUID, final BufferedImage image ) throws IOException { + if ((PluginConfiguration.LIMIT_SIZE_X.get() > 0 || PluginConfiguration.LIMIT_SIZE_Y.get() > 0) && !Permissions.BYPASS_SIZE.grantedTo(Bukkit.getPlayer(playerUUID))) + { + if (PluginConfiguration.LIMIT_SIZE_X.get() > 0) + { + if (image.getWidth() > PluginConfiguration.LIMIT_SIZE_X.get()) + throw new IOException(I.t("The image is too wide!")); + } + if (PluginConfiguration.LIMIT_SIZE_Y.get() > 0) + { + if (image.getHeight() > PluginConfiguration.LIMIT_SIZE_Y.get()) + throw new IOException(I.t("The image is too tall!")); + } + } + } + static public void render(final URL url, final ImageUtils.ScalingType scaling, final UUID playerUUID, final int width, final int height, WorkerCallback callback) { submitQuery(new WorkerRunnable() @@ -68,39 +101,17 @@ public class ImageRendererExecutor extends Worker @Override public ImageMap run() throws Throwable { - final URLConnection connection = url.openConnection(); - connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); - connection.connect(); - if (connection instanceof HttpURLConnection) - { - final HttpURLConnection httpConnection = (HttpURLConnection) connection; - final int httpCode = httpConnection.getResponseCode(); - if ((httpCode / 100) != 2) - { - throw new IOException(I.t("HTTP error: {0} {1}", httpCode, httpConnection.getResponseMessage())); - } - } + final URLConnection connection = HTTPconnection(url); final InputStream stream = connection.getInputStream(); final BufferedImage image = ImageIO.read(stream); + stream.close(); if (image == null) throw new IOException(I.t("The given URL is not a valid image")); // Limits are in place and the player does NOT have rights to avoid them. - if ((PluginConfiguration.LIMIT_SIZE_X.get() > 0 || PluginConfiguration.LIMIT_SIZE_Y.get() > 0) && !Permissions.BYPASS_SIZE.grantedTo(Bukkit.getPlayer(playerUUID))) - { - if (PluginConfiguration.LIMIT_SIZE_X.get() > 0) - { - if (image.getWidth() > PluginConfiguration.LIMIT_SIZE_X.get()) - throw new IOException(I.t("The image is too wide!")); - } - if (PluginConfiguration.LIMIT_SIZE_Y.get() > 0) - { - if (image.getHeight() > PluginConfiguration.LIMIT_SIZE_Y.get()) - throw new IOException(I.t("The image is too tall!")); - } - } + checkSizeLimit(playerUUID, image ); if (scaling != ImageUtils.ScalingType.NONE && height <= 1 && width <= 1) { @@ -113,6 +124,66 @@ public class ImageRendererExecutor extends Worker }, callback); } + public static void update(final URL url, final ImageUtils.ScalingType scaling, final UUID playerUUID, final ImageMap map, final int width, final int height, WorkerCallback callback) { + submitQuery(new WorkerRunnable() + { + @Override + public ImageMap run() throws Throwable + { + + final URLConnection connection = HTTPconnection(url); + + final InputStream stream = connection.getInputStream(); + final BufferedImage image = ImageIO.read(stream); + stream.close(); + + if (image == null) throw new IOException(I.t("The given URL is not a valid image")); + + // Limits are in place and the player does NOT have rights to avoid them. + checkSizeLimit(playerUUID, image ); + + + updateMap(ImageUtils.ScalingType.CONTAINED.resize(image, width*128, height*128),playerUUID,map.getMapsIDs()); + return map; + /*if (scaling != ImageUtils.ScalingType.NONE && height <= 1 && width <= 1) + { + return updateSingle(scaling.resize(image, ImageMap.WIDTH, ImageMap.HEIGHT), playerUUID); + } + + final BufferedImage resizedImage = scaling.resize(image, ImageMap.WIDTH * width, ImageMap.HEIGHT * height); + return updatePoster(resizedImage, playerUUID);*/ + } + }, callback); + + } + static private void updateMap(final BufferedImage image, final UUID playerUUID,int[] mapsIDs) throws Throwable + { + + final PosterImage poster = new PosterImage(image); + poster.splitImages(); + + + + ImageIOExecutor.saveImage(mapsIDs, poster); + + if (PluginConfiguration.SAVE_FULL_IMAGE.get()) + { + ImageIOExecutor.saveImage(ImageMap.getFullImageFile(mapsIDs[0], mapsIDs[mapsIDs.length - 1]), image); + } + + submitToMainThread(new Callable() + { + @Override + public Void call() throws Exception + { + Renderer.installRenderer(poster, mapsIDs); + return null; + } + + }); + + + } static private ImageMap renderSingle(final BufferedImage image, final UUID playerUUID) throws Throwable { MapManager.checkMapLimit(1, playerUUID); @@ -180,4 +251,6 @@ public class ImageRendererExecutor extends Worker return MapManager.createMap(poster, playerUUID, mapsIDs); } + + } diff --git a/src/main/java/fr/moribus/imageonmap/map/ImageMap.java b/src/main/java/fr/moribus/imageonmap/map/ImageMap.java index 0c0bf9e..27c0a88 100644 --- a/src/main/java/fr/moribus/imageonmap/map/ImageMap.java +++ b/src/main/java/fr/moribus/imageonmap/map/ImageMap.java @@ -39,7 +39,9 @@ package fr.moribus.imageonmap.map; import fr.moribus.imageonmap.ImageOnMap; import fr.moribus.imageonmap.ui.MapItemManager; import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.tools.PluginLogger; import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.entity.Player; @@ -47,6 +49,7 @@ import org.bukkit.inventory.ItemStack; import java.io.File; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -150,7 +153,34 @@ public abstract class ImageMap implements ConfigurationSerializable this.postSerialize(map); return map; } - + + static public Integer[] getSize(Map map, UUID playerUUID, String id){ + + ConfigurationSection section=MapManager.getPlayerMapStore(playerUUID).getToolConfig().getConfigurationSection("PlayerMapStore"); + + if(section == null) return null; + List> list = (List>) section.getList("mapList"); + if(list == null) return null; + + PluginLogger.info("list "); + for(Map tMap : list) + { + PluginLogger.info(" "+tMap.toString()); + if(tMap.get("id").equals(id)) { + + return new Integer[]{(Integer)tMap.get("columns"), (Integer)tMap.get("rows")}; + + } + } + + + + + + + + return null; + } static protected T getFieldValue(Map map, String fieldName) throws InvalidConfigurationException { T value = getNullableFieldValue(map, fieldName); diff --git a/src/main/java/fr/moribus/imageonmap/map/MapManager.java b/src/main/java/fr/moribus/imageonmap/map/MapManager.java index d06cf8c..fd7281d 100644 --- a/src/main/java/fr/moribus/imageonmap/map/MapManager.java +++ b/src/main/java/fr/moribus/imageonmap/map/MapManager.java @@ -106,7 +106,7 @@ abstract public class MapManager addMap(newMap); return newMap; } - + static public ImageMap createMap(PosterImage image, UUID playerUUID, int[] mapsIDs) throws MapManagerException { ImageMap newMap; @@ -122,7 +122,7 @@ abstract public class MapManager addMap(newMap); return newMap; } - + static public int[] getNewMapsIds(int amount) { int[] mapsIds = new int[amount]; @@ -161,7 +161,7 @@ abstract public class MapManager getPlayerMapStore(map.getUserUUID()).deleteMap(map); ImageIOExecutor.deleteImage(map); } - + static public void notifyModification(UUID playerUUID) { getPlayerMapStore(playerUUID).notifyModification(); @@ -376,7 +376,7 @@ abstract public class MapManager } } - static private PlayerMapStore getPlayerMapStore(UUID playerUUID) + static public PlayerMapStore getPlayerMapStore(UUID playerUUID) { PlayerMapStore store; synchronized(playerMaps) diff --git a/src/main/java/fr/moribus/imageonmap/map/PlayerMapStore.java b/src/main/java/fr/moribus/imageonmap/map/PlayerMapStore.java index 057c9e0..de4ee96 100644 --- a/src/main/java/fr/moribus/imageonmap/map/PlayerMapStore.java +++ b/src/main/java/fr/moribus/imageonmap/map/PlayerMapStore.java @@ -149,7 +149,11 @@ public class PlayerMapStore implements ConfigurationSerializable { return new ArrayList(mapList); } - + + /*public synchronized ArrayList getMapList() + { + return mapList; + }*/ public synchronized ImageMap[] getMaps() { return mapList.toArray(new ImageMap[mapList.size()]); @@ -256,7 +260,7 @@ public class PlayerMapStore implements ConfigurationSerializable private FileConfiguration mapConfig = null; private File mapsFile = null; - private FileConfiguration getToolConfig() + public FileConfiguration getToolConfig() { if(mapConfig == null) load();