Search in sources :

Example 11 with ItemFrameInfo

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

the class CommonMapController method findLookingAt.

private LookAtSearchResult findLookingAt(Player player, ItemFrame itemFrame, Vector startPosition, Vector lookDirection) {
    MapDisplayInfo info = getInfo(itemFrame);
    if (info == null) {
        // no map here
        return null;
    }
    // Find the Display this player is sees on this map
    MapDisplayInfo.ViewStack stack = info.getViewStackByPlayerUUID(player.getUniqueId());
    if (stack == null || stack.stack.isEmpty()) {
        // no visible display for this player
        return null;
    }
    // Find the item frame metadata information
    ItemFrameInfo frameInfo = this.itemFrames.get(itemFrame.getEntityId());
    if (frameInfo == null) {
        // not tracked
        return null;
    }
    // Ask item frame to compute look-at information
    // If looking further than 16 map pixels away from the edge, fail
    MapLookPosition position = frameInfo.findLookPosition(startPosition, lookDirection);
    final double limit = 16.0;
    if (position == null || position.getEdgeDistance() > (limit / 128.0)) {
        return null;
    }
    // Keep position within bounds of the display
    // If very much out of bounds (>16 pixels) fail the looking-at check
    // This loose-ness allows for smooth clicking between frames without failures
    MapDisplay display = stack.stack.getLast();
    double new_x = position.getDoubleX();
    double new_y = position.getDoubleY();
    if (new_x < -limit || new_y < -limit || new_x > (display.getWidth() + limit) || new_y >= (display.getHeight() + limit)) {
        return null;
    } else if (new_x < 0.0 || new_y < 0.0 || new_x >= display.getWidth() || new_y >= display.getHeight()) {
        new_x = MathUtil.clamp(new_x, 0.0, (double) display.getWidth() - 1e-10);
        new_y = MathUtil.clamp(new_y, 0.0, (double) display.getHeight() - 1e-10);
        position = new MapLookPosition(position.getItemFrameInfo(), new_x, new_y, position.getDistance(), position.getEdgeDistance());
    }
    return new LookAtSearchResult(display, position);
}
Also used : MapDisplay(com.bergerkiller.bukkit.common.map.MapDisplay) MapDisplayInfo(com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo) MapLookPosition(com.bergerkiller.bukkit.common.map.util.MapLookPosition) ItemFrameInfo(com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo)

Example 12 with ItemFrameInfo

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

ItemFrameInfo (com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo)12 MapDisplayInfo (com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo)4 MapSession (com.bergerkiller.bukkit.common.map.MapSession)3 MapUUID (com.bergerkiller.bukkit.common.map.util.MapUUID)3 EntityItemFrameHandle (com.bergerkiller.generated.net.minecraft.world.entity.decoration.EntityItemFrameHandle)3 ItemFrame (org.bukkit.entity.ItemFrame)3 ItemStack (org.bukkit.inventory.ItemStack)3 OfflineWorld (com.bergerkiller.bukkit.common.offline.OfflineWorld)2 DataWatcher (com.bergerkiller.bukkit.common.wrappers.DataWatcher)2 IntHashMap (com.bergerkiller.bukkit.common.wrappers.IntHashMap)2 OutputTypeMap (com.bergerkiller.mountiplex.reflection.util.OutputTypeMap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 UUID (java.util.UUID)2 World (org.bukkit.World)2 Entity (org.bukkit.entity.Entity)2 Player (org.bukkit.entity.Player)2 PlayerInventory (org.bukkit.inventory.PlayerInventory)2