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;
}
}
}
}
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);
}
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
}
Aggregations