Search in sources :

Example 6 with HangingEntity

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

the class BambooItemFrameItem method useOn.

@Override
public InteractionResult useOn(UseOnContext context) {
    BlockPos blockpos = context.getClickedPos();
    Direction direction = context.getClickedFace();
    BlockPos blockpos1 = blockpos.relative(direction);
    Player playerentity = context.getPlayer();
    ItemStack itemstack = context.getItemInHand();
    if (playerentity != null && !this.mayPlace(playerentity, direction, itemstack, blockpos1)) {
        return InteractionResult.FAIL;
    } else {
        Level world = context.getLevel();
        HangingEntity hangingentity = new BambooItemFrame(world, blockpos1, direction);
        CompoundTag compoundnbt = itemstack.getTag();
        if (compoundnbt != null) {
            EntityType.updateCustomEntityTag(world, playerentity, hangingentity, compoundnbt);
        }
        if (hangingentity.survives()) {
            if (!world.isClientSide) {
                hangingentity.playPlacementSound();
                world.addFreshEntity(hangingentity);
            }
            itemstack.shrink(1);
        }
        return InteractionResult.SUCCESS;
    }
}
Also used : HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Player(net.minecraft.world.entity.player.Player) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) BambooItemFrame(net.tropicraft.core.common.entity.BambooItemFrame) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 7 with HangingEntity

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

the class Structure method ScanStructure.

public static void ScanStructure(Level world, BlockPos originalPos, BlockPos cornerPos1, BlockPos cornerPos2, String fileLocation, BuildClear clearedSpace, Direction playerFacing, boolean includeAir, boolean excludeWater) {
    Structure scannedStructure = new Structure();
    scannedStructure.setClearSpace(clearedSpace);
    for (BlockPos currentPos : BlockPos.betweenClosed(cornerPos1, cornerPos2)) {
        if (world.isEmptyBlock(currentPos) && !includeAir) {
            continue;
        }
        BlockState currentState = world.getBlockState(currentPos);
        Block currentBlock = currentState.getBlock();
        if (currentState.getMaterial() == Material.WATER && excludeWater) {
            continue;
        }
        BuildBlock buildBlock = Structure.createBuildBlockFromBlockState(currentState, currentBlock, currentPos, originalPos);
        if (currentBlock instanceof DoorBlock) {
            DoubleBlockHalf blockHalf = currentState.getValue(DoorBlock.HALF);
            if (blockHalf == DoubleBlockHalf.LOWER) {
                BlockState upperHalfState = world.getBlockState(currentPos.above());
                if (upperHalfState.getBlock() instanceof DoorBlock) {
                    Block upperBlock = upperHalfState.getBlock();
                    BuildBlock upperHalf = Structure.createBuildBlockFromBlockState(upperHalfState, upperBlock, currentPos.above(), originalPos);
                    buildBlock.setSubBlock(upperHalf);
                }
            } else {
                // Don't process upper door halves. These were already done.
                continue;
            }
        } else if (currentBlock instanceof BedBlock) {
            BedPart bedPart = currentState.getValue(BedBlock.PART);
            if (bedPart == BedPart.HEAD) {
                BlockState bedFoot = null;
                boolean foundFoot = false;
                Direction facing = Direction.NORTH;
                while (!foundFoot) {
                    bedFoot = world.getBlockState(currentPos.relative(facing));
                    if (bedFoot.getBlock() instanceof BedBlock && bedFoot.getValue(BedBlock.PART) == BedPart.FOOT) {
                        foundFoot = true;
                        break;
                    }
                    facing = facing.getClockWise();
                    if (facing == Direction.NORTH) {
                        // Got back to north, break out to avoid infinite loop.
                        break;
                    }
                }
                if (foundFoot) {
                    Block footBedBlock = bedFoot.getBlock();
                    BuildBlock bed = Structure.createBuildBlockFromBlockState(bedFoot, footBedBlock, currentPos.relative(facing), originalPos);
                    buildBlock.setSubBlock(bed);
                }
            } else {
                // Don't process foot of bed, it was already done.
                continue;
            }
        }
        scannedStructure.getBlocks().add(buildBlock);
        BlockEntity tileEntity = world.getBlockEntity(currentPos);
        if (tileEntity != null) {
            // Don't write data for empty tile entities.
            if ((tileEntity instanceof ChestBlockEntity && ((ChestBlockEntity) tileEntity).isEmpty()) || (tileEntity instanceof FurnaceBlockEntity && ((FurnaceBlockEntity) tileEntity).isEmpty())) {
                continue;
            }
            ResourceLocation resourceLocation = ForgeRegistries.BLOCK_ENTITIES.getKey(tileEntity.getType());
            CompoundTag tagCompound = tileEntity.saveWithFullMetadata();
            BuildTileEntity buildTileEntity = new BuildTileEntity();
            assert resourceLocation != null;
            buildTileEntity.setEntityDomain(resourceLocation.getNamespace());
            buildTileEntity.setEntityName(resourceLocation.getPath());
            buildTileEntity.setStartingPosition(Structure.getStartingPositionFromOriginalAndCurrentPosition(currentPos, originalPos));
            buildTileEntity.setEntityNBTData(tagCompound);
            scannedStructure.tileEntities.add(buildTileEntity);
        }
    }
    int x_radiusRangeBegin = Math.min(cornerPos1.getX(), cornerPos2.getX());
    int x_radiusRangeEnd = Math.max(cornerPos1.getX(), cornerPos2.getX());
    int y_radiusRangeBegin = Math.min(cornerPos1.getY(), cornerPos2.getY());
    int y_radiusRangeEnd = Math.max(cornerPos1.getY(), cornerPos2.getY());
    int z_radiusRangeBegin = Math.min(cornerPos1.getZ(), cornerPos2.getZ());
    int z_radiusRangeEnd = Math.max(cornerPos1.getZ(), cornerPos2.getZ());
    AABB axis = new AABB(cornerPos1, cornerPos2);
    for (Entity entity : world.getEntities(null, axis)) {
        // TODO: This was the "getPosition" method.
        BlockPos entityPos = entity.blockPosition();
        if (entity instanceof HangingEntity) {
            // Use the HangingEntity getPos function instead since it is more accurate for itemframes and paintings.
            entityPos = ((HangingEntity) entity).getPos();
        }
        if (entityPos.getX() >= x_radiusRangeBegin && entityPos.getX() <= x_radiusRangeEnd && entityPos.getZ() >= z_radiusRangeBegin && entityPos.getZ() <= z_radiusRangeEnd && entityPos.getY() >= y_radiusRangeBegin && entityPos.getY() <= y_radiusRangeEnd) {
            BuildEntity buildEntity = new BuildEntity();
            buildEntity.setEntityResourceString(ForgeRegistries.ENTITIES.getKey(entity.getType()));
            buildEntity.setStartingPosition(Structure.getStartingPositionFromOriginalAndCurrentPosition(entityPos, originalPos));
            // The function calls below get the following fields from the "entity" class. posX, posY, posZ.
            // This will probably have to change when the mappings get updated.
            buildEntity.entityXAxisOffset = entityPos.getX() - entity.getX();
            buildEntity.entityYAxisOffset = entityPos.getY() - entity.getY();
            buildEntity.entityZAxisOffset = entityPos.getZ() - entity.getZ();
            if (entity instanceof ItemFrame) {
                buildEntity.entityYAxisOffset = buildEntity.entityYAxisOffset * -1;
            }
            if (entity instanceof HangingEntity) {
                buildEntity.entityFacing = entity.getDirection();
            }
            CompoundTag entityTagCompound = new CompoundTag();
            entity.saveAsPassenger(entityTagCompound);
            buildEntity.setEntityNBTData(entityTagCompound);
            scannedStructure.entities.add(buildEntity);
        }
    }
    Structure.CreateStructureFile(scannedStructure, fileLocation);
}
Also used : ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity) Entity(net.minecraft.world.entity.Entity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity) ItemFrame(net.minecraft.world.entity.decoration.ItemFrame) Direction(net.minecraft.core.Direction) BedPart(net.minecraft.world.level.block.state.properties.BedPart) BlockState(net.minecraft.world.level.block.state.BlockState) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) DoubleBlockHalf(net.minecraft.world.level.block.state.properties.DoubleBlockHalf) ResourceLocation(net.minecraft.resources.ResourceLocation) BlockPos(net.minecraft.core.BlockPos) CompoundTag(net.minecraft.nbt.CompoundTag) AABB(net.minecraft.world.phys.AABB) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity)

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