Search in sources :

Example 1 with HangingEntity

use of net.minecraft.world.entity.decoration.HangingEntity in project SpongeCommon by SpongePowered.

the class LevelMixin method bridge$createEntity.

@Override
@SuppressWarnings("unchecked")
public <E extends org.spongepowered.api.entity.Entity> E bridge$createEntity(final EntityType<E> type, final Vector3d position, final boolean naturally) throws IllegalArgumentException, IllegalStateException {
    if (type == net.minecraft.world.entity.EntityType.PLAYER) {
        // Unable to construct these
        throw new IllegalArgumentException("A Player cannot be created by the API!");
    }
    net.minecraft.world.entity.Entity entity = null;
    final double x = position.x();
    final double y = position.y();
    final double z = position.z();
    final net.minecraft.world.level.Level thisWorld = (net.minecraft.world.level.Level) (Object) this;
    // Not all entities have a single World parameter as their constructor
    if (type == net.minecraft.world.entity.EntityType.LIGHTNING_BOLT) {
        entity = net.minecraft.world.entity.EntityType.LIGHTNING_BOLT.create(thisWorld);
        entity.moveTo(x, y, z);
        ((LightningBolt) entity).setVisualOnly(false);
    }
    // TODO - archetypes should solve the problem of calling the correct constructor
    if (type == net.minecraft.world.entity.EntityType.ENDER_PEARL) {
        final ArmorStand tempEntity = new ArmorStand(thisWorld, x, y, z);
        tempEntity.setPos(tempEntity.getX(), tempEntity.getY() - tempEntity.getEyeHeight(), tempEntity.getZ());
        entity = new ThrownEnderpearl(thisWorld, tempEntity);
        ((EnderPearl) entity).offer(Keys.SHOOTER, UnknownProjectileSource.UNKNOWN);
    }
    // set them is to use the more specialised constructor).
    if (type == net.minecraft.world.entity.EntityType.FALLING_BLOCK) {
        entity = new FallingBlockEntity(thisWorld, x, y, z, Blocks.SAND.defaultBlockState());
    }
    if (type == net.minecraft.world.entity.EntityType.ITEM) {
        entity = new ItemEntity(thisWorld, x, y, z, new ItemStack(Blocks.STONE));
    }
    if (entity == null) {
        final ResourceKey key = (ResourceKey) (Object) Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) type);
        try {
            entity = ((net.minecraft.world.entity.EntityType) type).create(thisWorld);
            entity.moveTo(x, y, z);
        } catch (final Exception e) {
            throw new RuntimeException("There was an issue attempting to construct " + key, e);
        }
    }
    if (entity instanceof HangingEntity) {
        if (!((HangingEntity) entity).survives()) {
            throw new IllegalArgumentException("Hanging entity does not survive at the given position: " + position);
        }
    }
    if (naturally && entity instanceof Mob) {
        // Adding the default equipment
        final DifficultyInstance difficulty = this.shadow$getCurrentDifficultyAt(new BlockPos(x, y, z));
        ((MobAccessor) entity).invoker$populateDefaultEquipmentSlots(difficulty);
    }
    if (entity instanceof Painting) {
        // This is default when art is null when reading from NBT, could
        // choose a random art instead?
        ((Painting) entity).motive = Motive.KEBAB;
    }
    return (E) entity;
}
Also used : DifficultyInstance(net.minecraft.world.DifficultyInstance) ThrownEnderpearl(net.minecraft.world.entity.projectile.ThrownEnderpearl) LightningBolt(net.minecraft.world.entity.LightningBolt) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) BlockPos(net.minecraft.core.BlockPos) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Mob(net.minecraft.world.entity.Mob) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EnderPearl(org.spongepowered.api.entity.projectile.EnderPearl) ResourceKey(org.spongepowered.api.ResourceKey) Painting(net.minecraft.world.entity.decoration.Painting) MobAccessor(org.spongepowered.common.accessor.world.entity.MobAccessor) Level(net.minecraft.world.level.Level) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack)

Example 2 with HangingEntity

use of net.minecraft.world.entity.decoration.HangingEntity in project SpongeCommon by SpongePowered.

the class EntityTickPhaseState method postBlockTransactionApplication.

@Override
public void postBlockTransactionApplication(final EntityTickContext context, final BlockChange blockChange, final BlockTransactionReceipt transaction) {
    if (blockChange == BlockChange.BREAK) {
        final Entity tickingEntity = context.getSource(Entity.class).get();
        final BlockPos blockPos = VecHelper.toBlockPos(transaction.originalBlock().position());
        final List<HangingEntity> hangingEntities = ((ServerLevel) tickingEntity.world()).getEntitiesOfClass(HangingEntity.class, new AABB(blockPos, blockPos).inflate(1.1D, 1.1D, 1.1D), entityIn -> {
            if (entityIn == null) {
                return false;
            }
            final BlockPos entityPos = entityIn.getPos();
            // Hanging Neighbor Entity
            if (entityPos.equals(blockPos.offset(0, 1, 0))) {
                return true;
            }
            // Check around source block
            final Direction entityFacing = entityIn.getDirection();
            if (entityFacing == Direction.NORTH) {
                return entityPos.equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_NORTH));
            } else if (entityFacing == Direction.SOUTH) {
                return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_SOUTH));
            } else if (entityFacing == Direction.WEST) {
                return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_WEST));
            } else if (entityFacing == Direction.EAST) {
                return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_EAST));
            }
            return false;
        });
        for (final HangingEntity entityHanging : hangingEntities) {
            if (entityHanging instanceof ItemFrame) {
                final ItemFrame itemFrame = (ItemFrame) entityHanging;
                if (!itemFrame.removed) {
                    ((ItemFrameAccessor) itemFrame).invoker$dropItem((net.minecraft.world.entity.Entity) tickingEntity, true);
                }
                itemFrame.remove();
            }
        }
    }
}
Also used : HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Entity(org.spongepowered.api.entity.Entity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) ServerLevel(net.minecraft.server.level.ServerLevel) BlockPos(net.minecraft.core.BlockPos) ItemFrame(net.minecraft.world.entity.decoration.ItemFrame) Direction(net.minecraft.core.Direction) AABB(net.minecraft.world.phys.AABB) ItemFrameAccessor(org.spongepowered.common.accessor.world.entity.decoration.ItemFrameAccessor)

Example 3 with HangingEntity

use of net.minecraft.world.entity.decoration.HangingEntity in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method setItemFrameFacingAndRotation.

private static Entity setItemFrameFacingAndRotation(ItemFrame frame, BuildEntity buildEntity, BlockPos entityPos, Structure structure) {
    float yaw = frame.getYRot();
    Rotation rotation = Rotation.NONE;
    double x_axis_offset = buildEntity.entityXAxisOffset;
    double z_axis_offset = buildEntity.entityZAxisOffset;
    Direction facing = frame.getDirection();
    double y_axis_offset = buildEntity.entityYAxisOffset;
    x_axis_offset = x_axis_offset * -1;
    z_axis_offset = z_axis_offset * -1;
    Direction structureDirection = structure.getClearSpace().getShape().getDirection();
    Direction configurationDirection = structure.configuration.houseFacing.getOpposite();
    if (facing != Direction.UP && facing != Direction.DOWN) {
        if (configurationDirection == structureDirection.getOpposite()) {
            rotation = Rotation.CLOCKWISE_180;
            facing = facing.getOpposite();
        } else if (configurationDirection == structureDirection.getClockWise()) {
            rotation = Rotation.CLOCKWISE_90;
            facing = facing.getClockWise();
        } else if (configurationDirection == structureDirection.getCounterClockWise()) {
            rotation = Rotation.COUNTERCLOCKWISE_90;
            facing = facing.getCounterClockWise();
        } else {
            x_axis_offset = 0;
            z_axis_offset = 0;
        }
    }
    yaw = frame.rotate(rotation);
    CompoundTag compound = new CompoundTag();
    ((HangingEntity) frame).addAdditionalSaveData(compound);
    compound.putByte("Facing", (byte) facing.get3DDataValue());
    ((HangingEntity) frame).readAdditionalSaveData(compound);
    StructureEventHandler.updateEntityHangingBoundingBox(frame);
    frame.moveTo(entityPos.getX() + x_axis_offset, entityPos.getY() + y_axis_offset, entityPos.getZ() + z_axis_offset, yaw, frame.getXRot());
    StructureEventHandler.updateEntityHangingBoundingBox(frame);
    ChunkAccess chunk = structure.world.getChunkAt(entityPos);
    chunk.setUnsaved(true);
    return frame;
}
Also used : HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) Direction(net.minecraft.core.Direction) NetworkDirection(net.minecraftforge.network.NetworkDirection) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with HangingEntity

use of net.minecraft.world.entity.decoration.HangingEntity in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method onServerTick.

/**
 * This event is primarily used to build 100 blocks for any queued structures for all players.
 *
 * @param event The event object.
 */
@SubscribeEvent
public static void onServerTick(ServerTickEvent event) {
    if (event.phase == TickEvent.Phase.START) {
        ArrayList<Player> playersToRemove = new ArrayList<>();
        if (StructureEventHandler.entitiesToGenerate.size() > 0) {
            StructureEventHandler.ticksSinceLastEntitiesGenerated++;
            if (StructureEventHandler.ticksSinceLastEntitiesGenerated > 40) {
                // Process any entities.
                StructureEventHandler.processStructureEntities();
                StructureEventHandler.ticksSinceLastEntitiesGenerated = 0;
            }
        }
        if (StructureEventHandler.structuresToBuild.size() > 0) {
            for (Entry<Player, ArrayList<Structure>> entry : StructureEventHandler.structuresToBuild.entrySet()) {
                ArrayList<Structure> structuresToRemove = new ArrayList<>();
                // Build the first 100 blocks of each structure for this player.
                for (Structure structure : entry.getValue()) {
                    if (!structure.entitiesRemoved) {
                        // If there is a player there...they will probably die anyways.....
                        for (BlockPos clearedPos : structure.clearedBlockPos) {
                            AABB axisPos = Shapes.block().bounds().move(clearedPos);
                            List<Entity> list = structure.world.getEntities(null, axisPos);
                            if (!list.isEmpty()) {
                                for (Entity entity : list) {
                                    // Don't kill living entities.
                                    if (!(entity instanceof LivingEntity)) {
                                        if (entity instanceof HangingEntity) {
                                            structure.BeforeHangingEntityRemoved((HangingEntity) entity);
                                        }
                                        structure.world.removeEntity(entity, false);
                                    }
                                }
                            }
                        }
                        structure.entitiesRemoved = true;
                    }
                    if (structure.airBlocks.size() > 0) {
                        structure.hasAirBlocks = true;
                    }
                    for (int i = 0; i < 10; i++) {
                        i = StructureEventHandler.setBlock(i, structure, structuresToRemove);
                    }
                    // After building the blocks for this tick, find waterlogged blocks and remove them.
                    StructureEventHandler.removeWaterLogging(structure);
                }
                // Update the list of structures to remove this structure since it's done building.
                StructureEventHandler.removeStructuresFromList(structuresToRemove, entry);
                if (entry.getValue().size() == 0) {
                    playersToRemove.add(entry.getKey());
                }
            }
        }
        // Remove each player that has their structure's built.
        for (Player player : playersToRemove) {
            StructureEventHandler.structuresToBuild.remove(player);
        }
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Entity(net.minecraft.world.entity.Entity) BuildEntity(com.wuest.prefab.structures.base.BuildEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(net.minecraft.world.entity.player.Player) LivingEntity(net.minecraft.world.entity.LivingEntity) BlockPos(net.minecraft.core.BlockPos) Structure(com.wuest.prefab.structures.base.Structure) AABB(net.minecraft.world.phys.AABB) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 5 with HangingEntity

use of net.minecraft.world.entity.decoration.HangingEntity in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method setPaintingFacingAndRotation.

private static Entity setPaintingFacingAndRotation(Painting entity, BuildEntity buildEntity, BlockPos entityPos, Structure structure) {
    float yaw = entity.getYRot();
    Rotation rotation = Rotation.NONE;
    double x_axis_offset = 0;
    double z_axis_offset = 0;
    Direction facing = entity.getDirection();
    double y_axis_offset = buildEntity.entityYAxisOffset * -1;
    Direction structureDirection = structure.getClearSpace().getShape().getDirection();
    Direction configurationDirection = structure.configuration.houseFacing.getOpposite();
    if (configurationDirection == structureDirection.getOpposite()) {
        rotation = Rotation.CLOCKWISE_180;
        facing = facing.getOpposite();
    } else if (configurationDirection == structureDirection.getClockWise()) {
        rotation = Rotation.CLOCKWISE_90;
        facing = facing.getClockWise();
    } else if (configurationDirection == structureDirection.getCounterClockWise()) {
        rotation = Rotation.COUNTERCLOCKWISE_90;
        facing = facing.getCounterClockWise();
    }
    int paintingBlockWidth = entity.motive.getWidth() / 16;
    int paintingBlockHeight = entity.motive.getHeight() / 16;
    if ((paintingBlockHeight > paintingBlockWidth || paintingBlockHeight > 1) && !(paintingBlockWidth == 4 && paintingBlockHeight == 3)) {
        y_axis_offset--;
    }
    yaw = entity.rotate(rotation);
    CompoundTag compound = new CompoundTag();
    ((HangingEntity) entity).addAdditionalSaveData(compound);
    compound.putByte("Facing", (byte) facing.get2DDataValue());
    ((HangingEntity) entity).readAdditionalSaveData(compound);
    StructureEventHandler.updateEntityHangingBoundingBox(entity);
    entity.moveTo(entityPos.getX() + x_axis_offset, entityPos.getY() + y_axis_offset, entityPos.getZ() + z_axis_offset, yaw, entity.getXRot());
    StructureEventHandler.updateEntityHangingBoundingBox(entity);
    ChunkAccess chunk = structure.world.getChunkAt(entityPos);
    chunk.setUnsaved(true);
    return entity;
}
Also used : HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) Direction(net.minecraft.core.Direction) NetworkDirection(net.minecraftforge.network.NetworkDirection) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

HangingEntity (net.minecraft.world.entity.decoration.HangingEntity)7 BlockPos (net.minecraft.core.BlockPos)5 Direction (net.minecraft.core.Direction)5 CompoundTag (net.minecraft.nbt.CompoundTag)4 AABB (net.minecraft.world.phys.AABB)3 Entity (net.minecraft.world.entity.Entity)2 ItemFrame (net.minecraft.world.entity.decoration.ItemFrame)2 Player (net.minecraft.world.entity.player.Player)2 ItemStack (net.minecraft.world.item.ItemStack)2 Level (net.minecraft.world.level.Level)2 ChunkAccess (net.minecraft.world.level.chunk.ChunkAccess)2 NetworkDirection (net.minecraftforge.network.NetworkDirection)2 BuildEntity (com.wuest.prefab.structures.base.BuildEntity)1 Structure (com.wuest.prefab.structures.base.Structure)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 DifficultyInstance (net.minecraft.world.DifficultyInstance)1 LightningBolt (net.minecraft.world.entity.LightningBolt)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1