Skip to content

Commit 50669a6

Browse files
vLuckyyygemini-code-assist[bot]P1otrulla
authored
GH-1101 Refactor spawn join and /tprp settings to separate sections with migrations. (#1101)
* Enhance spawn join settings. * Move tprp to dedicated section. add migration's. * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnJoinConfig.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnJoinConfig.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnJoinConfig.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * follow gemini review. * Stylistic changes --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Piotr Zych <[email protected]>
1 parent 1268ea4 commit 50669a6

19 files changed

+218
-140
lines changed

eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import com.eternalcode.core.feature.serverlinks.ServerLinksSettings;
2828
import com.eternalcode.core.feature.spawn.SpawnJoinConfig;
2929
import com.eternalcode.core.feature.spawn.SpawnJoinSettings;
30-
import com.eternalcode.core.feature.spawn.SpawnSettings;
30+
import com.eternalcode.core.feature.teleportrandomplayer.TeleportToRandomPlayerConfig;
31+
import com.eternalcode.core.feature.teleportrandomplayer.TeleportToRandomPlayerSettings;
3132
import com.eternalcode.core.feature.teleportrequest.TeleportRequestConfig;
3233
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
3334
import com.eternalcode.core.feature.vanish.VanishConfig;
@@ -42,7 +43,6 @@
4243
import org.bukkit.Sound;
4344

4445
import java.io.File;
45-
import java.time.Duration;
4646

4747
@Header({
4848
"#",
@@ -72,43 +72,19 @@ public class PluginConfiguration extends AbstractConfigurationFile {
7272
@Comment("")
7373
@Comment("# Spawn & Join Configuration")
7474
@Comment("# Settings responsible for player spawn and join behavior")
75-
SpawnJoinConfig join = new SpawnJoinConfig();
75+
SpawnJoinConfig spawn = new SpawnJoinConfig();
7676

7777
@Bean(proxied = TeleportRequestSettings.class)
7878
@Comment("")
7979
@Comment("# Teleport Request Configuration")
8080
@Comment("# Settings for teleport requests between players")
8181
TeleportRequestConfig teleportAsk = new TeleportRequestConfig();
8282

83-
@Bean
83+
@Bean(proxied = TeleportToRandomPlayerSettings.class)
8484
@Comment("")
85-
@Comment("# Teleport Configuration")
86-
@Comment("# General teleportation settings")
87-
public Teleport teleport = new Teleport();
88-
89-
// TODO: Add migration, move option's to domain-specific configuration classes
90-
// teleportToSpawnOnDeath -> com.eternalcode.core.feature.spawn.SpawnConfig
91-
// teleportToRespawnPoint -> com.eternalcode.core.feature.spawn.SpawnConfig
92-
// teleportTimeToSpawn -> com.eternalcode.core.feature.spawn.SpawnConfig
93-
// includeOpPlayersInRandomTeleport -> com.eternalcode.core.feature.teleportrandomplayer.TeleportRandomPlayerConfig
94-
public static class Teleport extends OkaeriConfig implements SpawnSettings {
95-
@Comment("# Teleports the player to spawn after death")
96-
public boolean teleportToSpawnOnDeath = true;
97-
98-
@Comment("# Teleports the player to respawn point after death")
99-
public boolean teleportToRespawnPoint = true;
100-
101-
@Comment("# Time delay before teleporting to spawn")
102-
public Duration teleportTimeToSpawn = Duration.ofSeconds(5);
103-
104-
@Comment("# Include players with OP permission in random teleport selection")
105-
public boolean includeOpPlayersInRandomTeleport = false;
106-
107-
@Override
108-
public Duration teleportationTimeToSpawn() {
109-
return this.teleportTimeToSpawn;
110-
}
111-
}
85+
@Comment("# Configuration for teleporting to a random player")
86+
@Comment("# Settings for the /tprp command, allowing you to randomly teleport to any player on the server")
87+
TeleportToRandomPlayerConfig teleportToRandomPlayer = new TeleportToRandomPlayerConfig();
11288

11389
@Bean(proxied = RandomTeleportSettings.class)
11490
@Comment("")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.eternalcode.core.configuration.migrations;
2+
3+
import static eu.okaeri.configs.migrate.ConfigMigrationDsl.move;
4+
5+
import eu.okaeri.configs.migrate.builtin.NamedMigration;
6+
7+
class Migration_0002_Move_Spawn_Settings_to_spawn_config_section extends NamedMigration {
8+
9+
Migration_0002_Move_Spawn_Settings_to_spawn_config_section() {
10+
super(
11+
"Move spawn settings to spawn config section",
12+
move("join", "spawn"), // rename section join -> spawn
13+
move("teleport.teleportToSpawnOnDeath", "spawn.teleportToSpawnAfterDeath"),
14+
move("teleport.teleportToRespawnPoint", "spawn.teleportToPersonalRespawnPoint"),
15+
move("teleport.teleportTimeToSpawn", "spawn.spawnTeleportTime"),
16+
move("spawn.teleportToSpawnOnFirstJoin", "spawn.teleportNewPlayersToSpawn"),
17+
move("spawn.teleportToSpawnOnJoin", "spawn.teleportPlayersToSpawnOnJoin")
18+
);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.eternalcode.core.configuration.migrations;
2+
3+
import static eu.okaeri.configs.migrate.ConfigMigrationDsl.move;
4+
5+
import eu.okaeri.configs.migrate.builtin.NamedMigration;
6+
7+
class Migration_0003_Move_tprp_to_dedicated_section extends NamedMigration {
8+
9+
Migration_0003_Move_tprp_to_dedicated_section() {
10+
super(
11+
"Move tprp to dedicated config section",
12+
move("teleport", "teleportToRandomPlayer"),
13+
move(
14+
"teleportToRandomPlayer.includeOpPlayersInRandomTeleport",
15+
"teleportToRandomPlayer.teleportToOpPlayers")
16+
);
17+
}
18+
}

eternalcore-core/src/main/java/com/eternalcode/core/configuration/migrations/Migrations.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class Migrations {
66

77
public static final ConfigMigration[] ALL = new ConfigMigration[] {
88
new Migration_0001_Rename_privateChat_to_msg(),
9+
new Migration_0002_Move_Spawn_Settings_to_spawn_config_section(),
10+
new Migration_0003_Move_tprp_to_dedicated_section()
911
};
1012

1113
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SetSpawnCommand.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
@Permission("eternalcore.setspawn")
1515
class SetSpawnCommand {
1616

17-
private final SpawnService spawnService;
1817
private final NoticeService noticeService;
18+
private final SpawnService spawnService;
1919

2020
@Inject
21-
SetSpawnCommand(SpawnService spawnService, NoticeService noticeService) {
22-
this.spawnService = spawnService;
21+
SetSpawnCommand(
22+
NoticeService noticeService,
23+
SpawnService spawnService
24+
) {
2325
this.noticeService = noticeService;
26+
this.spawnService = spawnService;
2427
}
2528

2629
@Async

eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnCommand.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.eternalcode.annotations.scan.permission.PermissionDocs;
55
import com.eternalcode.commons.bukkit.position.Position;
66
import com.eternalcode.commons.bukkit.position.PositionAdapter;
7-
import com.eternalcode.core.configuration.implementation.LocationsConfiguration;
87
import com.eternalcode.core.feature.teleport.TeleportService;
98
import com.eternalcode.core.feature.teleport.TeleportTaskService;
109
import com.eternalcode.core.injector.annotations.Inject;
@@ -24,24 +23,25 @@ class SpawnCommand {
2423

2524
private static final String SPAWN_TELEPORT_BYPASS = "eternalcore.spawn.bypass";
2625

27-
private final LocationsConfiguration locations;
28-
private final SpawnSettings spawnSettings;
2926
private final TeleportTaskService teleportTaskService;
27+
private final SpawnJoinSettings spawnJoinSettings;
3028
private final TeleportService teleportService;
3129
private final NoticeService noticeService;
30+
private final SpawnService spawnService;
3231

3332
@Inject
3433
SpawnCommand(
35-
LocationsConfiguration locations,
36-
SpawnSettings spawnSettings,
37-
NoticeService noticeService,
3834
TeleportTaskService teleportTaskService,
39-
TeleportService teleportService) {
35+
SpawnJoinSettings spawnJoinSettings,
36+
TeleportService teleportService,
37+
NoticeService noticeService,
38+
SpawnService spawnService
39+
) {
4040
this.teleportTaskService = teleportTaskService;
41-
this.locations = locations;
42-
this.spawnSettings = spawnSettings;
43-
this.noticeService = noticeService;
41+
this.spawnJoinSettings = spawnJoinSettings;
4442
this.teleportService = teleportService;
43+
this.noticeService = noticeService;
44+
this.spawnService = spawnService;
4545
}
4646

4747
@Execute
@@ -53,21 +53,18 @@ class SpawnCommand {
5353
description = "Allows you to bypass spawn teleportation time"
5454
)
5555
void executeSelf(@Context Player sender) {
56-
Position position = this.locations.spawn;
56+
Location spawnLocation = this.spawnService.getSpawnLocation();
5757

58-
if (position.isNoneWorld()) {
58+
if (spawnLocation == null) {
5959
this.noticeService.create()
6060
.notice(translation -> translation.spawn().spawnNoSet())
6161
.player(sender.getUniqueId())
6262
.send();
63-
6463
return;
6564
}
6665

67-
Location destinationLocation = PositionAdapter.convert(this.locations.spawn);
68-
6966
if (sender.hasPermission(SPAWN_TELEPORT_BYPASS)) {
70-
this.teleportService.teleport(sender, destinationLocation);
67+
this.teleportService.teleport(sender, spawnLocation);
7168

7269
this.noticeService.create()
7370
.notice(translation -> translation.teleport().teleported())
@@ -82,15 +79,17 @@ void executeSelf(@Context Player sender) {
8279
.notice(translation -> translation.teleport().teleportTaskAlreadyExist())
8380
.player(sender.getUniqueId())
8481
.send();
85-
8682
return;
8783
}
8884

89-
Position convert = PositionAdapter.convert(destinationLocation);
90-
Duration time = this.spawnSettings.teleportationTimeToSpawn();
85+
Duration teleportDelay = this.spawnJoinSettings.spawnTeleportTime();
9186

92-
this.teleportTaskService.createTeleport(sender.getUniqueId(), PositionAdapter.convert(sender.getLocation()),
93-
convert, time);
87+
this.teleportTaskService.createTeleport(
88+
sender.getUniqueId(),
89+
PositionAdapter.convert(sender.getLocation()),
90+
PositionAdapter.convert(spawnLocation),
91+
teleportDelay
92+
);
9493

9594
this.noticeService.create()
9695
.notice(translation -> translation.teleport().teleporting())
@@ -102,20 +101,17 @@ void executeSelf(@Context Player sender) {
102101
@Permission("eternalcore.spawn.other")
103102
@DescriptionDocs(description = "Teleports specified player to spawn location", arguments = "<player>")
104103
void execute(@Context Viewer sender, @Arg Player player) {
105-
Position position = this.locations.spawn;
104+
Location spawnLocation = this.spawnService.getSpawnLocation();
106105

107-
if (position.isNoneWorld()) {
106+
if (spawnLocation == null) {
108107
this.noticeService.create()
109108
.notice(translation -> translation.spawn().spawnNoSet())
110109
.viewer(sender)
111110
.send();
112-
113111
return;
114112
}
115113

116-
Location destinationLocation = PositionAdapter.convert(this.locations.spawn);
117-
118-
this.teleportService.teleport(player, destinationLocation);
114+
this.teleportService.teleport(player, spawnLocation);
119115

120116
this.noticeService.create()
121117
.notice(translation -> translation.spawn().spawnTeleportedBy())

eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnJoinConfig.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,48 @@
22

33
import eu.okaeri.configs.OkaeriConfig;
44
import eu.okaeri.configs.annotation.Comment;
5+
import java.time.Duration;
56
import lombok.Getter;
67
import lombok.experimental.Accessors;
78

89
@Getter
910
@Accessors(fluent = true)
1011
public class SpawnJoinConfig extends OkaeriConfig implements SpawnJoinSettings {
11-
@Comment("# Teleport to spawn on first join")
12-
public boolean teleportToSpawnOnFirstJoin = true;
1312

14-
@Comment("# Teleport to spawn on join")
15-
public boolean teleportToSpawnOnJoin = false;
13+
14+
@Comment({
15+
"# Delay before teleporting player to spawn (in seconds)",
16+
"# Set to 0 to teleport instantly"
17+
})
18+
public Duration spawnTeleportTime = Duration.ofSeconds(5);
19+
20+
@Comment({
21+
"# Teleport to spawn on first join (only once, when the player joins the server for the first time)",
22+
"# Example: true = teleport new players to spawn automatically"
23+
})
24+
public boolean teleportNewPlayersToSpawn = true;
25+
26+
@Comment({
27+
"# Teleport to spawn on every join (excluding first join, which is handled above)",
28+
"# Example: true = always teleport players to spawn when they log in"
29+
})
30+
public boolean teleportPlayersToSpawnOnJoin = false;
31+
32+
@Comment({
33+
"# Teleport player to spawn after death",
34+
"# This only works if 'teleportToPersonalRespawnPoint' is false, or if 'alwaysTeleportToSpawnAfterDeath' is true"
35+
})
36+
public boolean teleportToSpawnAfterDeath = true;
37+
38+
@Comment({
39+
"# Teleports player to their personal respawn point (bed, respawn anchor) after death",
40+
"# If true, overrides 'teleportToSpawnAfterDeath' unless 'alwaysTeleportToSpawnAfterDeath' is true"
41+
})
42+
public boolean teleportToPersonalRespawnPoint = true;
43+
44+
@Comment({
45+
"# Forces teleport to spawn even if player has a personal respawn point",
46+
"# This overrides 'teleportToPersonalRespawnPoint' setting"
47+
})
48+
public boolean alwaysTeleportToSpawnAfterDeath = false;
1649
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package com.eternalcode.core.feature.spawn;
22

3+
import java.time.Duration;
4+
35
public interface SpawnJoinSettings {
4-
boolean teleportToSpawnOnJoin();
5-
boolean teleportToSpawnOnFirstJoin();
6+
7+
Duration spawnTeleportTime();
8+
boolean teleportNewPlayersToSpawn();
9+
boolean teleportPlayersToSpawnOnJoin();
10+
boolean teleportToSpawnAfterDeath();
11+
boolean teleportToPersonalRespawnPoint();
12+
boolean alwaysTeleportToSpawnAfterDeath();
13+
614
}
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
package com.eternalcode.core.feature.spawn;
22

3-
import com.eternalcode.core.configuration.implementation.LocationsConfiguration;
4-
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
3+
import com.eternalcode.core.feature.teleport.TeleportService;
54
import com.eternalcode.core.injector.annotations.Inject;
65
import com.eternalcode.core.injector.annotations.component.Controller;
7-
import com.eternalcode.commons.bukkit.position.Position;
8-
import com.eternalcode.commons.bukkit.position.PositionAdapter;
9-
import com.eternalcode.core.feature.teleport.TeleportService;
106
import org.bukkit.Location;
117
import org.bukkit.entity.Player;
128
import org.bukkit.event.EventHandler;
139
import org.bukkit.event.Listener;
1410
import org.bukkit.event.player.PlayerRespawnEvent;
1511

16-
import java.util.Objects;
17-
1812
@Controller
1913
class SpawnRespawnController implements Listener {
2014

15+
private final SpawnJoinSettings spawnJoinSettings;
2116
private final TeleportService teleportService;
22-
private final PluginConfiguration config;
23-
private final LocationsConfiguration locations;
17+
private final SpawnService spawnService;
2418

2519
@Inject
26-
SpawnRespawnController(TeleportService teleportService, PluginConfiguration config, LocationsConfiguration locations) {
20+
SpawnRespawnController(
21+
SpawnJoinSettings spawnJoinSettings,
22+
TeleportService teleportService,
23+
SpawnService spawnService
24+
) {
25+
this.spawnJoinSettings = spawnJoinSettings;
2726
this.teleportService = teleportService;
28-
this.config = config;
29-
this.locations = locations;
27+
this.spawnService = spawnService;
3028
}
3129

3230
@EventHandler
3331
void onPlayerRespawn(PlayerRespawnEvent event) {
3432
Player player = event.getPlayer();
3533

36-
if (this.config.teleport.teleportToRespawnPoint && this.hasRespawnPoint(player)) {
34+
boolean forceSpawn = this.spawnJoinSettings.alwaysTeleportToSpawnAfterDeath();
35+
boolean usePersonalRespawn = this.spawnJoinSettings.teleportToPersonalRespawnPoint() && this.hasRespawnPoint(player);
36+
37+
if (usePersonalRespawn && !forceSpawn) {
3738
return;
3839
}
3940

40-
Position spawnPosition = this.locations.spawn;
41-
42-
if (this.config.teleport.teleportToSpawnOnDeath && !Objects.equals(spawnPosition.world(), Position.NONE_WORLD)) {
43-
Location destinationLocation = PositionAdapter.convert(spawnPosition);
44-
this.teleportService.teleport(player, destinationLocation);
41+
if (this.spawnJoinSettings.teleportToSpawnAfterDeath()) {
42+
Location spawnLocation = this.spawnService.getSpawnLocation();
43+
if (spawnLocation != null) {
44+
this.teleportService.teleport(player, spawnLocation);
45+
}
4546
}
4647
}
4748

48-
boolean hasRespawnPoint(Player player) {
49-
return player.getBedSpawnLocation() != null;
49+
private boolean hasRespawnPoint(Player player) {
50+
return player.getRespawnLocation() != null;
5051
}
51-
5252
}

0 commit comments

Comments
 (0)