Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/main/java/ch/njol/skript/conditions/CondIsSleeping.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package ch.njol.skript.conditions;

import org.bukkit.entity.Player;

import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import org.bukkit.entity.LivingEntity;

@Name("Is Sleeping")
@Description("Checks whether a player is sleeping.")
@Description("Checks whether an entity is sleeping.")
@Examples({
"# cut your enemies' throats in their sleep >=)",
"on attack:",
"\tattacker is holding a sword",
"\tvictim is sleeping",
"\tincrease the damage by 1000"
"if player is sleeping:",
"\tmake player wake up without spawn location update",
"",
"if last spawned fox is sleeping:",
"\tmake last spawned fox stop sleeping"
})
@Since("1.4.4")
public class CondIsSleeping extends PropertyCondition<Player> {
@Since("1.4.4, INSERT VERSION (living entities)")
public class CondIsSleeping extends PropertyCondition<LivingEntity> {

static {
register(CondIsSleeping.class, "sleeping", "players");
register(CondIsSleeping.class, "sleeping", "livingentities");
}

@Override
public boolean check(Player player) {
return player.isSleeping();
public boolean check(LivingEntity entity) {
return entity.isSleeping();
}

@Override
Expand Down
138 changes: 138 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffWakeupSleep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.SyntaxStringBuilder;
import ch.njol.skript.util.Direction;
import ch.njol.util.Kleenean;
import org.bukkit.Location;
import org.bukkit.entity.*;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;

@Name("Wake And Sleep")
@Description({
"Make bats and foxes sleep or wake up.",
"Make villagers sleep by providing a location of a bed.",
"Make players sleep by providing a location of a bed. "
+ "Using 'with force' will bypass \"nearby monsters\" ,the max distance, allowing players to sleep even if the bed "
+ "is far away, and lets players sleep in the nether and end. "
+ "Does not work if the location of the bed is not in the world the player is currently in.",
"Using 'without spawn location update' will make players wake up without setting their spawn location to the bed."
})
@Examples({
"make {_fox} go to sleep",
"make {_bat} stop sleeping",
"make {_villager} start sleeping at location(0, 0, 0)",
"make player go to sleep at location(0, 0, 0) with force",
"make player wake up without spawn location update"
})
@Since("INSERT VERSION")
public class EffWakeupSleep extends Effect implements SyntaxRuntimeErrorProducer {

static {
Skript.registerEffect(EffWakeupSleep.class,
Comment thread
Absolutionism marked this conversation as resolved.
"make %livingentities% (start sleeping|[go to] sleep) [%-direction% %-location%]",
"force %livingentities% to (start sleeping|[go to] sleep) [%-direction% %-location%]",
"make %players% (start sleeping|[go to] sleep) %direction% %location% (force:with force)",
"force %players% to (start sleeping|[go to] sleep) %direction% %location% (force:with force)",
"make %livingentities% (stop sleeping|wake up)",
"force %livingentities% to (stop sleeping|wake up)",
"make %players% (stop sleeping|wake up) (spawn:without spawn [location] update)",
"force %players% to (stop sleeping|wake up) (spawn:without spawn [location] update)");
}

private Expression<LivingEntity> entities;
private @Nullable Expression<Location> location;
private boolean sleep;
private boolean force;
private boolean setSpawn;
private Node node;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
entities = (Expression<LivingEntity>) exprs[0];
sleep = matchedPattern <= 3;
force = parseResult.hasTag("force");
setSpawn = !parseResult.hasTag("spawn");
if (sleep && exprs[1] != null) {
if (exprs[2] == null)
return false;
//noinspection unchecked
this.location = Direction.combine((Expression<Direction>) exprs[1], (Expression<Location>) exprs[2]);
}
node = getParser().getNode();
return true;
}

@Override
protected void execute(Event event) {
Location location = null;
if (this.location != null)
location = this.location.getSingle(event);
boolean failed = false;
for (LivingEntity entity : entities.getArray(event)) {
if (entity instanceof Bat bat) {
bat.setAwake(!sleep);
} else if (entity instanceof Villager villager) {
if (sleep && location == null) {
failed = true;
continue;
}
if (!sleep) {
villager.wakeup();
} else {
villager.sleep(location);
}
} else if (entity instanceof Fox fox) {
fox.setSleeping(sleep);
} else if (entity instanceof HumanEntity humanEntity) {
if (sleep && location == null) {
failed = true;
continue;
}
if (!sleep) {
humanEntity.wakeup(setSpawn);
} else {
humanEntity.sleep(location, force);
Comment thread
Absolutionism marked this conversation as resolved.
}
}
}
if (failed)
warning("The provided location is not set. This effect will have no effect for villagers and players.");
}

@Override
public Node getNode() {
return node;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
builder.append("make", entities);
if (sleep) {
builder.append("start");
} else {
builder.append("stop");
}
Comment thread
Absolutionism marked this conversation as resolved.
builder.append("sleeping");
if (location != null)
builder.append(location);
if (force)
builder.append("with force");
if (!setSpawn)
builder.append("without spawn location update");
return builder.toString();
}

}
5 changes: 5 additions & 0 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ public class SimpleEvents {
.since("2.10");
}

Skript.registerEvent("Bat Toggle Sleep", SimpleEvent.class, BatToggleSleepEvent.class, "bat toggle sleep")
.description("Called when a bat attempts to go to sleep or wakes up.")
.examples("on bat toggle sleep:")
.since("INSERT VERSION");

// WorldBorder Events
if (Skript.classExists("io.papermc.paper.event.world.border.WorldBorderEvent")) {
Skript.registerEvent("World Border Bounds Change", SimpleEvent.class, WorldBorderBoundsChangeEvent.class, "world[ ]border [bounds] chang(e|ing)")
Expand Down
30 changes: 30 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffWakeupSleep.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# Bats require time to go to sleep

test "fox wakeup and sleep":
spawn a fox at test-location:
set {_entity} to entity
make {_entity} go to sleep
assert {_entity} is sleeping with "Fox should be sleeping"
make {_entity} wake up
assert {_entity} is not sleeping with "Fox should be awake"
clear entity within {_entity}

test "villager wakeup and sleep":
set {_old} to block at test-block
set block at test-block to red bed
spawn a villager at test-location:
set {_entity} to entity
make {_entity} go to sleep at test-block
assert {_entity} is sleeping with "Villager should be sleeping"
make {_entity} wake up
assert {_entity} is not sleeping with "Villager should be awake"
clear entity within {_entity}
set block at test-block to {_old}

test "invalid entities":
spawn a cow, a sheep and a pig at test-location:
add entity to {_entities::*}
make {_entities::*} go to sleep
assert {_entities::*} is not sleeping with "Invalid entities should not be able to sleep"
clear entities within {_entities::*}