|
| 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 | +} |
0 commit comments