Skip to content

Commit

Permalink
feat: add new API event for TeleportWarmupCancelledEvent (#628)
Browse files Browse the repository at this point in the history
* Added TeleportWarmupCancelledEvent

* Changed docs

* Added more JavaDocs
  • Loading branch information
alexdev03 authored May 9, 2024
1 parent 945fe0c commit 4d86f58
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 20 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import java.util.List;

import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason;

public interface BukkitEventDispatcher extends EventDispatcher {

@Override
Expand All @@ -52,6 +54,14 @@ default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.List;
import java.util.function.Consumer;

import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason;

/**
* An abstract dispatcher of events.
*/
Expand Down Expand Up @@ -71,6 +73,12 @@ default <T extends Event> void fireEvent(@NotNull T event, @Nullable Consumer<T>
@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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* 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,
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -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;

Expand All @@ -51,6 +53,7 @@ protected TimedTeleport(@NotNull OnlineUser executor, @NotNull OnlineUser telepo
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;
}
Expand Down Expand Up @@ -142,6 +145,8 @@ private boolean tickAndGetIfDone() {

// 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")
Expand All @@ -153,6 +158,8 @@ private boolean tickAndGetIfDone() {

// 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")
Expand Down
37 changes: 19 additions & 18 deletions docs/API-Events.md
Original file line number Diff line number Diff line change
@@ -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&dagger; |
| `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&dagger; |
| `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&ddagger; |
| `TeleportBackEvent` | 4.1 || Called when a player teleports to their last position (`/back`)&ddagger; |
| 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&dagger; |
| `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&dagger; |
| `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&ddagger; |
| `TeleportBackEvent` | 4.1 || Called when a player teleports to their last position (`/back`)&ddagger; |

&dagger; 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.
&ddagger; Called on the server the player *is teleported from*; not necessarily where the executor of the teleport is.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.List;
import java.util.logging.Level;

import static net.william278.huskhomes.event.ITeleportWarmupCancelledEvent.CancelReason;

public interface FabricEventDispatcher extends EventDispatcher {

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -68,6 +70,15 @@ default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) {
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) {
Expand Down
Loading

0 comments on commit 4d86f58

Please sign in to comment.