Search in sources :

Example 6 with MapSession

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

the class MapDisplayInfo method refreshResolution.

/**
 * Refreshes the desired width and height of the map displays based on the
 * item frames that are currently loaded.
 */
public void refreshResolution() {
    int min_x = 0;
    int min_y = 0;
    int max_x = 0;
    int max_y = 0;
    boolean first = true;
    for (ItemFrameInfo itemFrame : itemFrames) {
        if (itemFrame.lastMapUUID != null) {
            int tx = itemFrame.lastMapUUID.getTileX() << 7;
            int ty = itemFrame.lastMapUUID.getTileY() << 7;
            if (first) {
                first = false;
                min_x = max_x = tx;
                min_y = max_y = ty;
                continue;
            }
            if (tx < min_x)
                min_x = tx;
            if (tx > max_x)
                max_x = tx;
            if (ty < min_y)
                min_y = ty;
            if (ty > max_y)
                max_y = ty;
        }
    }
    int new_width = max_x - min_x + 128;
    int new_height = max_y - min_y + 128;
    if (new_width != this.desiredWidth || new_height != this.desiredHeight) {
        this.desiredWidth = new_width;
        this.desiredHeight = new_height;
        this.hasFrameResolutionChanges.set(true);
        for (MapSession session : this.sessions) {
            if (session.refreshResolutionRequested) {
                continue;
            }
            if (session.display.getWidth() != this.desiredWidth || session.display.getHeight() != this.desiredHeight) {
                session.refreshResolutionRequested = true;
            }
        }
    }
}
Also used : MapSession(com.bergerkiller.bukkit.common.map.MapSession)

Example 7 with MapSession

use of com.bergerkiller.bukkit.common.map.MapSession 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)

Example 8 with MapSession

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

the class MapDisplayInfo method addTileIfMissing.

/**
 * Adds a new display tile to already running displays.
 * Does nothing if the display is going to be reset, or no display sessions exist.
 * Since tile 0,0 is always added by the display, that tile is ignored.
 *
 * @param tileX
 * @param tileY
 */
public void addTileIfMissing(int tileX, int tileY) {
    if (this.sessions.isEmpty() || (tileX == 0 && tileY == 0)) {
        return;
    }
    if ((tileX << 7) >= this.desiredWidth) {
        return;
    }
    if ((tileY << 7) >= this.desiredHeight) {
        return;
    }
    for (MapSession session : this.sessions) {
        if (session.refreshResolutionRequested) {
            continue;
        }
        if (session.display.containsTile(tileX, tileY)) {
            continue;
        }
        MapDisplayTile newTile = new MapDisplayTile(this.uuid, tileX, tileY);
        session.tiles.add(newTile);
        for (MapSession.Owner owner : session.onlineOwners) {
            owner.sendDirtyTile(newTile);
        }
    }
}
Also used : MapSession(com.bergerkiller.bukkit.common.map.MapSession) MapDisplayTile(com.bergerkiller.bukkit.common.map.MapDisplayTile)

Example 9 with MapSession

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

the class MapDisplayMarkers method synchronize.

public void synchronize(MapSession session) {
    if (markersByTile.isEmpty()) {
        // skip
        return;
    }
    for (MapDisplayTile displayedTile : session.tiles) {
        MapDisplayMarkerTile tile = markersByTile.get(displayedTile.tile);
        if (tile == null || !tile.isChanged()) {
            continue;
        }
        // Has changes, verify whether all owners have received them
        MapDisplayTile.Update mapUpdate = null;
        for (MapSession.Owner owner : session.onlineOwners) {
            // Already synchronized during map content update
            if (tile.isSynchronized(owner.player)) {
                continue;
            }
            // Requires update, ask the tile to do this
            if (mapUpdate == null) {
                mapUpdate = new MapDisplayTile.Update(displayedTile.tile, displayedTile.getMapId());
                APPLIER.apply(mapUpdate.packet.getRaw(), tile);
            } else {
                mapUpdate = mapUpdate.clone();
            }
            // Send to this player
            PacketUtil.sendPacket(owner.player, mapUpdate.packet, false);
        }
    }
    // Go by all tiles and clean them up
    Iterator<MapDisplayMarkerTile> iter = markersByTile.values().iterator();
    while (iter.hasNext()) {
        MapDisplayMarkerTile tile = iter.next();
        if (tile.isEmpty()) {
            iter.remove();
        } else {
            tile.setChanged(false);
        }
    }
}
Also used : MapSession(com.bergerkiller.bukkit.common.map.MapSession) MapDisplayTile(com.bergerkiller.bukkit.common.map.MapDisplayTile)

Example 10 with MapSession

use of com.bergerkiller.bukkit.common.map.MapSession 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)

Aggregations

MapSession (com.bergerkiller.bukkit.common.map.MapSession)11 MapUUID (com.bergerkiller.bukkit.common.map.util.MapUUID)5 MapDisplayTile (com.bergerkiller.bukkit.common.map.MapDisplayTile)4 ItemFrameInfo (com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo)3 MapDisplayInfo (com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo)3 UUID (java.util.UUID)3 Player (org.bukkit.entity.Player)3 ItemStack (org.bukkit.inventory.ItemStack)3 PlayerInventory (org.bukkit.inventory.PlayerInventory)3 MapDisplay (com.bergerkiller.bukkit.common.map.MapDisplay)2 DataWatcher (com.bergerkiller.bukkit.common.wrappers.DataWatcher)2 Plugin (org.bukkit.plugin.Plugin)2 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)2 CommonPlugin (com.bergerkiller.bukkit.common.internal.CommonPlugin)1 MapDisplayProperties (com.bergerkiller.bukkit.common.map.MapDisplayProperties)1 CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)1 IntHashMap (com.bergerkiller.bukkit.common.wrappers.IntHashMap)1 LongHashSet (com.bergerkiller.bukkit.common.wrappers.LongHashSet)1 EntityItemFrameHandle (com.bergerkiller.generated.net.minecraft.world.entity.decoration.EntityItemFrameHandle)1 OutputTypeMap (com.bergerkiller.mountiplex.reflection.util.OutputTypeMap)1