* NEW: Implemented the list, get and delete maptool commands.

This commit is contained in:
Prokopyl 2015-03-31 19:17:06 +02:00
parent 244a3d7122
commit 6c95e8a95e
16 changed files with 475 additions and 28 deletions

View File

@ -19,6 +19,8 @@
package fr.moribus.imageonmap.commands;
import fr.moribus.imageonmap.commands.CommandException.Reason;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -129,6 +131,35 @@ abstract public class Command
return (Player)sender;
}
protected ImageMap getMapFromArgs() throws CommandException
{
return getMapFromArgs(playerSender(), 0, true);
}
protected ImageMap getMapFromArgs(Player player, int index, boolean expand) throws CommandException
{
if(args.length <= index) throwInvalidArgument("You need to give a map name.");
ImageMap map;
String mapName = args[index];
if(expand)
{
for(int i = index + 1, c = args.length; i < c; i++)
{
mapName += " " + args[i];
}
}
mapName = mapName.trim();
map = MapManager.getMap(player.getUniqueId(), mapName);
if(map == null) error("This map does not exist.");
return map;
}
///////////// Methods for command execution /////////////
@ -190,21 +221,21 @@ abstract public class Command
return matches;
}
/*protected List<String> getMatchingToolNames(Player player, String prefix)
protected List<String> getMatchingMapNames(Player player, String prefix)
{
return getMatchingToolNames(ToolManager.getToolList(player.getUniqueId()), prefix);
return getMatchingToolNames(MapManager.getMapList(player.getUniqueId()), prefix);
}
protected List<String> getMatchingToolNames(Iterable<? extends CommandTool> tools, String prefix)
protected List<String> getMatchingToolNames(Iterable<? extends ImageMap> maps, String prefix)
{
List<String> matches = new ArrayList<String>();
for(CommandTool tool : tools)
for(ImageMap map : maps)
{
if(tool.getId().startsWith(prefix)) matches.add(tool.getId());
if(map.getId().startsWith(prefix)) matches.add(map.getId());
}
return matches;
}*/
}
}

View File

@ -41,7 +41,12 @@ import org.bukkit.plugin.java.JavaPlugin;
public enum Commands implements TabCompleter, CommandExecutor
{
MAPTOOL(new String[]{"maptool"},
NewCommand.class),
NewCommand.class,
ListCommand.class,
GetCommand.class,
DeleteConfirmCommand.class,
DeleteNoConfirmCommand.class
),
TOMAP(MAPTOOL, NewCommand.class, "tomap");

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2013 Moribus
* Copyright (C) 2015 ProkopyL <prokopylmc@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.moribus.imageonmap.commands.maptool;
import fr.moribus.imageonmap.commands.*;
import fr.moribus.imageonmap.map.ImageMap;
import java.util.List;
@CommandInfo(name = "delete", usageParameters = "[tool name]")
public class DeleteConfirmCommand extends Command
{
public DeleteConfirmCommand(Commands commandGroup) {
super(commandGroup);
}
@Override
protected void run() throws CommandException
{
ImageMap map = getMapFromArgs();
tellRaw("{text:\"You are going to delete \",extra:[{text:\""+ map.getId() +"\",color:gold},{text:\". Are you sure ? \",color:white}," +
"{text:\"[Confirm]\", color:green, clickEvent:{action:run_command,value:\"/maptool delete-noconfirm "+ map.getId() +"\"}, " +
"hoverEvent:{action:show_text,value:{text:\"This map will be deleted \",extra:[{text:\"forever\",color:red,bold:true,italic:true,underlined:true}, {text:\" !\", underlined:true}],underlined:true}}}]}");
}
@Override
protected List<String> complete() throws CommandException
{
if(args.length == 1)
return getMatchingMapNames(playerSender(), args[0]);
return null;
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2013 Moribus
* Copyright (C) 2015 ProkopyL <prokopylmc@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.moribus.imageonmap.commands.maptool;
import fr.moribus.imageonmap.commands.*;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import java.util.List;
import org.bukkit.entity.Player;
@CommandInfo(name = "delete-noconfirm", usageParameters = "[map name]")
public class DeleteNoConfirmCommand extends Command
{
public DeleteNoConfirmCommand(Commands commandGroup) {
super(commandGroup);
}
@Override
protected void run() throws CommandException
{
Player player = playerSender();
ImageMap map = getMapFromArgs();
MapManager.clear(player.getInventory(), map);
MapManager.deleteMap(map);
info("Map successfully deleted.");
}
@Override
protected List<String> complete() throws CommandException
{
if(args.length == 1)
return getMatchingMapNames(playerSender(), args[0]);
return null;
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2013 Moribus
* Copyright (C) 2015 ProkopyL <prokopylmc@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.moribus.imageonmap.commands.maptool;
import fr.moribus.imageonmap.commands.*;
import java.util.List;
import org.bukkit.entity.Player;
@CommandInfo(name = "get")
public class GetCommand extends Command
{
public GetCommand(Commands commandGroup) {
super(commandGroup);
}
@Override
protected void run() throws CommandException
{
Player player = playerSender();
getMapFromArgs().give(player.getInventory());
}
@Override
protected List<String> complete() throws CommandException
{
if(args.length == 1)
return getMatchingMapNames(playerSender(), args[0]);
return null;
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2013 Moribus
* Copyright (C) 2015 ProkopyL <prokopylmc@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.moribus.imageonmap.commands.maptool;
import fr.moribus.imageonmap.commands.*;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import java.util.List;
import org.bukkit.entity.Player;
@CommandInfo(name = "list")
public class ListCommand extends Command
{
public ListCommand(Commands commandGroup) {
super(commandGroup);
}
@Override
protected void run() throws CommandException
{
Player player = playerSender();
List<ImageMap> mapList = MapManager.getMapList(player.getUniqueId());
if(mapList.isEmpty())
{
info("No map found.");
return;
}
info(mapList.size() + " maps found.");
String sMapList = mapList.get(0).getId();
for(int i = 1, c = mapList.size(); i < c; i++)
{
sMapList += "§7,§r" + mapList.get(i).getId();
}
player.sendMessage(sMapList);
}
}

View File

@ -19,10 +19,13 @@
package fr.moribus.imageonmap.image;
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.worker.Worker;
import fr.moribus.imageonmap.worker.WorkerCallback;
import fr.moribus.imageonmap.worker.WorkerRunnable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Files;
import javax.imageio.ImageIO;
public class ImageIOExecutor extends Worker
@ -86,4 +89,27 @@ public class ImageIOExecutor extends Worker
ImageIOExecutor.saveImage(ImageOnMap.getPlugin().getImageFile(mapsIDs[i]), image.getImageAt(i));
}
}
static public void deleteImage(ImageMap map)
{
short[] mapsIDs = map.getMapsIDs();
for(int i = 0, c = mapsIDs.length; i < c; i++)
{
deleteImage(ImageOnMap.getPlugin().getImageFile(mapsIDs[i]));
}
}
static public void deleteImage(final File file)
{
instance.submitQuery(new WorkerRunnable<Void>()
{
@Override
public Void run() throws Throwable
{
Files.delete(file.toPath());
return null;
}
});
}
}

View File

@ -36,21 +36,43 @@ public abstract class ImageMap implements ConfigurationSerializable
static public final int WIDTH = 128;
static public final int HEIGHT = 128;
static public final String DEFAULT_NAME = "Map";
private String id;
private final UUID userUUID;
private final Type mapType;
private String imageName;
private String name;
protected ImageMap(UUID userUUID, Type mapType)
{
this(userUUID, mapType, null, null);
}
protected ImageMap(UUID userUUID, Type mapType, String id, String name)
{
this.userUUID = userUUID;
this.mapType = mapType;
this.id = id;
this.name = name;
if(this.id == null)
{
if(this.name == null) this.name = DEFAULT_NAME;
this.id = MapManager.getNextAvailableMapID(name, userUUID);
}
}
public abstract short[] getMapsIDs();
public abstract boolean managesMap(short mapID);
public boolean managesMap(ItemStack item)
{
if(item == null) return false;
if(item.getType() != Material.MAP) return false;
return managesMap(item.getDurability());
}
public void give(Inventory inventory)
{
short[] mapsIDs = getMapsIDs();
@ -85,8 +107,10 @@ public abstract class ImageMap implements ConfigurationSerializable
protected ImageMap(Map<String, Object> map, UUID userUUID, Type mapType) throws InvalidConfigurationException
{
this(userUUID, mapType);
this.imageName = getNullableFieldValue(map, "name");
this(userUUID, mapType,
(String) getNullableFieldValue(map, "id"),
(String) getNullableFieldValue(map, "name"));
}
protected abstract void postSerialize(Map<String, Object> map);
@ -95,8 +119,9 @@ public abstract class ImageMap implements ConfigurationSerializable
public Map<String, Object> serialize()
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("type", mapType.toString());
map.put("name", imageName);
map.put("name", name);
this.postSerialize(map);
return map;
}
@ -128,13 +153,26 @@ public abstract class ImageMap implements ConfigurationSerializable
return userUUID;
}
public String getImageName()
public String getName()
{
return imageName;
return name;
}
public String getId()
{
return id;
}
public void setImageName(String imageName)
public void rename(String id, String name)
{
this.imageName = imageName;
this.id = id;
this.name = name;
}
public void rename(String name)
{
if(this.name.equals(name)) return;
this.id = MapManager.getNextAvailableMapID(name, userUUID);
this.name = name;
}
}

View File

@ -19,10 +19,15 @@
package fr.moribus.imageonmap.map;
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.image.ImageIOExecutor;
import fr.moribus.imageonmap.image.PosterImage;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;
abstract public class MapManager
@ -55,10 +60,22 @@ abstract public class MapManager
return false;
}
static public boolean managesMap(ItemStack item)
{
synchronized(playerMaps)
{
for(PlayerMapStore mapStore : playerMaps)
{
if(mapStore.managesMap(item)) return true;
}
}
return false;
}
static public ImageMap createMap(UUID playerUUID, short mapID)
{
ImageMap newMap = new SingleMap(playerUUID, mapID);
addMap(newMap, playerUUID);
addMap(newMap);
return newMap;
}
@ -73,7 +90,7 @@ abstract public class MapManager
{
newMap = new PosterMap(playerUUID, mapsIDs, image.getColumns(), image.getLines());
}
addMap(newMap, playerUUID);
addMap(newMap);
return newMap;
}
@ -87,9 +104,15 @@ abstract public class MapManager
return mapsIds;
}
static public void addMap(ImageMap map, UUID playerUUID)
static public void addMap(ImageMap map)
{
getPlayerMapStore(playerUUID).addMap(map);
getPlayerMapStore(map.getUserUUID()).addMap(map);
}
static public void deleteMap(ImageMap map)
{
ImageIOExecutor.deleteImage(map);
getPlayerMapStore(map.getUserUUID()).deleteMap(map);
}
static public void notifyModification(UUID playerUUID)
@ -99,13 +122,50 @@ abstract public class MapManager
Bukkit.getScheduler().runTaskLater(ImageOnMap.getPlugin(), new AutosaveRunnable(), SAVE_DELAY);
}
static public String getNextAvailableMapID(String mapId, UUID playerUUID)
{
return getPlayerMapStore(playerUUID).getNextAvailableMapID(mapId);
}
static public List<ImageMap> getMapList(UUID playerUUID)
{
return getPlayerMapStore(playerUUID).getMapList();
}
static public ImageMap getMap(UUID playerUUID, String mapId)
{
return getPlayerMapStore(playerUUID).getMap(mapId);
}
static public void clear(Inventory inventory)
{
for(int i = 0, c = inventory.getSize(); i < c; i++)
{
if(managesMap(inventory.getItem(i)))
{
inventory.setItem(i, new ItemStack(Material.AIR));
}
}
}
static public void clear(Inventory inventory, ImageMap map)
{
for(int i = 0, c = inventory.getSize(); i < c; i++)
{
if(map.managesMap(inventory.getItem(i)))
{
inventory.setItem(i, new ItemStack(Material.AIR));
}
}
}
static public void save()
{
synchronized(playerMaps)
{
for(PlayerMapStore tStore : playerMaps)
{
tStore.saveMapsFile();
tStore.save();
}
}
}
@ -117,6 +177,7 @@ abstract public class MapManager
{
store = new PlayerMapStore(playerUUID);
synchronized(playerMaps){playerMaps.add(store);}
store.load();
}
return store;
}
@ -142,7 +203,7 @@ abstract public class MapManager
{
for(PlayerMapStore toolStore : playerMaps)
{
if(toolStore.isModified()) toolStore.saveMapsFile();
if(toolStore.isModified()) toolStore.save();
}
autosaveTask = null;
}

View File

@ -32,6 +32,7 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.inventory.ItemStack;
public class PlayerMapStore implements ConfigurationSerializable
{
@ -42,7 +43,6 @@ public class PlayerMapStore implements ConfigurationSerializable
public PlayerMapStore(UUID playerUUID)
{
this.playerUUID = playerUUID;
loadMapsFile();
}
public boolean managesMap(short mapID)
@ -54,12 +54,65 @@ public class PlayerMapStore implements ConfigurationSerializable
return false;
}
public boolean managesMap(ItemStack item)
{
for(ImageMap map : mapList)
{
if(map.managesMap(item)) return true;
}
return false;
}
public void addMap(ImageMap map)
{
mapList.add(map);
notifyModification();
}
public void deleteMap(ImageMap map)
{
mapList.remove(map);
notifyModification();
}
public boolean mapExists(String id)
{
for(ImageMap map : mapList)
{
if(map.getId().equals(id)) return true;
}
return false;
}
public String getNextAvailableMapID(String mapId)
{
if(!mapExists(mapId)) return mapId;
int id = 0;
do
{
id++;
}while(mapExists(mapId + "-" + id));
return mapId + "-" + id;
}
public List<ImageMap> getMapList()
{
return new ArrayList(mapList);
}
public ImageMap getMap(String mapId)
{
for(ImageMap map : mapList)
{
if(map.getId().equals(mapId)) return map;
}
return null;
}
/* ===== Getters & Setters ===== */
public UUID getUUID()
@ -123,23 +176,23 @@ public class PlayerMapStore implements ConfigurationSerializable
private FileConfiguration getToolConfig()
{
if(mapConfig == null) loadMapsFile();
if(mapConfig == null) load();
return mapConfig;
}
private void loadMapsFile()
public void load()
{
if(mapsFile == null)
{
mapsFile = new File(ImageOnMap.getPlugin().getMapsDirectory(), playerUUID.toString() + ".yml");
if(!mapsFile.exists()) saveMapsFile();
if(!mapsFile.exists()) save();
}
mapConfig = YamlConfiguration.loadConfiguration(mapsFile);
loadFromConfig(getToolConfig().getConfigurationSection("PlayerMapStore"));
}
public void saveMapsFile()
public void save()
{
if(mapsFile == null || mapConfig == null) return;
getToolConfig().set("PlayerMapStore", this.serialize());

View File

@ -18,6 +18,7 @@
package fr.moribus.imageonmap.map;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.configuration.InvalidConfigurationException;
@ -61,7 +62,13 @@ public class PosterMap extends ImageMap
columnCount = getFieldValue(map, "columns");
rowCount = getFieldValue(map, "rows");
mapsIDs = getFieldValue(map, "mapsIDs");
List<Integer> idList = getFieldValue(map, "mapsIDs");
mapsIDs = new short[idList.size()];
for(int i = 0, c = idList.size(); i < c; i++)
{
mapsIDs[i] = (short) ((int) idList.get(i));
}
}
@Override

View File

@ -1,2 +1,7 @@
This command manages and creates ImagesOnMaps.
new: Creates a new ImageOnMap.
new: Creates a new ImageOnMap
delete: Deletes a map.
delete-noconfirm: Deletes a map. Deletion is permanent and made without confirmation
get: Gives you a map.
list: Lists all the map you currently have.
help : Use help for more information about a command.

View File

@ -0,0 +1,4 @@
Deletes a map, and removes all parts and copies from your inventory.
§cWARNING: §r THERE WILL BE NO CONFIRMATION. MAP DELETION IS §lPERMANENT§r.
[map name] : The name of the map you want to delete.

View File

@ -0,0 +1,5 @@
Deletes a map, and removes all parts and copies from your inventory.
As deletion is permanent, a confirmation message will be shown
to the sending player.
[map name] : The name of the map you want to delete.

View File

@ -0,0 +1,3 @@
Gives you a map, by putting it directly in your inventory.
<map name> : The name of the map you want to get.

View File

@ -0,0 +1 @@
Gives you a list of all the maps you currently have.