diff --git a/build.gradle b/build.gradle index aebe1d56..d1e97cd8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ import org.apache.tools.ant.filters.ReplaceTokens plugins { - id 'com.github.johnrengelman.shadow' version '8.1.1' + id 'io.github.goooler.shadow' version '8.1.7' id 'org.cadixdev.licenser' version '0.6.1' apply false id 'org.ajoberstar.grgit' version '5.2.2' id 'checkstyle' @@ -57,7 +57,7 @@ publishing { } allprojects { - apply plugin: 'com.github.johnrengelman.shadow' + apply plugin: 'io.github.goooler.shadow' apply plugin: 'org.cadixdev.licenser' apply plugin: 'checkstyle' apply plugin: 'java' diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java index 4563b626..1afc7808 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java @@ -33,6 +33,8 @@ import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason; + public interface BukkitEventDispatcher extends EventDispatcher { @Override @@ -52,6 +54,14 @@ public interface BukkitEventDispatcher extends EventDispatcher { return new TeleportWarmupEvent(teleport, duration); } + @Override + default @NotNull ITeleportWarmupCancelledEvent getTeleportWarmupCancelledEvent(@NotNull TimedTeleport teleport, + int duration, + int cancelledAfter, + @NotNull CancelReason cancelReason) { + return new TeleportWarmupCancelledEvent(teleport, duration, cancelledAfter, cancelReason); + } + @Override default @NotNull ISendTeleportRequestEvent getSendTeleportRequestEvent(@NotNull OnlineUser sender, @NotNull TeleportRequest request) { diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/TeleportWarmupCancelledEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/TeleportWarmupCancelledEvent.java new file mode 100644 index 00000000..f107b1e0 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/TeleportWarmupCancelledEvent.java @@ -0,0 +1,75 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.event; + +import net.william278.huskhomes.teleport.TimedTeleport; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class TeleportWarmupCancelledEvent extends Event implements ITeleportWarmupCancelledEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private final TimedTeleport warp; + private final int duration; + private final int cancelledAfter; + private final CancelReason cancelReason; + + public TeleportWarmupCancelledEvent(@NotNull TimedTeleport warp, int duration, + int cancelledAfter, @NotNull CancelReason cancelReason) { + this.warp = warp; + this.duration = duration; + this.cancelledAfter = cancelledAfter; + this.cancelReason = cancelReason; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + + @Override + public int getWarmupDuration() { + return duration; + } + + @Override + public int cancelledAfter() { + return cancelledAfter; + } + + @NotNull + @Override + public TimedTeleport getTimedTeleport() { + return warp; + } + + @Override + public @NotNull CancelReason getCancelReason() { + return cancelReason; + } +} \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java index 1d12bbe4..6fefb8cd 100644 --- a/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java +++ b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java @@ -35,6 +35,8 @@ import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.function.Consumer; +import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason; + /** * An abstract dispatcher of events. */ @@ -71,6 +73,12 @@ public interface EventDispatcher { @NotNull ITeleportWarmupEvent getTeleportWarmupEvent(@NotNull TimedTeleport teleport, int duration); + @NotNull + ITeleportWarmupCancelledEvent getTeleportWarmupCancelledEvent(@NotNull TimedTeleport teleport, + int duration, + int cancelledAfter, + @NotNull CancelReason cancelReason); + @NotNull ISendTeleportRequestEvent getSendTeleportRequestEvent(@NotNull OnlineUser sender, @NotNull TeleportRequest request); diff --git a/common/src/main/java/net/william278/huskhomes/event/ITeleportWarmupCancelledEvent.java b/common/src/main/java/net/william278/huskhomes/event/ITeleportWarmupCancelledEvent.java new file mode 100644 index 00000000..ac96d0d0 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/event/ITeleportWarmupCancelledEvent.java @@ -0,0 +1,74 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.event; + +import net.william278.huskhomes.teleport.TimedTeleport; +import org.jetbrains.annotations.NotNull; + +/** + * Representation of an event that fires when a timed teleport warmup is cancelled. + */ +public interface ITeleportWarmupCancelledEvent extends Event { + + /** + * The duration of the timed teleport warmup in seconds. + * + * @return the teleport warmup duration + */ + int getWarmupDuration(); + + /** + * The time passed before the teleport warmup was cancelled in seconds. + * + * @return the time passed before the teleport warmup was cancelled + */ + int cancelledAfter(); + + /** + * The {@link TimedTeleport} not being executed due to the warmup being cancelled. + * + * @return the timed teleport that has started + */ + @NotNull + TimedTeleport getTimedTeleport(); + + /** + * The reason the teleport warmup was cancelled. + * + * @return the reason the teleport warmup was cancelled + */ + @NotNull + CancelReason getCancelReason(); + + /** + * The reason the teleport warmup was cancelled. + */ + enum CancelReason { + /** + * The teleport warmup was cancelled due to the player moving. + */ + PLAYER_MOVE, + /** + * The teleport warmup was cancelled due to the player taking damage. + */ + PLAYER_DAMAGE, + } + +} \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java index 243b099e..80e919ec 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java @@ -22,6 +22,7 @@ package net.william278.huskhomes.teleport; import de.themoep.minedown.adventure.MineDown; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.config.Settings; +import net.william278.huskhomes.event.ITeleportWarmupCancelledEvent; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.util.Task; @@ -42,6 +43,7 @@ public class TimedTeleport extends Teleport implements Runnable, Completable { private final OnlineUser teleporter; private final Position startLocation; private final double startHealth; + private final int warmupTime; private Task.Repeating task; private int timeLeft; @@ -51,6 +53,7 @@ public class TimedTeleport extends Teleport implements Runnable, Completable { super(teleporter, executor, target, type, updateLastPosition, actions, plugin); this.startLocation = teleporter.getPosition(); this.startHealth = teleporter.getHealth(); + this.warmupTime = warmupTime; this.timeLeft = Math.max(warmupTime, 0); this.teleporter = teleporter; } @@ -142,6 +145,8 @@ public class TimedTeleport extends Teleport implements Runnable, Completable { // Cancel the timed teleport if the player takes damage if (hasTeleporterTakenDamage() && plugin.getSettings().getGeneral().isTeleportWarmupCancelOnDamage()) { + plugin.fireEvent(plugin.getTeleportWarmupCancelledEvent(this, warmupTime, + timeLeft, ITeleportWarmupCancelledEvent.CancelReason.PLAYER_DAMAGE), null); plugin.getLocales().getLocale("teleporting_cancelled_damage") .ifPresent(teleporter::sendMessage); plugin.getLocales().getLocale("teleporting_action_bar_cancelled") @@ -153,6 +158,8 @@ public class TimedTeleport extends Teleport implements Runnable, Completable { // Cancel the timed teleport if the player moves if (hasTeleporterMoved() && plugin.getSettings().getGeneral().isTeleportWarmupCancelOnMove()) { + plugin.fireEvent(plugin.getTeleportWarmupCancelledEvent(this, warmupTime, + timeLeft, ITeleportWarmupCancelledEvent.CancelReason.PLAYER_MOVE), null); plugin.getLocales().getLocale("teleporting_cancelled_movement") .ifPresent(teleporter::sendMessage); plugin.getLocales().getLocale("teleporting_action_bar_cancelled") diff --git a/docs/API-Events.md b/docs/API-Events.md index 934bb471..90aa711f 100644 --- a/docs/API-Events.md +++ b/docs/API-Events.md @@ -1,24 +1,25 @@ HuskHomes provides a number of API events your plugin can listen to when certain actions are performed. Most of these events are cancellable, letting you stop them from executing if you wish. ## List of API events -| Bukkit Event class | Since | Cancellable | Description | -|-------------------------------|:-----:|:-----------:|--------------------------------------------------------------------------| -| `HomeCreateEvent` | 4.0 | ✅ | Called when a player sets a home | -| `HomeListEvent` | 3.0 | ✅ | Called when a player requests to view a list of homes / public homes | -| `HomeEditEvent` | 4.0 | ✅ | Called when a player edits a home (privacy, relocate, description, name) | -| `HomeDeleteEvent` | 3.0 | ✅ | Called when a player deletes a home† | -| `DeleteAllHomesEvent` | 3.2.1 | ✅ | Called when a player uses `/delhome all` to delete all their homes | -| `WarpCreateEvent` | 4.0 | ✅ | Called when a player sets a warp | -| `WarpListEvent` | 3.0 | ✅ | Called when a player requests to view a list of warps | -| `WarpEditEvent` | 4.0 | ✅ | Called when a player edits a warp (relocate, description, name) | -| `WarpDeleteEvent` | 3.0 | ✅ | Called when a player deletes a warp† | -| `DeleteAllWarpsEvent` | 3.2.1 | ✅ | Called when a player uses `/delwarp all` to delete all warps | -| `SendTeleportRequestEvent` | 4.1 | ✅ | Called when a player sends a teleport request (`/tpa`) | -| `ReceiveTeleportRequestEvent` | 4.1 | ✅ | Called when a player receives a teleport request from someone | -| `ReplyTeleportRequestEvent` | 4.1 | ✅ | Called when a player accepts or declines a teleport request | -| `TeleportWarmupEvent` | 3.0 | ✅ | Called when a player starts a teleport warmup countdown | -| `TeleportEvent` | 3.0 | ✅ | Called when a player is teleported‡ | -| `TeleportBackEvent` | 4.1 | ✅ | Called when a player teleports to their last position (`/back`)‡ | +| Bukkit Event class | Since | Cancellable | Description | +|--------------------------------|:-----:|:-----------:|--------------------------------------------------------------------------| +| `HomeCreateEvent` | 4.0 | ✅ | Called when a player sets a home | +| `HomeListEvent` | 3.0 | ✅ | Called when a player requests to view a list of homes / public homes | +| `HomeEditEvent` | 4.0 | ✅ | Called when a player edits a home (privacy, relocate, description, name) | +| `HomeDeleteEvent` | 3.0 | ✅ | Called when a player deletes a home† | +| `DeleteAllHomesEvent` | 3.2.1 | ✅ | Called when a player uses `/delhome all` to delete all their homes | +| `WarpCreateEvent` | 4.0 | ✅ | Called when a player sets a warp | +| `WarpListEvent` | 3.0 | ✅ | Called when a player requests to view a list of warps | +| `WarpEditEvent` | 4.0 | ✅ | Called when a player edits a warp (relocate, description, name) | +| `WarpDeleteEvent` | 3.0 | ✅ | Called when a player deletes a warp† | +| `DeleteAllWarpsEvent` | 3.2.1 | ✅ | Called when a player uses `/delwarp all` to delete all warps | +| `SendTeleportRequestEvent` | 4.1 | ✅ | Called when a player sends a teleport request (`/tpa`) | +| `ReceiveTeleportRequestEvent` | 4.1 | ✅ | Called when a player receives a teleport request from someone | +| `ReplyTeleportRequestEvent` | 4.1 | ✅ | Called when a player accepts or declines a teleport request | +| `TeleportWarmupEvent` | 3.0 | ✅ | Called when a player starts a teleport warmup countdown | +| `TeleportWarmupCancelledEvent` | 4.6.3 | ❌ | Called when a player cancels the teleport warmup | +| `TeleportEvent` | 3.0 | ✅ | Called when a player is teleported‡ | +| `TeleportBackEvent` | 4.1 | ✅ | Called when a player teleports to their last position (`/back`)‡ | † If the player uses `/delhome all` or `/delwarp all` to delete all their homes or all the warps, a single `DeleteAllHomesEvent` or `DeleteAllWarpsEvent` is fired instead. ‡ Called on the server the player *is teleported from*; not necessarily where the executor of the teleport is. diff --git a/fabric/src/main/java/net/william278/huskhomes/event/FabricEventDispatcher.java b/fabric/src/main/java/net/william278/huskhomes/event/FabricEventDispatcher.java index 56973a6b..c7b5c2ab 100644 --- a/fabric/src/main/java/net/william278/huskhomes/event/FabricEventDispatcher.java +++ b/fabric/src/main/java/net/william278/huskhomes/event/FabricEventDispatcher.java @@ -37,6 +37,8 @@ import java.lang.reflect.Method; import java.util.List; import java.util.logging.Level; +import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason; + public interface FabricEventDispatcher extends EventDispatcher { @SuppressWarnings("unchecked") @@ -68,6 +70,15 @@ public interface FabricEventDispatcher extends EventDispatcher { return TeleportWarmupCallback.SUPPLIER.apply(teleport, duration); } + @Override + @NotNull + default ITeleportWarmupCancelledEvent getTeleportWarmupCancelledEvent(@NotNull TimedTeleport teleport, + int duration, + int cancelledAfter, + @NotNull CancelReason cancelReason) { + return TeleportWarmupCancelledCallback.SUPPLIER.apply(teleport, duration, cancelledAfter, cancelReason); + } + @Override default @NotNull ISendTeleportRequestEvent getSendTeleportRequestEvent(@NotNull OnlineUser sender, @NotNull TeleportRequest request) { diff --git a/fabric/src/main/java/net/william278/huskhomes/event/TeleportWarmupCancelledCallback.java b/fabric/src/main/java/net/william278/huskhomes/event/TeleportWarmupCancelledCallback.java new file mode 100644 index 00000000..35efedd0 --- /dev/null +++ b/fabric/src/main/java/net/william278/huskhomes/event/TeleportWarmupCancelledCallback.java @@ -0,0 +1,75 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.util.ActionResult; +import net.william278.huskhomes.teleport.TimedTeleport; +import net.william278.huskhomes.util.QuadFunction; +import org.jetbrains.annotations.NotNull; + +import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason; + + +public interface TeleportWarmupCancelledCallback extends FabricEventCallback { + @NotNull + Event EVENT = EventFactory.createArrayBacked(TeleportWarmupCancelledCallback.class, + (listeners) -> (event) -> { + for (TeleportWarmupCancelledCallback listener : listeners) { + listener.invoke(event); + } + + return ActionResult.PASS; + }); + + @NotNull + QuadFunction SUPPLIER = + (timedTeleport, duration, cancelledAfter, cancelReason) -> + new ITeleportWarmupCancelledEvent() { + + @Override + @NotNull + public TimedTeleport getTimedTeleport() { + return timedTeleport; + } + + @Override + public int getWarmupDuration() { + return duration; + } + + @Override + public int cancelledAfter() { + return cancelledAfter; + } + + @Override + public @NotNull ITeleportWarmupCancelledEvent.CancelReason getCancelReason() { + return cancelReason; + } + + @NotNull + public Event getEvent() { + return EVENT; + } + + }; +} \ No newline at end of file diff --git a/fabric/src/main/java/net/william278/huskhomes/util/QuadFunction.java b/fabric/src/main/java/net/william278/huskhomes/util/QuadFunction.java new file mode 100644 index 00000000..ff50c48e --- /dev/null +++ b/fabric/src/main/java/net/william278/huskhomes/util/QuadFunction.java @@ -0,0 +1,35 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.util; + + +import java.util.Objects; +import java.util.function.Function; + +@FunctionalInterface +public interface QuadFunction { + + default QuadFunction andThen(Function after) { + Objects.requireNonNull(after); + return (t, u, v, w) -> after.apply(this.apply(t, u, v, w)); + } + + R apply(T var1, U var2, V var3, W var4); +} diff --git a/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java b/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java index dfb5248e..4e190f64 100644 --- a/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java +++ b/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java @@ -34,6 +34,8 @@ import org.spongepowered.api.event.Event; import java.util.List; +import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason; + public interface SpongeEventDispatcher extends EventDispatcher { @Override @@ -52,6 +54,15 @@ public interface SpongeEventDispatcher extends EventDispatcher { return new SpongeTeleportWarmupEvent(teleport, duration); } + @Override + @NotNull + default ITeleportWarmupCancelledEvent getTeleportWarmupCancelledEvent(@NotNull TimedTeleport teleport, + int duration, + int cancelledAfter, + @NotNull CancelReason cancelReason) { + return new SpongeTeleportWarmupCancelledEvent(teleport, duration, cancelledAfter, cancelReason); + } + @Override default @NotNull ISendTeleportRequestEvent getSendTeleportRequestEvent(@NotNull OnlineUser sender, @NotNull TeleportRequest request) { diff --git a/sponge/src/main/java/net/william278/huskhomes/event/SpongeTeleportWarmupCancelledEvent.java b/sponge/src/main/java/net/william278/huskhomes/event/SpongeTeleportWarmupCancelledEvent.java new file mode 100644 index 00000000..31a9e890 --- /dev/null +++ b/sponge/src/main/java/net/william278/huskhomes/event/SpongeTeleportWarmupCancelledEvent.java @@ -0,0 +1,70 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.event; + +import net.william278.huskhomes.teleport.TimedTeleport; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.api.event.Cause; +import org.spongepowered.api.event.Event; + +public class SpongeTeleportWarmupCancelledEvent implements ITeleportWarmupCancelledEvent, Event { + + private final TimedTeleport teleport; + private final int duration; + private final int cancelledAfter; + private final CancelReason cancelReason; + + public SpongeTeleportWarmupCancelledEvent(@NotNull TimedTeleport teleport, int duration, + int cancelledAfter, @NotNull CancelReason cancelReason) { + this.teleport = teleport; + this.duration = duration; + this.cancelledAfter = cancelledAfter; + this.cancelReason = cancelReason; + } + + @Override + public Cause cause() { + return Cause.builder() + .append(teleport.getTeleporter()) + .build(); + } + + @Override + public int getWarmupDuration() { + return duration; + } + + @NotNull + @Override + public TimedTeleport getTimedTeleport() { + return teleport; + } + + @Override + public int cancelledAfter() { + return cancelledAfter; + } + + @Override + public @NotNull CancelReason getCancelReason() { + return cancelReason; + } + +} \ No newline at end of file