Keep working on the GUI API.

* NEW: The manage command now opens the MapList GUI.
* NEW: MapListGui: First actual implementation.
* NEW: MapManager/PlayerMapStore: Added the getMaps() method.
* BUG: GuiUtils: The hideAttributes() method does not crash anymore if
  the ItemFlags API is not available.
* OPT: ExplorerGui: Inventory variables are now computed only on data update.
This commit is contained in:
Adrien Prokopowicz 2015-07-30 08:52:18 +02:00
parent 586644e486
commit 0ad44db92b
6 changed files with 82 additions and 50 deletions

View File

@ -20,10 +20,8 @@ package fr.moribus.imageonmap.commands.maptool;
import fr.moribus.imageonmap.commands.*;
import fr.moribus.imageonmap.gui.core.*;
import fr.moribus.imageonmap.gui.list.*;
import fr.moribus.imageonmap.guiproko.list.MapListGui;
import fr.moribus.imageonmap.guiproko.core.Gui;
import fr.moribus.imageonmap.guiproko.list.MaterialGui;
@CommandInfo(name = "manage")
@ -38,6 +36,6 @@ public class ManageCommand extends Command
protected void run() throws CommandException
{
//GuiManager.openGui(playerSender(), new MapListGui());
Gui.open(playerSender(), new MaterialGui());
Gui.open(playerSender(), new MapListGui());
}
}

View File

@ -34,8 +34,9 @@ abstract public class ExplorerGui<T> extends ActionGui
private int currentPageX = 0;
private int pageCountX = 0;
private int pageCountY = 0;
private int pageCountX;
private int pageCountY;
private int inventoryViewSize;
private Mode mode = Mode.CREATIVE;
@ -81,22 +82,14 @@ abstract public class ExplorerGui<T> extends ActionGui
if(hasActions()) super.populate(inventory);
}
@Override
public void update()
{
//TODO: Make inventory fit to content
setSize(MAX_INVENTORY_SIZE);
super.update();
}
@Override
protected void onClick(InventoryClickEvent event)
{
int slot = event.getRawSlot();
//Clicked in the action bar
if(slot > MAX_INVENTORY_SIZE - INVENTORY_ROW_SIZE
if(hasActions() &&
slot >= MAX_INVENTORY_SIZE - INVENTORY_ROW_SIZE
&& slot < MAX_INVENTORY_SIZE)
{
super.onClick(event);
@ -127,13 +120,6 @@ abstract public class ExplorerGui<T> extends ActionGui
}
}
private int getDataIndex(int slot)
{
int inventorySize = MAX_INVENTORY_SIZE;
if(getPageCount() > 1) inventorySize -= INVENTORY_ROW_SIZE;
return currentPageX * inventorySize + slot;
}
private void onActionPickup(InventoryClickEvent event)
{
if(mode.equals(Mode.READONLY))
@ -141,13 +127,19 @@ abstract public class ExplorerGui<T> extends ActionGui
event.setCancelled(true);
return;
}
int dataIndex = getDataIndex(event.getSlot());
int dataIndex = currentPageX * inventoryViewSize + event.getSlot();
if(dataIndex < 0 || dataIndex >= data.length)
{
event.setCancelled(true);
return;
}
event.setCurrentItem(getPickedUpItem(data[dataIndex]));
ItemStack pickedUpItem = getPickedUpItem(data[dataIndex]);
if(pickedUpItem == null)
{
event.setCancelled(true);
return;
}
event.setCurrentItem(pickedUpItem);
GuiUtils.setItemLater(this, event.getSlot(), getViewItem(data[dataIndex]));
}
@ -170,7 +162,31 @@ abstract public class ExplorerGui<T> extends ActionGui
@Override
protected void onAfterUpdate()
{
if(getPageCount() > 1)
inventoryViewSize = MAX_INVENTORY_SIZE;
//Calculating page count
if(data.length <= 0)
{
pageCountX = 1;
pageCountY = 1;
}
else if(viewWidth <= 0)
{
if(hasActions() || data.length > MAX_INVENTORY_SIZE)
inventoryViewSize -= INVENTORY_ROW_SIZE;
pageCountX = (int)Math.ceil(data.length / inventoryViewSize);
pageCountY = 1;
}
else
{
//TODO: NYI
}
//TODO: Make inventory fit to content
setSize(MAX_INVENTORY_SIZE);
if(pageCountX > 1)
{
action("previous", MAX_INVENTORY_SIZE - INVENTORY_ROW_SIZE);
action("next", MAX_INVENTORY_SIZE - 1);
@ -208,7 +224,7 @@ abstract public class ExplorerGui<T> extends ActionGui
public boolean canGoNext()
{
return currentPageX < getPageCount();
return currentPageX < pageCountX;
}
public boolean canGoPrevious()
@ -218,22 +234,12 @@ abstract public class ExplorerGui<T> extends ActionGui
public int getPageCount()
{
if(data.length == 0) return 0;
if(viewWidth > 0)
{
if(viewWidth > INVENTORY_ROW_SIZE) return 1;
return (int)Math.ceil(viewWidth / (INVENTORY_ROW_SIZE - 1));
}
return (int)Math.ceil(data.length / (MAX_INVENTORY_SIZE - (hasActions() ? INVENTORY_ROW_SIZE : 0)));
return pageCountX;
}
public int getVerticalPageCount()
{
if(viewWidth <= 0) return 0;
return (int)Math.ceil(data.length / (MAX_INVENTORY_COLUMN_SIZE - (hasActions() ? 0 : 1)));
return pageCountY;
}
protected Mode getMode() {return mode;}

View File

@ -63,6 +63,7 @@ abstract public class GuiUtils
static public void hideItemAttributes(ItemMeta meta)
{
if(addItemFlagsMethod == null) return;
try
{
addItemFlagsMethod.invoke(meta, itemFlagValues);
@ -113,6 +114,7 @@ abstract public class GuiUtils
meta.setLore(loreLines);
if(itemStack.getType().equals(Material.MAP))
hideItemAttributes(meta);
itemStack.setItemMeta(meta);
return itemStack;
}

View File

@ -19,33 +19,49 @@
package fr.moribus.imageonmap.guiproko.list;
import fr.moribus.imageonmap.guiproko.core.ExplorerGui;
import fr.moribus.imageonmap.guiproko.core.GuiUtils;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import java.util.List;
import fr.moribus.imageonmap.map.PosterMap;
import fr.moribus.imageonmap.map.SingleMap;
import fr.moribus.imageonmap.ui.MapItemManager;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class MapListGui extends ExplorerGui<ImageMap>
{
private ImageMap[] maps;
@Override
protected ItemStack getViewItem(ImageMap data)
{
if(data instanceof SingleMap)
{
return GuiUtils.makeItem(Material.EMPTY_MAP, data.getName(), "Single map", "#" + data.getId());
}
PosterMap map = (PosterMap) data;
return GuiUtils.makeItem(Material.MAP, data.getName(),
"Poster map ("+map.getColumnCount()+"x"+map.getRowCount()+")", "#" + data.getId());
}
@Override
protected ItemStack getPickedUpItem(ImageMap map)
{
if(map instanceof SingleMap)
{
return MapItemManager.createMapItem(map.getMapsIDs()[0], map.getName());
}
MapItemManager.give((Player) getPlayer(), map);
return null;
}
@Override
protected void onUpdate()
{
List<ImageMap> listMaps = MapManager.getMapList(getPlayer().getUniqueId());
maps = new ImageMap[listMaps.size()];
for(int i = listMaps.size(); i --> 0;)
{
maps[i] = listMaps.get(i);
}
ImageMap[] maps = MapManager.getMaps(getPlayer().getUniqueId());
setData(maps);
setTitle("Your maps (" + maps.length + " total)");
}
}

View File

@ -139,6 +139,11 @@ abstract public class MapManager
return getPlayerMapStore(playerUUID).getMapList();
}
static public ImageMap[] getMaps(UUID playerUUID)
{
return getPlayerMapStore(playerUUID).getMaps();
}
/**
* Returns the number of minecraft maps used by the images rendered by the given player.
*

View File

@ -127,6 +127,11 @@ public class PlayerMapStore implements ConfigurationSerializable
return new ArrayList(mapList);
}
public synchronized ImageMap[] getMaps()
{
return mapList.toArray(new ImageMap[mapList.size()]);
}
public synchronized ImageMap getMap(String mapId)
{
for(ImageMap map : mapList)