Skip to content

Commit

Permalink
Complete velocity implementation of punishment log system (no usabili…
Browse files Browse the repository at this point in the history
…ty yet, only availability)
  • Loading branch information
JvstvsHD committed Dec 21, 2024
1 parent 7a5b2aa commit 2672abe
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of Necrify (formerly Velocity Punishment), a plugin designed to manage player's punishments for the platforms Velocity and partly Paper.
* Copyright (C) 2022-2024 JvstvsHD
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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

import de.jvstvshd.necrify.api.event.NecrifyEvent;
import de.jvstvshd.necrify.api.punishment.log.PunishmentLogEntry;

public class PunishmentLogEvent extends NecrifyEvent {

private final PunishmentLogEntry action;

public PunishmentLogEvent(PunishmentLogEntry action) {
super("punishment_log");
this.action = action;
}

public PunishmentLogEntry getEntry() {
return action;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import de.jvstvshd.necrify.api.PunishmentException;
import de.jvstvshd.necrify.api.punishment.log.PunishmentLog;
import de.jvstvshd.necrify.api.punishment.log.PunishmentLogEntry;
import de.jvstvshd.necrify.api.punishment.util.ReasonHolder;
import de.jvstvshd.necrify.api.user.NecrifyUser;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -190,9 +191,19 @@ default Punishment getSuccessorOrNull() {

/**
* Loads the punishment log of this punishment. This will load all log entries that have been made on this punishment.
*
* @return a {@link CompletableFuture} containing the punishment log of this punishment
* @since 1.2.2
*/
@NotNull
CompletableFuture<PunishmentLog> loadPunishmentLog();

/**
* Creates a log entry object that contains all the data the punishment currently holds. Its value may be equivalent
* to the last log entry in {@link #loadPunishmentLog() punishment log}.<br>
* Please note that this entry is not able to iterate over previous and next entries. If you want to do so, use
* {@link #loadPunishmentLog()} instead.
*/
@NotNull
PunishmentLogEntry createCurrentLogEntry();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,30 @@

/**
* Represents a log of a punishment, containing all actions that have been performed on the punishment.
*
* @since 1.2.2
*/
public interface PunishmentLog {

/**
* Returns the punishment this log belongs to.
*
* @return the punishment this log belongs to.
*/
@NotNull
Punishment getPunishment();

/**
* Returns a list containing all entries of this log.
*
* @return a list containing all entries of this log.
*/
@NotNull
List<PunishmentLogEntry> getEntries();

/**
* Returns a list containing all entries of this log with the given action. If no entries with the given action are found, an empty list is returned.
*
* @param action the action to filter the entries by
* @return a list containing all entries of this log with the given action.
*/
Expand All @@ -55,6 +59,7 @@ public interface PunishmentLog {

/**
* Returns the first entry of this log with the given action. If no entry with the given action is found, null is returned.
*
* @param action the action to filter the entries by
* @return the first entry of this log with the given action.
*/
Expand All @@ -63,6 +68,7 @@ public interface PunishmentLog {

/**
* Returns the entry at the given index. If the index is out of bounds, an exception is thrown.
*
* @param index the index of the entry
* @return the entry at the given index.
* @throws IndexOutOfBoundsException if the index is out of bounds
Expand All @@ -72,6 +78,7 @@ public interface PunishmentLog {

/**
* Returns the latest entry of this log. If the log is empty, an exception is thrown.
*
* @return the latest entry of this log.
* @throws IndexOutOfBoundsException if the log is empty
*/
Expand All @@ -82,8 +89,11 @@ default PunishmentLogEntry getLatestEntry() {

/**
* Logs a new action with the given message. The action is automatically associated with the punishment of this log.
* @param action the action to log
*
* @param action the action to log
* @param message the message to log
* @param actor the actor that performed the action
* @throws IllegalArgumentException if the action can only be logged once and has already been logged
*/
void log(@NotNull PunishmentLogAction action, @NotNull String message, @NotNull NecrifyUser actor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface PunishmentLogAction {
boolean onlyOnce();

/**
* A punishment was created. This action can only be logged once.
* A punishment was created (it was created on the storage and enforced - no instantiation). This action can only be logged once.
*/
PunishmentLogAction CREATED = new SimplePunishmentLogAction("created", true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
import java.util.Objects;
import java.time.LocalDateTime;
import java.util.Optional;

/**
* Represents an entry in a {@link PunishmentLog}. This contains all information about a punishment log entry.
* There is no information about old values if they have been changed, this has to be done by using {@link #previous() the previous entry}.
* There is no information about old values if they have been changed, this has to be done by using {@link #previousOrThis() the previous entry}.
*
* @param actor the actor who performed the action or null if the user does not exist anymore
* @param message the message of the action
Expand All @@ -43,23 +44,45 @@
* @param log the log this entry belongs to
* @param instant the instant the action was performed
* @param index the index of this entry in the log (0-based)
* @param beginsAt the instant the punishment begins at
* @since 1.2.2
*/
public record PunishmentLogEntry(@Nullable NecrifyUser actor, @Nullable String message,
@NotNull PunishmentDuration duration, @NotNull Component reason,
@Nullable Punishment predecessor, @NotNull Punishment punishment,
@Nullable Punishment successor,
@Nullable Punishment successor, @NotNull LocalDateTime beginsAt,
@NotNull PunishmentLogAction action, @NotNull PunishmentLog log,
@NotNull Instant instant, int index) implements Comparable<PunishmentLogEntry> {
@NotNull LocalDateTime instant,
int index) implements Comparable<PunishmentLogEntry> {

/**
* Returns the previous entry in the log. If this is the first entry, an empty optional is returned.
*
* @return the previous entry in the log or an empty optional if this is the first entry
*/
@NotNull
public Optional<PunishmentLogEntry> previous() {
return Optional.ofNullable(log.getEntries().get(index - 1));
}

/**
* Returns the previous entry in the log. If this is the first entry, this entry is returned.
*
* @return the previous entry in the log
*/
@NotNull
public PunishmentLogEntry previous() {
return Objects.requireNonNullElse(log.getEntries().get(index - 1), this);
public PunishmentLogEntry previousOrThis() {
return previous().orElse(this);
}

/**
* Returns the next entry in the log. If this is the last entry, an empty optional is returned.
*
* @return the next entry in the log or an empty optional if this is the last entry
*/
@NotNull
public Optional<PunishmentLogEntry> next() {
return Optional.ofNullable(log.getEntries().get(index + 1));
}

/**
Expand All @@ -68,8 +91,8 @@ public PunishmentLogEntry previous() {
* @return the next entry in the log
*/
@NotNull
public PunishmentLogEntry next() {
return Objects.requireNonNullElse(log.getEntries().get(index + 1), this);
public PunishmentLogEntry nextOrThis() {
return next().orElse(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,8 @@ public Logger getLogger() {
//TODO return just a set of objects. Create a new User object that does not get loaded from the database.
public abstract Set<Pair<String, UUID>> getOnlinePlayers();

@NotNull
public abstract NecrifyUser getSystemUser();

public abstract boolean isWhitelistActive();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
package de.jvstvshd.necrify.common.punishment;


import de.jvstvshd.necrify.api.duration.PunishmentDuration;
import de.jvstvshd.necrify.api.event.EventDispatcher;
import de.jvstvshd.necrify.api.event.punishment.PunishmentCancelledEvent;
import de.jvstvshd.necrify.api.event.punishment.PunishmentPersecutedEvent;
import de.jvstvshd.necrify.api.message.MessageProvider;
import de.jvstvshd.necrify.api.punishment.Punishment;
import de.jvstvshd.necrify.api.punishment.log.PunishmentLog;
import de.jvstvshd.necrify.api.punishment.log.PunishmentLogAction;
import de.jvstvshd.necrify.api.punishment.log.PunishmentLogEntry;
import de.jvstvshd.necrify.api.user.NecrifyUser;
import de.jvstvshd.necrify.common.AbstractNecrifyPlugin;
import de.jvstvshd.necrify.common.punishment.log.NecrifyPunishmentLog;
Expand All @@ -35,7 +38,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -253,4 +258,15 @@ void setSuccessor0(Punishment successor) {
return (cachedLog = log);
}, executor);
}

@Override
public @NotNull PunishmentLogEntry createCurrentLogEntry() {
return new PunishmentLogEntry(plugin.getSystemUser(), "Current state", PunishmentDuration.ofPunishment(this),
getReason(), getPredecessor(), this, getSuccessorOrNull(), getCreationTime(), PunishmentLogAction.CREATED,
cachedLog, LocalDateTime.now(), -1);
}

public void log(PunishmentLogAction action, String message, NecrifyUser actor) {
loadPunishmentLog().thenAccept(log -> log.log(action, message, actor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import de.jvstvshd.necrify.api.punishment.Punishment;
import de.jvstvshd.necrify.api.punishment.StandardPunishmentType;
import de.jvstvshd.necrify.api.punishment.TemporalPunishment;
import de.jvstvshd.necrify.api.punishment.log.PunishmentLogAction;
import de.jvstvshd.necrify.api.user.NecrifyUser;
import de.jvstvshd.necrify.common.AbstractNecrifyPlugin;
import de.jvstvshd.necrify.common.io.Adapters;
Expand Down Expand Up @@ -94,6 +95,8 @@ protected void checkValidity() {
if (!getType().isBan() && !getType().isMute()) {
throw new IllegalStateException("only bans and mutes can be changed");
}
var oldDuration = getDuration();
var oldReason = getReason();
return executeAsync(() -> {
var newCreatedAt = creationTime == null ? getCreationTime() : creationTime;
var newRsn = newReason == null ? getReason() : newReason;
Expand Down
Loading

0 comments on commit 2672abe

Please sign in to comment.