Merge pull request #115 from zDevelopers/feature-update-function

Add the maptool update command.
This commit is contained in:
Vlammar 2020-11-05 01:18:21 +01:00 committed by GitHub
commit 265dea7e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 435 additions and 27 deletions

View File

@ -127,7 +127,8 @@ public final class ImageOnMap extends ZPlugin
GetRemainingCommand.class,
ExploreCommand.class,
ExploreOtherCommand.class,
MigrateCommand.class
MigrateCommand.class,
UpdateCommand.class
);
Commands.registerShortcut("maptool", NewCommand.class, "tomap");

View File

@ -50,6 +50,8 @@ public enum Permissions
REMOVE_SPLATTER_MAP("imageonmap.removesplattermap"),
DELETE("imageonmap.delete"),
DELETEOTHER("imageonmap.deleteother"),
UPDATE("imageonmap.update"),
UPDATEOTHER("imageonmap.updateother"),
ADMINISTRATIVE("imageonmap.administrative"),
BYPASS_SIZE("imageonmap.bypasssize")

View File

@ -40,10 +40,13 @@ import fr.moribus.imageonmap.map.MapManager;
import fr.zcraft.zlib.components.commands.Command;
import fr.zcraft.zlib.components.commands.CommandException;
import fr.zcraft.zlib.components.i18n.I;
import fr.zcraft.zlib.tools.PluginLogger;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class IoMCommand extends Command
@ -52,7 +55,46 @@ public abstract class IoMCommand extends Command
{
return getMapFromArgs(playerSender(), 0, true);
}
//TODO:Add the quote system to zlib and refactor this
protected ImageMap getMapFromArgs(Player player, int index) throws CommandException
{
if(args.length <= index) throwInvalidArgument(I.t("You need to give a map name."));
ImageMap map;
String mapName = args[index];
for (int i = index + 1, c = args.length; i < c; i++) {
mapName += " " + args[i];
}
String regex="((\"([^\\\"]*(\\\\\\\")*)*([^\\\\\\\"]\"))|([^\\\"\\s\\\\]*(\\\\\\s)*[\\\\]*)*\"?)";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(mapName);
StringBuilder result = new StringBuilder();
matcher.find();
result.append(matcher.group(0));
if(result!=null)
if(result.charAt(0)=='\"')
if(result.length()==1){
result.deleteCharAt(0);
}
else
if(result.charAt(result.length()-1)=='\"') {
result=result.deleteCharAt(result.length() - 1);
if(result!=null&&!result.equals("")&&result.charAt(0)=='\"')
mapName=result.deleteCharAt(0).toString();
}
mapName = mapName.trim();
map = MapManager.getMap(player.getUniqueId(), mapName);
if(map == null) error(I.t("This map does not exist."));
return map;
}
protected ImageMap getMapFromArgs(Player player, int index, boolean expand) throws CommandException
{
if(args.length <= index) throwInvalidArgument(I.t("You need to give a map name."));
@ -69,7 +111,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,133 @@
/*
* 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.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> [stretched|covered] \"<map name to update>\"")
public class UpdateCommand extends IoMCommand
{
@Override
protected void run() throws CommandException
{
final Player player = playerSender();
ImageUtils.ScalingType scaling;
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."));
switch(args[1]) {
case "stretched":
scaling = ImageUtils.ScalingType.STRETCHED;
break;
case "covered":
scaling = ImageUtils.ScalingType.COVERED;
break;
default:
scaling = ImageUtils.ScalingType.CONTAINED;
}
ImageMap map;
if(scaling.equals(ImageUtils.ScalingType.CONTAINED))
map=getMapFromArgs(player,1);
else
map=getMapFromArgs(player,2);
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("The map was updated using the new image!"));
}
@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());
}
});
}
finally {
ActionBar.removeMessage(player);
}
}
catch(MalformedURLException ex)
{
throwInvalidArgument(I.t("Invalid URL."));
}
}
@Override
public boolean canExecute(CommandSender sender)
{
return Permissions.UPDATE.grantedTo(sender);
}
}

View File

@ -0,0 +1,134 @@
/*
* 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.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> [stretched|covered] \"<map name to update>\"")
public class UpdateOtherCommand extends IoMCommand
{
@Override
protected void run() throws CommandException
{
//TODO separer les deux update(update et update other)
final Player player = playerSender();
ImageUtils.ScalingType scaling;
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."));
switch(args[1]) {
case "stretched":
scaling = ImageUtils.ScalingType.STRETCHED;
break;
case "covered":
scaling = ImageUtils.ScalingType.COVERED;
break;
default:
scaling = ImageUtils.ScalingType.CONTAINED;
}
ImageMap map;
if(scaling.equals(ImageUtils.ScalingType.CONTAINED))
map=getMapFromArgs(player,1);
else
map=getMapFromArgs(player,2);
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("The map was updated using the new image!"));
}
@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());
}
});
}
finally {
ActionBar.removeMessage(player);
}
}
catch(MalformedURLException ex)
{
throwInvalidArgument(I.t("Invalid URL."));
}
}
@Override
public boolean canExecute(CommandSender sender)
{
return Permissions.UPDATEOTHER.grantedTo(sender);
}
}

View File

@ -61,6 +61,7 @@ import java.util.concurrent.Future;
@WorkerAttributes(name = "Image Renderer", queriesMainThread = true)
public class ImageRendererExecutor extends Worker
{
private static URLConnection connecting(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");
@ -70,7 +71,6 @@ public class ImageRendererExecutor extends Worker
{
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()));
@ -78,11 +78,27 @@ public class ImageRendererExecutor extends Worker
}
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!"));
}
}
}
private enum extension{
png, jpg, jpeg, gif
}
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>()
@ -121,32 +137,16 @@ public class ImageRendererExecutor extends Worker
}
//If not an Imgur link
else {
//Try connecting
URLConnection connection = connecting(url);
final InputStream stream = connection.getInputStream();
image = ImageIO.read(stream);
}
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) {
return renderSingle(scaling.resize(image, ImageMap.WIDTH, ImageMap.HEIGHT), playerUUID);
}
@ -158,6 +158,55 @@ public class ImageRendererExecutor extends Worker
}
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(scaling.resize(image, width*128, height*128),playerUUID,map.getMapsIDs());
return map;
}
}, 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);
@ -225,4 +274,6 @@ public class ImageRendererExecutor extends Worker
return MapManager.createMap(poster, playerUUID, mapsIDs);
}
}

View File

@ -40,6 +40,7 @@ import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.ui.MapItemManager;
import fr.zcraft.zlib.components.i18n.I;
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 +48,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 +152,23 @@ 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;
for(Map<String, Object> tMap : list)
{
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

@ -256,7 +256,7 @@ public class PlayerMapStore implements ConfigurationSerializable
private FileConfiguration mapConfig = null;
private File mapsFile = null;
private FileConfiguration getToolConfig()
public FileConfiguration getToolConfig()
{
if(mapConfig == null) load();

View File

@ -593,3 +593,12 @@ msgstr ""
#: src/main/java/fr/moribus/imageonmap/ui/SplatterMapManager.java:101
msgid "{ce}There is not enough space to place this map ({0} × {1})."
msgstr "{ce}There is not enough space to place this map ({0} × {1})."
#New part, added for update (TODO add to other .po file
#: src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java:71
msgid "{ce}You must give an URL and a map name to update."
msgstr "{ce}You must give an URL and a map name to update."
#: src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java:72
msgid "{ce}You must give a map name to update.."
msgstr "{ce}You must give a map name to update.."

View File

@ -568,3 +568,12 @@ msgstr "Carte"
#~ msgid_plural "{white}{0}{gray} maps left"
#~ msgstr[0] "{white}{0}{gray} carte restante"
#~ msgstr[1] "{white}{0}{gray} cartes restantes"
#: src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java:71
msgid "{ce}You must give an URL and a map name to update."
msgstr "{ce}Veuillez donner une URL et un nom de carte a update."
#: src/main/java/fr/moribus/imageonmap/commands/maptool/UpdateCommand.java:72
msgid "{ce}You must give a map name to update."
msgstr "{ce}Veuillez un nom de carte a update"

View File

@ -30,6 +30,8 @@ permissions:
imageonmap.delete: true
imageonmap.deleteother: false
imageonmap.bypasssize: false
imageonmap.update: true
imageonmap.updateother: false
imageonmap.userender:
description: "Allows you to use /tomap and related commands (/maptool getremaining). Alias of imageonmap.new."
@ -86,3 +88,11 @@ permissions:
imageonmap.bypasssize:
description: "Allows you to create maps larger than the configured limit."
default: op
imageonmap.update:
description: "Allows you to update an existing map with a new image."
default: true
imageonmap.updateother:
description: "Allows you to update an existing map of an other player with a new image."
default: op