Search in sources :

Example 6 with DataWatcher

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);
        }
    }
}
Also used : Vector(org.bukkit.util.Vector) PacketPlayOutEntityTeleportHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityTeleportHandle) PacketPlayOutPositionHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutPositionHandle) Location(org.bukkit.Location) DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) PacketPlayOutSpawnEntityLivingHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityLivingHandle)

Example 7 with DataWatcher

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());
}
Also used : Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) Test(org.junit.Test)

Example 8 with DataWatcher

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());
// }
}
Also used : DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) Test(org.junit.Test)

Example 9 with DataWatcher

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

Example 10 with DataWatcher

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();
    }
}
Also used : EntityLivingHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityLivingHandle) DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) AttributeModifiableHandle(com.bergerkiller.generated.net.minecraft.world.entity.ai.attributes.AttributeModifiableHandle)

Aggregations

DataWatcher (com.bergerkiller.bukkit.common.wrappers.DataWatcher)11 Test (org.junit.Test)6 ItemStack (org.bukkit.inventory.ItemStack)3 MapSession (com.bergerkiller.bukkit.common.map.MapSession)2 MapUUID (com.bergerkiller.bukkit.common.map.util.MapUUID)2 EntityLivingHandle (com.bergerkiller.generated.net.minecraft.world.entity.EntityLivingHandle)2 AttributeModifiableHandle (com.bergerkiller.generated.net.minecraft.world.entity.ai.attributes.AttributeModifiableHandle)2 UUID (java.util.UUID)2 Player (org.bukkit.entity.Player)2 PlayerInventory (org.bukkit.inventory.PlayerInventory)2 IntVector3 (com.bergerkiller.bukkit.common.bases.IntVector3)1 ItemFrameInfo (com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo)1 MapDisplayInfo (com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo)1 PacketPlayOutEntityTeleportHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityTeleportHandle)1 PacketPlayOutPositionHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutPositionHandle)1 PacketPlayOutSpawnEntityLivingHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityLivingHandle)1 MobEffectHandle (com.bergerkiller.generated.net.minecraft.world.effect.MobEffectHandle)1 Location (org.bukkit.Location)1 Material (org.bukkit.Material)1 EquipmentSlot (org.bukkit.inventory.EquipmentSlot)1