* NEW: Added the "map item cache" and the associated /maptool getremaining (or getrest) command.

This commit is contained in:
Prokopyl 2015-04-01 03:53:52 +02:00
parent 2280e96125
commit 27f836977f
11 changed files with 174 additions and 15 deletions

View File

@ -23,6 +23,7 @@ import fr.moribus.imageonmap.image.ImageIOExecutor;
import fr.moribus.imageonmap.image.ImageRendererExecutor;
import fr.moribus.imageonmap.image.MapInitEvent;
import fr.moribus.imageonmap.map.MapManager;
import fr.moribus.imageonmap.ui.MapItemManager;
import java.io.File;
import org.bukkit.plugin.java.JavaPlugin;
@ -86,6 +87,7 @@ public final class ImageOnMap extends JavaPlugin
Commands.init(this);
getServer().getPluginManager().registerEvents(new MapInitEvent(), this);
MapInitEvent.init();
MapItemManager.init();
}
@Override
@ -94,6 +96,7 @@ public final class ImageOnMap extends JavaPlugin
ImageIOExecutor.stop();
ImageRendererExecutor.stop();
MapManager.exit();
MapItemManager.exit();
}
}

View File

@ -34,6 +34,7 @@ abstract public class Command
protected final String commandName;
protected final String usageParameters;
protected final String commandDescription;
protected final String[] aliases;
protected CommandSender sender;
protected String[] args;
@ -51,6 +52,7 @@ abstract public class Command
commandName = commandInfo.name().toLowerCase();
usageParameters = commandInfo.usageParameters();
commandDescription = commandGroup.getDescription(commandName);
aliases = commandInfo.aliases();
}
public boolean canExecute(CommandSender sender)
@ -111,9 +113,21 @@ abstract public class Command
return commandGroup;
}
public String[] getAliases()
{
return aliases;
}
public boolean matches(String name)
{
return commandName.equals(name.toLowerCase());
if(commandName.equals(name.toLowerCase())) return true;
for(String alias : aliases)
{
if(alias.equals(name)) return true;
}
return false;
}

View File

@ -26,4 +26,5 @@ public @interface CommandInfo
{
String name();
String usageParameters() default "";
String[] aliases() default {};
}

View File

@ -45,7 +45,8 @@ public enum Commands implements TabCompleter, CommandExecutor
ListCommand.class,
GetCommand.class,
DeleteConfirmCommand.class,
DeleteNoConfirmCommand.class
DeleteNoConfirmCommand.class,
GetRemainingCommand.class
),
TOMAP(MAPTOOL, NewCommand.class, "tomap");

View File

@ -33,7 +33,11 @@ public class GetCommand extends Command
protected void run() throws CommandException
{
Player player = playerSender();
getMapFromArgs().give(player.getInventory());
if(getMapFromArgs().give(player))
{
info("The requested map was too big to fit in your inventory.");
info("Use '/maptool getremaining' to get the remaining maps.");
}
}
@Override

View File

@ -0,0 +1,57 @@
/*
* 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.Command;
import fr.moribus.imageonmap.commands.CommandException;
import fr.moribus.imageonmap.commands.CommandInfo;
import fr.moribus.imageonmap.commands.Commands;
import fr.moribus.imageonmap.ui.MapItemManager;
import org.bukkit.entity.Player;
@CommandInfo(name = "getremaining", aliases = {"getrest"})
public class GetRemainingCommand extends Command
{
public GetRemainingCommand(Commands commandGroup) {
super(commandGroup);
}
@Override
protected void run() throws CommandException
{
Player player = playerSender();
if(MapItemManager.getCacheSize(player) <= 0)
{
info("You have no remaining map.");
return;
}
int givenMaps = MapItemManager.giveCache(player);
if(givenMaps == 0)
{
error("Your inventory is full ! Make some space before requesting the remaining maps.");
}
else
{
info("There are " + MapItemManager.getCacheSize(player) + " maps remaining.");
}
}
}

View File

@ -68,7 +68,11 @@ public class NewCommand extends Command
public void finished(ImageMap result)
{
player.sendMessage("§7Rendering finished !");
result.give(player.getInventory());
if(result.give(player))
{
info("The rendered map was too big to fit in your inventory.");
info("Use '/maptool getremaining' to get the remaining maps.");
}
}
@Override

View File

@ -25,7 +25,7 @@ import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.inventory.Inventory;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public abstract class ImageMap implements ConfigurationSerializable
@ -74,9 +74,9 @@ public abstract class ImageMap implements ConfigurationSerializable
return managesMap(item.getDurability());
}
public void give(Inventory inventory)
public boolean give(Player player)
{
MapItemManager.give(inventory, this);
return MapItemManager.give(player, this);
}
/* ====== Serialization methods ====== */

View File

@ -21,7 +21,12 @@ package fr.moribus.imageonmap.ui;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.PosterMap;
import fr.moribus.imageonmap.map.SingleMap;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Queue;
import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -29,25 +34,73 @@ import org.bukkit.inventory.meta.ItemMeta;
public class MapItemManager implements Listener
{
static public void give(Inventory inventory, ImageMap map)
static private HashMap<UUID, Queue<ItemStack>> mapItemCache;
static public void init()
{
if(map instanceof PosterMap) give(inventory, (PosterMap) map);
else if(map instanceof SingleMap) give(inventory, (SingleMap) map);
mapItemCache = new HashMap();
}
static public void give(Inventory inventory, SingleMap map)
static public void exit()
{
inventory.addItem(createMapItem(map.getMapsIDs()[0], map.getName()));
mapItemCache.clear();
mapItemCache = null;
}
static public void give(Inventory inventory, PosterMap map)
static public boolean give(Player player, ImageMap map)
{
if(map instanceof PosterMap) return give(player, (PosterMap) map);
else if(map instanceof SingleMap) return give(player, (SingleMap) map);
return false;
}
static public boolean give(Player player, SingleMap map)
{
return give(player, createMapItem(map.getMapsIDs()[0], map.getName()));
}
static public boolean give(Player player, PosterMap map)
{
short[] mapsIDs = map.getMapsIDs();
boolean inventoryFull = false;
for(int i = 0, c = mapsIDs.length; i < c; i++)
{
inventory.addItem(createMapItem(mapsIDs[i], map.getName() +
inventoryFull = give(player,
createMapItem(mapsIDs[i], map.getName() +
" (row " + map.getRowAt(i) +
", column " + map.getColumnAt(i) + ")"));
", column " + map.getColumnAt(i) + ")")) || inventoryFull;
}
return inventoryFull;
}
static public int giveCache(Player player)
{
Queue<ItemStack> cache = getCache(player);
Inventory inventory = player.getInventory();
int givenItemsCount = 0;
while(inventory.firstEmpty() >= 0 && !cache.isEmpty())
{
inventory.addItem(cache.poll());
givenItemsCount++;
}
return givenItemsCount;
}
static private boolean give(Player player, ItemStack item)
{
if(player.getInventory().firstEmpty() <= -1)
{
getCache(player).add(item);
return true;
}
else
{
player.getInventory().addItem(item);
return false;
}
}
@ -61,4 +114,20 @@ public class MapItemManager implements Listener
return itemMap;
}
static public int getCacheSize(Player player)
{
return getCache(player).size();
}
static private Queue<ItemStack> getCache(Player player)
{
Queue<ItemStack> cache = mapItemCache.get(player.getUniqueId());
if(cache == null)
{
cache = new ArrayDeque<>();
mapItemCache.put(player.getUniqueId(), cache);
}
return cache;
}
}

View File

@ -3,5 +3,6 @@ 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.
getremaining: Gives you the remaining maps that could not fit in your inventory
list: Lists all the map you currently have.
help : Use help for more information about a command.

View File

@ -0,0 +1,5 @@
Gives you the remaining maps that could not fit in your inventory.
If you requested a multi-part (poster) map, and all the parts can't fit in
your inventory, then only first ones are given to you, and the remaining
ones are saved. You can then use this command to retreive them.