use of com.bergerkiller.bukkit.common.map.util.MapUUID in project BKCommonLib by bergerhealer.
the class MapDisplayInfo method loadTiles.
/**
* Loads the tiles in a Map Display. This also removes tiles in the display
* that don't actually exist.
*
* @param session The session of the map display
* @param initialize Whether the tiles are initialized, and contents are not yet drawn
*/
public void loadTiles(MapSession session, boolean initialize) {
// Collect all tile x/y coordinates into a long hashset
LongHashSet tile_coords = new LongHashSet();
for (ItemFrameInfo itemFrame : this.itemFrames) {
MapUUID uuid = itemFrame.lastMapUUID;
if (uuid != null) {
tile_coords.add(uuid.getTileX(), uuid.getTileY());
}
}
tile_coords.add(0, 0);
if (initialize) {
// Wipe previous tiles when initializing
session.tiles.clear();
} else {
// Remove tiles from the display that are no longer present
// Remove existing tiles from the set at the same time
// We are left with a set containing tiles that must be added
Iterator<MapDisplayTile> iter = session.tiles.iterator();
while (iter.hasNext()) {
MapDisplayTile tile = iter.next();
if (!tile_coords.remove(tile.tileX, tile.tileY)) {
iter.remove();
}
}
}
// Add all remaining tiles to the display
LongHashSet.LongIterator iter = tile_coords.longIterator();
while (iter.hasNext()) {
long coord = iter.next();
MapDisplayTile newTile = new MapDisplayTile(this.uuid, MathUtil.longHashMsw(coord), MathUtil.longHashLsw(coord));
session.tiles.add(newTile);
// Send map packets for the added tile
if (!initialize) {
for (MapSession.Owner owner : session.onlineOwners) {
owner.sendDirtyTile(newTile);
}
}
}
}
use of com.bergerkiller.bukkit.common.map.util.MapUUID in project BKCommonLib by bergerhealer.
the class MapDisplay method preRunInitialize.
// called when setRunning(true) is called, right before onAttached
private void preRunInitialize() {
// Figure out what item frame tiles exist on the server
this.tiles.clear();
for (ItemFrameInfo itemFrame : this.info.itemFrames) {
MapUUID uuid = itemFrame.lastMapUUID;
if (uuid != null && (uuid.getTileX() != 0 || uuid.getTileY() != 0)) {
if (!this.containsTile(uuid.getTileX(), uuid.getTileY())) {
this.tiles.add(new MapDisplayTile(this, uuid.getTileX(), uuid.getTileY()));
}
}
}
// Tile 0,0 always exists (held map)
if (!this.containsTile(0, 0)) {
this.tiles.add(0, new MapDisplayTile(this, 0, 0));
}
// Calculate the dimensions from the tiles and further initialize the buffers
int minTileX = Integer.MAX_VALUE;
int minTileY = Integer.MAX_VALUE;
int maxTileX = 0;
int maxTileY = 0;
for (MapDisplayTile tile : this.tiles) {
if (tile.tileX < minTileX)
minTileX = tile.tileX;
if (tile.tileX > maxTileX)
maxTileX = tile.tileX;
if (tile.tileY < minTileY)
minTileY = tile.tileY;
if (tile.tileY > maxTileY)
maxTileY = tile.tileY;
}
this.width = (maxTileX - minTileX + 1) * 128;
this.height = (maxTileY - minTileY + 1) * 128;
this.zbuffer = new byte[this.width * this.height];
this.livebuffer = new byte[this.width * this.height];
this.layerStack = new Layer(this);
}
use of com.bergerkiller.bukkit.common.map.util.MapUUID in project BKCommonLib by bergerhealer.
the class CommonMapController method storeStaticMapId.
/**
* Forces a particular map Id to stay static (unchanging) and stores it
* as such in the mappings. No tiling is possible with static map Ids.
*
* @param mapId
*/
private synchronized void storeStaticMapId(short mapId) {
if (storeDynamicMapId(mapUUIDById.get(mapId)) != mapId) {
MapUUID mapUUID = new MapUUID(CommonMapUUIDStore.getStaticMapUUID(mapId), 0, 0);
mapUUIDById.put(mapId, mapUUID);
mapIdByUUID.put(mapUUID, mapId);
}
}
use of com.bergerkiller.bukkit.common.map.util.MapUUID in project BKCommonLib by bergerhealer.
the class CommonMapController method cleanupUnusedUUIDs.
private synchronized void cleanupUnusedUUIDs(Set<MapUUID> existingMapUUIDs) {
HashSet<MapUUID> idsToRemove = new HashSet<MapUUID>(mapIdByUUID.keySet());
idsToRemove.removeAll(existingMapUUIDs);
for (MapUUID toRemove : idsToRemove) {
// Clean up the map display information first
MapDisplayInfo displayInfo = maps.get(toRemove.getUUID());
if (displayInfo != null) {
if (displayInfo.sessions.isEmpty()) {
maps.remove(toRemove.getUUID());
} else {
// still has an active session; cannot remove
continue;
}
}
// Clean up from bi-directional mapping
Short mapId = mapIdByUUID.remove(toRemove);
if (mapId != null) {
mapUUIDById.remove(mapId.intValue());
}
// Clean up from 'dirty' set (probably never needed)
dirtyMapUUIDSet.remove(toRemove);
}
}
use of com.bergerkiller.bukkit.common.map.util.MapUUID in project BKCommonLib by bergerhealer.
the class CommonMapController method updateMapItem.
/**
* Updates the information of a map item, refreshing all item frames
* and player inventories storing the item. Map displays are also
* updated.
*
* @param oldItem that was changed
* @param newItem the old item was changed into
*/
public synchronized void updateMapItem(ItemStack oldItem, ItemStack newItem) {
boolean unchanged = isItemUnchanged(oldItem, newItem);
UUID oldMapUUID = CommonMapUUIDStore.getMapUUID(oldItem);
if (oldMapUUID != null) {
// Change in the inventories of all player owners
for (Player player : Bukkit.getOnlinePlayers()) {
PlayerInventory inv = player.getInventory();
for (int i = 0; i < inv.getSize(); i++) {
UUID mapUUID = CommonMapUUIDStore.getMapUUID(inv.getItem(i));
if (oldMapUUID.equals(mapUUID)) {
if (unchanged) {
disableMapItemChanges.set(true);
inv.setItem(i, newItem);
PlayerUtil.markItemUnchanged(player, i);
disableMapItemChanges.set(false);
} else {
inv.setItem(i, newItem);
}
}
}
}
// All item frames that show this same map
for (ItemFrameInfo itemFrameInfo : CommonPlugin.getInstance().getMapController().getItemFrames()) {
if (itemFrameInfo.lastMapUUID != null && oldMapUUID.equals(itemFrameInfo.lastMapUUID.getUUID())) {
if (unchanged) {
// When unchanged set the item in the metadata without causing a refresh
disableMapItemChanges.set(true);
DataWatcher data = EntityHandle.fromBukkit(itemFrameInfo.itemFrame).getDataWatcher();
DataWatcher.Item<ItemStack> dataItem = data.getItem(EntityItemFrameHandle.DATA_ITEM);
dataItem.setValue(newItem, dataItem.isChanged());
disableMapItemChanges.set(false);
} else {
// When changed, set it normally so the item is refreshed
CommonMapController.setItemFrameItem(itemFrameInfo.itemFrame, newItem);
}
}
}
// All map displays showing this item
MapDisplayInfo info = maps.get(oldMapUUID);
if (info != null) {
for (MapSession session : info.sessions) {
session.display.setMapItemSilently(newItem);
}
}
}
}
Aggregations