Skip to content

Commit

Permalink
GH-874 Expand Random Teleport configuration. (#874)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Dec 12, 2024
1 parent 9978731 commit 18e5722
Show file tree
Hide file tree
Showing 26 changed files with 770 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@

import java.time.Duration;
import java.time.Instant;
import java.util.function.Supplier;

public class Delay<T> {

private final Cache<T, Instant> delays;

private final DelaySettings delaySettings;
private final Supplier<Duration> delaySettings;

@Deprecated
public Delay(DelaySettings delaySettings) {
this.delaySettings = delaySettings;
this((Supplier<Duration>) () -> delaySettings.delay());
}

public Delay(Supplier<Duration> delayProvider) {
this.delaySettings = delayProvider;

this.delays = CacheBuilder.newBuilder()
.expireAfterWrite(delaySettings.delay())
.expireAfterWrite(delayProvider.get())
.build();
}

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

public void markDelay(T key) {
this.markDelay(key, this.delaySettings.delay());
this.markDelay(key, this.delaySettings.get());
}

public void unmarkDelay(T key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.Duration;

@Deprecated
public interface DelaySettings {

Duration delay();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.eternalcode.core.feature.randomteleport;

public interface RandomTeleportRadius {
int maxZ();
int minZ();
int maxX();
int minX();

static RandomTeleportRadius of(int minX, int maxX, int minZ, int maxZ) {
return new SimpleRandomTeleportRadius(minX, maxX, minZ, maxZ);
}

static RandomTeleportRadius of(int radius) {
return new SimpleRandomTeleportRadius(-radius, radius, -radius, radius);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.bukkit.Location;

public record TeleportResult(boolean success, Location location) {
public record RandomTeleportResult(boolean success, Location location) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface RandomTeleportService {
* @param player The player to teleport.
* @return A CompletableFuture containing the TeleportResult indicating the success or failure of the teleportation.
*/
CompletableFuture<TeleportResult> teleport(Player player);
CompletableFuture<RandomTeleportResult> teleport(Player player);

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

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

/**
* Asynchronously retrieves a safe random location within the specified world, using radius.
*
* @param world The world in which to find a random location.
* @param radius The radius around the player to search for a safe location.
* @param attemptCount The number of attempts to find a safe location.
* @return A CompletableFuture containing the random Location that is deemed safe.
*/
CompletableFuture<Location> getSafeRandomLocation(World world, RandomTeleportRadius radius, int attemptCount);

/**
* Asynchronously retrieves a safe random location within the border in specified world.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.eternalcode.core.feature.randomteleport;

record SimpleRandomTeleportRadius(int minX, int maxX, int minZ, int maxZ) implements RandomTeleportRadius {
public SimpleRandomTeleportRadius {
if (minX > maxX) {
throw new IllegalArgumentException("minX cannot be greater than maxX");
}
if (minZ > maxZ) {
throw new IllegalArgumentException("minZ cannot be greater than maxZ");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.eternalcode.multification.notice.resolver.NoticeResolverRegistry;
import java.io.File;
import java.time.Duration;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import net.dzikoysk.cdn.Cdn;
Expand Down Expand Up @@ -77,4 +78,9 @@ public void reload() {
this.load(config);
}
}

public Set<ReloadableConfig> getConfigs() {
return Collections.unmodifiableSet(this.configs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import com.eternalcode.core.feature.automessage.AutoMessageSettings;
import com.eternalcode.core.feature.chat.ChatSettings;
import com.eternalcode.core.feature.jail.JailSettings;
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettings;
import com.eternalcode.core.feature.randomteleport.RandomTeleportType;
import com.eternalcode.core.feature.helpop.HelpOpSettings;
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettingsImpl;
import com.eternalcode.core.feature.spawn.SpawnSettings;
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
Expand Down Expand Up @@ -125,58 +124,7 @@ public Duration teleportationTimeToSpawn() {
}

@Description({ "", "# Random Teleport Section" })
public RandomTeleport randomTeleport = new RandomTeleport();

@Contextual
public static class RandomTeleport implements RandomTeleportSettings, DelaySettings {
@Description({
"# Type of random teleportation,",
"# WORLD_BORDER_RADIUS - radius based on the world-border size.",
"# STATIC_RADIUS - radius based on the manually value."
})
public RandomTeleportType randomTeleportType = RandomTeleportType.WORLD_BORDER_RADIUS;

@Description({
"# Radius of random teleportation, this uses for starting point spawn via /setworldspawn.",
"# If you want to use a static radius, set the type to STATIC_RADIUS and set the radius here.",
"# If you using WORLD_BORDER_RADIUS, this value will be ignored."
})
public int randomTeleportRadius = 1000;

@Description("# Teleport to a specific world, if left empty it will teleport to the player's current world")
public String randomTeleportWorld = "world";

@Description("# Number of attempts to teleport to a random location")
public int randomTeleportAttempts = 10;

@Override
public int randomTeleportRadius() {
return this.randomTeleportRadius;
}

@Override
public RandomTeleportType randomTeleportType() {
return this.randomTeleportType;
}

@Override
public String randomTeleportWorld() {
return this.randomTeleportWorld;
}

@Override
public int randomTeleportAttempts() {
return this.randomTeleportAttempts;
}

@Description("# Delay to request next random teleportation")
public Duration randomTeleportDelay = Duration.ofSeconds(60);

@Override
public Duration delay() {
return this.randomTeleportDelay;
}
}
public RandomTeleportSettingsImpl randomTeleport = new RandomTeleportSettingsImpl();

@Description({ " ", "# Homes Section" })
public Homes homes = new Homes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eternalcode.core.configuration.migration;

public interface Migration {

boolean migrate();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.eternalcode.core.configuration.migration;

import com.eternalcode.core.configuration.ConfigurationManager;
import com.eternalcode.core.configuration.ReloadableConfig;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Controller;
import com.eternalcode.core.publish.Subscribe;
import com.eternalcode.core.publish.Subscriber;
import com.eternalcode.core.publish.event.EternalInitializeEvent;
import java.util.logging.Logger;

@Controller
class MigrationController implements Subscriber {

private final MigrationService migrationService;
private final ConfigurationManager configurationManager;
private final Logger logger;

@Inject
MigrationController(MigrationService migrationService, ConfigurationManager configurationManager, Logger logger) {
this.migrationService = migrationService;
this.configurationManager = configurationManager;
this.logger = logger;
}

@Subscribe
void onMigration(EternalInitializeEvent event) {
for (ReloadableConfig config : configurationManager.getConfigs()) {
boolean wasMigrated = migrationService.migrate(config);

if (wasMigrated) {
configurationManager.save(config);
logger.info("Configuration " + config.getClass().getSimpleName() + " was migrated and saved.");
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.eternalcode.core.configuration.migration;

import com.eternalcode.core.configuration.ReloadableConfig;
import com.eternalcode.core.injector.annotations.component.Service;
import com.eternalcode.core.util.ReflectUtil;
import java.lang.reflect.Field;
import net.dzikoysk.cdn.entity.Contextual;

@Service
class MigrationService {

public <T extends ReloadableConfig> boolean migrate(T config) {
return reflectMigrate(config);
}

private <T> boolean reflectMigrate(T config) {
boolean isMigrated = false;

for (Field declaredField : ReflectUtil.getAllSuperFields(config.getClass())) {
Class<?> fieldType = declaredField.getType();

if (Migration.class.isAssignableFrom(fieldType)) {
Migration migration = ReflectUtil.getFieldValue(declaredField, config);
boolean wasMigrationSuccessful = migration.migrate();
isMigrated |= wasMigrationSuccessful;
}

if (fieldType.isAnnotationPresent(Contextual.class)) {
Object fieldValue = ReflectUtil.getFieldValue(declaredField, config);
isMigrated |= reflectMigrate(fieldValue);
}
}

return isMigrated;
}

}
Loading

0 comments on commit 18e5722

Please sign in to comment.