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