Search in sources :

Example 1 with SpongeTicks

use of org.spongepowered.common.util.SpongeTicks 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;
    });
}
Also used : FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Keys(org.spongepowered.api.data.Keys) FireworkRocketEntityAccessor(org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor) FireworkUtil(org.spongepowered.common.util.FireworkUtil) CompoundTag(net.minecraft.nbt.CompoundTag) ImmutableList(com.google.common.collect.ImmutableList) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) SpongeTicks(org.spongepowered.common.util.SpongeTicks) ItemStack(net.minecraft.world.item.ItemStack) FireworkRocketEntityAccessor(org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) FireworkUtil(org.spongepowered.common.util.FireworkUtil) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) ItemStack(net.minecraft.world.item.ItemStack) SpongeTicks(org.spongepowered.common.util.SpongeTicks) FireworkRocketEntityAccessor(org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with SpongeTicks

use of org.spongepowered.common.util.SpongeTicks 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));
}
Also used : Keys(org.spongepowered.api.data.Keys) Constants(org.spongepowered.common.util.Constants) AgableMob(net.minecraft.world.entity.AgableMob) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) SpongeTicks(org.spongepowered.common.util.SpongeTicks) SpongeTicks(org.spongepowered.common.util.SpongeTicks)

Example 3 with SpongeTicks

use of org.spongepowered.common.util.SpongeTicks 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);
}
Also used : DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater) Constants(org.spongepowered.common.util.Constants) ItemEntityBridge(org.spongepowered.common.bridge.world.entity.item.ItemEntityBridge) Key(org.spongepowered.api.data.Key) Value(org.spongepowered.api.data.value.Value) SpongeDataManager(org.spongepowered.common.data.SpongeDataManager) Keys(org.spongepowered.api.data.Keys) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) ItemEntity(net.minecraft.world.entity.item.ItemEntity) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) ResourceKey(org.spongepowered.api.ResourceKey) ByteToBooleanContentUpdater(org.spongepowered.common.data.ByteToBooleanContentUpdater) SpongeTicks(org.spongepowered.common.util.SpongeTicks) ItemEntityBridge(org.spongepowered.common.bridge.world.entity.item.ItemEntityBridge) SpongeTicks(org.spongepowered.common.util.SpongeTicks) ResourceKey(org.spongepowered.api.ResourceKey)

Example 4 with SpongeTicks

use of org.spongepowered.common.util.SpongeTicks in project SpongeCommon by SpongePowered.

the class MobSpawnerData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(SpawnerBlockEntityAccessor.class).create(Keys.MAX_NEARBY_ENTITIES).get(h -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$maxNearbyEntities()).set((h, v) -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$maxNearbyEntities(v)).create(Keys.MAX_SPAWN_DELAY).get(h -> new SpongeTicks(((BaseSpawnerAccessor) h.accessor$spawner()).accessor$maxSpawnDelay())).set((h, v) -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$maxSpawnDelay((int) v.ticks())).create(Keys.MIN_SPAWN_DELAY).get(h -> new SpongeTicks(((BaseSpawnerAccessor) h.accessor$spawner()).accessor$minSpawnDelay())).set((h, v) -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$minSpawnDelay((int) v.ticks())).create(Keys.NEXT_ENTITY_TO_SPAWN).get(h -> MobSpawnerData.getNextEntity((BaseSpawnerAccessor) h.accessor$spawner())).set((h, v) -> MobSpawnerData.setNextEntity(h.accessor$spawner(), v)).create(Keys.REMAINING_SPAWN_DELAY).get(h -> new SpongeTicks(((BaseSpawnerAccessor) h.accessor$spawner()).accessor$spawnDelay())).set((h, v) -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$spawnDelay((int) v.ticks())).create(Keys.REQUIRED_PLAYER_RANGE).get(h -> (double) ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$requiredPlayerRange()).set((h, v) -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$requiredPlayerRange(v.intValue())).create(Keys.SPAWN_COUNT).get(h -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$spawnCount()).set((h, v) -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$spawnCount(v)).create(Keys.SPAWN_RANGE).get(h -> (double) ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$spawnRange()).set((h, v) -> ((BaseSpawnerAccessor) h.accessor$spawner()).accessor$spawnRange(v.intValue())).create(Keys.SPAWNABLE_ENTITIES).get(h -> MobSpawnerData.getEntities(h.accessor$spawner())).set((h, v) -> {
        final BaseSpawnerAccessor logic = (BaseSpawnerAccessor) h.accessor$spawner();
        MobSpawnerData.setEntities(logic, v);
        MobSpawnerData.setNextEntity((BaseSpawner) logic, MobSpawnerData.getNextEntity(logic));
    });
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Constants(org.spongepowered.common.util.Constants) SpawnData(net.minecraft.world.level.SpawnData) TableEntry(org.spongepowered.api.util.weighted.TableEntry) SpongeEntityArchetypeBuilder(org.spongepowered.common.entity.SpongeEntityArchetypeBuilder) WeightedSerializableObject(org.spongepowered.api.util.weighted.WeightedSerializableObject) NBTTranslator(org.spongepowered.common.data.persistence.NBTTranslator) Registry(net.minecraft.core.Registry) WeighedRandom_WeighedRandomItemAccessor(org.spongepowered.common.accessor.util.WeighedRandom_WeighedRandomItemAccessor) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) EntityTypes(org.spongepowered.api.entity.EntityTypes) WeightedObject(org.spongepowered.api.util.weighted.WeightedObject) BaseSpawnerAccessor(org.spongepowered.common.accessor.world.level.BaseSpawnerAccessor) BaseSpawner(net.minecraft.world.level.BaseSpawner) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ResourceKey(org.spongepowered.api.ResourceKey) EntityType(org.spongepowered.api.entity.EntityType) SpongeTicks(org.spongepowered.common.util.SpongeTicks) SpawnerBlockEntityAccessor(org.spongepowered.common.accessor.world.level.block.entity.SpawnerBlockEntityAccessor) EntityArchetype(org.spongepowered.api.entity.EntityArchetype) BaseSpawnerAccessor(org.spongepowered.common.accessor.world.level.BaseSpawnerAccessor) SpawnerBlockEntityAccessor(org.spongepowered.common.accessor.world.level.block.entity.SpawnerBlockEntityAccessor) SpongeTicks(org.spongepowered.common.util.SpongeTicks)

Example 5 with SpongeTicks

use of org.spongepowered.common.util.SpongeTicks in project SpongeCommon by SpongePowered.

the class EntityData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(Entity.class).create(Keys.AGE).get(h -> h.tickCount).setAnd((h, v) -> {
        if (v < 0) {
            return false;
        }
        h.tickCount = v;
        return true;
    }).create(Keys.BASE_SIZE).get(h -> (double) h.getBbWidth()).create(Keys.BASE_VEHICLE).get(h -> {
        final Entity rootVehicle = h.getRootVehicle();
        if (rootVehicle == h) {
            return null;
        }
        return (org.spongepowered.api.entity.Entity) rootVehicle;
    }).create(Keys.CUSTOM_NAME).get(h -> h.hasCustomName() ? SpongeAdventure.asAdventure(h.getCustomName()) : null).set((h, v) -> h.setCustomName(SpongeAdventure.asVanilla(v))).delete(h -> {
        h.setCustomName(null);
        h.setCustomNameVisible(false);
    }).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(h.getDisplayName())).create(Keys.EYE_HEIGHT).get(h -> (double) h.getEyeHeight()).create(Keys.EYE_POSITION).get(h -> VecHelper.toVector3d(h.getEyePosition(1f))).create(Keys.FALL_DISTANCE).get(h -> (double) h.fallDistance).setAnd((h, v) -> {
        if (v < 0) {
            return false;
        }
        h.fallDistance = v.floatValue();
        return true;
    }).create(Keys.FIRE_DAMAGE_DELAY).get(h -> new SpongeTicks(((EntityAccessor) h).invoker$getFireImmuneTicks())).setAnd((h, v) -> {
        final int ticks = (int) v.ticks();
        if (ticks < 1 || ticks > Short.MAX_VALUE) {
            return false;
        }
        ((EntityBridge) h).bridge$setFireImmuneTicks(ticks);
        return ((EntityAccessor) h).invoker$getFireImmuneTicks() == ticks;
    }).create(Keys.FIRE_TICKS).get(h -> ((EntityAccessor) h).accessor$remainingFireTicks() > 0 ? Ticks.of(((EntityAccessor) h).accessor$remainingFireTicks()) : null).set((h, v) -> {
        final int ticks = (int) v.ticks();
        ((EntityAccessor) h).accessor$remainingFireTicks(Math.max(ticks, Constants.Entity.MINIMUM_FIRE_TICKS));
    }).deleteAndGet(h -> {
        final EntityAccessor accessor = (EntityAccessor) h;
        final int ticks = accessor.accessor$remainingFireTicks();
        if (ticks < Constants.Entity.MINIMUM_FIRE_TICKS) {
            return DataTransactionResult.failNoData();
        }
        final DataTransactionResult.Builder dtrBuilder = DataTransactionResult.builder();
        dtrBuilder.replace(Value.immutableOf(Keys.FIRE_TICKS, new SpongeTicks(ticks)));
        dtrBuilder.replace(Value.immutableOf(Keys.FIRE_DAMAGE_DELAY, new SpongeTicks(((EntityAccessor) h).invoker$getFireImmuneTicks())));
        h.clearFire();
        return dtrBuilder.result(DataTransactionResult.Type.SUCCESS).build();
    }).create(Keys.HEIGHT).get(h -> (double) h.getBbHeight()).create(Keys.INVULNERABILITY_TICKS).get(h -> new SpongeTicks(h.invulnerableTime)).setAnd((h, v) -> {
        final int ticks = (int) v.ticks();
        if (ticks < 0) {
            return false;
        }
        h.invulnerableTime = ticks;
        if (h instanceof LivingEntity) {
            ((LivingEntity) h).hurtTime = ticks;
        }
        return true;
    }).create(Keys.IS_CUSTOM_NAME_VISIBLE).get(Entity::isCustomNameVisible).set(Entity::setCustomNameVisible).create(Keys.IS_FLYING).get(h -> h.hasImpulse).set((h, v) -> h.hasImpulse = v).supports(h -> !(h instanceof Player)).create(Keys.IS_GLOWING).get(Entity::isGlowing).set(Entity::setGlowing).create(Keys.IS_GRAVITY_AFFECTED).get(h -> !h.isNoGravity()).set((h, v) -> h.setNoGravity(!v)).create(Keys.IS_SNEAKING).get(Entity::isShiftKeyDown).set(Entity::setShiftKeyDown).create(Keys.IS_SPRINTING).get(Entity::isSprinting).set(Entity::setSprinting).create(Keys.IS_SILENT).get(Entity::isSilent).set(Entity::setSilent).create(Keys.IS_WET).get(Entity::isInWaterOrRain).create(Keys.ON_GROUND).get(Entity::isOnGround).create(Keys.PASSENGERS).get(h -> h.getPassengers().stream().map(org.spongepowered.api.entity.Entity.class::cast).collect(Collectors.toList())).set((h, v) -> {
        h.ejectPassengers();
        v.forEach(v1 -> ((Entity) v1).startRiding(h, true));
    }).create(Keys.REMAINING_AIR).get(h -> Math.max(0, h.getAirSupply())).setAnd((h, v) -> {
        if (v < 0 || v > h.getMaxAirSupply()) {
            return false;
        }
        if (v == 0 && h.getAirSupply() < 0) {
            return false;
        }
        h.setAirSupply(v);
        return true;
    }).create(Keys.SCALE).get(h -> 1d).create(Keys.SCOREBOARD_TAGS).get(Entity::getTags).set((h, v) -> {
        h.getTags().clear();
        h.getTags().addAll(v);
    }).create(Keys.TRANSIENT).get(h -> ((EntityAccessor) h).invoker$getEncodeId() == null).set((h, v) -> ((EntityBridge) h).bridge$setTransient(v)).create(Keys.VEHICLE).get(h -> (org.spongepowered.api.entity.Entity) h.getVehicle()).set((h, v) -> h.startRiding((Entity) v, true)).create(Keys.VELOCITY).get(h -> VecHelper.toVector3d(h.getDeltaMovement())).set((h, v) -> h.setDeltaMovement(VecHelper.toVanillaVector3d(v))).create(Keys.SWIFTNESS).get(m -> m.getDeltaMovement().length()).set((m, v) -> m.setDeltaMovement(m.getDeltaMovement().normalize().scale(v))).supports(m -> m.getDeltaMovement().lengthSqr() > 0).asMutable(EntityMaxAirBridge.class).create(Keys.MAX_AIR).get(EntityMaxAirBridge::bridge$getMaxAir).set(EntityMaxAirBridge::bridge$setMaxAir);
    registrator.spongeDataStore(ResourceKey.sponge("max_air"), EntityMaxAirBridge.class, Keys.MAX_AIR);
    registrator.newDataStore(SpongeEntitySnapshot.class, SpongeEntityArchetype.class).dataStore(Keys.CUSTOM_NAME, (dv, v) -> dv.set(Constants.Entity.CUSTOM_NAME, GsonComponentSerializer.gson().serialize(v)), dv -> dv.getString(Constants.Entity.CUSTOM_NAME).map(GsonComponentSerializer.gson()::deserialize));
// @formatter:on
}
Also used : GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Ticks(org.spongepowered.api.util.Ticks) LivingEntity(net.minecraft.world.entity.LivingEntity) EntityMaxAirBridge(org.spongepowered.common.bridge.world.entity.EntityMaxAirBridge) Constants(org.spongepowered.common.util.Constants) SpongeEntityArchetype(org.spongepowered.common.entity.SpongeEntityArchetype) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) Collectors(java.util.stream.Collectors) Value(org.spongepowered.api.data.value.Value) SpongeEntitySnapshot(org.spongepowered.common.entity.SpongeEntitySnapshot) Player(net.minecraft.world.entity.player.Player) EntityBridge(org.spongepowered.common.bridge.world.entity.EntityBridge) Keys(org.spongepowered.api.data.Keys) Entity(net.minecraft.world.entity.Entity) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) VecHelper(org.spongepowered.common.util.VecHelper) ResourceKey(org.spongepowered.api.ResourceKey) SpongeTicks(org.spongepowered.common.util.SpongeTicks) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) Player(net.minecraft.world.entity.player.Player) EntityMaxAirBridge(org.spongepowered.common.bridge.world.entity.EntityMaxAirBridge) SpongeEntityArchetype(org.spongepowered.common.entity.SpongeEntityArchetype) LivingEntity(net.minecraft.world.entity.LivingEntity) SpongeEntitySnapshot(org.spongepowered.common.entity.SpongeEntitySnapshot) SpongeTicks(org.spongepowered.common.util.SpongeTicks)

Aggregations

SpongeTicks (org.spongepowered.common.util.SpongeTicks)8 Keys (org.spongepowered.api.data.Keys)7 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)7 Constants (org.spongepowered.common.util.Constants)5 ResourceKey (org.spongepowered.api.ResourceKey)3 CompoundTag (net.minecraft.nbt.CompoundTag)2 Value (org.spongepowered.api.data.value.Value)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 GsonComponentSerializer (net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)1 Registry (net.minecraft.core.Registry)1 ClientboundAddPaintingPacket (net.minecraft.network.protocol.game.ClientboundAddPaintingPacket)1 ClientboundRemoveEntitiesPacket (net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 AgableMob (net.minecraft.world.entity.AgableMob)1 Entity (net.minecraft.world.entity.Entity)1