Search in sources :

Example 16 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(int 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 17 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) {
    if (oldItem == null) {
        throw new IllegalArgumentException("oldItem is null");
    } else if (!CraftItemStackHandle.T.isAssignableFrom(oldItem)) {
        // Ensure CraftItemStack
        oldItem = ItemUtil.createItem(oldItem);
    }
    if (newItem != null && !CraftItemStackHandle.T.isAssignableFrom(newItem)) {
        // Ensure CraftItemStack
        newItem = ItemUtil.createItem(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) {
                        PlayerUtil.setItemSilently(player, i, newItem);
                    } 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
                    DataWatcher data = EntityHandle.fromBukkit(itemFrameInfo.itemFrame).getDataWatcher();
                    DataWatcher.Item<ItemStack> dataItem = data.getItem(EntityItemFrameHandle.DATA_ITEM);
                    dataItem.setValue(newItem, dataItem.isChanged());
                } else {
                    // When changed, set it normally so the item is refreshed
                    itemFrameInfo.itemFrameHandle.setItem(newItem);
                    this.itemFrameUpdateList.prioritize(itemFrameInfo.updateEntry);
                }
            }
        }
        // All map displays showing this item
        MapDisplayInfo info = maps.get(oldMapUUID);
        if (info != null) {
            for (MapSession session : info.getSessions()) {
                session.display.setMapItemSilently(newItem);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) MapDisplayInfo(com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo) 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) ItemFrameInfo(com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo) DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher)

Example 18 with MapUUID

use of com.bergerkiller.bukkit.common.map.util.MapUUID in project BKCommonLib by bergerhealer.

the class CommonMapController method cleanupUnusedUUIDs.

protected 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.getSessions().isEmpty()) {
                MapDisplayInfo removed = maps.remove(toRemove.getUUID());
                if (removed != null) {
                    mapsValues.remove(removed);
                    removed.onRemoved();
                }
            } else {
                // still has an active session; cannot remove
                continue;
            }
        }
        // Clean up from bi-directional mapping
        Integer mapId = mapIdByUUID.remove(toRemove);
        if (mapId != null) {
            mapUUIDById.remove(mapId.intValue());
        }
        // Clean up from 'dirty' set (probably never needed)
        dirtyMapUUIDSet.removeAll(toRemove.getUUID());
    }
}
Also used : MapDisplayInfo(com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) HashSet(java.util.HashSet)

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