Ajout de la commande getmap; Ajout fichier config pour les maps

This commit is contained in:
Coutume 2014-01-12 23:45:43 +01:00
parent f967564107
commit 17d6813f3e
4 changed files with 168 additions and 17 deletions

View File

@ -0,0 +1,70 @@
package fr.moribus.ImageOnMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.map.MapView;
public class GetMapCommand implements CommandExecutor
{
short id;
ImageOnMap plugin;
MapView map;
Player joueur;
Inventory inv;
GetMapCommand(ImageOnMap p)
{
plugin = p;
}
@SuppressWarnings("deprecation")
@Override
public boolean onCommand(CommandSender sender, Command command, String arg2, String[] arg3)
{
if(!ImgUtility.VerifierIdentite(sender))
return false;
joueur = (Player) sender;
inv = (Inventory) joueur.getInventory();
try
{
id = Short.parseShort(arg3[0]);
}
catch(NumberFormatException err)
{
joueur.sendMessage("you must enter a number !");
return false;
}
if(!ImgUtility.EstDansFichier(plugin, id))
{
joueur.sendMessage("The given id does not match any map !");
return false;
}
if(inv.firstEmpty() == -1)
{
joueur.sendMessage("Your inventory is full, you can't take the map !");
return false;
}
map = Bukkit.getMap(id);
if(map == null)
joueur.sendMessage("An eroor occured while getting map by ID !");
inv.addItem(new ItemStack(Material.MAP, 1, map.getId()));
joueur.sendMessage("Map "+ ChatColor.ITALIC+ id+ ChatColor.RESET+ " was added in your inventory.");
return true;
}
}

View File

@ -2,8 +2,12 @@ package fr.moribus.ImageOnMap;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
public final class ImageOnMap extends JavaPlugin
@ -11,6 +15,9 @@ public final class ImageOnMap extends JavaPlugin
int test = 0;
File dossier;
boolean dossierCree;
private FileConfiguration customConfig = null;
private File customConfigFile = null;
@Override
public void onEnable()
{
@ -20,6 +27,9 @@ public final class ImageOnMap extends JavaPlugin
// On ajoute si besoin les params par défaut du plugin
ImgUtility.CreeSectionConfig(this);
if(getConfig().getBoolean("import-maps"))
ImgUtility.ImporterConfig(this);
if(this.getConfig().getBoolean("collect-data"))
{
try
@ -35,6 +45,7 @@ public final class ImageOnMap extends JavaPlugin
if(dossierCree)
{
getCommand("tomap").setExecutor(new ImageRenduCommande(this));
getCommand("getmap").setExecutor(new GetMapCommand(this));
//getCommand("rmmap").setExecutor(new ImageSupprCommande(this));
this.saveDefaultConfig();
ChargerMap();
@ -44,6 +55,7 @@ public final class ImageOnMap extends JavaPlugin
System.out.println("[ImageOnMap] An error occured ! Unable to create Image folder. Plugin will NOT work !");
this.setEnabled(false);
}
}
@Override
@ -54,13 +66,13 @@ public final class ImageOnMap extends JavaPlugin
public void ChargerMap()
{
Set<String> cle = getConfig().getKeys(false);
Set<String> cle = getCustomConfig().getKeys(false);
int nbMap = 0, nbErr = 0;
for (String s: cle)
{
if(getConfig().getStringList(s).size() >= 3)
if(getCustomConfig().getStringList(s).size() >= 3)
{
SavedMap map = new SavedMap(this, Short.valueOf(getConfig().getStringList(s).get(0)));
SavedMap map = new SavedMap(this, Short.valueOf(getCustomConfig().getStringList(s).get(0)));
if(map.LoadMap())
nbMap++;
@ -73,5 +85,43 @@ public final class ImageOnMap extends JavaPlugin
if(nbErr != 0)
System.out.println(nbErr +" maps can't be loaded");
}
public void reloadCustomConfig()
{
if (customConfigFile == null)
{
customConfigFile = new File(getDataFolder(), "map.yml");
}
customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
// Look for defaults in the jar
InputStream defConfigStream = this.getResource("map.yml");
if (defConfigStream != null)
{
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
customConfig.setDefaults(defConfig);
}
}
public FileConfiguration getCustomConfig()
{
if (customConfig == null)
{
reloadCustomConfig();
}
return customConfig;
}
public void saveCustomConfig()
{
if (customConfig == null || customConfigFile == null) {
return;
}
try {
getCustomConfig().save(customConfigFile);
} catch (IOException ex) {
getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
}
}
}

View File

@ -1,6 +1,7 @@
package fr.moribus.ImageOnMap;
import java.io.File;
import java.util.ArrayList;
import java.util.Set;
import org.bukkit.ChatColor;
@ -48,6 +49,8 @@ public class ImgUtility
plugin.getConfig().set("Limit-map-by-player", 0);
if(plugin.getConfig().get("collect-data") == null)
plugin.getConfig().set("collect-data", true);
if(plugin.getConfig().get("import-maps") == null)
plugin.getConfig().set("import-maps", true);
plugin.saveConfig();
}
@ -55,10 +58,10 @@ public class ImgUtility
static int getNombreDeMaps(ImageOnMap plugin)
{
int nombre = 0;
Set<String> cle = plugin.getConfig().getKeys(false);
Set<String> cle = plugin.getCustomConfig().getKeys(false);
for (String s: cle)
{
if(plugin.getConfig().getStringList(s).size() >= 3)
if(plugin.getCustomConfig().getStringList(s).size() >= 3)
{
nombre++;
}
@ -69,10 +72,10 @@ public class ImgUtility
static int getNombreDeMapsParJoueur(ImageOnMap plugin, String pseudo)
{
int nombre = 0;
Set<String> cle = plugin.getConfig().getKeys(false);
Set<String> cle = plugin.getCustomConfig().getKeys(false);
for (String s: cle)
{
if(plugin.getConfig().getStringList(s).size() >= 3 && plugin.getConfig().getStringList(s).get(2).contentEquals(pseudo))
if(plugin.getCustomConfig().getStringList(s).size() >= 3 && plugin.getCustomConfig().getStringList(s).get(2).contentEquals(pseudo))
{
nombre++;
}
@ -82,10 +85,10 @@ public class ImgUtility
static boolean EstDansFichier(ImageOnMap plugin, short id)
{
Set<String> cle = plugin.getConfig().getKeys(false);
Set<String> cle = plugin.getCustomConfig().getKeys(false);
for (String s: cle)
{
if(plugin.getConfig().getStringList(s).size() >= 3 && Short.parseShort(plugin.getConfig().getStringList(s).get(0)) == id)
if(plugin.getCustomConfig().getStringList(s).size() >= 3 && Short.parseShort(plugin.getCustomConfig().getStringList(s).get(0)) == id)
{
return true;
}
@ -95,14 +98,42 @@ public class ImgUtility
public static boolean EstDansFichier(ImageOnMap plugin, short id, String pseudo)
{
Set<String> cle = plugin.getConfig().getKeys(false);
Set<String> cle = plugin.getCustomConfig().getKeys(false);
for (String s: cle)
{
if(plugin.getConfig().getStringList(s).size() >= 3 && Short.parseShort(plugin.getConfig().getStringList(s).get(0)) == id && plugin.getConfig().getStringList(s).get(2).contentEquals(pseudo))
if(plugin.getCustomConfig().getStringList(s).size() >= 3 && Short.parseShort(plugin.getCustomConfig().getStringList(s).get(0)) == id && plugin.getCustomConfig().getStringList(s).get(2).contentEquals(pseudo))
{
return true;
}
}
return false;
}
static boolean ImporterConfig(ImageOnMap plugin)
{
Set<String> cle = plugin.getConfig().getKeys(false);
plugin.getLogger().info("Start importing maps config to maps.yml...");
int i = 0;
for (String s: cle)
{
if(plugin.getConfig().getStringList(s).size() >= 3)
{
//plugin.getLogger().info("Importing "+ plugin.getConfig().getStringList(s).get(1));
ArrayList<String> liste = new ArrayList<String>();
liste.add(String.valueOf(plugin.getConfig().getStringList(s).get(0)));
liste.add(plugin.getConfig().getStringList(s).get(1));
liste.add(plugin.getConfig().getStringList(s).get(2));
plugin.getCustomConfig().set(plugin.getConfig().getStringList(s).get(1), liste);
plugin.getConfig().set(s, null);
i++;
}
}
plugin.getLogger().info("Importing finished. "+ i+ "maps were imported");
plugin.getConfig().set("import-maps", false);
plugin.saveConfig();
plugin.saveCustomConfig();
return true;
}
}

View File

@ -32,17 +32,17 @@ public class SavedMap
{
idMap = id;
plugin = plug;
Set<String> cle = plugin.getConfig().getKeys(false);
Set<String> cle = plugin.getCustomConfig().getKeys(false);
int tmp = 0;
for (String s: cle)
{
if(plugin.getConfig().getStringList(s).size() >= 3 && Short.valueOf(plugin.getConfig().getStringList(s).get(0)) == id)
if(plugin.getCustomConfig().getStringList(s).size() >= 3 && Short.valueOf(plugin.getCustomConfig().getStringList(s).get(0)) == id)
{
tmp++;
//System.out.println(tmp);
//MapView carte = Bukkit.getMap(Short.parseShort(plugin.getConfig().getStringList(s).get(0)));
nomImg = plugin.getConfig().getStringList(s).get(1);
nomJoueur = plugin.getConfig().getStringList(s).get(2);
nomImg = plugin.getCustomConfig().getStringList(s).get(1);
nomJoueur = plugin.getCustomConfig().getStringList(s).get(2);
try {
image = ImageIO.read(new File("./plugins/ImageOnMap/Image/"+ nomImg + ".png"));
//System.out.println("Chargement de l'image fini");
@ -76,8 +76,8 @@ public class SavedMap
liste.add(String.valueOf(idMap));
liste.add(nomImg);
liste.add(nomJoueur);
plugin.getConfig().set(nomImg, liste);
plugin.saveConfig();
plugin.getCustomConfig().set(nomImg, liste);
plugin.saveCustomConfig();
return true;
}