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