Code cleanup, starting to reorganize packages, and various (unstable!) updates.

* NEW: Added the commands and image packages.
* NEW: ImageMap is now an abstract class rather than an interface, 
  allowing to add several useful common methods.
* NEW: The MapType enumeration is now part of the ImageMap class.
* NEW: Added some utility methods to the MapType enumeration.
* NEW: imageName, ownerName and worldName are now common map properties.
* NEW: The PosterImage class (was previously Poster) is now used to hold
  the sub-images in the PosterMap class, instead of holding an array of 
  SingleMaps.
* NEW: Completely rewrote the image splitting algorithm.
* NEW: Removed the MapData and PosterData classes. They were uselessly
  wrapped by the PosterMap and SingleMap classes.
* BUG: Fixed some method's accessibility.
* OPT: Cleaned up the map renderer a bit.
This commit is contained in:
ProkopyL 2015-02-17 18:31:55 +01:00
parent d541aede3f
commit e31c20191f
21 changed files with 479 additions and 542 deletions

View File

@ -1,5 +1,7 @@
package fr.moribus.imageonmap;
import fr.moribus.imageonmap.commands.MapToolCommand;
import fr.moribus.imageonmap.commands.ImageRenduCommande;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

View File

@ -24,7 +24,7 @@ public class ImgUtility
{
// Vérifie que c'est bien un joueur qui exécute la commande
static boolean VerifierIdentite(CommandSender sender)
static public boolean VerifierIdentite(CommandSender sender)
{
if (sender instanceof Player)
{
@ -39,7 +39,7 @@ public class ImgUtility
}
// Creation du dossier sera stocké les images
static boolean CreeRepImg(ImageOnMap plugin)
static public boolean CreeRepImg(ImageOnMap plugin)
{
File dossier;
dossier = new File(plugin.getDataFolder().getPath() + "/Image");
@ -170,7 +170,7 @@ public class ImgUtility
return map;
}
static boolean RemoveMap(ImageOnMap plugin, short id)
static public boolean RemoveMap(ImageOnMap plugin, short id)
{
@SuppressWarnings("deprecation")
MapView carte = Bukkit.getMap(id);
@ -231,7 +231,7 @@ public class ImgUtility
return false;
}
static ArrayList<String> getListMapByPlayer(ImageOnMap plugin, String pseudo)
static public ArrayList<String> getListMapByPlayer(ImageOnMap plugin, String pseudo)
{
ArrayList<String> listeMap = new ArrayList<String>();
Set<String> cle = plugin.getCustomConfig().getKeys(false);
@ -245,7 +245,7 @@ public class ImgUtility
return listeMap;
}
static void AddMap(ItemStack map, Inventory inv, ArrayList<ItemStack> restant)
static public void AddMap(ItemStack map, Inventory inv, ArrayList<ItemStack> restant)
{
HashMap<Integer,ItemStack> reste = inv.addItem(map);

View File

@ -1,115 +0,0 @@
package fr.moribus.imageonmap;
import java.awt.image.BufferedImage;
import java.util.HashMap;
/* Class which represents a picture cut into several parts */
public class Poster
{
private BufferedImage src;
private BufferedImage[] ImgDecoupe;
private HashMap<Integer, String> NumeroMap;
private int nbPartie;
private int nbColonne;
public Poster(BufferedImage img)
{
src = img;
NumeroMap = new HashMap<Integer, String>();
DecoupeImg();
}
public BufferedImage[] getPoster()
{
return ImgDecoupe;
}
public int getNbColonne()
{
return nbColonne;
}
private void DecoupeImg()
{
int ligne, colonne;
int x = 0, y = 0;
int index = 0;
int resteX = src.getWidth() % 128;
int resteY = src.getHeight() % 128;
if (src.getWidth() / 128 <= 0)
{
ligne = 1;
}
else if (src.getWidth() % 128 != 0)
{
ligne = src.getWidth() / 128 + 1;
}
else
{
ligne = src.getWidth() / 128;
}
if (src.getHeight() <= 0)
{
colonne = 1;
}
else if (src.getHeight() % 128 != 0)
{
colonne = src.getHeight() / 128 + 1;
}
else
{
colonne = src.getHeight() / 128;
}
nbColonne = colonne;
nbPartie = ligne * colonne;
ImgDecoupe = new BufferedImage[nbPartie];
for (int lig = 0; lig < ligne; lig++)
{
y = 0;
if (lig == ligne - 1 && resteX != 0)
{
for (int col = 0; col < colonne; col++)
{
if (col == colonne - 1 && resteY != 0)
{
ImgDecoupe[index] = src.getSubimage(x, y, resteX, resteY);
}
else
{
ImgDecoupe[index] = src.getSubimage(x, y, resteX, 128);
y += 128;
}
NumeroMap.put(index, "column " + (lig + 1) + ", row " + (col + 1));
index++;
}
}
else
{
for (int col = 0; col < colonne; col++)
{
if (col == colonne - 1 && resteY != 0)
{
ImgDecoupe[index] = src.getSubimage(x, y, 128, resteY);
}
else
{
ImgDecoupe[index] = src.getSubimage(x, y, 128, 128);
y += 128;
}
NumeroMap.put(index, "column " + (lig + 1) + ", row " + (col + 1));
index++;
}
x += 128;
}
}
}
}

View File

@ -1,58 +0,0 @@
package fr.moribus.imageonmap;
import java.awt.Image;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class Rendu extends MapRenderer implements Runnable
{
private boolean estRendu;
private Image imageARendre;
private MapCanvas canvas;
public Rendu(Image img)
{
estRendu = false;
setImageARendre(img);
}
@Override
public void render(MapView v, final MapCanvas mc, Player p)
{
canvas = mc;
if (!estRendu) // Si la map a déjà été rendu, on n'entre plus dans la fonction, ce qui évite de surcharger le serveur
{
run();
estRendu = true;
}
}
@Override
public void run()
{
// on dessine l'image redimensionnée dans le canvas (et donc, sur la map !)
canvas.drawImage(0, 0, getImageARendre());
}
public Image getImageARendre()
{
return imageARendre;
}
public void setImageARendre(Image imageARendre)
{
if(imageARendre != null)
{
this.imageARendre = imageARendre;
estRendu = false;
}
}
}

View File

@ -1,5 +1,7 @@
package fr.moribus.imageonmap;
import fr.moribus.imageonmap.image.Renderer;
import fr.moribus.imageonmap.image.ImageRendererThread;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@ -83,13 +85,13 @@ public class SavedMap
}
@SuppressWarnings("deprecation")
Boolean LoadMap()
public Boolean LoadMap()
{
MapView carte = Bukkit.getMap(idMap);
if (carte != null && loaded)
{
ImageRendererThread.SupprRendu(carte);
carte.addRenderer(new Rendu(image));
carte.addRenderer(new Renderer(image));
if (!plugin.mapChargee.contains(idMap))
{
plugin.mapChargee.add(idMap);

View File

@ -98,7 +98,7 @@ public class SavedPoster
return posterName;
}
ArrayList<String> getListMapByPlayer(ImageOnMap plugin, String pseudo)
public ArrayList<String> getListMapByPlayer(ImageOnMap plugin, String pseudo)
{
ArrayList<String> listeMap = new ArrayList<String>();
Set<String> cle = getCustomConfig().getKeys(false);

View File

@ -31,9 +31,8 @@ public class SendMapOnFrameEvent implements Listener
{
chunk = event.getChunk();
entites = chunk.getEntities().clone();
for (int i = 0; i < entites.length; i++)
for (Entity entite : entites)
{
Entity entite = entites[i];
if (entite instanceof ItemFrame)
{
ArrayList<Short> ListeId = plugin.mapChargee;

View File

@ -1,5 +1,6 @@
package fr.moribus.imageonmap;
import fr.moribus.imageonmap.image.DownloadImageThread;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

View File

@ -5,17 +5,15 @@ import java.awt.image.BufferedImage;
import org.bukkit.entity.Player;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapType;
import fr.moribus.imageonmap.map.PosterMap;
import fr.moribus.imageonmap.map.SingleMap;
import fr.moribus.imageonmap.map.ImageMap.Type;
import java.io.IOException;
import java.net.URL;
public class TacheTraitementNouvelleMap extends TacheTraitementMap
{
private final MapType type;
private final Type type;
public TacheTraitementNouvelleMap(Player player, URL url, MapType type, boolean rs, boolean rn)
public TacheTraitementNouvelleMap(Player player, URL url, Type type, boolean rs, boolean rn)
{
super(player, url, rs, rn);
this.type = type;
@ -24,12 +22,7 @@ public class TacheTraitementNouvelleMap extends TacheTraitementMap
@Override
public void traiterMap(BufferedImage img) throws IOException
{
ImageMap m;
if(type == MapType.Single)
m = new SingleMap(img, getJoueur());
else
m = new PosterMap(img, getJoueur());
ImageMap m = ImageMap.Type.createNewMap(type, img, getJoueur());
m.load();
m.give(getJoueur().getInventory());

View File

@ -1,12 +1,16 @@
package fr.moribus.imageonmap;
package fr.moribus.imageonmap.commands;
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.ImgUtility;
import fr.moribus.imageonmap.TacheTraitementMap;
import fr.moribus.imageonmap.TacheTraitementNouvelleMap;
import fr.moribus.imageonmap.map.ImageMap;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import fr.moribus.imageonmap.map.MapType;
import java.net.MalformedURLException;
import java.net.URL;
@ -16,7 +20,6 @@ public class ImageRenduCommande implements CommandExecutor
boolean renderName, imgSvg;
ImageOnMap plugin;
boolean resize, rename;
MapType type;
public ImageRenduCommande(ImageOnMap p)
{
@ -59,11 +62,13 @@ public class ImageRenduCommande implements CommandExecutor
return false;
}
ImageMap.Type type = ImageMap.Type.SINGLE;
if (args.length >= 2)
{
try
{
type = Enum.valueOf(MapType.class, args[1]);
type = Enum.valueOf(ImageMap.Type.class, args[1]);
}
catch (IllegalArgumentException ex)
{

View File

@ -1,5 +1,10 @@
package fr.moribus.imageonmap;
package fr.moribus.imageonmap.commands;
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.ImgUtility;
import fr.moribus.imageonmap.PluginLogger;
import fr.moribus.imageonmap.SavedPoster;
import fr.moribus.imageonmap.TacheTraitementMajMap;
import java.util.ArrayList;
import org.bukkit.ChatColor;

View File

@ -1,4 +1,4 @@
package fr.moribus.imageonmap;
package fr.moribus.imageonmap.image;
import java.awt.image.BufferedImage;
import java.io.IOException;
@ -13,7 +13,7 @@ public class DownloadImageThread implements Callable<BufferedImage>
private final URL imageURL;
private BufferedImage imgSrc;
DownloadImageThread(URL imageURL)
public DownloadImageThread(URL imageURL)
{
this.imageURL = imageURL;
}

View File

@ -1,5 +1,6 @@
package fr.moribus.imageonmap;
package fr.moribus.imageonmap.image;
import fr.moribus.imageonmap.image.PosterImage;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
@ -17,7 +18,7 @@ public class ImageRendererThread extends Thread
private String URL;
private BufferedImage imgSrc;
private BufferedImage[] img;
private Poster poster;
private PosterImage poster;
private boolean estPrete = false, resized;
public boolean erreur = false;
@ -26,7 +27,7 @@ public class ImageRendererThread extends Thread
return erreur;
}
ImageRendererThread(String u, boolean r)
public ImageRendererThread(String u, boolean r)
{
URL = u;
resized = r;
@ -124,9 +125,10 @@ public class ImageRendererThread extends Thread
//graph.rotate(45);
// on dessine l'image dans le canvas
graph.drawImage(imgSrc, null, null);
// on crée un Poster à partir de notre canvas
poster = new Poster(canvas);
img = poster.getPoster();
poster = new PosterImage(canvas);
img = poster.getImages();
}
estPrete = true;
@ -134,7 +136,7 @@ public class ImageRendererThread extends Thread
}
static void SupprRendu(MapView map)
static public void SupprRendu(MapView map)
{
if (map.getRenderers().size() > 0)
{

View File

@ -0,0 +1,148 @@
package fr.moribus.imageonmap.image;
import fr.moribus.imageonmap.map.ImageMap;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
/**
* This class represents an image split into pieces
*/
public class PosterImage
{
private BufferedImage[] cutImages;
private int lines;
private int columns;
private int cutImagesCount;
private int remainderX, remainderY;
/**
* Creates and splits a new Poster from an entire image
* @param originalImage the original image
*/
public PosterImage(BufferedImage originalImage)
{
splitImages(originalImage);
}
private void splitImages(BufferedImage originalImage)
{
int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight();
columns = (int) Math.ceil(originalWidth / ImageMap.WIDTH);
lines = (int) Math.ceil(originalHeight / ImageMap.HEIGHT);
remainderX = originalWidth % ImageMap.WIDTH;
remainderY = originalHeight % ImageMap.HEIGHT;
if(remainderX > 0) columns++;
if(remainderY > 0) lines++;
cutImagesCount = columns * lines;
cutImages = new BufferedImage[cutImagesCount];
int imageX;
int imageY = (remainderY - ImageMap.HEIGHT) / 2;
for(int i = 0; i < lines; i++)
{
imageX = (remainderX - ImageMap.WIDTH) / 2;
for(int j = 0; j < columns; j++)
{
cutImages[i * columns + j] = makeSubImage(originalImage, imageX, imageY);
imageX += ImageMap.WIDTH;
}
imageY += ImageMap.HEIGHT;
}
}
/**
* Generates the subimage that intersects with the given map rectangle.
* @param x X coordinate of top-left point of the map.
* @param y Y coordinate of top-left point of the map.
* @return the requested subimage.
*/
private BufferedImage makeSubImage(BufferedImage originalImage, int x, int y)
{
BufferedImage newImage = new BufferedImage(ImageMap.WIDTH, ImageMap.HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = newImage.getGraphics();
graphics.drawImage(originalImage, -x, -y, null);
graphics.dispose();
return newImage;
}
private int boundValue(int min, int value, int max)
{
return Math.max(Math.min(value, max), min);
}
/**
*
* @return the split images
*/
public BufferedImage[] getImages()
{
return cutImages;
}
public BufferedImage getImageAt(int i)
{
return cutImages[i];
}
public BufferedImage getImageAt(int x, int y)
{
return cutImages[x * columns + y];
}
/**
*
* @return the number of lines of the poster
*/
public int getLines()
{
return lines;
}
/**
*
* @return the number of columns of the poster
*/
public int getColumns()
{
return columns;
}
/**
*
* @return the number of split images
*/
public int getImagesCount()
{
return cutImagesCount;
}
public int getRemainderX()
{
return remainderX;
}
public void setRemainderX(int remainderX)
{
this.remainderX = remainderX;
}
public int getRemainderY()
{
return remainderY;
}
public void setRemainderY(int remainderY)
{
this.remainderY = remainderY;
}
}

View File

@ -0,0 +1,29 @@
package fr.moribus.imageonmap.image;
import java.awt.image.BufferedImage;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class Renderer extends MapRenderer
{
private boolean isRendered;
private final BufferedImage image;
public Renderer(BufferedImage image)
{
isRendered = false;
this.image = image;
}
@Override
public void render(MapView v, final MapCanvas canvas, Player p)
{
//Render only once to avoid overloading the server
if (!isRendered)
{
canvas.drawImage(0, 0, image);
isRendered = true;
}
}
}

View File

@ -1,17 +1,120 @@
package fr.moribus.imageonmap.map;
import fr.moribus.imageonmap.image.PosterImage;
import java.awt.image.BufferedImage;
import java.io.IOException;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public interface ImageMap
public abstract class ImageMap
{
public void load() throws IOException;
public void save() throws IOException;
public void give(Inventory inv);
public boolean isNamed();
public void setImage(BufferedImage image);
public void send(Player joueur);
static public enum Type
{
SINGLE, POSTER, ANIMATED;
static public ImageMap createNewMap(Type type, BufferedImage image, Player player)
{
switch(type)
{
case POSTER:
return new PosterMap(new PosterImage(image), player);
default:
return new SingleMap(image, player);
}
}
static public Type fromString(String string)
{
switch(string.toLowerCase())
{
case "animated":
return ANIMATED;
case "poster":
case "multi":
return POSTER;
default:
return SINGLE;
}
}
}
static public final int WIDTH = 128;
static public final int HEIGHT = 128;
protected String imageName;
protected String ownerName;
protected String worldName;
public abstract void load() throws IOException;
public abstract void save() throws IOException;
public abstract void give(Inventory inv);
public abstract void setImage(BufferedImage image);
public abstract void send(Player joueur);
public ImageMap()
{
this(null, null, null);
}
public ImageMap(String imageName, String ownerName, String worldName)
{
this.imageName = imageName;
this.ownerName = ownerName;
this.worldName = worldName;
}
protected void give(Inventory inventory, short mapID)
{
ItemStack itemMap = new ItemStack(Material.MAP, 1, mapID);
if(isNamed())
{
ItemMeta meta = itemMap.getItemMeta();
meta.setDisplayName(imageName);
itemMap.setItemMeta(meta);
}
inventory.addItem(itemMap);
}
// Getters & Setters
public String getImageName()
{
return imageName;
}
public void setImageName(String imageName)
{
this.imageName = imageName;
}
public boolean isNamed()
{
return imageName != null;
}
public String getOwnerName()
{
return ownerName;
}
public void setOwnerName(String ownerName)
{
this.ownerName = ownerName;
}
public String getWorldName()
{
return worldName;
}
public void setWorldName(String worldName)
{
this.worldName = worldName;
}
}

View File

@ -1,89 +0,0 @@
package fr.moribus.imageonmap.map;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.bukkit.Bukkit;
import fr.moribus.imageonmap.ImageOnMap;
public class MapData
{
private final short id;
private String playerName;
private String worldName;
private String name;
private BufferedImage image;
public MapData(short id, String playerName, BufferedImage image, String worldName, String name)
{
this.id = id;
this.playerName = playerName;
this.image = image;
this.worldName = worldName;
this.name = name;
}
public MapData(short id)
{
this(id, null, null, null, null);
}
public MapData(short id, String playerName, BufferedImage image, String worldName)
{
this(id, playerName, image, worldName, null);
}
public Image getImage()
{
return image;
}
public void setImage(BufferedImage image)
{
this.image = image;
}
public void save() throws IOException
{
String nomImg = "map" + id;
ImageOnMap plugin = (ImageOnMap) Bukkit.getPluginManager().getPlugin("ImageOnMap");
File outputfile = new File("./plugins/ImageOnMap/Image/" + nomImg + ".png");
ImageIO.write(image, "png", outputfile);
// Enregistrement de la map dans la config
ArrayList<String> liste = new ArrayList<String>();
liste.add(String.valueOf(id));
liste.add(nomImg);
liste.add(playerName);
liste.add(worldName);
plugin.getCustomConfig().set(nomImg, liste);
plugin.saveCustomConfig();
}
public void load() throws IOException
{
ImageOnMap plugin = (ImageOnMap) Bukkit.getPluginManager().getPlugin("ImageOnMap");
List<String> svg = plugin.getCustomConfig().getStringList("map" + id);
String nomImg = svg.get(1);
playerName = svg.get(2);
image = ImageIO.read(new File("./plugins/ImageOnMap/Image/" + nomImg + ".png"));
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}

View File

@ -1,6 +0,0 @@
package fr.moribus.imageonmap.map;
public enum MapType
{
Single, Multi, Animated
}

View File

@ -1,142 +0,0 @@
package fr.moribus.imageonmap.map;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import fr.moribus.imageonmap.ImageOnMap;
public class PosterData
{
private String joueur;
private short[] idMaps;
private String nomPoster;
private FileConfiguration customConfig = null;
private File customConfigFile = null;
public PosterData(String joueur, short[] ids)
{
setJoueur(joueur);
setIdMaps(ids);
nomPoster = "poster"+ increment();
}
public PosterData(String id) throws Exception
{
load(id);
}
public String getJoueur()
{
return joueur;
}
private void setJoueur(String joueur)
{
this.joueur = joueur;
}
public short[] getIdMaps()
{
return idMaps;
}
private void setIdMaps(short[] idMaps)
{
this.idMaps = idMaps;
}
private void load(String id) throws Exception
{
List<String> svg = getCustomConfig().getStringList(id);
if(svg != null && !svg.isEmpty())
{
nomPoster = id;
setJoueur(svg.get(0));
short[] ids = new short[svg.size()-1];
for(int i = 0; i < ids.length; i++)
{
ids[i] = Short.parseShort(svg.get(i+1));
}
setIdMaps(ids);
}
else
{
throw new Exception("Le poster est introuvable.");
}
}
public void save() throws IOException
{
ArrayList<String> liste = new ArrayList<String>();
liste.add(joueur);
for(int i= 0; i< idMaps.length; i++)
{
liste.add(String.valueOf(idMaps[i]));
}
getCustomConfig().set(nomPoster, liste);
saveCustomConfig();
}
private int increment()
{
int i;
if(getCustomConfig().get("IdCount") != null)
i = getCustomConfig().getInt("IdCount");
else
i = 0;
i++;
this.getCustomConfig().set("IdCount", i);
return i;
}
/* Méthodes pour charger / recharger / sauvegarder
* le fichier conf des Posters (poster.yml).
* Je les ai juste copié depuis un tuto du wiki Bukkit.
*/
@SuppressWarnings("deprecation")
public void reloadCustomConfig()
{
ImageOnMap plugin = (ImageOnMap)Bukkit.getPluginManager().getPlugin("ImageOnMap");
if (customConfigFile == null)
{
customConfigFile = new File(plugin.getDataFolder(), "poster.yml");
}
customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
// Look for defaults in the jar
InputStream defConfigStream = plugin.getResource("poster.yml");
if (defConfigStream != null)
{
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
customConfig.setDefaults(defConfig);
}
}
public FileConfiguration getCustomConfig()
{
if (customConfig == null)
{
reloadCustomConfig();
}
return customConfig;
}
public void saveCustomConfig() throws IOException
{
ImageOnMap plugin = (ImageOnMap)Bukkit.getPluginManager().getPlugin("ImageOnMap");
if (customConfig == null || customConfigFile == null) {
return;
}
getCustomConfig().save(customConfigFile);
}
}

View File

@ -1,104 +1,156 @@
package fr.moribus.imageonmap.map;
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.image.Renderer;
import java.awt.image.BufferedImage;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import fr.moribus.imageonmap.Poster;
import fr.moribus.imageonmap.image.PosterImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.map.MapView;
public class PosterMap implements ImageMap
public class PosterMap extends ImageMap
{
private PosterImage image;
private final short[] mapsIDs;
private FileConfiguration customConfig = null;
private File customConfigFile = null;
private SingleMap[] posterMap;
private PosterData data;
public PosterMap(BufferedImage img, Player joueur)
public PosterMap(PosterImage image, Player player)
{
Poster poster = new Poster(img);
BufferedImage[] imgs = poster.getPoster();
posterMap = new SingleMap[imgs.length];
super(null, player.getName(), player.getWorld().getName());
this.image = image;
short[] idsMap = new short[posterMap.length];
for (int i = 0; i < posterMap.length; i++)
mapsIDs = new short[image.getImagesCount()];
for (int i = 0; i < mapsIDs.length; i++)
{
SingleMap map = new SingleMap(imgs[i], joueur);
posterMap[i] = map;
idsMap[i] = map.getId();
mapsIDs[i] = Bukkit.createMap(player.getWorld()).getId();
}
data = new PosterData(joueur.getName(), idsMap);
}
public PosterMap(String id) throws Exception
{
data = new PosterData(id);
short[] ids = data.getIdMaps();
posterMap = new SingleMap[ids.length];
for (int i = 0; i < ids.length; i++)
this.imageName = id;
List<String> svg = getCustomConfig().getStringList(imageName);
if(svg != null && !svg.isEmpty())
{
SingleMap map = new SingleMap(ids[i]);
posterMap[i] = map;
this.ownerName = svg.get(0);
mapsIDs = new short[svg.size()-1];
for(int i = 0; i < mapsIDs.length; i++)
{
mapsIDs[i] = Short.parseShort(svg.get(i+1));
}
}
else
{
throw new Exception("Le poster est introuvable.");
}
}
@Override
public void load()
{
for(SingleMap map : posterMap)
for(int i = 0; i < mapsIDs.length; i++)
{
map.load();
MapView map = Bukkit.getMap(mapsIDs[i]);
SingleMap.SupprRendu(map);
map.addRenderer(new Renderer(image.getImageAt(i)));
}
}
@Override
public void save() throws IOException
{
for(SingleMap map : posterMap)
ImageOnMap plugin = ImageOnMap.getPlugin();
for(int i = 0; i < mapsIDs.length; i++)
{
map.save();
short mapID = mapsIDs[i];
String mapName = "map" + mapID;
File outputfile = new File("./plugins/ImageOnMap/Image/" + mapName + ".png");
ImageIO.write(image.getImageAt(i), "png", outputfile);
// Enregistrement de la map dans la config
ArrayList<String> liste = new ArrayList<String>();
liste.add(String.valueOf(mapID));
liste.add(mapName);
liste.add(ownerName);
liste.add(worldName);
plugin.getCustomConfig().set(mapName, liste);
}
plugin.saveCustomConfig();
}
@Override
public void give(Inventory inv)
{
for(SingleMap map : posterMap)
for(short mapID : mapsIDs)
{
map.give(inv);
give(inv, mapID);
}
}
@Override
public boolean isNamed()
{
for(SingleMap map : posterMap)
{
if(!map.isNamed()) return false;
}
return true;
}
@Override
public void setImage(BufferedImage image)
{
Poster poster = new Poster(image);
BufferedImage[] imgs = poster.getPoster();
for(int i = 0; i < posterMap.length; i++)
{
posterMap[i].setImage(imgs[i]);
}
}
@Override
public void send(Player player)
{
for(SingleMap map : posterMap)
for(short mapID: mapsIDs)
{
map.send(player);
player.sendMap(Bukkit.getMap(mapID));
}
}
private void reloadCustomConfig()
{
ImageOnMap plugin = (ImageOnMap)Bukkit.getPluginManager().getPlugin("ImageOnMap");
if (customConfigFile == null)
{
customConfigFile = new File(plugin.getDataFolder(), "poster.yml");
}
customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
// Look for defaults in the jar
InputStream defConfigStream = plugin.getResource("poster.yml");
if (defConfigStream != null)
{
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
customConfig.setDefaults(defConfig);
}
}
private FileConfiguration getCustomConfig()
{
if (customConfig == null)
{
reloadCustomConfig();
}
return customConfig;
}
private void saveCustomConfig() throws IOException
{
ImageOnMap plugin = (ImageOnMap)Bukkit.getPluginManager().getPlugin("ImageOnMap");
if (customConfig == null || customConfigFile == null) {
return;
}
getCustomConfig().save(customConfigFile);
}
}

View File

@ -1,111 +1,117 @@
package fr.moribus.imageonmap.map;
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.ImgUtility;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.map.MapView;
import fr.moribus.imageonmap.Rendu;
import fr.moribus.imageonmap.image.Renderer;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.bukkit.map.MapRenderer;
public class SingleMap implements ImageMap
public class SingleMap extends ImageMap
{
private final MapData data;
private final MapView map;
public final int LARGEUR = 128;
public final int HAUTEUR = 128;
private final short mapID;
private BufferedImage image;
public SingleMap(BufferedImage img, Player player)
{
this(img, player, null);
this(img, null, player);
}
public SingleMap(BufferedImage img, Player player, String imageName)
public SingleMap(BufferedImage image, String imageName, Player player)
{
map = Bukkit.createMap(player.getWorld());
data = new MapData(map.getId(), player.getName(), ImgUtility.scaleImage(img, LARGEUR, HAUTEUR), player.getWorld().getName(), imageName);
super(imageName, player.getName(), player.getWorld().getName());
this.mapID = Bukkit.createMap(player.getWorld()).getId();
this.image = ImgUtility.scaleImage(image, WIDTH, HEIGHT);
}
@SuppressWarnings("deprecation")
public SingleMap(short id) throws IOException, IllegalArgumentException
public SingleMap(short mapID) throws IOException, IllegalArgumentException
{
data = new MapData(id);
data.load();
map = Bukkit.getMap(id);
this.mapID = mapID;
//Testing if the map id exists
MapView map = Bukkit.getMap(mapID);
if(map == null)
throw new IllegalArgumentException("Map ID '"+id+"' doesn't exist");
throw new IllegalArgumentException("Map ID '" + mapID + "' doesn't exist");
List<String> svg = ImageOnMap.getPlugin().getCustomConfig().getStringList("map" + mapID);
String nomImg = svg.get(1);
ownerName = svg.get(2);
image = ImageIO.read(new File("./plugins/ImageOnMap/Image/" + nomImg + ".png"));
}
@Override
public void save() throws IOException
{
data.save();
String mapName = "map" + mapID;
ImageOnMap plugin = ImageOnMap.getPlugin();
File outputfile = new File("./plugins/ImageOnMap/Image/" + mapName + ".png");
ImageIO.write(image, "png", outputfile);
// Enregistrement de la map dans la config
ArrayList<String> liste = new ArrayList<String>();
liste.add(String.valueOf(mapID));
liste.add(mapName);
liste.add(ownerName);
liste.add(worldName);
plugin.getCustomConfig().set(mapName, liste);
plugin.saveCustomConfig();
}
@SuppressWarnings("deprecation")
@Override
public void give(Inventory inv)
public void give(Inventory inventory)
{
ItemStack itemMap = new ItemStack(Material.MAP, 1, map.getId());
if (isNamed())
{
ItemMeta meta = itemMap.getItemMeta();
meta.setDisplayName(data.getName());
itemMap.setItemMeta(meta);
}
inv.addItem(itemMap);
give(inventory, mapID);
}
@Override
public void load()
{
MapView map = Bukkit.getMap(mapID);
SingleMap.SupprRendu(map);
map.addRenderer(new Rendu(data.getImage()));
map.addRenderer(new Renderer(image));
}
@Override
public boolean isNamed()
{
return data.getName() != null;
return imageName != null;
}
@SuppressWarnings("deprecation")
public short getId()
{
return map.getId();
return mapID;
}
@Override
public void setImage(BufferedImage image)
{
data.setImage(image);
this.image = image;
load();
}
@Override
public void send(Player joueur)
{
joueur.sendMap(map);
joueur.sendMap(Bukkit.getMap(mapID));
}
public static void SupprRendu(MapView map)
{
if (map.getRenderers().size() > 0)
for(MapRenderer renderer : map.getRenderers())
{
int i = 0, t = map.getRenderers().size();
while (i < t)
{
map.removeRenderer(map.getRenderers().get(i));
i++;
}
map.removeRenderer(renderer);
}
}