-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathTestPlugin.java
More file actions
79 lines (67 loc) · 3.42 KB
/
TestPlugin.java
File metadata and controls
79 lines (67 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package io.papermc.testplugin;
import java.util.Comparator;
import java.util.List;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
public final class TestPlugin extends JavaPlugin implements Listener {
@Override
public void onEnable() {
this.getServer().getPluginManager().registerEvents(this, this);
this.registerSleepTestCommands();
// io.papermc.testplugin.brigtests.Registration.registerViaOnEnable(this);
}
private void registerSleepTestCommands() {
this.getServer().getCommandMap().register("test-plugin", new BukkitCommand("testsleep", "Sleep the nearest living entity at its current location", "/testsleep", List.of()) {
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only players can use this command.");
return true;
}
LivingEntity target = findNearestLivingEntity(player);
if (target == null) {
player.sendMessage("No living entity found nearby.");
return true;
}
boolean slept = target.sleep(target.getLocation());
player.sendMessage("testsleep: " + target.getType() + " -> " + slept + " (sleeping=" + target.isSleeping() + ")");
return true;
}
});
this.getServer().getCommandMap().register("test-plugin", new BukkitCommand("testwakeup", "Wake the nearest sleeping living entity", "/testwakeup", List.of()) {
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only players can use this command.");
return true;
}
LivingEntity target = findNearestSleepingLivingEntity(player);
if (target == null) {
player.sendMessage("No sleeping living entity found nearby.");
return true;
}
target.wakeup();
player.sendMessage("testwakeup: " + target.getType() + " -> sleeping=" + target.isSleeping());
return true;
}
});
}
private static LivingEntity findNearestLivingEntity(Player player) {
return player.getLocation().getNearbyEntitiesByType(LivingEntity.class, 8.0, entity -> !(entity instanceof Player))
.stream()
.min(Comparator.comparingDouble(entity -> entity.getLocation().distanceSquared(player.getLocation())))
.orElse(null);
}
private static LivingEntity findNearestSleepingLivingEntity(Player player) {
return player.getLocation().getNearbyEntitiesByType(LivingEntity.class, 8.0, entity -> !(entity instanceof Player) && entity.isSleeping())
.stream()
.min(Comparator.comparingDouble(entity -> entity.getLocation().distanceSquared(player.getLocation())))
.orElse(null);
}
}