Skip to content

Commit

Permalink
Implement events II
Browse files Browse the repository at this point in the history
- Implemented event calling
- fix mutes not working properly with the paper-extension
This closes #12.
  • Loading branch information
JvstvsHD committed Jul 14, 2024
1 parent 68b93c0 commit a9fdb85
Show file tree
Hide file tree
Showing 40 changed files with 632 additions and 239 deletions.
41 changes: 37 additions & 4 deletions api/src/main/java/de/jvstvshd/necrify/api/Necrify.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package de.jvstvshd.necrify.api;

import de.jvstvshd.necrify.api.event.EventDispatcher;
import de.jvstvshd.necrify.api.message.MessageProvider;
import de.jvstvshd.necrify.api.punishment.Punishment;
import de.jvstvshd.necrify.api.punishment.PunishmentManager;
Expand Down Expand Up @@ -68,33 +69,50 @@ public interface Necrify {
* @deprecated Will be removed with {@link PunishmentManager}.
*/
@Deprecated(since = "1.2.0", forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@ApiStatus.ScheduledForRemoval(inVersion = "1.3.0")
PunishmentManager getPunishmentManager();

/**
* @deprecated Will be removed with {@link PunishmentManager}.
*/
@Deprecated(since = "1.2.0", forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@ApiStatus.ScheduledForRemoval(inVersion = "1.3.0")
void setPunishmentManager(PunishmentManager punishmentManager);

/**
* @deprecated Will be removed with {@link PunishmentManager}.
*/
@Deprecated(since = "1.2.0", forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@ApiStatus.ScheduledForRemoval(inVersion = "1.3.0")
PlayerResolver getPlayerResolver();

/**
* @deprecated Will be removed with {@link PunishmentManager}.
*/
@Deprecated(since = "1.2.0", forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@ApiStatus.ScheduledForRemoval(inVersion = "1.3.0")
void setPlayerResolver(PlayerResolver playerResolver);

/**
* Returns the executor service used by the plugin.
*
* @return the executor service.
* @deprecated Rename in favor of {@link #getExecutor()}.
*/
@ApiStatus.ScheduledForRemoval(inVersion = "1.3.0")
@Deprecated(since = "1.2.0", forRemoval = true)
@NotNull
ExecutorService getService();

/**
* Returns the executor service used by the plugin. This {@link ExecutorService} is used for asynchronous operations,
* such as database queries, event dispatching or time-consuming network/IO operations.
*
* @return the executor service.
*/
@NotNull
ExecutorService getExecutor();

@NotNull
MessageProvider getMessageProvider();

Expand All @@ -116,4 +134,19 @@ public interface Necrify {
default <T extends Punishment> CompletableFuture<Optional<T>> getPunishment(@NotNull UUID punishmentId) {
return getPunishmentManager().getPunishment(punishmentId, getService());
}

/**
* Returns the system's event dispatcher.
*
* @return the event dispatcher.
*/
@NotNull
EventDispatcher getEventDispatcher();

/**
* Sets the event dispatcher.
*
* @param eventDispatcher the event dispatcher to set.
*/
void setEventDispatcher(@NotNull EventDispatcher eventDispatcher);
}
23 changes: 23 additions & 0 deletions api/src/main/java/de/jvstvshd/necrify/api/event/NecrifyEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package de.jvstvshd.necrify.api.event;

import de.jvstvshd.necrify.api.event.origin.EventOrigin;

/**
* Represents an event that can be dispatched by the {@link EventDispatcher}.
* All events must extend this class.
Expand All @@ -33,6 +35,7 @@
public abstract class NecrifyEvent {

private final String name;
private EventOrigin origin = null;
private EventDispatcher executingDispatcher = null;

public NecrifyEvent(String name) {
Expand Down Expand Up @@ -62,4 +65,24 @@ NecrifyEvent setExecutingDispatcher(EventDispatcher executingDispatcher) {
this.executingDispatcher = executingDispatcher;
return this;
}

/**
* Gets the origin of this event.
*
* @return the origin of this event.
*/
public EventOrigin getOrigin() {
return origin;
}

/**
* Sets the origin of this event.
*
* @param origin the origin of this event.
* @return this event.
*/
public NecrifyEvent setOrigin(EventOrigin origin) {
this.origin = origin;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of Necrify (formerly Velocity Punishment), which is licensed under the MIT license.
*
* Copyright (c) 2022-2024 JvstvsHD
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package de.jvstvshd.necrify.api.event.origin;

public record ClassEventOrigin(Class<?> origin) implements EventOrigin {

@Override
public boolean originatesFrom(Object object) {
return origin.isInstance(object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of Necrify (formerly Velocity Punishment), which is licensed under the MIT license.
*
* Copyright (c) 2022-2024 JvstvsHD
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package de.jvstvshd.necrify.api.event.origin;

public interface EventOrigin {

static EventOrigin ofString(String name) {
return new StringEventOrigin(name);
}

static EventOrigin ofClass(Class<?> clazz) {
return new ClassEventOrigin(clazz);
}

boolean originatesFrom(Object object);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of Necrify (formerly Velocity Punishment), which is licensed under the MIT license.
*
* Copyright (c) 2022-2024 JvstvsHD
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package de.jvstvshd.necrify.api.event.origin;

public record StringEventOrigin(String origin) implements EventOrigin {

@Override
public boolean originatesFrom(Object object) {
return object instanceof String string && string.equals(origin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class PunishmentChangedEvent extends PunishmentEvent {

private final Punishment oldPunishment;

public PunishmentChangedEvent(String name, Punishment punishment, Punishment oldPunishment) {
super(name, punishment);
public PunishmentChangedEvent(Punishment punishment, Punishment oldPunishment) {
super("punishment_changed", punishment);
this.oldPunishment = oldPunishment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package de.jvstvshd.necrify.api.event.user;

import de.jvstvshd.necrify.api.user.NecrifyUser;
import de.jvstvshd.necrify.api.user.UserDeletionReason;

/**
* Event that is fired when a user is deleted.
Expand All @@ -33,38 +34,20 @@
*/
public class UserDeletedEvent extends UserEvent {

private final Reason reason;
private final UserDeletionReason reason;

/**
* Creates a new UserDeletedEvent.
*
* @param user The user that was deleted.
* @param reason The reason why the user was deleted.
*/
public UserDeletedEvent(NecrifyUser user, Reason reason) {
public UserDeletedEvent(NecrifyUser user, UserDeletionReason reason) {
super("user_deleted", user);
this.reason = reason;
}

public Reason getReason() {
public UserDeletionReason getReason() {
return reason;
}

/**
* The reason why the user was deleted.
*
* @since 1.2.0
*/
public enum Reason {

/**
* The user was deleted because the user requested it. This may due to data protection regulation.
*/
USER_REQUESTED,

/**
* The user was deleted by the system or a team member.
*/
USER_DELETED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import de.jvstvshd.necrify.api.punishment.util.ReasonHolder;
import de.jvstvshd.necrify.api.user.NecrifyUser;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.LocalDateTime;
import java.util.UUID;
Expand All @@ -51,51 +53,56 @@ public interface Punishment extends ReasonHolder {
* Punishes the player finally.
*
* @return a {@link CompletableFuture} containing the exerted punishment
* @throws PunishmentException if the punishment could not be exerted
* @throws PunishmentException if the punishment could not be exerted (inside the future)
*/
CompletableFuture<Punishment> punish();

/**
* Cancels this punishment thus allowing the player e.g. to join the server
*
* @return a {@link CompletableFuture} containing the cancelled punishment
* @throws PunishmentException if the punishment could not be cancelled
* @throws PunishmentException if the punishment could not be cancelled (inside the future)
*/
CompletableFuture<Punishment> cancel();

/**
* Changes the duration and reason of this punishment. This method can be used if a player created an appeal an it was accepted.
*
* @param newReason the new reason which should be displayed to the player
* @param newReason the new reason which should be displayed to the player or null if the reason should not be changed
* @return a {@link CompletableFuture} containing the new punishment
* @throws PunishmentException if the punishment could not be changed
* @see #cancel()
*/
CompletableFuture<Punishment> change(Component newReason);
CompletableFuture<Punishment> change(@Nullable Component newReason);

/**
* Returns the type of this punishment. By default, this is a field from {@link StandardPunishmentType}.
*
* @return the type of this punishment
* @see StandardPunishmentType
*/
@NotNull
PunishmentType getType();

/**
* @return the id of this punishment.
*/
@NotNull
UUID getPunishmentUuid();

/**
* This will return the value of {@link NecrifyUser#getUuid()} from {@link #getUser()}.
*
* @return the uuid of the punished player.
* @since 1.0.1
* @deprecated Consider using {@link #getUser()} instead.
*/
@NotNull
UUID getUuid();

/**
* @return the user this punishment is affecting
* @since 1.2.0
*/
@NotNull
NecrifyUser getUser();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* <a href="https://github.com/users/JvstvsHD/projects/5/views/1?pane=issue&itemId=65367115">NecrifyUser</a>.
*/
@Deprecated(since = "1.2.0", forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@ApiStatus.ScheduledForRemoval(inVersion = "1.3.0")
public interface PunishmentManager {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* <a href="https://github.com/users/JvstvsHD/projects/5/views/1?pane=issue&itemId=65367115">NecrifyUser</a>.
*/
@Deprecated(since = "1.2.0", forRemoval = true)
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@ApiStatus.ScheduledForRemoval(inVersion = "1.3.0")
public interface PlayerResolver {

/**
Expand Down
Loading

0 comments on commit a9fdb85

Please sign in to comment.