Skip to content

Commit

Permalink
Fix events life cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Jan 12, 2025
1 parent e68afcc commit a67693c
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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() {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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() {
Expand All @@ -31,6 +33,10 @@ public Warp getWarp() {
return this.warp;
}

public Location getDestination() {
return this.destination;
}

@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 = "<warp>")
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<String> permissions = warp.getPermissions();
if (permissions.isEmpty()) {
return true;
}

return permissions
.stream()
.anyMatch(permission -> player.hasPermission(permission));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, WarpConfigRepresenter> warps = new HashMap<>();
public Map<String, WarpConfigEntry> 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<String> permissions;

WarpConfigEntry() {
}

public WarpConfigEntry(Position position, List<String> permissions) {
this.position = position;
this.permissions = permissions;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class WarpRepositoryImpl implements WarpRepository {

@Override
public CompletableFuture<Void> 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
Expand All @@ -60,18 +60,18 @@ public CompletableFuture<Void> removeWarp(String warp) {
@Override
public CompletableFuture<Optional<Warp>> 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)
));
}

@Override
public CompletableFuture<List<Warp>> 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()));
Expand All @@ -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<>()))
)
));

Expand All @@ -97,17 +97,17 @@ private void migrateWarps() {
}
}

private CompletableFuture<Void> transactionalRun(Consumer<Map<String, WarpConfigRepresenter>> editor) {
private CompletableFuture<Void> transactionalRun(Consumer<Map<String, WarpDataConfig.WarpConfigEntry>> editor) {
return transactionalSupply(warps -> {
editor.accept(warps);
return null;
});
}

private <T> CompletableFuture<T> transactionalSupply(Function<Map<String, WarpConfigRepresenter>, T> editor) {
private <T> CompletableFuture<T> transactionalSupply(Function<Map<String, WarpDataConfig.WarpConfigEntry>, T> editor) {
return scheduler.completeAsync(() -> {
synchronized (READ_WRITE_LOCK) {
Map<String, WarpConfigRepresenter> warps = new HashMap<>(this.warpDataConfig.warps);
Map<String, WarpDataConfig.WarpConfigEntry> warps = new HashMap<>(this.warpDataConfig.warps);
T result = editor.apply(warps);
this.warpDataConfig.warps.putAll(warps);
this.configurationManager.save(this.warpDataConfig);
Expand Down

0 comments on commit a67693c

Please sign in to comment.