Search in sources :

Example 1 with EntityHanging

use of net.minecraft.entity.EntityHanging in project BetterWithAddons by DaedalusGame.

the class ItemArtifactFrame method onItemUse.

public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    ItemStack itemstack = player.getHeldItem(hand);
    BlockPos blockpos = pos.offset(facing);
    if (facing != EnumFacing.DOWN && facing != EnumFacing.UP && player.canPlayerEdit(blockpos, facing, itemstack)) {
        EntityHanging entityhanging = this.createEntity(worldIn, blockpos, facing);
        if (entityhanging != null && entityhanging.onValidSurface()) {
            if (!worldIn.isRemote) {
                entityhanging.playPlaceSound();
                worldIn.spawnEntity(entityhanging);
            }
            itemstack.shrink(1);
        }
        return EnumActionResult.SUCCESS;
    } else {
        return EnumActionResult.FAIL;
    }
}
Also used : EntityHanging(net.minecraft.entity.EntityHanging) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 2 with EntityHanging

use of net.minecraft.entity.EntityHanging in project MC-Prefab by Brian-Wuest.

the class ModEventHandler 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) {
    ArrayList<EntityPlayer> playersToRemove = new ArrayList<EntityPlayer>();
    for (Entry<EntityPlayer, ArrayList<Structure>> entry : ModEventHandler.structuresToBuild.entrySet()) {
        ArrayList<Structure> structuresToRemove = new ArrayList<Structure>();
        EntityPlayer player = entry.getKey();
        // Build the first 100 blocks of each structure for this player.
        for (Structure structure : entry.getValue()) {
            for (int i = 0; i < 100; i++) {
                // Structure clearing happens before anything else.
                if (structure.clearedBlockPos.size() > 0) {
                    BlockPos currentPos = structure.clearedBlockPos.get(0);
                    structure.clearedBlockPos.remove(0);
                    IBlockState clearBlockState = structure.world.getBlockState(currentPos);
                    // This will also break other mod's logic blocks but they would probably be broken due to structure generation anyways.
                    if (clearBlockState.getBlock() != Blocks.AIR) {
                        structure.BeforeClearSpaceBlockReplaced(currentPos);
                        structure.world.setBlockToAir(currentPos);
                    } else {
                        // This is just an air block, move onto the next block don't need to wait for the next tick.
                        i--;
                    }
                    continue;
                }
                BuildBlock currentBlock = null;
                if (structure.priorityOneBlocks.size() > 0) {
                    currentBlock = structure.priorityOneBlocks.get(0);
                    structure.priorityOneBlocks.remove(0);
                } else if (structure.priorityTwoBlocks.size() > 0) {
                    currentBlock = structure.priorityTwoBlocks.get(0);
                    structure.priorityTwoBlocks.remove(0);
                } else if (structure.priorityThreeBlocks.size() > 0) {
                    currentBlock = structure.priorityThreeBlocks.get(0);
                    structure.priorityThreeBlocks.remove(0);
                } else {
                    // There are no more blocks to set.
                    structuresToRemove.add(structure);
                    break;
                }
                IBlockState state = currentBlock.getBlockState();
                BuildingMethods.ReplaceBlock(structure.world, currentBlock.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing), state);
                // After placing the initial block, set the sub-block. This needs to happen as the list isn't always in the correct order.
                if (currentBlock.getSubBlock() != null) {
                    BuildBlock subBlock = currentBlock.getSubBlock();
                    BuildingMethods.ReplaceBlock(structure.world, subBlock.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing), subBlock.getBlockState());
                }
            }
        }
        for (Structure structure : structuresToRemove) {
            for (BuildTileEntity buildTileEntity : structure.tileEntities) {
                BlockPos tileEntityPos = buildTileEntity.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing);
                TileEntity tileEntity = structure.world.getTileEntity(tileEntityPos);
                if (tileEntity == null) {
                    TileEntity.create(structure.world, buildTileEntity.getEntityDataTag());
                } else {
                    structure.world.removeTileEntity(tileEntityPos);
                    tileEntity = TileEntity.create(structure.world, buildTileEntity.getEntityDataTag());
                    structure.world.setTileEntity(tileEntityPos, tileEntity);
                    structure.world.getChunkFromBlockCoords(tileEntityPos).markDirty();
                    tileEntity.markDirty();
                    SPacketUpdateTileEntity packet = tileEntity.getUpdatePacket();
                    if (packet != null) {
                        structure.world.getMinecraftServer().getPlayerList().sendPacketToAllPlayers(tileEntity.getUpdatePacket());
                    }
                }
            }
            for (BuildEntity buildEntity : structure.entities) {
                Entity entity = EntityList.createEntityByIDFromName(buildEntity.getEntityResource(), structure.world);
                NBTTagCompound tagCompound = buildEntity.getEntityDataTag();
                BlockPos entityPos = buildEntity.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing);
                if (tagCompound != null) {
                    if (tagCompound.hasUniqueId("UUID")) {
                        tagCompound.setUniqueId("UUID", UUID.randomUUID());
                    }
                    entity.readFromNBT(tagCompound);
                }
                entity.forceSpawn = true;
                float yaw = entity.rotationYaw;
                Rotation rotation = Rotation.NONE;
                double x_axis_offset = buildEntity.entityXAxisOffset;
                double z_axis_offset = buildEntity.entityZAxisOffset;
                EnumFacing facing = entity instanceof EntityHanging ? ((EntityHanging) entity).facingDirection : structure.assumedNorth;
                double y_axis_offset = entity instanceof EntityHanging ? buildEntity.entityYAxisOffset * -1 : buildEntity.entityYAxisOffset;
                if (structure.configuration.houseFacing == structure.assumedNorth.getOpposite()) {
                    rotation = Rotation.CLOCKWISE_180;
                    x_axis_offset = x_axis_offset * -1;
                    z_axis_offset = z_axis_offset * -1;
                    facing = facing.getOpposite();
                } else if (structure.configuration.houseFacing == structure.assumedNorth.rotateY()) {
                    rotation = rotation.CLOCKWISE_90;
                    x_axis_offset = x_axis_offset * -1;
                    z_axis_offset = z_axis_offset * -1;
                    facing = facing.rotateY();
                } else if (structure.configuration.houseFacing == structure.assumedNorth.rotateYCCW()) {
                    rotation = rotation.COUNTERCLOCKWISE_90;
                    x_axis_offset = x_axis_offset * -1;
                    z_axis_offset = z_axis_offset * -1;
                    facing = facing.rotateYCCW();
                } else {
                    x_axis_offset = 0;
                    z_axis_offset = 0;
                }
                yaw = entity.getRotatedYaw(rotation);
                if (entity instanceof EntityHanging) {
                    ((EntityHanging) entity).facingDirection = facing;
                    ModEventHandler.updateEntityHangingBoundingBox((EntityHanging) entity);
                }
                entity.setPositionAndRotation(entityPos.getX() + x_axis_offset, entityPos.getY() + y_axis_offset, entityPos.getZ() + z_axis_offset, yaw, entity.rotationPitch);
                if (entity instanceof EntityHanging) {
                    ModEventHandler.updateEntityHangingBoundingBox((EntityHanging) entity);
                    Chunk chunk = structure.world.getChunkFromBlockCoords(entityPos);
                    chunk.markDirty();
                }
                structure.world.spawnEntity(entity);
            }
            // This structure is done building. Do any post-building operations.
            structure.AfterBuilding(structure.configuration, structure.world, structure.originalPos, structure.assumedNorth, entry.getKey());
            entry.getValue().remove(structure);
        }
        if (entry.getValue().size() == 0) {
            playersToRemove.add(entry.getKey());
        }
    }
    // Remove each player that has their structure's built.
    for (EntityPlayer player : playersToRemove) {
        ModEventHandler.structuresToBuild.remove(player);
    }
}
Also used : SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) BuildTileEntity(com.wuest.prefab.StructureGen.BuildTileEntity) Entity(net.minecraft.entity.Entity) BuildEntity(com.wuest.prefab.StructureGen.BuildEntity) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BuildTileEntity(com.wuest.prefab.StructureGen.BuildTileEntity) Chunk(net.minecraft.world.chunk.Chunk) SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) Rotation(net.minecraft.util.Rotation) SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) BuildTileEntity(com.wuest.prefab.StructureGen.BuildTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BuildBlock(com.wuest.prefab.StructureGen.BuildBlock) EntityHanging(net.minecraft.entity.EntityHanging) BuildEntity(com.wuest.prefab.StructureGen.BuildEntity) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) Structure(com.wuest.prefab.StructureGen.Structure) ItemBasicStructure(com.wuest.prefab.Items.Structures.ItemBasicStructure) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with EntityHanging

use of net.minecraft.entity.EntityHanging in project SpongeCommon by SpongePowered.

the class EntityTickPhaseState method handleBlockChangeWithUser.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Override
public void handleBlockChangeWithUser(@Nullable BlockChange blockChange, Transaction<BlockSnapshot> transaction, EntityTickContext context) {
    if (blockChange == BlockChange.BREAK) {
        final Entity tickingEntity = context.getSource(Entity.class).get();
        final BlockPos blockPos = VecHelper.toBlockPos(transaction.getOriginal().getPosition());
        for (EntityHanging entityHanging : EntityUtil.findHangingEntities(EntityUtil.getMinecraftWorld(tickingEntity), blockPos)) {
            if (entityHanging instanceof EntityItemFrame) {
                final EntityItemFrame frame = (EntityItemFrame) entityHanging;
                if (tickingEntity != null && !frame.isDead) {
                    frame.dropItemOrSelf(EntityUtil.toNative(tickingEntity), true);
                }
                frame.setDead();
            }
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) EntityItemFrame(net.minecraft.entity.item.EntityItemFrame) EntityHanging(net.minecraft.entity.EntityHanging) BlockPos(net.minecraft.util.math.BlockPos)

Example 4 with EntityHanging

use of net.minecraft.entity.EntityHanging in project BuildCraft by BuildCraft.

the class SchematicHanging method isAlreadyBuilt.

@Override
public boolean isAlreadyBuilt(IBuilderContext context) {
    Vec3d newPosition = new Vec3d(entityNBT.getInteger("TileX"), entityNBT.getInteger("TileY"), entityNBT.getInteger("TileZ"));
    int dir = entityNBT.getByte("Facing");
    for (Object o : context.world().loadedEntityList) {
        Entity e = (Entity) o;
        if (e instanceof EntityHanging) {
            EntityHanging h = (EntityHanging) e;
            Vec3d existingPosition = new Vec3d(h.chunkCoordX, h.chunkCoordY, h.chunkCoordZ);
            if (existingPosition.distanceTo(newPosition) < 0.1F && EnumFacing.getFront(dir) == ((EntityHanging) e).facingDirection) {
                return true;
            }
        }
    }
    return false;
}
Also used : Entity(net.minecraft.entity.Entity) SchematicEntity(buildcraft.api.blueprints.SchematicEntity) EntityHanging(net.minecraft.entity.EntityHanging) Vec3d(net.minecraft.util.math.Vec3d)

Example 5 with EntityHanging

use of net.minecraft.entity.EntityHanging in project BuildCraft by BuildCraft.

the class SchematicEntityDefault method init.

@Override
public void init(SchematicEntityContext context) {
    entityNbt = context.entity.serializeNBT();
    pos = context.entity.getPositionVector().subtract(new Vec3d(context.basePos));
    if (context.entity instanceof EntityHanging) {
        EntityHanging entityHanging = (EntityHanging) context.entity;
        hangingPos = entityHanging.getHangingPosition().subtract(context.basePos);
        hangingFacing = entityHanging.getHorizontalFacing();
    } else {
        hangingPos = new BlockPos(pos);
        hangingFacing = EnumFacing.NORTH;
    }
}
Also used : EntityHanging(net.minecraft.entity.EntityHanging) BlockPos(net.minecraft.util.math.BlockPos) Vec3d(net.minecraft.util.math.Vec3d)

Aggregations

EntityHanging (net.minecraft.entity.EntityHanging)8 BlockPos (net.minecraft.util.math.BlockPos)5 ItemStack (net.minecraft.item.ItemStack)4 Entity (net.minecraft.entity.Entity)2 Vec3d (net.minecraft.util.math.Vec3d)2 EntityWallItem (net.tropicraft.core.common.entity.placeable.EntityWallItem)2 SchematicEntity (buildcraft.api.blueprints.SchematicEntity)1 ItemBasicStructure (com.wuest.prefab.Items.Structures.ItemBasicStructure)1 BuildBlock (com.wuest.prefab.StructureGen.BuildBlock)1 BuildEntity (com.wuest.prefab.StructureGen.BuildEntity)1 BuildTileEntity (com.wuest.prefab.StructureGen.BuildTileEntity)1 Structure (com.wuest.prefab.StructureGen.Structure)1 ArrayList (java.util.ArrayList)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityItemFrame (net.minecraft.entity.item.EntityItemFrame)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 SPacketUpdateTileEntity (net.minecraft.network.play.server.SPacketUpdateTileEntity)1 TileEntity (net.minecraft.tileentity.TileEntity)1 EnumFacing (net.minecraft.util.EnumFacing)1