Search in sources :

Example 16 with EntityType

use of org.spongepowered.api.entity.EntityType in project SpongeCommon by SpongePowered.

the class MixinMobSpawnerBaseLogic method readEntityFromCompoundAtWorld.

/**
 * @author gabizou - April 10th, 2016
 *
 * This is close to a verbatim copy of {@link AnvilChunkLoader#readWorldEntityPos(NBTTagCompound, World, double, double, double, boolean)}
 * with the added bonus of throwing events before entities are constructed with appropriate causes.
 *
 * @param compound The compound of the entity to spawn with
 * @param world The world to spawn at
 * @param x The x position
 * @param y The y position
 * @param z The z position
 * @param attemptToSpawn If false, the entity is not going to be spawned into the world yet
 * @return The entity, if successfully created
 */
private static Entity readEntityFromCompoundAtWorld(NBTTagCompound compound, World world, double x, double y, double z, boolean attemptToSpawn) {
    final String entityTypeString = compound.getString(NbtDataUtil.ENTITY_TYPE_ID);
    final Class<? extends Entity> clazz = SpongeImplHooks.getEntityClass(new ResourceLocation(entityTypeString));
    if (clazz == null) {
        final PrettyPrinter printer = new PrettyPrinter(60).add("Unknown Entity for MobSpawners").centre().hr().addWrapped(60, "Sponge has found a MobSpawner attempting to locate potentially" + "a foreign entity type for a MobSpawner, unfortunately, there isn't a" + "way to get around the deserialization process looking up unregistered" + "entity types. This may be a bug with a mod or sponge.").add("%s : %s", "Entity Name", entityTypeString).add();
        PhaseTracker.getInstance().generateVersionInfo(printer);
        printer.trace(System.err, SpongeImpl.getLogger(), Level.WARN);
        return null;
    }
    EntityType type = EntityTypeRegistryModule.getInstance().getForClass(clazz);
    if (type == null) {
        return null;
    }
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.MOB_SPAWNER);
        Transform<org.spongepowered.api.world.World> transform = new Transform<>(((org.spongepowered.api.world.World) world), new Vector3d(x, y, z));
        ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), type, transform);
        SpongeImpl.postEvent(event);
        if (event.isCancelled()) {
            return null;
        }
    }
    Entity entity;
    try {
        entity = EntityList.createEntityFromNBT(compound, world);
    } catch (Exception e) {
        return null;
    }
    if (entity == null) {
        return null;
    }
    entity.setLocationAndAngles(x, y, z, entity.rotationYaw, entity.rotationPitch);
    if (attemptToSpawn && !world.spawnEntity(entity)) {
        return null;
    }
    if (compound.hasKey(NbtDataUtil.Minecraft.PASSENGERS, NbtDataUtil.TAG_LIST)) {
        final NBTTagList passengerList = compound.getTagList(NbtDataUtil.Minecraft.PASSENGERS, NbtDataUtil.TAG_COMPOUND);
        for (int i = 0; i < passengerList.tagCount(); i++) {
            final Entity passenger = readEntityFromCompoundAtWorld(passengerList.getCompoundTagAt(i), world, x, y, z, attemptToSpawn);
            if (passenger != null) {
                passenger.startRiding(entity, true);
            }
        }
    }
    return entity;
}
Also used : Entity(net.minecraft.entity.Entity) World(net.minecraft.world.World) EntityType(org.spongepowered.api.entity.EntityType) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) NBTTagList(net.minecraft.nbt.NBTTagList) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) Vector3d(com.flowpowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ResourceLocation(net.minecraft.util.ResourceLocation) Transform(org.spongepowered.api.entity.Transform)

Example 17 with EntityType

use of org.spongepowered.api.entity.EntityType in project SpongeCommon by SpongePowered.

the class MixinBlockFalling method onIsAreaLoadedCheck.

@Redirect(method = "checkFallable", at = @At(value = "INVOKE", target = WORLD_IS_AREA_LOADED))
private boolean onIsAreaLoadedCheck(World world, BlockPos pos, BlockPos to) {
    if (world.isAreaLoaded(pos, to)) {
        if (!world.isRemote) {
            BlockPos actualPos = pos.add(32, 32, 32);
            EntityType fallingBlock = EntityTypes.FALLING_BLOCK;
            Vector3d position = new Vector3d(actualPos.getX() + 0.5D, actualPos.getY(), actualPos.getZ() + 0.5D);
            BlockSnapshot snapshot = ((org.spongepowered.api.world.World) world).createSnapshot(actualPos.getX(), actualPos.getY(), actualPos.getZ());
            try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
                Sponge.getCauseStackManager().pushCause(snapshot);
                Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.FALLING_BLOCK);
                Transform<org.spongepowered.api.world.World> worldTransform = new Transform<>((org.spongepowered.api.world.World) world, position);
                ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), fallingBlock, worldTransform);
                SpongeImpl.postEvent(event);
                return !event.isCancelled();
            }
        }
    }
    return false;
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(net.minecraft.world.World) EntityType(org.spongepowered.api.entity.EntityType) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.util.math.BlockPos) Transform(org.spongepowered.api.entity.Transform) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 18 with EntityType

use of org.spongepowered.api.entity.EntityType in project SpongeCommon by SpongePowered.

the class SpawnerUtils method getEntities.

public static WeightedTable<EntityArchetype> getEntities(MobSpawnerBaseLogic logic) {
    WeightedTable<EntityArchetype> possibleEntities = new WeightedTable<>();
    for (WeightedSpawnerEntity weightedEntity : logic.potentialSpawns) {
        NBTTagCompound nbt = weightedEntity.getNbt();
        EntityType type = EntityUtil.fromNameToType(nbt.getString(NbtDataUtil.ENTITY_TYPE_ID)).orElse(EntityTypes.PIG);
        EntityArchetype archetype = EntityArchetype.builder().type(type).entityData(NbtTranslator.getInstance().translateFrom(nbt)).build();
        possibleEntities.add(new WeightedSerializableObject<>(archetype, weightedEntity.itemWeight));
    }
    return possibleEntities;
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) EntityArchetype(org.spongepowered.api.entity.EntityArchetype) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) WeightedSpawnerEntity(net.minecraft.util.WeightedSpawnerEntity)

Example 19 with EntityType

use of org.spongepowered.api.entity.EntityType in project SpongeCommon by SpongePowered.

the class SpawnableDataProcessor method removeFrom.

@Override
public DataTransactionResult removeFrom(ValueContainer<?> container) {
    if (!supports(container)) {
        return DataTransactionResult.failNoData();
    }
    ItemStack itemStack = (ItemStack) container;
    Optional<EntityType> old = getVal(itemStack);
    if (!old.isPresent()) {
        return DataTransactionResult.successNoData();
    }
    try {
        NbtDataUtil.getItemCompound(itemStack).get().removeTag(NbtDataUtil.SPAWNABLE_ENTITY_TAG);
        return DataTransactionResult.successRemove(constructImmutableValue(old.get()));
    } catch (Exception e) {
        return DataTransactionResult.builder().result(DataTransactionResult.Type.ERROR).build();
    }
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) ItemStack(net.minecraft.item.ItemStack)

Example 20 with EntityType

use of org.spongepowered.api.entity.EntityType in project SpongeCommon by SpongePowered.

the class SpongeTimings method getEntityTiming.

/**
 * Get a named timer for the specified entity type to track type specific
 * timings.
 *
 * @param entity
 * @return
 */
public static Timing getEntityTiming(Entity entity) {
    EntityType type = entity.getType();
    String entityType = type != null ? type.getId() : entity.getClass().getName();
    return SpongeTimingsFactory.ofSafe("Minecraft", "## tickEntity - " + entityType);
}
Also used : TileEntityType(org.spongepowered.api.block.tileentity.TileEntityType) EntityType(org.spongepowered.api.entity.EntityType)

Aggregations

EntityType (org.spongepowered.api.entity.EntityType)33 Vector3d (com.flowpowered.math.vector.Vector3d)7 World (net.minecraft.world.World)6 Entity (org.spongepowered.api.entity.Entity)6 Transform (org.spongepowered.api.entity.Transform)6 ConstructEntityEvent (org.spongepowered.api.event.entity.ConstructEntityEvent)6 SpongeEntityType (org.spongepowered.common.entity.SpongeEntityType)4 Entity (net.minecraft.entity.Entity)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 Player (org.spongepowered.api.entity.living.player.Player)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3 World (org.spongepowered.api.world.World)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 NBTTagList (net.minecraft.nbt.NBTTagList)2