From a67693cc410fe95b25cc4c104c9e6f6b5934c3ca Mon Sep 17 00:00:00 2001 From: Rollczi Date: Sun, 12 Jan 2025 15:50:05 +0100 Subject: [PATCH] Fix events life cycle --- .../warp/event/PreWarpTeleportEvent.java | 28 ++++++++- .../feature/warp/event/WarpTeleportEvent.java | 10 ++- .../warp/WarpPermissionController.java | 63 ------------------- .../core/feature/warp/WarpServiceImpl.java | 2 +- .../feature/warp/WarpTeleportService.java | 25 ++++---- .../feature/warp/command/WarpCommand.java | 25 ++++++++ .../repository/WarpConfigRepresenter.java | 19 ------ .../warp/repository/WarpDataConfig.java | 19 +++++- .../warp/repository/WarpRepositoryImpl.java | 20 +++--- 9 files changed, 103 insertions(+), 108 deletions(-) delete mode 100644 eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpPermissionController.java delete mode 100644 eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpConfigRepresenter.java diff --git a/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/PreWarpTeleportEvent.java b/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/PreWarpTeleportEvent.java index ebdfe99e1..e8a37c916 100644 --- a/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/PreWarpTeleportEvent.java +++ b/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/PreWarpTeleportEvent.java @@ -1,10 +1,14 @@ package com.eternalcode.core.feature.warp.event; import com.eternalcode.core.feature.warp.Warp; +import com.google.common.base.Preconditions; +import java.time.Duration; +import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.Nullable; /** * Called before teleportation to warp. @@ -16,12 +20,15 @@ public class PreWarpTeleportEvent extends Event implements Cancellable { private final Player player; private Warp warp; private boolean cancelled; + private Duration teleportTime; + private @Nullable Location destination; - public PreWarpTeleportEvent(Player player, Warp warp) { + public PreWarpTeleportEvent(Player player, Warp warp, Duration teleportTime) { super(false); this.player = player; this.warp = warp; + this.teleportTime = teleportTime; } public Player getPlayer() { @@ -33,9 +40,28 @@ public Warp getWarp() { } public void setWarp(Warp warp) { + Preconditions.checkNotNull(warp, "Warp cannot be null"); this.warp = warp; } + public Duration getTeleportTime() { + return this.teleportTime; + } + + public void setTeleportTime(Duration teleportTime) { + Preconditions.checkNotNull(teleportTime, "Teleport time cannot be null"); + this.teleportTime = teleportTime; + } + + public Location getDestination() { + return this.destination != null ? this.destination : this.warp.getLocation(); + } + + public void setDestination(Location destination) { + Preconditions.checkNotNull(destination, "Destination cannot be null"); + this.destination = destination; + } + @Override public boolean isCancelled() { return this.cancelled; diff --git a/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/WarpTeleportEvent.java b/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/WarpTeleportEvent.java index c323bc037..65fc77cda 100644 --- a/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/WarpTeleportEvent.java +++ b/eternalcore-api/src/main/java/com/eternalcode/core/feature/warp/event/WarpTeleportEvent.java @@ -1,8 +1,8 @@ package com.eternalcode.core.feature.warp.event; import com.eternalcode.core.feature.warp.Warp; +import org.bukkit.Location; import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; @@ -15,12 +15,14 @@ public class WarpTeleportEvent extends Event { private final Player player; private final Warp warp; + private final Location destination; - public WarpTeleportEvent(Player player, Warp warp) { + public WarpTeleportEvent(Player player, Warp warp, Location destination) { super(false); this.player = player; this.warp = warp; + this.destination = destination; } public Player getPlayer() { @@ -31,6 +33,10 @@ public Warp getWarp() { return this.warp; } + public Location getDestination() { + return this.destination; + } + @Override public HandlerList getHandlers() { return HANDLER_LIST; diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpPermissionController.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpPermissionController.java deleted file mode 100644 index 8f39392eb..000000000 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpPermissionController.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.eternalcode.core.feature.warp; - -import com.eternalcode.annotations.scan.feature.FeatureDocs; -import com.eternalcode.core.configuration.implementation.PluginConfiguration; -import com.eternalcode.core.feature.warp.event.PreWarpTeleportEvent; -import com.eternalcode.core.injector.annotations.Inject; -import com.eternalcode.core.injector.annotations.component.Controller; -import com.eternalcode.core.notice.NoticeService; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; - -import java.util.List; -import java.util.Optional; -import java.util.UUID; - -@FeatureDocs( - description = "Check if a player has permission to use a specific warp", - name = "Warp permission" -) -@Controller -public class WarpPermissionController implements Listener { - - private final NoticeService noticeService; - private final PluginConfiguration config; - - @Inject - public WarpPermissionController(NoticeService noticeService, PluginConfiguration config) { - this.noticeService = noticeService; - this.config = config; - } - - @EventHandler - void onWarpPreTeleportation(PreWarpTeleportEvent event) { - Player player = event.getPlayer(); - UUID uniqueId = player.getUniqueId(); - Warp warp = event.getWarp(); - - this.checkWarpPermission(event, warp, player, uniqueId); - } - - private void checkWarpPermission(PreWarpTeleportEvent event, Warp warp, Player player, UUID uniqueId) { - List permissions = warp.getPermissions(); - Optional isPlayerAllowedToUseWarp = permissions - .stream() - .filter(player::hasPermission) - .findAny(); - - if (isPlayerAllowedToUseWarp.isPresent() || permissions.isEmpty()) { - return; - } - - event.setCancelled(true); - - this.noticeService.create() - .player(uniqueId) - .placeholder("{WARP}", warp.getName()) - .placeholder("{PERMISSIONS}", String.join(this.config.format.separator, permissions)) - .notice(translation -> translation.warp().noPermission()) - .send(); - } - -} diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpServiceImpl.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpServiceImpl.java index 7ce6a032c..5011d6564 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpServiceImpl.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpServiceImpl.java @@ -65,7 +65,7 @@ public Warp addPermissions(String warpName, String... permissions) { @Override public Warp removePermissions(String warpName, String... permissions) { - Warp warp = this.modifyPermissions(warpName, perms -> perms.addAll(List.of(permissions))); + Warp warp = this.modifyPermissions(warpName, perms -> perms.removeAll(List.of(permissions))); this.warpRepository.saveWarp(warp); return warp; } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpTeleportService.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpTeleportService.java index 5c388d4d7..080fdd947 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpTeleportService.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpTeleportService.java @@ -12,6 +12,7 @@ import com.eternalcode.core.injector.annotations.component.Service; import java.time.Duration; import java.util.UUID; +import org.bukkit.Location; import org.bukkit.entity.Player; @Service @@ -35,29 +36,31 @@ public WarpTeleportService( } public void teleport(Player player, Warp warp) { - PreWarpTeleportEvent pre = this.eventCaller.callEvent(new PreWarpTeleportEvent(player, warp)); + Duration teleportTime = player.hasPermission(WARP_BYPASS) + ? Duration.ZERO + : this.pluginConfiguration.warp.teleportTimeToWarp; + + PreWarpTeleportEvent pre = this.eventCaller.callEvent(new PreWarpTeleportEvent(player, warp, teleportTime)); if (pre.isCancelled()) { return; } - Duration teleportTime = player.hasPermission(WARP_BYPASS) - ? Duration.ZERO - : this.pluginConfiguration.warp.teleportTimeToWarp; - Warp destinationWarp = pre.getWarp(); + Location destination = pre.getDestination(); + Position destinationLocation = PositionAdapter.convert(destination); Position playerLocation = PositionAdapter.convert(player.getLocation()); - Position warpLocation = PositionAdapter.convert(destinationWarp.getLocation()); UUID uniqueId = player.getUniqueId(); - WarpTeleportEvent post = new WarpTeleportEvent(player, destinationWarp); - Teleport teleport = this.teleportTaskService.createTeleport( uniqueId, playerLocation, - warpLocation, - teleportTime + destinationLocation, + pre.getTeleportTime() ); - teleport.getResult().whenComplete((result, throwable) -> this.eventCaller.callEvent(post)); + + teleport.getResult().whenComplete((result, throwable) -> { + this.eventCaller.callEvent(new WarpTeleportEvent(player, destinationWarp, destination)); + }); } } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/WarpCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/WarpCommand.java index bc8c860cf..e0503e0ca 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/WarpCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/WarpCommand.java @@ -6,6 +6,7 @@ import com.eternalcode.core.feature.warp.WarpInventory; import com.eternalcode.core.feature.warp.WarpService; import com.eternalcode.core.feature.warp.WarpTeleportService; +import com.eternalcode.core.feature.warp.event.PreWarpTeleportEvent; import com.eternalcode.core.injector.annotations.Inject; import com.eternalcode.core.notice.NoticeService; import com.eternalcode.core.user.User; @@ -15,6 +16,8 @@ import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; import java.util.List; +import java.util.Optional; +import java.util.UUID; import org.bukkit.entity.Player; @RootCommand @@ -72,6 +75,28 @@ void warp(@Context Player player, @Context User user) { @Execute(name = "warp") @DescriptionDocs(description = "Teleport to warp, if player has permission eternalcore.warp.bypass teleport will be instant", arguments = "") void warp(@Context Player player, @Arg Warp warp) { + if (!this.hasPermission(player, warp)) { + this.noticeService.create() + .player(player.getUniqueId()) + .placeholder("{WARP}", warp.getName()) + .placeholder("{PERMISSIONS}", String.join(this.config.format.separator, warp.getPermissions())) + .notice(translation -> translation.warp().noPermission()) + .send(); + return; + } + this.warpTeleportService.teleport(player, warp); } + + private boolean hasPermission(Player player, Warp warp) { + List permissions = warp.getPermissions(); + if (permissions.isEmpty()) { + return true; + } + + return permissions + .stream() + .anyMatch(permission -> player.hasPermission(permission)); + } + } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpConfigRepresenter.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpConfigRepresenter.java deleted file mode 100644 index a2b2bebba..000000000 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpConfigRepresenter.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.eternalcode.core.feature.warp.repository; - -import com.eternalcode.commons.bukkit.position.Position; -import java.util.List; -import net.dzikoysk.cdn.entity.Contextual; - -@Contextual -class WarpConfigRepresenter { - public Position position; - public List permissions; - - WarpConfigRepresenter() { - } - - public WarpConfigRepresenter(Position position, List permissions) { - this.position = position; - this.permissions = permissions; - } -} diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpDataConfig.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpDataConfig.java index 366ebd8dd..2d0d609ca 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpDataConfig.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpDataConfig.java @@ -1,10 +1,13 @@ package com.eternalcode.core.feature.warp.repository; +import com.eternalcode.commons.bukkit.position.Position; import com.eternalcode.core.configuration.ReloadableConfig; import com.eternalcode.core.injector.annotations.component.ConfigurationFile; import java.io.File; import java.util.HashMap; +import java.util.List; import java.util.Map; +import net.dzikoysk.cdn.entity.Contextual; import net.dzikoysk.cdn.entity.Description; import net.dzikoysk.cdn.entity.Exclude; import net.dzikoysk.cdn.source.Resource; @@ -17,10 +20,24 @@ class WarpDataConfig implements ReloadableConfig { public static final String WARP_DATA_FILE_PATH = "data" + File.separator + "warps.yml"; @Description({"# Warps data", "# These are warp locations, for your own safety, please don't touch it."}) - public Map warps = new HashMap<>(); + public Map warps = new HashMap<>(); @Override public Resource resource(File folder) { return Source.of(folder, WARP_DATA_FILE_PATH); } + + @Contextual + static class WarpConfigEntry { + public Position position; + public List permissions; + + WarpConfigEntry() { + } + + public WarpConfigEntry(Position position, List permissions) { + this.position = position; + this.permissions = permissions; + } + } } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpRepositoryImpl.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpRepositoryImpl.java index 8135f59e0..e71a95385 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpRepositoryImpl.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/repository/WarpRepositoryImpl.java @@ -44,12 +44,12 @@ class WarpRepositoryImpl implements WarpRepository { @Override public CompletableFuture saveWarp(Warp warp) { - WarpConfigRepresenter warpConfigRepresenter = new WarpConfigRepresenter( + WarpDataConfig.WarpConfigEntry warpConfigEntry = new WarpDataConfig.WarpConfigEntry( PositionAdapter.convert(warp.getLocation()), warp.getPermissions() ); - return this.transactionalRun(warps -> warps.put(warp.getName(), warpConfigRepresenter)); + return this.transactionalRun(warps -> warps.put(warp.getName(), warpConfigEntry)); } @Override @@ -60,10 +60,10 @@ public CompletableFuture removeWarp(String warp) { @Override public CompletableFuture> getWarp(String name) { return transactionalSupply(warps -> Optional.ofNullable(this.warpDataConfig.warps.get(name)) - .map(warpConfigRepresenter -> new WarpImpl( + .map(warpConfigEntry -> new WarpImpl( name, - warpConfigRepresenter.position, - warpConfigRepresenter.permissions) + warpConfigEntry.position, + warpConfigEntry.permissions) )); } @@ -71,7 +71,7 @@ public CompletableFuture> getWarp(String name) { public CompletableFuture> getWarps() { return transactionalSupply(warps -> warps.entrySet().stream() .map(warpConfigEntry -> { - WarpConfigRepresenter warpContextual = warpConfigEntry.getValue(); + WarpDataConfig.WarpConfigEntry warpContextual = warpConfigEntry.getValue(); return new WarpImpl(warpConfigEntry.getKey(), warpContextual.position, warpContextual.permissions); }) .collect(Collectors.toList())); @@ -88,7 +88,7 @@ private void migrateWarps() { .stream() .collect(Collectors.toMap( entry -> entry.getKey(), - entry -> new WarpConfigRepresenter(entry.getValue(), new ArrayList<>())) + entry -> new WarpDataConfig.WarpConfigEntry(entry.getValue(), new ArrayList<>())) ) )); @@ -97,17 +97,17 @@ private void migrateWarps() { } } - private CompletableFuture transactionalRun(Consumer> editor) { + private CompletableFuture transactionalRun(Consumer> editor) { return transactionalSupply(warps -> { editor.accept(warps); return null; }); } - private CompletableFuture transactionalSupply(Function, T> editor) { + private CompletableFuture transactionalSupply(Function, T> editor) { return scheduler.completeAsync(() -> { synchronized (READ_WRITE_LOCK) { - Map warps = new HashMap<>(this.warpDataConfig.warps); + Map warps = new HashMap<>(this.warpDataConfig.warps); T result = editor.apply(warps); this.warpDataConfig.warps.putAll(warps); this.configurationManager.save(this.warpDataConfig);