Search in sources :

Example 1 with DataWatcher

use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.

the class DataWatcherTest method testShulkerPeek.

@Test
public void testShulkerPeek() {
    if (!EntityShulkerHandle.T.isAvailable()) {
        return;
    }
    DataWatcher dataWatcher = new DataWatcher();
    // DATA_PEEK
    dataWatcher.set(EntityShulkerHandle.DATA_PEEK, (byte) 5);
    assertEquals((byte) 5, dataWatcher.get(EntityShulkerHandle.DATA_PEEK).byteValue());
// DATA_AP (attached point)
// dataWatcher.set(EntityShulkerHandle.DATA_AP, new IntVector3(5, 6, 7));
// assertEquals(new IntVector3(5, 6, 7), dataWatcher.get(EntityShulkerHandle.DATA_AP));
}
Also used : DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) Test(org.junit.Test)

Example 2 with DataWatcher

use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.

the class DataWatcherTest method testBasicOperation.

@Test
public void testBasicOperation() {
    DataWatcher dataWatcher = new DataWatcher();
    // Do a single test run with a String type, which is quite safe
    assertFalse(dataWatcher.isWatched(EntityHandle.DATA_CUSTOM_NAME));
    dataWatcher.watch(EntityHandle.DATA_CUSTOM_NAME, ChatText.fromMessage("original"));
    assertTrue(dataWatcher.isWatched(EntityHandle.DATA_CUSTOM_NAME));
    assertEquals("original", dataWatcher.get(EntityHandle.DATA_CUSTOM_NAME).getMessage());
    dataWatcher.set(EntityHandle.DATA_CUSTOM_NAME, ChatText.fromMessage("new"));
    assertEquals("new", dataWatcher.get(EntityHandle.DATA_CUSTOM_NAME).getMessage());
    // Verify that when setting to null, it sets to Optional.empty correctly (MC 1.13)
    if (Common.evaluateMCVersion(">=", "1.13")) {
        // Using converter
        Object raw = EntityHandle.DATA_CUSTOM_NAME.getType().getConverter().convertReverse(null);
        checkCustomNameOptional(raw);
        // Set and get
        dataWatcher.set(EntityHandle.DATA_CUSTOM_NAME, null);
        raw = DataWatcher.Item.getRawValue(dataWatcher.getItem(EntityHandle.DATA_CUSTOM_NAME));
        checkCustomNameOptional(raw);
        // Reset DW and watch using entry from old DW
        DataWatcher dataWatcher_copy = new DataWatcher();
        for (DataWatcher.Item<?> old_item : dataWatcher.getWatchedItems()) {
            // System.out.println("TYPE: " + old_item.getKey().getType());
            // System.out.println("VALUE: " + old_item.getValue());
            // System.out.println("VALUE_FIX: " + old_item.getKey().getType().getConverter().convertReverse(old_item.getValue()));
            dataWatcher_copy.watch(old_item);
        }
        raw = DataWatcher.Item.getRawValue(dataWatcher_copy.getItem(EntityHandle.DATA_CUSTOM_NAME));
        checkCustomNameOptional(raw);
    }
    // Now do a run with an Integer type
    dataWatcher.watch(EntityHandle.DATA_AIR_TICKS, 300);
    assertEquals(300, dataWatcher.get(EntityHandle.DATA_AIR_TICKS).intValue());
    dataWatcher.set(EntityHandle.DATA_AIR_TICKS, 200);
    assertEquals(200, dataWatcher.get(EntityHandle.DATA_AIR_TICKS).intValue());
    // This might fail on 1.8.8! Test Boolean type.
    dataWatcher.watch(EntityHandle.DATA_CUSTOM_NAME_VISIBLE, false);
    assertEquals(false, dataWatcher.get(EntityHandle.DATA_CUSTOM_NAME_VISIBLE).booleanValue());
    dataWatcher.set(EntityHandle.DATA_CUSTOM_NAME_VISIBLE, true);
    assertEquals(true, dataWatcher.get(EntityHandle.DATA_CUSTOM_NAME_VISIBLE).booleanValue());
    dataWatcher.set(EntityHandle.DATA_CUSTOM_NAME_VISIBLE, false);
    assertEquals(false, dataWatcher.get(EntityHandle.DATA_CUSTOM_NAME_VISIBLE).booleanValue());
}
Also used : DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) Test(org.junit.Test)

Example 3 with DataWatcher

use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.

the class DataWatcherTest method testConstruction.

@Test
public void testConstruction() {
    DataWatcher dataWatcher = new DataWatcher();
    assertTrue(dataWatcher.isEmpty());
}
Also used : DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) Test(org.junit.Test)

Example 4 with DataWatcher

use of com.bergerkiller.bukkit.common.wrappers.DataWatcher in project BKCommonLib by bergerhealer.

the class DataWatcherTest method testEnderCrystalBeamTarget.

@Test
public void testEnderCrystalBeamTarget() {
    if (!EntityEnderCrystalHandle.T.isAvailable()) {
        return;
    }
    DataWatcher dataWatcher = new DataWatcher();
    // DATA_BEAM_TARGET
    dataWatcher.set(EntityEnderCrystalHandle.DATA_BEAM_TARGET, new IntVector3(5, 6, 7));
    assertEquals(new IntVector3(5, 6, 7), dataWatcher.get(EntityEnderCrystalHandle.DATA_BEAM_TARGET));
}
Also used : IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher) Test(org.junit.Test)

Example 5 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) {
    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);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) 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) DataWatcher(com.bergerkiller.bukkit.common.wrappers.DataWatcher)

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