Search in sources :

Example 6 with MapUUID

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);
            }
        }
    }
}
Also used : LongHashSet(com.bergerkiller.bukkit.common.wrappers.LongHashSet) MapSession(com.bergerkiller.bukkit.common.map.MapSession) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) MapDisplayTile(com.bergerkiller.bukkit.common.map.MapDisplayTile)

Example 7 with MapUUID

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);
}
Also used : ItemFrameInfo(com.bergerkiller.bukkit.common.internal.CommonMapController.ItemFrameInfo) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID)

Example 8 with MapUUID

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);
    }
}
Also used : MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID)

Example 9 with MapUUID

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);
    }
}
Also used : MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with MapUUID

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);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) PlayerInventory(org.bukkit.inventory.PlayerInventory) MapSession(com.bergerkiller.bukkit.common.map.MapSession) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID) ItemStack(org.bukkit.inventory.ItemStack) DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher)

Aggregations

MapUUID (com.bergerkiller.bukkit.common.map.util.MapUUID)18 UUID (java.util.UUID)10 ItemStack (org.bukkit.inventory.ItemStack)6 MapSession (com.bergerkiller.bukkit.common.map.MapSession)5 Player (org.bukkit.entity.Player)5 ItemFrameInfo (com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo)3 MapDisplayInfo (com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo)3 DataWatcher (com.bergerkiller.bukkit.common.wrappers.DataWatcher)3 HashSet (java.util.HashSet)3 PlayerInventory (org.bukkit.inventory.PlayerInventory)3 MapDisplayTile (com.bergerkiller.bukkit.common.map.MapDisplayTile)2 MapPlayerInput (com.bergerkiller.bukkit.common.map.MapPlayerInput)2 CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)2 IntVector3 (com.bergerkiller.bukkit.common.bases.IntVector3)1 ItemFrameInfo (com.bergerkiller.bukkit.common.internal.CommonMapController.ItemFrameInfo)1 CommonPacket (com.bergerkiller.bukkit.common.protocol.CommonPacket)1 IntHashMap (com.bergerkiller.bukkit.common.wrappers.IntHashMap)1 LongHashSet (com.bergerkiller.bukkit.common.wrappers.LongHashSet)1 PacketPlayInSteerVehicleHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayInSteerVehicleHandle)1 EntityItemFrameHandle (com.bergerkiller.generated.net.minecraft.world.entity.decoration.EntityItemFrameHandle)1