feat: add new API event for TeleportWarmupCancelledEvent (#628)

* Added TeleportWarmupCancelledEvent

* Changed docs

* Added more JavaDocs
This commit is contained in:
AlexDev_ authored and GitHub committed 2024-05-09 15:32:58 +01:00
1 parent 945fe0c0e0
commit 4d86f580ae
12 files changed
+397 -20

No files matched your search

@@ -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);
@@ -0,0 +1,74 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* 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,
}
}
@@ -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")