Search in sources :

Example 11 with MapUUID

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

the class ItemFrameInfo method checkItemChanged.

private boolean checkItemChanged() {
    // Avoid expensive conversion and creation of CraftItemStack by detecting changes
    boolean raw_item_changed = false;
    Object raw_item = DataWatcher.Item.getRawValue(this.itemFrame_dw_item);
    // May be needed
    raw_item = CommonNMS.unwrapDWROptional(raw_item);
    if (this.lastFrameRawItem != raw_item || this.lastFrameItemUpdateNeeded) {
        this.lastFrameRawItem = raw_item;
        this.lastFrameItem = WrapperConversion.toItemStack(this.lastFrameRawItem);
        raw_item_changed = true;
    }
    // Reset flag
    this.lastFrameItemUpdateNeeded = false;
    // Always passes the first time
    if (!raw_item_changed || LogicUtil.bothNullOrEqual(this.lastFrameItemUpdate, this.lastFrameItem)) {
        return false;
    }
    // Assign & clone so that changes can be detected
    this.lastFrameItemUpdate = this.lastFrameItem;
    if (this.lastFrameItemUpdate != null) {
        this.lastFrameItemUpdate = this.lastFrameItemUpdate.clone();
    }
    // Handle changes in map item shown in item frames
    UUID mapUUID = CommonMapUUIDStore.getMapUUID(this.lastFrameItemUpdate);
    if (mapUUID == null) {
        // Map was removed
        this.sentMapInfoToPlayers = false;
        this.requiresFurtherLoading = false;
        if (lastMapUUID != null) {
            remove();
        }
    } else if (lastMapUUID == null || !lastMapUUID.getUUID().equals(mapUUID)) {
        // Map UUID was changed, or neighbours need to be re-calculated
        return true;
    }
    // No map changes
    return false;
}
Also used : MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID)

Example 12 with MapUUID

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

the class ItemFrameInfo method recalculateUUIDInCluster.

private void recalculateUUIDInCluster(ItemFrameCluster cluster) {
    this.requiresFurtherLoading = false;
    IntVector3 itemFramePosition = this.coordinates;
    UUID mapUUID = this.itemFrameHandle.getItemMapDisplayDynamicOnlyUUID();
    if (mapUUID == null) {
        return;
    }
    MapUUID newMapUUID;
    if (cluster.hasMultipleTiles()) {
        int tileX = 0;
        int tileY = 0;
        if (cluster.facing.getModY() > 0) {
            // We use rotation of the item frame to decide which side is up
            switch(cluster.rotation) {
                case 90:
                    tileX = (itemFramePosition.z - cluster.min_coord.z);
                    tileY = (cluster.max_coord.x - itemFramePosition.x);
                    break;
                case 180:
                    tileX = (cluster.max_coord.x - itemFramePosition.x);
                    tileY = (cluster.max_coord.z - itemFramePosition.z);
                    break;
                case 270:
                    tileX = (cluster.max_coord.z - itemFramePosition.z);
                    tileY = (itemFramePosition.x - cluster.min_coord.x);
                    break;
                default:
                    tileX = (itemFramePosition.x - cluster.min_coord.x);
                    tileY = (itemFramePosition.z - cluster.min_coord.z);
                    break;
            }
        } else if (cluster.facing.getModY() < 0) {
            // We use rotation of the item frame to decide which side is up
            switch(cluster.rotation) {
                case 90:
                    tileX = (cluster.max_coord.z - itemFramePosition.z);
                    tileY = (cluster.max_coord.x - itemFramePosition.x);
                    break;
                case 180:
                    tileX = (cluster.max_coord.x - itemFramePosition.x);
                    tileY = (itemFramePosition.z - cluster.min_coord.z);
                    break;
                case 270:
                    tileX = (itemFramePosition.z - cluster.min_coord.z);
                    tileY = (itemFramePosition.x - cluster.min_coord.x);
                    break;
                default:
                    tileX = (itemFramePosition.x - cluster.min_coord.x);
                    tileY = (cluster.max_coord.z - itemFramePosition.z);
                    break;
            }
        } else {
            // On the wall
            switch(cluster.facing) {
                case NORTH:
                    tileX = (cluster.max_coord.x - itemFramePosition.x);
                    break;
                case EAST:
                    tileX = (cluster.max_coord.z - itemFramePosition.z);
                    break;
                case SOUTH:
                    tileX = (itemFramePosition.x - cluster.min_coord.x);
                    break;
                case WEST:
                    tileX = (itemFramePosition.z - cluster.min_coord.z);
                    break;
                default:
                    tileX = 0;
                    break;
            }
            tileY = cluster.max_coord.y - itemFramePosition.y;
        }
        newMapUUID = new MapUUID(mapUUID, tileX, tileY);
    } else {
        newMapUUID = new MapUUID(mapUUID, 0, 0);
    }
    if (lastMapUUID == null || !lastMapUUID.getUUID().equals(mapUUID)) {
        // Map item UUID changed entirely. Remove the previous and add the new.
        remove();
        lastMapUUID = newMapUUID;
        needsItemRefresh.set(sentMapInfoToPlayers && !newMapUUID.equals(preReloadMapUUID));
        preReloadMapUUID = null;
        add();
    } else if (newMapUUID.equals(lastMapUUID)) {
    // No change occurred
    } else if (this.displayInfo != null) {
        // Tile coordinates of this map were changed
        // Ensure the new tile coordinates are added
        this.displayInfo.addTileIfMissing(newMapUUID.getTileX(), newMapUUID.getTileY());
        // Refresh state now so that removeTileIfMissing works correctly
        int oldTileX = lastMapUUID.getTileX();
        int oldTileY = lastMapUUID.getTileY();
        lastMapUUID = newMapUUID;
        needsItemRefresh.set(sentMapInfoToPlayers);
        preReloadMapUUID = null;
        // If the previous coordinates are now no longer used, remove the tile
        this.displayInfo.removeTileIfMissing(oldTileX, oldTileY);
    } else {
        // Tile coordinates changed, but we had no previous display info
        // Strange.
        lastMapUUID = newMapUUID;
        needsItemRefresh.set(sentMapInfoToPlayers);
        preReloadMapUUID = null;
    }
}
Also used : MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID)

Example 13 with MapUUID

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

the class CommonMapReloadFile method readMapUUID.

private static MapUUID readMapUUID(DataInputStream stream) throws IOException {
    long uuidMostSig = stream.readLong();
    long uuidLeastSig = stream.readLong();
    int tileX = stream.readInt();
    int tileY = stream.readInt();
    return new MapUUID(new UUID(uuidMostSig, uuidLeastSig), tileX, tileY);
}
Also used : MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID)

Example 14 with MapUUID

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

the class CommonMapController method handleItemSync.

/**
 * Adjusts the internal remapping from UUID to Map Id taking into account the new item
 * being synchronized to the player. If the item is that of a virtual map, the map Id
 * of the item is updated. NBT data that should not be synchronized is dropped.
 *
 * @param item
 * @param tileX the X-coordinate of the tile in which the item is displayed
 * @param tileY the Y-coordinate of the tile in which the item is displayed
 * @return True if the item was changed and needs to be updated in the packet
 */
public ItemStack handleItemSync(ItemStack item, int tileX, int tileY) {
    if (!CommonMapUUIDStore.isMap(item)) {
        return null;
    }
    // When a map UUID is specified, use that to dynamically allocate a map Id to use
    CommonTagCompound tag = ItemUtil.getMetaTag(item, false);
    if (tag != null) {
        UUID mapUUID = tag.getUUID("mapDisplay");
        if (mapUUID != null) {
            item = trimExtraData(item);
            int id = getMapId(new MapUUID(mapUUID, tileX, tileY));
            CommonMapUUIDStore.setItemMapId(item, id);
            return item;
        }
    }
    // Static map Id MUST be enforced
    int mapId = CommonMapUUIDStore.getItemMapId(item);
    if (mapId != -1) {
        storeStaticMapId(mapId);
    }
    return null;
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID)

Example 15 with MapUUID

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

the class CommonMapController method onPacketReceive.

@Override
public synchronized void onPacketReceive(PacketReceiveEvent event) {
    // Handle input coming from the player for the map
    if (event.getType() == PacketType.IN_STEER_VEHICLE) {
        Player p = event.getPlayer();
        MapPlayerInput input = playerInputs.get(p);
        if (input != null) {
            PacketPlayInSteerVehicleHandle packet = PacketPlayInSteerVehicleHandle.createHandle(event.getPacket().getHandle());
            int dx = (int) -Math.signum(packet.getSideways());
            int dy = (int) -Math.signum(packet.getForwards());
            int dz = 0;
            if (packet.isUnmount()) {
                dz -= 1;
            }
            if (packet.isJump()) {
                dz += 1;
            }
            // Receive input. If it will be handled, it will cancel further handling of this packet
            event.setCancelled(input.receiveInput(dx, dy, dz));
        }
    }
    // We have to prevent that in here
    if (event.getType() == PacketType.IN_SET_CREATIVE_SLOT) {
        ItemStack item = event.getPacket().read(PacketType.IN_SET_CREATIVE_SLOT.item);
        UUID mapUUID = CommonMapUUIDStore.getMapUUID(item);
        if (mapUUID != null && CommonMapUUIDStore.getStaticMapId(mapUUID) == -1) {
            // Dynamic Id map. Since we do not refresh NBT data over the network, this packet contains incorrect data
            // Find the original item the player took (by UUID). If it exists, merge its NBT data with this item.
            // For this we also have the map item cache, which is filled with data the moment a player picks up an item
            // This data is kept around for 10 minutes (unlikely a player will hold onto it for that long...)
            ItemStack originalMapItem = null;
            CreativeDraggedMapItem cachedItem = this.creativeDraggedMapItems.get(mapUUID);
            if (cachedItem != null) {
                cachedItem.life = CreativeDraggedMapItem.CACHED_ITEM_MAX_LIFE;
                originalMapItem = cachedItem.item;
            } else {
                for (ItemStack oldItem : event.getPlayer().getInventory()) {
                    if (mapUUID.equals(CommonMapUUIDStore.getMapUUID(oldItem))) {
                        originalMapItem = oldItem.clone();
                        break;
                    }
                }
            }
            if (originalMapItem != null) {
                // Original item was found. Restore all properties of that item.
                // Keep metadata the player can control, replace everything else
                ItemUtil.setMetaTag(item, ItemUtil.getMetaTag(originalMapItem));
                event.getPacket().write(PacketType.IN_SET_CREATIVE_SLOT.item, item);
            } else {
                // Dynamic Id. Force a map id value of 0 to prevent creation of new World Map instances
                item = ItemUtil.cloneItem(item);
                CommonMapUUIDStore.setItemMapId(item, 0);
                event.getPacket().write(PacketType.IN_SET_CREATIVE_SLOT.item, item);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) MapPlayerInput(com.bergerkiller.bukkit.common.map.MapPlayerInput) PacketPlayInSteerVehicleHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayInSteerVehicleHandle) ItemStack(org.bukkit.inventory.ItemStack) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID)

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