use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class FireworkRocketData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(FireworkRocketEntity.class).create(Keys.FIREWORK_EFFECTS).get(h -> FireworkUtil.getFireworkEffects(h).orElse(null)).set(FireworkUtil::setFireworkEffects).resetOnDelete(ImmutableList.of()).create(Keys.FIREWORK_FLIGHT_MODIFIER).get(h -> {
final ItemStack item = FireworkUtil.getItem(h);
final CompoundTag fireworks = item.getOrCreateTagElement(Constants.Item.Fireworks.FIREWORKS);
if (fireworks.contains(Constants.Item.Fireworks.FLIGHT)) {
return new SpongeTicks(fireworks.getByte(Constants.Item.Fireworks.FLIGHT));
}
return null;
}).setAnd((h, v) -> {
final int ticks = (int) v.ticks();
if (ticks < 0 || ticks > Byte.MAX_VALUE) {
return false;
}
final ItemStack item = FireworkUtil.getItem(h);
final CompoundTag fireworks = item.getOrCreateTagElement(Constants.Item.Fireworks.FIREWORKS);
fireworks.putByte(Constants.Item.Fireworks.FLIGHT, (byte) ticks);
((FireworkRocketEntityAccessor) h).accessor$lifetime(10 * ticks + ((EntityAccessor) h).accessor$random().nextInt(6) + ((EntityAccessor) h).accessor$random().nextInt(7));
return true;
});
}
use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class ItemData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemEntity.class).create(Keys.ITEM_STACK_SNAPSHOT).get(h -> ItemStackUtil.snapshotOf(h.getItem())).set((h, v) -> h.setItem(ItemStackUtil.fromSnapshotToNative(v))).asMutable(ItemEntityBridge.class).create(Keys.DESPAWN_DELAY).get(h -> new SpongeTicks(h.bridge$getDespawnDelay())).setAnd((h, v) -> {
final int ticks = (int) v.ticks();
if (ticks < 0) {
return false;
}
h.bridge$setDespawnDelay(ticks, false);
return true;
}).create(Keys.INFINITE_DESPAWN_DELAY).get(ItemEntityBridge::bridge$infiniteDespawnDelay).set((h, v) -> h.bridge$setDespawnDelay(h.bridge$getDespawnDelay(), v)).create(Keys.INFINITE_PICKUP_DELAY).get(ItemEntityBridge::bridge$infinitePickupDelay).set((h, v) -> h.bridge$setPickupDelay(h.bridge$getPickupDelay(), v)).create(Keys.PICKUP_DELAY).get(h -> new SpongeTicks(h.bridge$getPickupDelay())).set((h, v) -> h.bridge$setPickupDelay((int) v.ticks(), false)).create(ItemData.PREVIOUS_PICKUP_DELAY).get(v -> -1).set(ItemEntityBridge::bridge$setPrevPickupDelay).create(ItemData.PREVIOUS_DESPAWN_DELAY).get(v -> -1).set(ItemEntityBridge::bridge$setPrevDespawnDelay);
final ResourceKey item = ResourceKey.sponge("item");
registrator.spongeDataStore(item, 2, new DataContentUpdater[] { ItemData.INFINITE_DELAYS_UPDATER_BYTE_TO_BOOL_FIX }, ItemEntityBridge.class, Keys.INFINITE_PICKUP_DELAY, ItemData.PREVIOUS_PICKUP_DELAY, Keys.INFINITE_DESPAWN_DELAY, ItemData.PREVIOUS_DESPAWN_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.INFINITE_PICKUP_DELAY, item, Keys.INFINITE_PICKUP_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.PREVIOUS_PICKUP_DELAY, item, ItemData.PREVIOUS_PICKUP_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.INFINITE_DESPAWN_DELAY, item, Keys.INFINITE_DESPAWN_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.PREVIOUS_DESPAWN_DELAY, item, ItemData.PREVIOUS_DESPAWN_DELAY);
}
use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class LivingData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(LivingEntity.class).create(Keys.ABSORPTION).get(h -> (double) h.getAbsorptionAmount()).setAnd((h, v) -> {
if (v < 0) {
return false;
}
h.setAbsorptionAmount(v.floatValue());
return true;
}).create(Keys.ACTIVE_ITEM).get(h -> ItemStackUtil.snapshotOf(h.getUseItem())).setAnd((h, v) -> {
if (v.isEmpty()) {
h.releaseUsingItem();
return true;
}
return false;
}).delete(LivingEntity::releaseUsingItem).create(Keys.AUTO_SPIN_ATTACK_TICKS).get(h -> Ticks.of(((LivingEntityAccessor) h).accessor$autoSpinAttackTicks())).set((h, v) -> h.startAutoSpinAttack((int) v.ticks())).create(Keys.BODY_ROTATIONS).get(h -> {
final double headYaw = h.getYHeadRot();
final double pitch = h.xRot;
final double yaw = h.yRot;
return ImmutableMap.of(BodyParts.HEAD.get(), new Vector3d(pitch, headYaw, 0), BodyParts.CHEST.get(), new Vector3d(pitch, yaw, 0));
}).set((h, v) -> {
final Vector3d headRotation = v.get(BodyParts.HEAD.get());
final Vector3d bodyRotation = v.get(BodyParts.CHEST.get());
if (bodyRotation != null) {
h.yRot = (float) bodyRotation.y();
h.xRot = (float) bodyRotation.x();
}
if (headRotation != null) {
h.yHeadRot = (float) headRotation.y();
h.xRot = (float) headRotation.x();
}
}).create(Keys.CHEST_ROTATION).get(h -> new Vector3d(h.xRot, h.yRot, 0)).set((h, v) -> {
final float yaw = (float) v.y();
final float pitch = (float) v.x();
h.yRot = yaw;
h.xRot = pitch;
}).create(Keys.HEAD_ROTATION).get(h -> new Vector3d(h.xRot, h.getYHeadRot(), 0)).set((h, v) -> {
final float headYaw = (float) v.y();
final float pitch = (float) v.x();
h.yHeadRot = headYaw;
h.xRot = pitch;
}).create(Keys.HEALTH).get(h -> (double) h.getHealth()).setAnd((h, v) -> {
final double maxHealth = h.getMaxHealth();
// Check bounds
if (v < 0 || v > maxHealth) {
return false;
}
if (v == 0) {
// Cause DestructEntityEvent to fire first
h.hurt((DamageSource) SpongeDamageSources.IGNORED, Float.MAX_VALUE);
}
h.setHealth(v.floatValue());
return true;
}).create(Keys.IS_AUTO_SPIN_ATTACK).get(LivingEntity::isAutoSpinAttack).create(Keys.IS_ELYTRA_FLYING).get(LivingEntity::isFallFlying).set((h, v) -> ((EntityAccessor) h).invoker$setSharedFlag(Constants.Entity.ELYTRA_FLYING_FLAG, v)).create(Keys.LAST_ATTACKER).get(h -> (Entity) h.getLastHurtByMob()).setAnd((h, v) -> {
if (v instanceof LivingEntity) {
h.setLastHurtByMob((LivingEntity) v);
return true;
}
return false;
}).delete(h -> h.setLastHurtByMob(null)).create(Keys.MAX_HEALTH).get(h -> (double) h.getMaxHealth()).set((h, v) -> h.getAttribute(Attributes.MAX_HEALTH).setBaseValue(v)).create(Keys.POTION_EFFECTS).get(h -> {
final Collection<MobEffectInstance> effects = h.getActiveEffects();
return PotionEffectUtil.copyAsPotionEffects(effects);
}).set((h, v) -> {
h.removeAllEffects();
for (final PotionEffect effect : v) {
h.addEffect(PotionEffectUtil.copyAsEffectInstance(effect));
}
}).create(Keys.SCALE).get(h -> (double) h.getScale()).create(Keys.STUCK_ARROWS).get(LivingEntity::getArrowCount).setAnd((h, v) -> {
if (v < 0 || v > Integer.MAX_VALUE) {
return false;
}
h.setArrowCount(v);
return true;
}).create(Keys.WALKING_SPEED).get(h -> h.getAttribute(Attributes.MOVEMENT_SPEED).getValue()).setAnd((h, v) -> {
if (v < 0) {
return false;
}
h.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(v);
return true;
}).asMutable(LivingEntityAccessor.class).create(Keys.LAST_DAMAGE_RECEIVED).get(h -> (double) h.accessor$lastHurt()).set((h, v) -> h.accessor$lastHurt(v.floatValue()));
}
use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class AgeableData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(AgableMob.class).create(Keys.BABY_TICKS).get(h -> h.getAge() < 0 ? new SpongeTicks(-h.getAge()) : null).setAnd((h, v) -> {
final int ticks = (int) v.ticks();
if (ticks < 0) {
return false;
}
h.setAge(-ticks);
return true;
}).create(Keys.BREEDING_COOLDOWN).get(h -> h.getAge() >= 0 ? new SpongeTicks(h.getAge()) : null).setAnd((h, v) -> {
final int ticks = (int) v.ticks();
if (ticks < 0) {
return false;
}
h.setAge(ticks);
return true;
}).create(Keys.CAN_BREED).get(h -> h.getAge() == 0).setAnd((h, v) -> {
if (h.getAge() < 0) {
return false;
}
h.setAge(v ? 0 : 6000);
return true;
}).create(Keys.IS_ADULT).get(h -> !h.isBaby()).set((h, v) -> h.setAge(v ? Constants.Entity.Ageable.ADULT : Constants.Entity.Ageable.CHILD));
}
use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class ServerLocationData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ServerLocation.class).create(Keys.BIOME_TEMPERATURE).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
final Biome biome = world.getBiome(pos);
return (double) biome.getBaseTemperature();
}).create(Keys.BLOCK_LIGHT).get(h -> {
final Level world = (Level) h.world();
return world.getBrightness(LightLayer.BLOCK, VecHelper.toBlockPos(h));
}).create(Keys.BLOCK_TEMPERATURE).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
final Biome biome = world.getBiome(pos);
return (double) biome.getTemperature(pos);
}).create(Keys.SKY_LIGHT).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
return world.getBrightness(LightLayer.SKY, pos);
}).create(Keys.IS_FULL_BLOCK).get(h -> {
final BlockState block = (BlockState) h.block();
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h.position());
return block.isSolidRender(world, pos);
}).create(Keys.IS_INDIRECTLY_POWERED).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
return world.getBestNeighborSignal(pos) > 0;
}).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(((Nameable) h.blockEntity().get()).getDisplayName())).supports(h -> h.blockEntity().isPresent() && h.blockEntity().get() instanceof NameableBlockEntity).create(Keys.CUSTOM_NAME).get(h -> {
final BlockEntity blockEntity = h.blockEntity().get();
return ((Nameable) blockEntity).hasCustomName() ? SpongeAdventure.asAdventure(((Nameable) blockEntity).getCustomName()) : null;
}).set((h, v) -> (((CustomNameableBridge) h.blockEntity().get())).bridge$setCustomDisplayName(SpongeAdventure.asVanilla(v))).delete(h -> (((CustomNameableBridge) h.blockEntity().get())).bridge$setCustomDisplayName(null)).supports(h -> h.blockEntity().isPresent() && h.blockEntity().get() instanceof NameableBlockEntity).create(Keys.CREATOR).get(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$getBlockCreatorUUID(VecHelper.toBlockPos(h.blockPosition())).orElse(null)).set((h, v) -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockCreator(VecHelper.toBlockPos(h.blockPosition()), v)).delete(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockCreator(VecHelper.toBlockPos(h.blockPosition()), null)).create(Keys.NOTIFIER).get(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$getBlockNotifierUUID(VecHelper.toBlockPos(h.blockPosition())).orElse(null)).set((h, v) -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockNotifier(VecHelper.toBlockPos(h.blockPosition()), v)).delete(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockNotifier(VecHelper.toBlockPos(h.blockPosition()), null));
}
Aggregations