public static DataWatcher getWatcher(String text, int health) { DataWatcher watcher = new DataWatcher(null); watcher.a(0, (Byte) (byte) 0x20); // Flags, 0x20 = invisible watcher.a(6, (Float) (float) health); watcher.a(10, (String) text); // Entity name watcher.a(11, (Byte) (byte) 1); // Show name, 1 = show, 0 = don't show // watcher.a(16, (Integer) (int) health); //Wither health, 300 = full health return watcher; }
/** * Get the meta data of this service version, including IP list etc. 返回值里面的数据是动态更新的,您不需要再次调用本接口。 * * @param service 服务的唯一名称,例如org.apache.niolex.address.Test * @param version 服务的版本信息,例如100 * @return 当前的元数据;系统会监听该元数据的变化 * @throws ZKException 当发生异常时 */ public ConcurrentHashMap<String, MetaData> getMetaData(String service, int version) { if (this.root == null) { throw new IllegalStateException("Root not set."); } if (version < 1) { throw new IllegalArgumentException("Version must greater than 0."); } String path = PathUtil.makeMeta2VersionPath(root, service, version); LOG.info("Try to get meta data from: {}", path); ConcurrentHashMap<String, MetaData> map = new ConcurrentHashMap<String, MetaData>(); DataWatcher wat = new DataWatcher(path, map); byte[] before = this.watchData(path, wat); wat.setData(before); // Merge all the meta data for the first time. List<String> clients = this.getChildren(path); for (String client : clients) { wat.parseMetaData(client); } return map; }
public static PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher) { PacketPlayOutEntityMetadata metaPacket = new PacketPlayOutEntityMetadata(); Field a = ReflectionUtil.getDeclaredField(metaPacket.getClass(), "a"); a.setAccessible(true); try { a.set(metaPacket, (int) ENTITY_ID); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } try { Field b = PacketPlayOutEntityMetadata.class.getDeclaredField("b"); b.setAccessible(true); b.set(metaPacket, watcher.c()); } catch (Exception e) { e.printStackTrace(); } return metaPacket; }
@EntitySize(width = 0.6F, height = 1.9F) public class EntityMyVillager extends EntityMyPet { private static final DataWatcherObject<Boolean> ageWatcher = DataWatcher.a(EntityMyVillager.class, DataWatcherRegistry.h); private static final DataWatcherObject<Integer> professionWatcher = DataWatcher.a(EntityMyVillager.class, DataWatcherRegistry.b); public EntityMyVillager(World world, MyPet myPet) { super(world, myPet); } protected String getDeathSound() { return "entity.villager.death"; } protected String getHurtSound() { return "entity.villager.hurt"; } protected String getLivingSound() { return "entity.villager.ambient"; } public boolean handlePlayerInteraction( EntityHuman entityhuman, EnumHand enumhand, ItemStack itemStack) { if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack)) { return true; } if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) { if (Configuration.MyPet.Villager.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) { if (!entityhuman.abilities.canInstantlyBuild) { if (--itemStack.count <= 0) { entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null); } } getMyPet().setBaby(false); return true; } } return false; } protected void initDatawatcher() { super.initDatawatcher(); this.datawatcher.register(ageWatcher, false); // age this.datawatcher.register(professionWatcher, 0); // profession } @Override public void updateVisuals() { this.datawatcher.set(ageWatcher, getMyPet().isBaby()); this.datawatcher.set(professionWatcher, getMyPet().getProfession()); } public MyVillager getMyPet() { return (MyVillager) myPet; } }