use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.
the class MapPlayerInput method updateInputInterception.
private void updateInputInterception(boolean intercept) {
if (!intercept && _fakeMountShown) {
_fakeMountShown = false;
// Despawn the mount
PacketUtil.sendPacket(player, PacketType.OUT_ENTITY_DESTROY.newInstanceSingle(this._fakeMountId));
// Resend current player position to the player
Location loc = this.player.getLocation();
PacketPlayOutPositionHandle positionPacket = PacketPlayOutPositionHandle.createAbsolute(loc);
PacketUtil.sendPacket(player, positionPacket);
return;
}
if (intercept) {
// Get expected position of the mount
Vector pos = player.getLocation().toVector();
pos.setZ(pos.getZ() + 0.1);
pos.setY(pos.getY() + 0.002);
if (!_fakeMountShown) {
_fakeMountShown = true;
// Generate unique mount Id (we can re-use it)
if (this._fakeMountId == -1) {
this._fakeMountId = EntityUtil.getUniqueEntityId();
}
// Store initial position
this._fakeMountLastPos = pos;
// Spawn the mount
{
DataWatcher data = new DataWatcher();
data.set(EntityHandle.DATA_FLAGS, (byte) (EntityHandle.DATA_FLAG_INVISIBLE));
data.set(EntityLivingHandle.DATA_HEALTH, 10.0F);
PacketPlayOutSpawnEntityLivingHandle packet = PacketPlayOutSpawnEntityLivingHandle.createNew();
packet.setEntityId(this._fakeMountId);
packet.setEntityUUID(UUID.randomUUID());
packet.setEntityType(EntityType.CHICKEN);
packet.setPosX(pos.getX());
packet.setPosY(pos.getY());
packet.setPosZ(pos.getZ());
PacketUtil.sendEntityLivingSpawnPacket(player, packet, data);
// Send attribute for max health = 0 to hide the health bar
PacketUtil.sendPacket(player, PacketPlayOutUpdateAttributesHandle.createZeroMaxHealth(this._fakeMountId));
}
sendMountPacket();
}
// When player position changes, refresh mount position with a simple teleport packet
if (this._fakeMountId != -1 && !pos.equals(this._fakeMountLastPos)) {
this._fakeMountLastPos = pos;
PacketPlayOutEntityTeleportHandle tp_packet = PacketPlayOutEntityTeleportHandle.createNew(this._fakeMountId, pos.getX(), pos.getY(), pos.getZ(), 0.0f, 0.0f, false);
PacketUtil.sendPacket(player, tp_packet);
}
}
}
use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.
the class DataWatcherTest method testItemItemStack.
@Test
public void testItemItemStack() {
Material itemType = MaterialUtil.getFirst("STONE", "LEGACY_STONE");
DataWatcher metadata = new DataWatcher();
metadata.set(EntityItemHandle.DATA_ITEM, new ItemStack(itemType, 1));
ItemStack stored = metadata.get(EntityItemHandle.DATA_ITEM);
assertNotNull(stored);
assertEquals(itemType, stored.getType());
}
use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.
the class DataWatcherTest method testChanges.
@Test
public void testChanges() {
DataWatcher dataWatcher = new DataWatcher();
assertFalse(dataWatcher.isChanged());
dataWatcher.watch(EntityHandle.DATA_AIR_TICKS, 500);
assertFalse(dataWatcher.isChanged());
dataWatcher.set(EntityHandle.DATA_AIR_TICKS, 500);
assertFalse(dataWatcher.isChanged());
dataWatcher.set(EntityHandle.DATA_AIR_TICKS, 200);
assertTrue(dataWatcher.isChanged());
dataWatcher.watch(EntityHandle.DATA_CUSTOM_NAME, ChatText.fromMessage("frank"));
// debug
// for (DataWatcher.Item<?> item : dataWatcher.getWatchedItems()) {
// System.out.println(item.toString());
// }
}
use of com.bergerkiller.bukkit.common.wrappers.DataWatcher 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);
}
}
}
}
use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.
the class EntityNetworkController method syncMetaData.
/**
* Synchronizes all Entity Meta Data including Entity Attributes and other
* specific flags. Movement and positioning information is not updated. Only
* the changes are sent, it is a relative update.
*/
public void syncMetaData() {
// Meta Data
DataWatcher meta = entity.getMetaData();
if (meta.isChanged()) {
broadcast(PacketType.OUT_ENTITY_METADATA.newInstance(entity.getEntityId(), meta, false), true);
}
// Living Entity - only data
Object entityHandle = this.entity.getHandle();
if (EntityLivingHandle.T.isAssignableFrom(entityHandle)) {
EntityLivingHandle living = EntityLivingHandle.createHandle(entityHandle);
// Entity Attributes
Collection<AttributeModifiableHandle> attributes = living.getAttributeMap().getSynchronizedAttributes();
if (!attributes.isEmpty()) {
this.broadcast(PacketType.OUT_ENTITY_UPDATE_ATTRIBUTES.newInstance(entity.getEntityId(), attributes), true);
}
attributes.clear();
}
}
Aggregations