Skip to content

Commit 5f2cc81

Browse files
Absolutionismsovdeetherenkarakal
authored
Bat Support (#7362)
* Initial Commit * Requested Changes * Requested Changes * Fix Malformed Pattern * Requested Changes * Runtime warning * Update Runtime Warning * Requested Changes * warning update --------- Co-authored-by: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> Co-authored-by: Eren <67760502+erenkarakal@users.noreply.github.com>
1 parent de0ee5e commit 5f2cc81

4 files changed

Lines changed: 185 additions & 13 deletions

File tree

src/main/java/ch/njol/skript/conditions/CondIsSleeping.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
package ch.njol.skript.conditions;
22

3-
import org.bukkit.entity.Player;
4-
53
import ch.njol.skript.conditions.base.PropertyCondition;
64
import ch.njol.skript.doc.Description;
75
import ch.njol.skript.doc.Examples;
86
import ch.njol.skript.doc.Name;
97
import ch.njol.skript.doc.Since;
8+
import org.bukkit.entity.LivingEntity;
109

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

2322
static {
24-
register(CondIsSleeping.class, "sleeping", "players");
23+
register(CondIsSleeping.class, "sleeping", "livingentities");
2524
}
2625

2726
@Override
28-
public boolean check(Player player) {
29-
return player.isSleeping();
27+
public boolean check(LivingEntity entity) {
28+
return entity.isSleeping();
3029
}
3130

3231
@Override
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package ch.njol.skript.effects;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.config.Node;
5+
import ch.njol.skript.doc.Description;
6+
import ch.njol.skript.doc.Examples;
7+
import ch.njol.skript.doc.Name;
8+
import ch.njol.skript.doc.Since;
9+
import ch.njol.skript.lang.Effect;
10+
import ch.njol.skript.lang.Expression;
11+
import ch.njol.skript.lang.SkriptParser.ParseResult;
12+
import ch.njol.skript.lang.SyntaxStringBuilder;
13+
import ch.njol.skript.util.Direction;
14+
import ch.njol.util.Kleenean;
15+
import org.bukkit.Location;
16+
import org.bukkit.entity.*;
17+
import org.bukkit.event.Event;
18+
import org.jetbrains.annotations.Nullable;
19+
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;
20+
21+
@Name("Wake And Sleep")
22+
@Description({
23+
"Make bats and foxes sleep or wake up.",
24+
"Make villagers sleep by providing a location of a bed.",
25+
"Make players sleep by providing a location of a bed. "
26+
+ "Using 'with force' will bypass \"nearby monsters\" ,the max distance, allowing players to sleep even if the bed "
27+
+ "is far away, and lets players sleep in the nether and end. "
28+
+ "Does not work if the location of the bed is not in the world the player is currently in.",
29+
"Using 'without spawn location update' will make players wake up without setting their spawn location to the bed."
30+
})
31+
@Examples({
32+
"make {_fox} go to sleep",
33+
"make {_bat} stop sleeping",
34+
"make {_villager} start sleeping at location(0, 0, 0)",
35+
"make player go to sleep at location(0, 0, 0) with force",
36+
"make player wake up without spawn location update"
37+
})
38+
@Since("INSERT VERSION")
39+
public class EffWakeupSleep extends Effect implements SyntaxRuntimeErrorProducer {
40+
41+
static {
42+
Skript.registerEffect(EffWakeupSleep.class,
43+
"make %livingentities% (start sleeping|[go to] sleep) [%-direction% %-location%]",
44+
"force %livingentities% to (start sleeping|[go to] sleep) [%-direction% %-location%]",
45+
"make %players% (start sleeping|[go to] sleep) %direction% %location% (force:with force)",
46+
"force %players% to (start sleeping|[go to] sleep) %direction% %location% (force:with force)",
47+
"make %livingentities% (stop sleeping|wake up)",
48+
"force %livingentities% to (stop sleeping|wake up)",
49+
"make %players% (stop sleeping|wake up) (spawn:without spawn [location] update)",
50+
"force %players% to (stop sleeping|wake up) (spawn:without spawn [location] update)");
51+
}
52+
53+
private Expression<LivingEntity> entities;
54+
private @Nullable Expression<Location> location;
55+
private boolean sleep;
56+
private boolean force;
57+
private boolean setSpawn;
58+
private Node node;
59+
60+
@Override
61+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
62+
//noinspection unchecked
63+
entities = (Expression<LivingEntity>) exprs[0];
64+
sleep = matchedPattern <= 3;
65+
force = parseResult.hasTag("force");
66+
setSpawn = !parseResult.hasTag("spawn");
67+
if (sleep && exprs[1] != null) {
68+
if (exprs[2] == null)
69+
return false;
70+
//noinspection unchecked
71+
this.location = Direction.combine((Expression<Direction>) exprs[1], (Expression<Location>) exprs[2]);
72+
}
73+
node = getParser().getNode();
74+
return true;
75+
}
76+
77+
@Override
78+
protected void execute(Event event) {
79+
Location location = null;
80+
if (this.location != null)
81+
location = this.location.getSingle(event);
82+
boolean failed = false;
83+
for (LivingEntity entity : entities.getArray(event)) {
84+
if (entity instanceof Bat bat) {
85+
bat.setAwake(!sleep);
86+
} else if (entity instanceof Villager villager) {
87+
if (sleep && location == null) {
88+
failed = true;
89+
continue;
90+
}
91+
if (!sleep) {
92+
villager.wakeup();
93+
} else {
94+
villager.sleep(location);
95+
}
96+
} else if (entity instanceof Fox fox) {
97+
fox.setSleeping(sleep);
98+
} else if (entity instanceof HumanEntity humanEntity) {
99+
if (sleep && location == null) {
100+
failed = true;
101+
continue;
102+
}
103+
if (!sleep) {
104+
humanEntity.wakeup(setSpawn);
105+
} else {
106+
humanEntity.sleep(location, force);
107+
}
108+
}
109+
}
110+
if (failed)
111+
warning("The provided location is not set. This effect will have no effect for villagers and players.");
112+
}
113+
114+
@Override
115+
public Node getNode() {
116+
return node;
117+
}
118+
119+
@Override
120+
public String toString(@Nullable Event event, boolean debug) {
121+
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
122+
builder.append("make", entities);
123+
if (sleep) {
124+
builder.append("start");
125+
} else {
126+
builder.append("stop");
127+
}
128+
builder.append("sleeping");
129+
if (location != null)
130+
builder.append(location);
131+
if (force)
132+
builder.append("with force");
133+
if (!setSpawn)
134+
builder.append("without spawn location update");
135+
return builder.toString();
136+
}
137+
138+
}

src/main/java/ch/njol/skript/events/SimpleEvents.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,11 @@ public class SimpleEvents {
766766
.since("2.10");
767767
}
768768

769+
Skript.registerEvent("Bat Toggle Sleep", SimpleEvent.class, BatToggleSleepEvent.class, "bat toggle sleep")
770+
.description("Called when a bat attempts to go to sleep or wakes up.")
771+
.examples("on bat toggle sleep:")
772+
.since("INSERT VERSION");
773+
769774
// WorldBorder Events
770775
if (Skript.classExists("io.papermc.paper.event.world.border.WorldBorderEvent")) {
771776
Skript.registerEvent("World Border Bounds Change", SimpleEvent.class, WorldBorderBoundsChangeEvent.class, "world[ ]border [bounds] chang(e|ing)")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
# Bats require time to go to sleep
3+
4+
test "fox wakeup and sleep":
5+
spawn a fox at test-location:
6+
set {_entity} to entity
7+
make {_entity} go to sleep
8+
assert {_entity} is sleeping with "Fox should be sleeping"
9+
make {_entity} wake up
10+
assert {_entity} is not sleeping with "Fox should be awake"
11+
clear entity within {_entity}
12+
13+
test "villager wakeup and sleep":
14+
set {_old} to block at test-block
15+
set block at test-block to red bed
16+
spawn a villager at test-location:
17+
set {_entity} to entity
18+
make {_entity} go to sleep at test-block
19+
assert {_entity} is sleeping with "Villager should be sleeping"
20+
make {_entity} wake up
21+
assert {_entity} is not sleeping with "Villager should be awake"
22+
clear entity within {_entity}
23+
set block at test-block to {_old}
24+
25+
test "invalid entities":
26+
spawn a cow, a sheep and a pig at test-location:
27+
add entity to {_entities::*}
28+
make {_entities::*} go to sleep
29+
assert {_entities::*} is not sleeping with "Invalid entities should not be able to sleep"
30+
clear entities within {_entities::*}

0 commit comments

Comments
 (0)