At a basic level, nighttime is just a darker version of the day, but your passive NPCs (pigs, sheep, etc) stop spawning in most areas (since they only spawn where there is light) and aggressive ones (zombies, skeletons, spiders, etc) begin to spawn.
Dealing with each type of aggressive mob will take different strategies. You should read up on each type of mob at the Minecraft Wiki.
Generally-speaking, I'd suggest having a stone or iron sword (the 3rd and 2nd best weapons in the game according to damage they deal). A bow and arrows are also great since they allow you to fight at range, but arrows aren't as easy to make. If you go this route, remember that you can retrieve arrows after you fire them. This will cut down on how often you have to replenish your supply with new ones.
Armor will help, especially when you begin dealing with the more difficult mobs such as Creepers, which start hissing and explode when they get close. Taking some food for emergency healing is also a wise idea.
EDIT- In the current version of the game, food does not instantly heal you. Please keep this in mind while trying to survive.
So two things. First, in your second example you have spaces in your target selector, which isn't allowed. Instead of /tp @p[dx=#, dy=#, dz=#] # # #
you should have /tp @p[dx=#,dy=#,dz=#] # # #
.
But this belies the bigger problem with that example in that in order to use dx
, dy
, and dz
(which represent the lengths of the sides of a box), you need x
, y
, and z
, which marks the corner of the box, so the command should be /tp @p[x=#,y=#,z=#,dx=#,dy=#,dz=#] # # #
. (You technically don't need the x
, y
, and z
, but that makes the command block be the corner, and your command blocks are generally to far away from the parts of the map you're interested in.)
You can also use the r
selector variable with the x
, y
, and z
selector variables. In your example with the r
, the selection area is centred on the command block that's running it, not on the player that you're targeting. In that case, the command would be /tp @p[x=#,y=#,z=#,r=#] # # #
.
Best Answer
It might seem like the best idea to use a daylight sensor for this, but that has lots of little problems, like being visible when placed at the player's location, potentially overwriting blocks (like grass), not being able to check its output level directly, …
Instead I would suggest simply checking for air in a column from the player's head to the build limit.
First make sure that you have a column of air at a known position, for example 0 0 (preferably in the spawn chunks):
Whenever you now want to check if everything above the player is air, you just need to compare with partially relative coordinates:
The downside of this is that all blocks except air protect a player, including glass, buttons, signs, …