Update working

This commit is contained in:
Vlammar 2020-07-16 02:50:17 +02:00
parent fd2d5e2cf0
commit b01f133639
8 changed files with 376 additions and 34 deletions

View File

@ -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");

View File

@ -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")

View File

@ -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."));

View File

@ -0,0 +1,234 @@
/*
* Copyright or © or Copr. Moribus (2013)
* Copyright or © or Copr. ProkopyL <prokopylmc@gmail.com> (2015)
* Copyright or © or Copr. Amaury Carrade <amaury@carrade.eu> (2016 2020)
* Copyright or © or Copr. Vlammar <valentin.jabre@gmail.com> (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 = "<new url> <map name to update> [--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<String, Object>(),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<ImageMap>() {
@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<ImageMap>() {
@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<String> 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);
}
}

View File

@ -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<ImageMap> callback)
{
submitQuery(new WorkerRunnable<ImageMap>()
@ -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<ImageMap> callback) {
submitQuery(new WorkerRunnable<ImageMap>()
{
@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<Void>()
{
@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);
}
}

View File

@ -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<String, Object> map, UUID playerUUID, String id){
ConfigurationSection section=MapManager.getPlayerMapStore(playerUUID).getToolConfig().getConfigurationSection("PlayerMapStore");
if(section == null) return null;
List<Map<String, Object>> list = (List<Map<String, Object>>) section.getList("mapList");
if(list == null) return null;
PluginLogger.info("list ");
for(Map<String, Object> 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> T getFieldValue(Map<String, Object> map, String fieldName) throws InvalidConfigurationException
{
T value = getNullableFieldValue(map, fieldName);

View File

@ -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)

View File

@ -149,7 +149,11 @@ public class PlayerMapStore implements ConfigurationSerializable
{
return new ArrayList(mapList);
}
/*public synchronized ArrayList<ImageMap> 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();