Skip to content

Commit 18e5722

Browse files
vLuckyyycoderabbitai[bot]Rollczi
authored
GH-874 Expand Random Teleport configuration. (#874)
* Add more flexible configuration for Random Teleport. * Complete configurations with missing unsafe blocks and air blocks * Fix issues reported by @coderabbitai * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportRadiusRepresenterImpl.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Resolve issues reported in review, create `RandomTeleportRadiusRepresenterImpl#of` methods. * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportRadiusRepresenterImpl.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportServiceImpl.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportSettingsImpl.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Add import. * Remvoe buttons, add height range * Fix comments * Add teleportation counting. * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportTaskService.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportSafeLocationService.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Move description. * Rename some classes make API more usable. * Teleport players without waiting with the `/rtp <player> (admin command). * Add migrations, rename configs * Fix migration --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Rollczi <[email protected]>
1 parent 9978731 commit 18e5722

26 files changed

+770
-227
lines changed

eternalcore-api/src/main/java/com/eternalcode/core/delay/Delay.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@
55

66
import java.time.Duration;
77
import java.time.Instant;
8+
import java.util.function.Supplier;
89

910
public class Delay<T> {
1011

1112
private final Cache<T, Instant> delays;
1213

13-
private final DelaySettings delaySettings;
14+
private final Supplier<Duration> delaySettings;
1415

16+
@Deprecated
1517
public Delay(DelaySettings delaySettings) {
16-
this.delaySettings = delaySettings;
18+
this((Supplier<Duration>) () -> delaySettings.delay());
19+
}
20+
21+
public Delay(Supplier<Duration> delayProvider) {
22+
this.delaySettings = delayProvider;
1723

1824
this.delays = CacheBuilder.newBuilder()
19-
.expireAfterWrite(delaySettings.delay())
25+
.expireAfterWrite(delayProvider.get())
2026
.build();
2127
}
2228

@@ -25,7 +31,7 @@ public void markDelay(T key, Duration delay) {
2531
}
2632

2733
public void markDelay(T key) {
28-
this.markDelay(key, this.delaySettings.delay());
34+
this.markDelay(key, this.delaySettings.get());
2935
}
3036

3137
public void unmarkDelay(T key) {

eternalcore-api/src/main/java/com/eternalcode/core/delay/DelaySettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.time.Duration;
44

5+
@Deprecated
56
public interface DelaySettings {
67

78
Duration delay();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.eternalcode.core.feature.randomteleport;
2+
3+
public interface RandomTeleportRadius {
4+
int maxZ();
5+
int minZ();
6+
int maxX();
7+
int minX();
8+
9+
static RandomTeleportRadius of(int minX, int maxX, int minZ, int maxZ) {
10+
return new SimpleRandomTeleportRadius(minX, maxX, minZ, maxZ);
11+
}
12+
13+
static RandomTeleportRadius of(int radius) {
14+
return new SimpleRandomTeleportRadius(-radius, radius, -radius, radius);
15+
}
16+
17+
}

eternalcore-api/src/main/java/com/eternalcode/core/feature/randomteleport/TeleportResult.java renamed to eternalcore-api/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
import org.bukkit.Location;
44

5-
public record TeleportResult(boolean success, Location location) {
5+
public record RandomTeleportResult(boolean success, Location location) {
66
}

eternalcore-api/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface RandomTeleportService {
1414
* @param player The player to teleport.
1515
* @return A CompletableFuture containing the TeleportResult indicating the success or failure of the teleportation.
1616
*/
17-
CompletableFuture<TeleportResult> teleport(Player player);
17+
CompletableFuture<RandomTeleportResult> teleport(Player player);
1818

1919
/**
2020
* Asynchronously teleports the specified player to a random location within the specified world.
@@ -23,7 +23,7 @@ public interface RandomTeleportService {
2323
* @param world The world to which the player should be teleported.
2424
* @return A CompletableFuture containing the TeleportResult indicating the success or failure of the teleportation.
2525
*/
26-
CompletableFuture<TeleportResult> teleport(Player player, World world);
26+
CompletableFuture<RandomTeleportResult> teleport(Player player, World world);
2727

2828
/**
2929
* Asynchronously retrieves a safe random location within the specified world.
@@ -45,6 +45,16 @@ public interface RandomTeleportService {
4545
*/
4646
CompletableFuture<Location> getSafeRandomLocation(World world, int radius, int attemptCount);
4747

48+
/**
49+
* Asynchronously retrieves a safe random location within the specified world, using radius.
50+
*
51+
* @param world The world in which to find a random location.
52+
* @param radius The radius around the player to search for a safe location.
53+
* @param attemptCount The number of attempts to find a safe location.
54+
* @return A CompletableFuture containing the random Location that is deemed safe.
55+
*/
56+
CompletableFuture<Location> getSafeRandomLocation(World world, RandomTeleportRadius radius, int attemptCount);
57+
4858
/**
4959
* Asynchronously retrieves a safe random location within the border in specified world.
5060
*
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.eternalcode.core.feature.randomteleport;
2+
3+
record SimpleRandomTeleportRadius(int minX, int maxX, int minZ, int maxZ) implements RandomTeleportRadius {
4+
public SimpleRandomTeleportRadius {
5+
if (minX > maxX) {
6+
throw new IllegalArgumentException("minX cannot be greater than maxX");
7+
}
8+
if (minZ > maxZ) {
9+
throw new IllegalArgumentException("minZ cannot be greater than maxZ");
10+
}
11+
}
12+
}

eternalcore-core/src/main/java/com/eternalcode/core/configuration/ConfigurationManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.eternalcode.multification.notice.resolver.NoticeResolverRegistry;
1515
import java.io.File;
1616
import java.time.Duration;
17+
import java.util.Collections;
1718
import java.util.HashSet;
1819
import java.util.Set;
1920
import net.dzikoysk.cdn.Cdn;
@@ -77,4 +78,9 @@ public void reload() {
7778
this.load(config);
7879
}
7980
}
81+
82+
public Set<ReloadableConfig> getConfigs() {
83+
return Collections.unmodifiableSet(this.configs);
84+
}
85+
8086
}

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

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import com.eternalcode.core.feature.automessage.AutoMessageSettings;
88
import com.eternalcode.core.feature.chat.ChatSettings;
99
import com.eternalcode.core.feature.jail.JailSettings;
10-
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettings;
11-
import com.eternalcode.core.feature.randomteleport.RandomTeleportType;
1210
import com.eternalcode.core.feature.helpop.HelpOpSettings;
11+
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettingsImpl;
1312
import com.eternalcode.core.feature.spawn.SpawnSettings;
1413
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
1514
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
@@ -125,58 +124,7 @@ public Duration teleportationTimeToSpawn() {
125124
}
126125

127126
@Description({ "", "# Random Teleport Section" })
128-
public RandomTeleport randomTeleport = new RandomTeleport();
129-
130-
@Contextual
131-
public static class RandomTeleport implements RandomTeleportSettings, DelaySettings {
132-
@Description({
133-
"# Type of random teleportation,",
134-
"# WORLD_BORDER_RADIUS - radius based on the world-border size.",
135-
"# STATIC_RADIUS - radius based on the manually value."
136-
})
137-
public RandomTeleportType randomTeleportType = RandomTeleportType.WORLD_BORDER_RADIUS;
138-
139-
@Description({
140-
"# Radius of random teleportation, this uses for starting point spawn via /setworldspawn.",
141-
"# If you want to use a static radius, set the type to STATIC_RADIUS and set the radius here.",
142-
"# If you using WORLD_BORDER_RADIUS, this value will be ignored."
143-
})
144-
public int randomTeleportRadius = 1000;
145-
146-
@Description("# Teleport to a specific world, if left empty it will teleport to the player's current world")
147-
public String randomTeleportWorld = "world";
148-
149-
@Description("# Number of attempts to teleport to a random location")
150-
public int randomTeleportAttempts = 10;
151-
152-
@Override
153-
public int randomTeleportRadius() {
154-
return this.randomTeleportRadius;
155-
}
156-
157-
@Override
158-
public RandomTeleportType randomTeleportType() {
159-
return this.randomTeleportType;
160-
}
161-
162-
@Override
163-
public String randomTeleportWorld() {
164-
return this.randomTeleportWorld;
165-
}
166-
167-
@Override
168-
public int randomTeleportAttempts() {
169-
return this.randomTeleportAttempts;
170-
}
171-
172-
@Description("# Delay to request next random teleportation")
173-
public Duration randomTeleportDelay = Duration.ofSeconds(60);
174-
175-
@Override
176-
public Duration delay() {
177-
return this.randomTeleportDelay;
178-
}
179-
}
127+
public RandomTeleportSettingsImpl randomTeleport = new RandomTeleportSettingsImpl();
180128

181129
@Description({ " ", "# Homes Section" })
182130
public Homes homes = new Homes();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.eternalcode.core.configuration.migration;
2+
3+
public interface Migration {
4+
5+
boolean migrate();
6+
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.eternalcode.core.configuration.migration;
2+
3+
import com.eternalcode.core.configuration.ConfigurationManager;
4+
import com.eternalcode.core.configuration.ReloadableConfig;
5+
import com.eternalcode.core.injector.annotations.Inject;
6+
import com.eternalcode.core.injector.annotations.component.Controller;
7+
import com.eternalcode.core.publish.Subscribe;
8+
import com.eternalcode.core.publish.Subscriber;
9+
import com.eternalcode.core.publish.event.EternalInitializeEvent;
10+
import java.util.logging.Logger;
11+
12+
@Controller
13+
class MigrationController implements Subscriber {
14+
15+
private final MigrationService migrationService;
16+
private final ConfigurationManager configurationManager;
17+
private final Logger logger;
18+
19+
@Inject
20+
MigrationController(MigrationService migrationService, ConfigurationManager configurationManager, Logger logger) {
21+
this.migrationService = migrationService;
22+
this.configurationManager = configurationManager;
23+
this.logger = logger;
24+
}
25+
26+
@Subscribe
27+
void onMigration(EternalInitializeEvent event) {
28+
for (ReloadableConfig config : configurationManager.getConfigs()) {
29+
boolean wasMigrated = migrationService.migrate(config);
30+
31+
if (wasMigrated) {
32+
configurationManager.save(config);
33+
logger.info("Configuration " + config.getClass().getSimpleName() + " was migrated and saved.");
34+
}
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)