Skip to content

Commit

Permalink
Add migrations, rename configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Dec 9, 2024
1 parent 2c8f248 commit f190e79
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 63 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
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
@@ -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,37 @@
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.event.EternalInitializeEvent;
import java.util.logging.Logger;

@Controller
class MigrationController {

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dev.rollczi.litecommands.annotations.permission.Permission;
import java.time.Duration;
import java.util.UUID;
import java.util.function.Supplier;
import org.bukkit.entity.Player;

@Command(name = "rtp", aliases = "randomteleport")
Expand All @@ -28,7 +29,7 @@ class RandomTeleportCommand {
private final RandomTeleportService randomTeleportService;
private final RandomTeleportTaskService randomTeleportTaskService;
private final PluginConfiguration config;
private final Delay<UUID> delay;
private final Delay<UUID> cooldown;

@Inject
RandomTeleportCommand(
Expand All @@ -41,7 +42,7 @@ class RandomTeleportCommand {
this.randomTeleportService = randomTeleportService;
this.randomTeleportTaskService = randomTeleportTaskService;
this.config = config;
this.delay = new Delay<>(this.config.randomTeleport);
this.cooldown = new Delay<>((Supplier<Duration>) () -> this.config.randomTeleport.cooldown());
}

@Execute
Expand Down Expand Up @@ -69,7 +70,7 @@ void executeSelf(@Context Player player) {
this.handleTeleportSuccess(player);
});

this.delay.markDelay(uuid, this.config.randomTeleport.delay());
this.cooldown.markDelay(uuid, this.config.randomTeleport.delay());
}

@Execute
Expand Down Expand Up @@ -97,7 +98,7 @@ void executeOther(@Context Viewer sender, @Arg Player player) {
this.handleAdminTeleport(sender, player);
});

this.delay.markDelay(uuid, this.config.randomTeleport.delay());
this.cooldown.markDelay(uuid, this.config.randomTeleport.delay());
}

private void handleTeleportSuccess(Player player) {
Expand Down Expand Up @@ -129,8 +130,8 @@ private boolean hasRandomTeleportDelay(Player player) {
return false;
}

if (this.delay.hasDelay(uniqueId)) {
Duration time = this.delay.getDurationToExpire(uniqueId);
if (this.cooldown.hasDelay(uniqueId)) {
Duration time = this.cooldown.getDurationToExpire(uniqueId);

this.noticeService.create()
.notice(translation -> translation.randomTeleport().randomTeleportDelay())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ final class RandomTeleportResolveWorldUtil {
static World resolveWorld(Player player, RandomTeleportSettings settings) {
World world = player.getWorld();

if (!settings.randomTeleportWorld().isBlank()) {
world = getWorld(settings.randomTeleportWorld());
if (!settings.world().isBlank()) {
world = getWorld(settings.world());

if (world == null) {
throw new IllegalStateException(
"World " + settings.randomTeleportWorld() + " does not exist!");
"World " + settings.world() + " does not exist!");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ private boolean isSafeLocation(Chunk chunk, Location location) {
Block blockAbove = block.getRelative(BlockFace.UP);
Block blockFloor = block.getRelative(BlockFace.DOWN);

if (this.randomTeleportSettings.randomTeleportUnsafeBlocks().contains(blockFloor.getType())) {
if (this.randomTeleportSettings.unsafeBlocks().contains(blockFloor.getType())) {
return false;
}

Set<Material> airBlocks = this.randomTeleportSettings.randomTeleportAirBlocks();
Set<Material> airBlocks = this.randomTeleportSettings.airBlocks();
if (!airBlocks.contains(block.getType()) || !airBlocks.contains(blockAbove.getType())) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public CompletableFuture<RandomTeleportResult> teleport(Player player, World wor
return CompletableFuture.completedFuture(new RandomTeleportResult(false, player.getLocation()));
}

return this.getSafeRandomLocation(world, this.randomTeleportSettings.randomTeleportAttempts())
return this.getSafeRandomLocation(world, this.randomTeleportSettings.teleportAttempts())
.thenCompose(location -> PaperLib.teleportAsync(player, location).thenApply(success -> {
RandomTeleportResult teleportResult = new RandomTeleportResult(success, location);

Expand All @@ -58,8 +58,8 @@ public CompletableFuture<RandomTeleportResult> teleport(Player player, World wor

@Override
public CompletableFuture<Location> getSafeRandomLocation(World world, int attemptCount) {
RandomTeleportRadius radius = switch (this.randomTeleportSettings.randomTeleportType()) {
case STATIC_RADIUS -> this.randomTeleportSettings.randomTeleportRadius();
RandomTeleportRadius radius = switch (this.randomTeleportSettings.radiusType()) {
case STATIC_RADIUS -> this.randomTeleportSettings.radius();
case WORLD_BORDER_RADIUS -> this.getWorldBorderRadius(world);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@

interface RandomTeleportSettings {

Duration randomTeleportTime();
RandomTeleportRadius radius();

RandomTeleportRadius randomTeleportRadius();
RandomTeleportType radiusType();

RandomTeleportType randomTeleportType();
String world();

String randomTeleportWorld();
int teleportAttempts();

int randomTeleportAttempts();
Set<Material> unsafeBlocks();

Set<Material> randomTeleportUnsafeBlocks();

Set<Material> randomTeleportAirBlocks();
Set<Material> airBlocks();

RandomTeleportHeightRange heightRange();

Duration delay();

Duration cooldown();

}
Loading

0 comments on commit f190e79

Please sign in to comment.