Skip to content

Commit 93fc38e

Browse files
committed
Add Teleport request (/tpa) events API
1 parent dba274a commit 93fc38e

File tree

5 files changed

+181
-49
lines changed

5 files changed

+181
-49
lines changed

eternalcore-api-example/src/main/java/com/eternalcode/example/EternalCoreApiExamplePlugin.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.eternalcode.example.feature.randomteleport.ApiRandomTeleportCommand;
1616
import com.eternalcode.example.feature.randomteleport.ApiRandomTeleportListener;
1717
import com.eternalcode.example.feature.spawn.ApiSpawnCommand;
18+
import com.eternalcode.example.feature.teleportrequest.ApiTeleportRequestListener;
1819
import dev.rollczi.litecommands.LiteCommands;
1920
import dev.rollczi.litecommands.bukkit.LiteBukkitFactory;
2021
import dev.rollczi.litecommands.bukkit.LiteBukkitMessages;
@@ -38,32 +39,33 @@ public void onEnable() {
3839
EternalCoreApi provide = EternalCoreApiProvider.provide();
3940

4041
this.liteCommands = LiteBukkitFactory.builder(FALLBACK_PREFIX, this, server)
41-
.message(LiteBukkitMessages.PLAYER_ONLY, input -> "You must be a player to execute this command!")
42-
.message(LiteBukkitMessages.PLAYER_NOT_FOUND, input -> "Player not found!")
43-
.message(LiteMessages.MISSING_PERMISSIONS, input -> "You don't have permission to execute this command!")
42+
.message(LiteBukkitMessages.PLAYER_ONLY, input -> "You must be a player to execute this command!")
43+
.message(LiteBukkitMessages.PLAYER_NOT_FOUND, input -> "Player not found!")
44+
.message(LiteMessages.MISSING_PERMISSIONS,
45+
input -> "You don't have permission to execute this command!")
4446

45-
.commands(
46-
new ApiAfkCommand(provide.getAfkService()),
47-
new ApiIgnoreCommand(provide.getIgnoreService()),
48-
new ApiJailCommand(provide.getJailService()),
49-
new ApiRandomTeleportCommand(provide.getRandomTeleportService()),
50-
new ApiSpawnCommand(provide.getSpawnService()),
51-
new ApiRandomTeleportCommand(provide.getRandomTeleportService()),
52-
new ApiHomeCommand(provide.getHomeService())
53-
)
47+
.commands(
48+
new ApiAfkCommand(provide.getAfkService()),
49+
new ApiIgnoreCommand(provide.getIgnoreService()),
50+
new ApiJailCommand(provide.getJailService()),
51+
new ApiRandomTeleportCommand(provide.getRandomTeleportService()),
52+
new ApiSpawnCommand(provide.getSpawnService()),
53+
new ApiRandomTeleportCommand(provide.getRandomTeleportService()),
54+
new ApiHomeCommand(provide.getHomeService()))
5455

55-
.build();
56+
.build();
5657

5758
Stream.of(
58-
new ApiAfkListener(),
59-
new CatBoyListener(provide.getCatboyService()),
60-
new ApiRandomTeleportListener(provide.getRandomTeleportService()),
61-
new ApiPrivateChatListener(server),
62-
new ApiRandomTeleportListener(provide.getRandomTeleportService()),
63-
new ApiHomeListener(server),
64-
new ApiJailListener(server),
65-
new ApiIgnoreListener()
66-
).forEach(listener -> server.getPluginManager().registerEvents(listener, this));
59+
new ApiAfkListener(),
60+
new CatBoyListener(provide.getCatboyService()),
61+
new ApiRandomTeleportListener(provide.getRandomTeleportService()),
62+
new ApiPrivateChatListener(server),
63+
new ApiRandomTeleportListener(provide.getRandomTeleportService()),
64+
new ApiHomeListener(server),
65+
new ApiJailListener(server),
66+
new ApiIgnoreListener(),
67+
new ApiTeleportRequestListener())
68+
.forEach(listener -> server.getPluginManager().registerEvents(listener, this));
6769
}
6870

6971
@Override
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.eternalcode.core.feature.teleportrequest;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.Cancellable;
5+
import org.bukkit.event.Event;
6+
import org.bukkit.event.HandlerList;
7+
8+
/**
9+
* Called before a teleport request is sent (via /tpa or /tpahere).
10+
* If cancelled, the teleport request will not be created.
11+
*/
12+
public class PreTeleportRequestEvent extends Event implements Cancellable {
13+
14+
private static final HandlerList HANDLER_LIST = new HandlerList();
15+
16+
private final Player sender;
17+
private final Player target;
18+
private boolean cancelled;
19+
20+
public PreTeleportRequestEvent(Player sender, Player target) {
21+
super(false);
22+
23+
this.sender = sender;
24+
this.target = target;
25+
}
26+
27+
public static HandlerList getHandlerList() {
28+
return HANDLER_LIST;
29+
}
30+
31+
public Player getSender() {
32+
return this.sender;
33+
}
34+
35+
public Player getTarget() {
36+
return this.target;
37+
}
38+
39+
@Override
40+
public boolean isCancelled() {
41+
return this.cancelled;
42+
}
43+
44+
@Override
45+
public void setCancelled(boolean cancel) {
46+
this.cancelled = cancel;
47+
}
48+
49+
@Override
50+
public HandlerList getHandlers() {
51+
return HANDLER_LIST;
52+
}
53+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.eternalcode.core.feature.teleportrequest;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.Event;
5+
import org.bukkit.event.HandlerList;
6+
7+
/**
8+
* Called after a teleport request has been successfully created (via /tpa or
9+
* /tpahere).
10+
* This event is informational and cannot be cancelled.
11+
*/
12+
public class TeleportRequestEvent extends Event {
13+
14+
private static final HandlerList HANDLER_LIST = new HandlerList();
15+
16+
private final Player sender;
17+
private final Player target;
18+
19+
public TeleportRequestEvent(Player sender, Player target) {
20+
super(false);
21+
22+
this.sender = sender;
23+
this.target = target;
24+
}
25+
26+
public static HandlerList getHandlerList() {
27+
return HANDLER_LIST;
28+
}
29+
30+
public Player getSender() {
31+
return this.sender;
32+
}
33+
34+
public Player getTarget() {
35+
return this.target;
36+
}
37+
38+
@Override
39+
public HandlerList getHandlers() {
40+
return HANDLER_LIST;
41+
}
42+
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TpaCommand.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.eternalcode.core.feature.teleportrequest;
22

33
import com.eternalcode.annotations.scan.command.DescriptionDocs;
4+
import com.eternalcode.core.event.EventCaller;
45
import com.eternalcode.core.feature.ignore.IgnoreService;
56
import com.eternalcode.core.injector.annotations.Inject;
67
import com.eternalcode.core.notice.NoticeService;
@@ -19,12 +20,19 @@ class TpaCommand {
1920
private final TeleportRequestService requestService;
2021
private final IgnoreService ignoreService;
2122
private final NoticeService noticeService;
23+
private final EventCaller eventCaller;
2224

2325
@Inject
24-
TpaCommand(TeleportRequestService requestService, IgnoreService ignoreService, NoticeService noticeService) {
26+
TpaCommand(
27+
TeleportRequestService requestService,
28+
IgnoreService ignoreService,
29+
NoticeService noticeService,
30+
EventCaller eventCaller
31+
) {
2532
this.requestService = requestService;
2633
this.ignoreService = ignoreService;
2734
this.noticeService = noticeService;
35+
this.eventCaller = eventCaller;
2836
}
2937

3038
@Execute
@@ -42,6 +50,12 @@ void execute(@Sender Player player, @Arg Player target) {
4250
return;
4351
}
4452

53+
PreTeleportRequestEvent preEvent = this.eventCaller.callEvent(new PreTeleportRequestEvent(player, target));
54+
55+
if (preEvent.isCancelled()) {
56+
return;
57+
}
58+
4559
this.noticeService
4660
.create()
4761
.player(player.getUniqueId())
@@ -67,6 +81,7 @@ void execute(@Sender Player player, @Arg Player target) {
6781
.send();
6882

6983
this.requestService.createRequest(player.getUniqueId(), target.getUniqueId());
84+
this.eventCaller.callEvent(new TeleportRequestEvent(player, target));
7085
});
7186
}
7287

eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TpaHereCommand.java

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.eternalcode.core.feature.teleportrequest.self;
22

33
import com.eternalcode.annotations.scan.command.DescriptionDocs;
4+
import com.eternalcode.core.event.EventCaller;
45
import com.eternalcode.core.feature.ignore.IgnoreService;
6+
import com.eternalcode.core.feature.teleportrequest.PreTeleportRequestEvent;
7+
import com.eternalcode.core.feature.teleportrequest.TeleportRequestEvent;
58
import com.eternalcode.core.injector.annotations.Inject;
69
import com.eternalcode.core.notice.NoticeService;
710
import dev.rollczi.litecommands.annotations.argument.Arg;
811
import dev.rollczi.litecommands.annotations.command.Command;
912
import dev.rollczi.litecommands.annotations.context.Sender;
1013
import dev.rollczi.litecommands.annotations.execute.Execute;
1114
import dev.rollczi.litecommands.annotations.permission.Permission;
12-
import org.bukkit.entity.Player;
13-
1415
import java.util.concurrent.CompletableFuture;
16+
import org.bukkit.entity.Player;
1517

1618
@Command(name = "tpahere")
1719
@Permission("eternalcore.tpahere")
@@ -20,19 +22,26 @@ class TpaHereCommand {
2022
private final TeleportHereRequestService requestService;
2123
private final IgnoreService ignoreService;
2224
private final NoticeService noticeService;
25+
private final EventCaller eventCaller;
2326

2427
@Inject
25-
TpaHereCommand(TeleportHereRequestService requestService, IgnoreService ignoreService, NoticeService noticeService) {
28+
TpaHereCommand(
29+
TeleportHereRequestService requestService,
30+
IgnoreService ignoreService,
31+
NoticeService noticeService,
32+
EventCaller eventCaller
33+
) {
2634
this.requestService = requestService;
2735
this.ignoreService = ignoreService;
2836
this.noticeService = noticeService;
37+
this.eventCaller = eventCaller;
2938
}
3039

3140
@Execute
3241
@DescriptionDocs(description = "Send teleport request to player to teleport to you", arguments = "<player>")
3342
void execute(@Sender Player sender, @Arg Player target) {
3443
if (sender.equals(target)) {
35-
this.noticeService.player(sender.getUniqueId() , translation -> translation.tpa().tpaSelfMessage());
44+
this.noticeService.player(sender.getUniqueId(), translation -> translation.tpa().tpaSelfMessage());
3645

3746
return;
3847
}
@@ -43,34 +52,41 @@ void execute(@Sender Player sender, @Arg Player target) {
4352
return;
4453
}
4554

55+
PreTeleportRequestEvent preEvent = this.eventCaller.callEvent(new PreTeleportRequestEvent(sender, target));
56+
57+
if (preEvent.isCancelled()) {
58+
return;
59+
}
60+
4661
this.isIgnoring(target, sender).thenAccept(isIgnoring -> {
47-
if (isIgnoring) {
48-
this.noticeService.create()
49-
.player(sender.getUniqueId())
50-
.notice(translation -> translation.tpa().tpaTargetIgnoresYou())
51-
.placeholder("{PLAYER}", target.getName())
52-
.send();
53-
return;
54-
}
55-
56-
this.noticeService
57-
.create()
58-
.player(sender.getUniqueId())
59-
.notice(translation -> translation.tpa().tpaHereSent())
60-
.placeholder("{PLAYER}", target.getName())
61-
.send();
62-
63-
this.noticeService.create()
62+
if (isIgnoring) {
63+
this.noticeService.create()
64+
.player(sender.getUniqueId())
65+
.notice(translation -> translation.tpa().tpaTargetIgnoresYou())
66+
.placeholder("{PLAYER}", target.getName())
67+
.send();
68+
return;
69+
}
70+
71+
this.noticeService
72+
.create()
73+
.player(sender.getUniqueId())
74+
.notice(translation -> translation.tpa().tpaHereSent())
75+
.placeholder("{PLAYER}", target.getName())
76+
.send();
77+
78+
this.noticeService.create()
6479
.player(target.getUniqueId())
6580
.notice(translation -> translation.tpa().tpaHereReceived())
6681
.placeholder("{PLAYER}", sender.getName())
6782
.send();
6883

69-
this.requestService.createRequest(sender.getUniqueId(), target.getUniqueId());
84+
this.requestService.createRequest(sender.getUniqueId(), target.getUniqueId());
85+
this.eventCaller.callEvent(new TeleportRequestEvent(sender, target));
7086
});
7187
}
7288

73-
@Execute(name = "-all", aliases = { "*" })
89+
@Execute(name = "-all", aliases = {"*"})
7490
@Permission("eternalcore.tpahere.all")
7591
@DescriptionDocs(description = "Send teleport request to all online players to teleport to you")
7692
void executeAll(@Sender Player sender) {
@@ -83,6 +99,12 @@ void executeAll(@Sender Player sender) {
8399
continue;
84100
}
85101

102+
PreTeleportRequestEvent preEvent = this.eventCaller.callEvent(new PreTeleportRequestEvent(sender, target));
103+
104+
if (preEvent.isCancelled()) {
105+
continue;
106+
}
107+
86108
this.isIgnoring(target, sender).thenAccept(isIgnoring -> {
87109
if (isIgnoring) {
88110
return;
@@ -95,7 +117,7 @@ void executeAll(@Sender Player sender) {
95117
.send();
96118

97119
this.requestService.createRequest(sender.getUniqueId(), target.getUniqueId());
98-
120+
this.eventCaller.callEvent(new TeleportRequestEvent(sender, target));
99121
});
100122
}
101123

@@ -105,8 +127,6 @@ void executeAll(@Sender Player sender) {
105127
.send();
106128
}
107129

108-
109-
110130
private CompletableFuture<Boolean> isIgnoring(Player target, Player sender) {
111131
return this.ignoreService.isIgnored(target.getUniqueId(), sender.getUniqueId());
112132
}

0 commit comments

Comments
 (0)