Skip to content

Commit ac0a158

Browse files
committed
WIP permission for back on death.
1 parent 6f7fb56 commit ac0a158

File tree

11 files changed

+262
-97
lines changed

11 files changed

+262
-97
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.eternalcode.core.configuration.migrations;
2+
3+
import eu.okaeri.configs.migrate.builtin.NamedMigration;
4+
5+
import static eu.okaeri.configs.migrate.ConfigMigrationDsl.move;
6+
7+
public class Migration_0010_Move_back_to_dedicated_section extends NamedMigration {
8+
Migration_0010_Move_back_to_dedicated_section() {
9+
super(
10+
"Improve homes config",
11+
move("teleport.teleportedToLastLocation", "back.lastLocationNoExist"),
12+
move("teleport.teleportedSpecifiedPlayerLastLocation", "back.teleportedSpecifiedPlayerLastLocation"),
13+
move("teleport.lastLocationNoExist", "back.lastLocationNoExist")
14+
);
15+
}
16+
}
17+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.eternalcode.core.feature.back;
2+
3+
import com.eternalcode.annotations.scan.command.DescriptionDocs;
4+
import com.eternalcode.core.injector.annotations.Inject;
5+
import com.eternalcode.core.notice.NoticeService;
6+
import com.eternalcode.core.viewer.Viewer;
7+
import dev.rollczi.litecommands.annotations.argument.Arg;
8+
import dev.rollczi.litecommands.annotations.command.Command;
9+
import dev.rollczi.litecommands.annotations.context.Sender;
10+
import dev.rollczi.litecommands.annotations.execute.Execute;
11+
import dev.rollczi.litecommands.annotations.permission.Permission;
12+
import org.bukkit.entity.Player;
13+
14+
import java.util.Optional;
15+
16+
@Command(name = "back")
17+
public class BackCommand {
18+
19+
private static final String PERMISSION_BACK_ON_DEATH = "eternalcore.back.ondeath";
20+
21+
private final BackService backService;
22+
private final NoticeService noticeService;
23+
24+
@Inject
25+
public BackCommand(BackService backService, NoticeService noticeService) {
26+
this.backService = backService;
27+
this.noticeService = noticeService;
28+
}
29+
30+
@Execute
31+
@Permission("eternalcore.back")
32+
@DescriptionDocs(description = "Teleport to last location")
33+
public void execute(@Sender Player player) {
34+
Optional<BackService.BackLocation> backLocation = this.backService.getBackLocation(player.getUniqueId());
35+
36+
if (backLocation.isEmpty()) {
37+
this.noticeService.player(player.getUniqueId(), translation -> translation.back().lastLocationNoExist());
38+
return;
39+
}
40+
41+
BackService.BackLocation location = backLocation.get();
42+
43+
if (location.isFromDeath() && !player.hasPermission(PERMISSION_BACK_ON_DEATH)) {
44+
this.noticeService.player(player.getUniqueId(), translation -> translation.back().noPermissionBackOnDeath());
45+
return;
46+
}
47+
48+
this.backService.teleportBack(player, location.location());
49+
this.noticeService.player(player.getUniqueId(), translation -> translation.back().teleportedToLastLocation());
50+
}
51+
52+
@Execute
53+
@Permission("eternalcore.back.other")
54+
@DescriptionDocs(description = "Teleport specified player to last location", arguments = "<player>")
55+
public void execute(@Sender Viewer viewer, @Arg Player target) {
56+
Optional<BackService.BackLocation> backLocation = this.backService.getBackLocation(target.getUniqueId());
57+
58+
if (backLocation.isEmpty()) {
59+
this.noticeService.viewer(viewer, translation -> translation.back().lastLocationNoExist());
60+
return;
61+
}
62+
63+
BackService.BackLocation location = backLocation.get();
64+
65+
if (location.isFromDeath() && !target.hasPermission(PERMISSION_BACK_ON_DEATH)) {
66+
this.noticeService.viewer(viewer, translation -> translation.back().targetNoPermissionBackOnDeath());
67+
return;
68+
}
69+
70+
this.backService.teleportBack(target, location.location());
71+
this.noticeService.player(target.getUniqueId(), translation -> translation.back().teleportedToLastLocation());
72+
73+
this.noticeService.create()
74+
.viewer(viewer)
75+
.notice(translation -> translation.back().teleportedSpecifiedPlayerLastLocation())
76+
.placeholder("{PLAYER}", target.getName())
77+
.send();
78+
}
79+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.eternalcode.core.feature.back;
2+
3+
import com.eternalcode.core.injector.annotations.Inject;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.event.EventHandler;
6+
import org.bukkit.event.EventPriority;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.entity.PlayerDeathEvent;
9+
import org.bukkit.event.player.PlayerTeleportEvent;
10+
11+
public class BackController implements Listener {
12+
13+
private final BackService backService;
14+
15+
@Inject
16+
public BackController(BackService backService) {
17+
this.backService = backService;
18+
}
19+
20+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
21+
public void onPlayerDeath(PlayerDeathEvent event) {
22+
Player entity = event.getEntity();
23+
24+
this.backService.setBackLocation(entity.getUniqueId(), entity.getLocation(), true);
25+
}
26+
27+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
28+
public void onPlayerTeleport(PlayerTeleportEvent event) {
29+
if (event.getCause() == PlayerTeleportEvent.TeleportCause.PLUGIN) {
30+
return;
31+
}
32+
33+
this.backService.setBackLocation(event.getPlayer().getUniqueId(), event.getFrom(), false);
34+
}
35+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.eternalcode.core.feature.back;
2+
3+
import com.eternalcode.commons.bukkit.position.PositionAdapter;
4+
import com.eternalcode.core.feature.teleport.TeleportService;
5+
import com.eternalcode.core.feature.teleport.TeleportTaskService;
6+
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
7+
import com.eternalcode.core.injector.annotations.Inject;
8+
import com.eternalcode.core.injector.annotations.component.Service;
9+
import org.bukkit.Location;
10+
import org.bukkit.entity.Player;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.Optional;
15+
import java.util.UUID;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
18+
@Service
19+
public class BackService {
20+
21+
private final TeleportService teleportService;
22+
private final TeleportTaskService teleportTaskService;
23+
private final TeleportRequestSettings settings;
24+
25+
private final Map<UUID, BackLocation> backLocations = new HashMap<>();
26+
27+
@Inject
28+
public BackService(
29+
TeleportService teleportService,
30+
TeleportTaskService teleportTaskService,
31+
TeleportRequestSettings settings
32+
) {
33+
this.teleportService = teleportService;
34+
this.teleportTaskService = teleportTaskService;
35+
this.settings = settings;
36+
}
37+
38+
public Optional<BackLocation> getBackLocation(UUID playerId) {
39+
return Optional.ofNullable(backLocations.get(playerId));
40+
}
41+
42+
public void setBackLocation(UUID playerId, Location location, boolean isFromDeath) {
43+
backLocations.put(playerId, new BackLocation(location, isFromDeath));
44+
}
45+
46+
public void teleportBack(Player player, Location location) {
47+
if (player.hasPermission("eternalcore.teleport.bypass")) {
48+
teleportService.teleport(player, location);
49+
}
50+
else {
51+
teleportTaskService.createTeleport(
52+
player.getUniqueId(),
53+
PositionAdapter.convert(player.getLocation()),
54+
PositionAdapter.convert(location),
55+
settings.tpaTimer()
56+
);
57+
}
58+
}
59+
60+
public record BackLocation(Location location, boolean isFromDeath) {
61+
}
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.eternalcode.core.feature.back.messages;
2+
3+
import com.eternalcode.multification.notice.Notice;
4+
5+
public interface BackMessages {
6+
7+
Notice lastLocationNoExist();
8+
9+
Notice noPermissionBackOnDeath();
10+
11+
Notice targetNoPermissionBackOnDeath();
12+
13+
Notice teleportedToLastLocation();
14+
15+
Notice teleportedSpecifiedPlayerLastLocation();
16+
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.eternalcode.core.feature.back.messages;
2+
3+
import com.eternalcode.multification.notice.Notice;
4+
import eu.okaeri.configs.OkaeriConfig;
5+
import lombok.Getter;
6+
import lombok.experimental.Accessors;
7+
8+
@Getter
9+
@Accessors(fluent = true)
10+
public class ENBackMessages extends OkaeriConfig implements BackMessages {
11+
12+
public Notice lastLocationNoExist = Notice.chat("<red>► <white>You don't have any last location to teleport to!");
13+
public Notice noPermissionBackOnDeath = Notice.chat("<red>► <white>You don't have permission to use <red>/back <white>after death!");
14+
public Notice targetNoPermissionBackOnDeath = Notice.chat("<red>► <white>Player doesn't have permission to use <red>/back <white>after death!");
15+
public Notice teleportedToLastLocation = Notice.chat("<green>► <white>You have been teleported to your last location!");
16+
public Notice teleportedSpecifiedPlayerLastLocation = Notice.chat("<green>► <white>Player <green>{PLAYER} <white>has been teleported to their last location!");
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.eternalcode.core.feature.back.messages;
2+
3+
import com.eternalcode.multification.notice.Notice;
4+
import eu.okaeri.configs.OkaeriConfig;
5+
import lombok.Getter;
6+
import lombok.experimental.Accessors;
7+
8+
@Getter
9+
@Accessors(fluent = true)
10+
public class PLBackMessages extends OkaeriConfig implements BackMessages {
11+
12+
public Notice lastLocationNoExist = Notice.chat("<red>► <white>Nie masz żadnej ostatniej lokalizacji, do której można się teleportować!");
13+
public Notice noPermissionBackOnDeath = Notice.chat("<red>► <white>Nie masz uprawnień do używania <red>/back <white>po śmierci!");
14+
public Notice targetNoPermissionBackOnDeath = Notice.chat("<red>► <white>Gracz nie ma uprawnień do używania <red>/back <white>po śmierci!");
15+
public Notice teleportedToLastLocation = Notice.chat("<green>► <white>Zostałeś przeteleportowany do ostatniej lokalizacji!");
16+
public Notice teleportedSpecifiedPlayerLastLocation = Notice.chat("<green>► <white>Gracz <green>{PLAYER} <white>został przeteleportowany do swojej ostatniej lokalizacji!");
17+
18+
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportBackCommand.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.eternalcode.core.translation;
22

3+
import com.eternalcode.core.feature.back.messages.BackMessages;
34
import com.eternalcode.core.feature.freeze.messages.FreezeMessages;
45
import com.eternalcode.core.feature.playtime.messages.PlaytimeMessages;
56
import com.eternalcode.core.feature.clear.messages.ClearMessages;
@@ -67,11 +68,6 @@ interface TeleportSection {
6768
// Coordinates XYZ
6869
Notice teleportedToCoordinates();
6970
Notice teleportedSpecifiedPlayerToCoordinates();
70-
71-
// Back
72-
Notice teleportedToLastLocation();
73-
Notice teleportedSpecifiedPlayerLastLocation();
74-
Notice lastLocationNoExist();
7571
}
7672

7773
interface ChatSection {
@@ -195,6 +191,7 @@ interface ItemSection {
195191
SudoMessages sudo();
196192
// Teleport Section
197193
TeleportSection teleport();
194+
BackMessages back();
198195
// teleport to random player section.
199196
TeleportToRandomPlayerMessages teleportToRandomPlayer();
200197
// Random Teleport Section

eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.eternalcode.core.translation.implementation;
22

3+
import com.eternalcode.core.feature.back.messages.ENBackMessages;
34
import com.eternalcode.core.feature.freeze.messages.ENFreezeMessages;
45
import com.eternalcode.core.feature.playtime.messages.ENPlaytimeMessages;
56
import com.eternalcode.core.feature.clear.messages.ENClearMessages;
@@ -215,16 +216,14 @@ public static class ENTeleportSection extends OkaeriConfig implements TeleportSe
215216
public Notice teleportedToCoordinates = Notice.chat("<green>► <white>Teleported to location x: <green>{X}<white>, y: <green>{Y}<white>, z: <green>{Z}");
216217
@Comment({" ", "# {PLAYER} - Player who has been teleported, {X} - X coordinate, {Y} - Y coordinate, {Z} - Z coordinate"})
217218
public Notice teleportedSpecifiedPlayerToCoordinates = Notice.chat("<green>► <white>Teleported <green>{PLAYER} <white>to location x: <green>{X}<white>, y: <green>{Y}<white>, z: <green>{Z}");
218-
219-
// Back
220-
@Comment(" ")
221-
public Notice teleportedToLastLocation = Notice.chat("<green>► <white>Teleported to the last location!");
222-
@Comment({" ", "# {PLAYER} - Player who has been teleported"})
223-
public Notice teleportedSpecifiedPlayerLastLocation = Notice.chat("<green>► <white>Teleported <green>{PLAYER} <white>to the last location!");
224-
@Comment(" ")
225-
public Notice lastLocationNoExist = Notice.chat("<red>✘ <dark_red>Last location is not exist!");
226219
}
227220

221+
@Comment({
222+
" ",
223+
"# This section is responsible for the messages of the /back command",
224+
})
225+
public ENBackMessages back = new ENBackMessages();
226+
228227
@Comment({
229228
" ",
230229
"# This section is responsible for the messages of the /tprp command",

0 commit comments

Comments
 (0)