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