Skip to content

Commit

Permalink
Merge pull request #38 from JvstvsHD/feature/cloud-commands
Browse files Browse the repository at this point in the history
Feature/cloud commands
  • Loading branch information
JvstvsHD authored Jul 28, 2024
2 parents 94a60fc + 7eecfb5 commit 194fd93
Show file tree
Hide file tree
Showing 42 changed files with 1,382 additions and 129 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.gradle
.idea
build
necrify-api/build
necrify-common/build
necrify-paper/build
necrify-velocity/build
necrify-velocity/run
8 changes: 6 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ sqlite = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqlite" }
mysql = { group = "com.mysql", name = "mysql-connector-j", version.ref = "mysql" }
adventure-api = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" }
adventure-text-minimessage = { group = "net.kyori", name = "adventure-text-minimessage", version.ref = "adventure" }
adventure-text-serializer-plain = { group = "net.kyori", name = "adventure-text-serializer-plain", version.ref = "adventure" }
cloud-core = { group = "org.incendo", name = "cloud-core", version.ref = "cloud" }
cloud-annotations = { group = "org.incendo", name = "cloud-annotations", version.ref = "cloud" }
cloud-velocity = { group = "org.incendo", name = "cloud-velocity", version.ref = "cloud" }
cloud-minecraft-extras = { group = "org.incendo", name = "cloud-minecraft-extras", version.ref = "cloud" }
cloud-brigadier = { group = "org.incendo", name = "cloud-brigadier", version.ref = "cloud" }
jetbrains-annotations = { group = "org.jetbrains", name = "annotations", version.ref = "jetbrains-annotations" }
eventbus = { group = "org.greenrobot", name = "eventbus-java", version.ref = "eventbus" }
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
Expand All @@ -51,5 +55,5 @@ slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }

database = ["postgresql", "hikari", "sadu", "mariadb", "sqlite", "mysql", "sadu-queries"]
jackson = ["jackson-databind", "jackson-yaml", "jackson-datatype-jsr310"]
adventure = ["adventure-api", "adventure-text-minimessage"]
cloud = ["cloud-core", "cloud-annotations"]
adventure = ["adventure-api", "adventure-text-minimessage", "adventure-text-serializer-plain"]
cloud = ["cloud-core", "cloud-annotations", "cloud-minecraft-extras"]
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.sql.Timestamp;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -43,6 +44,13 @@ public interface PunishmentDuration extends Comparable<PunishmentDuration> {
*
* @param source the source string.
* @return the parsed duration
* @throws Parser.ParseException if... <ul>
* <li>the source string is empty</li>
* <li>the source string does not contain parsable tokens</li>
* <li>the source string does not contain a unit character after a number</li>
* <li>the source string contains an unknown unit character</li>
* <li>the numeric value is negative</li>
* </ul>
* @see Parser#parse()
*/
static PunishmentDuration parse(String source) {
Expand Down Expand Up @@ -94,6 +102,7 @@ static PunishmentDuration fromMillis(long millis) {
/**
* Converts the given {@link Duration} into a {@link PunishmentDuration}. The duration is relative the given length
* into the future from a given point in time. It is absolute as soon as a punishment is enforced.
*
* @param duration how long the punishment should last
* @return the converted duration
*/
Expand Down Expand Up @@ -205,18 +214,23 @@ public void convert() {
int index = 0;
for (String number : numbers) {
index += number.length();
final long numericValue = Long.parseLong(number);
final long numericValue;
try {
numericValue = Long.parseLong(number);
} catch (NumberFormatException e) {
throw new ParseException("Not a number: " + e.getMessage());
}
if (numericValue < 0)
throw new IllegalArgumentException("Illegal numeric value: " + numericValue);
throw new ParseException("Illegal numeric value: " + numericValue);
final char unit;
try {
unit = Character.toLowerCase(source.charAt(index));
} catch (IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Number is not followed by unit marking character.");
throw new ParseException("Number is not followed by unit marking character.");
}
TimeUnit timeUnit = characterMapping.get(unit);
if (timeUnit == null)
throw new IllegalArgumentException("Unknown time unit for character '" + unit + "'");
throw new ParseException("Unknown time unit for character '" + unit + "'");
converted.put(timeUnit, numericValue);
index++;
}
Expand All @@ -231,9 +245,11 @@ public void convert() {
* @return the parsed duration
*/
public PunishmentDuration parse() {
if (source.isEmpty())
throw new ParseException("Source string is empty.");
convert();
if (rawDuration.isEmpty()) {
throw new IllegalArgumentException("Converted map is empty.");
throw new ParseException("Converted map is empty.");
}
return fromMillis(durationToMillis());
}
Expand All @@ -249,5 +265,11 @@ private long durationToMillis() {
}
return total;
}

public static class ParseException extends RuntimeException {
public ParseException(String message) {
super(message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public abstract class NecrifyEvent {

private final String name;
private EventOrigin origin = null;
private EventOrigin origin = EventOrigin.nullOrigin();
private EventDispatcher executingDispatcher = null;

public NecrifyEvent(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ static EventOrigin ofClass(Class<?> clazz) {
}

boolean originatesFrom(Object object);

static EventOrigin nullOrigin() {
return new NullEventOrigin();
}

class NullEventOrigin implements EventOrigin {

@Override
public boolean originatesFrom(Object object) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface PunishmentType {
* @since 1.0.1
*/
default boolean isMute() {
return this == StandardPunishmentType.MUTE || this == StandardPunishmentType.PERMANENT_MUTE;
return this == StandardPunishmentType.TEMPORARY_MUTE || this == StandardPunishmentType.PERMANENT_MUTE;
}

/**
Expand All @@ -45,6 +45,6 @@ default boolean isMute() {
* @since 1.0.1
*/
default boolean isBan() {
return this == StandardPunishmentType.BAN || this == StandardPunishmentType.PERMANENT_BAN;
return this == StandardPunishmentType.TEMPORARY_BAN || this == StandardPunishmentType.PERMANENT_BAN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

public enum StandardPunishmentType implements PunishmentType {

BAN(false, "BAN", 1),
TEMPORARY_BAN(false, "BAN", 1),
PERMANENT_BAN(true, "PERMANENT_BAN", 2),
MUTE(false, "MUTE", 3),
TEMPORARY_MUTE(false, "MUTE", 3),
PERMANENT_MUTE(true, "PERMANENT_MUTE", 4),
KICK(false, "KICK", 5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public interface TemporalPunishment extends Punishment {
CompletableFuture<Punishment> change(@NotNull PunishmentDuration newDuration, @Nullable Component newReason) throws PunishmentException;

@Override
default CompletableFuture<Punishment> change(Component newReason) throws PunishmentException {
return change(getDuration(), newReason);
default CompletableFuture<Punishment> change(@Nullable Component newReason) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,50 @@

package de.jvstvshd.necrify.api.user;

import de.jvstvshd.necrify.api.message.MessageProvider;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;

/**
* Represents an entity that is able to interact with the server via messages and may send commands to it. It is also
* able to hold permissions.
*/
public interface CommandSender {

/**
* Sends a message to the command sender. The message may be given in form of a {@link net.kyori.adventure.text.TranslatableComponent},
* which will be translated to the correct language when being displayed.
* @param message a non-null component that represents the message to be sent.
*/
void sendMessage(@NotNull Component message);

/**
* Sends a message to the command sender. The message must contain a valid translation key that is present in the
* language files of the server. The message will be translated to the correct language when being displayed, if a
* translation is available. If this key does not map to a translation, the key itself will be displayed.
* <p>
* Per default, this will use {@link de.jvstvshd.necrify.api.message.MessageProvider#provide(String, Component...)}
* @param key a non-null string that represents the translation key of the message to be sent.
*/
void sendMessage(@NotNull String key, Component... args);

/**
* Sends an error message to the command sender. This should be used to inform the command sender about an error
* that happened within the command execution, e.g. while updating data in the database. Ideally, this yields the same
* result as {@code sendMessage("error.internal")} content-wise, but may differ style-wise.
* @see #sendMessage(String, Component...)
* @see MessageProvider#internalError()
* @see MessageProvider#internalError(Locale)
*/
void sendErrorMessage();

/**
* Checks whether the command sender has a certain permission. This requires a permission system to be set up
* and the entity in question to exist.
* @param permission a non-null string that represents the permission to be checked.
* @return true if the command sender has the permission, false otherwise.
*/
boolean hasPermission(@NotNull String permission);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -80,7 +81,7 @@ public interface NecrifyUser extends CommandSender {
* @return the ban object representing the ban. The ban may not be active yet, as the execution takes some time to complete.
*/
@NotNull
Ban ban(@Nullable Component reason, @NotNull PunishmentDuration duration);
CompletableFuture<Ban> ban(@Nullable Component reason, @NotNull PunishmentDuration duration);

/**
* Bans the user permanently with the given reason. A permanently banned user is not able to join the server this system
Expand All @@ -93,7 +94,7 @@ public interface NecrifyUser extends CommandSender {
* @return the ban object representing the ban. The ban may not be active yet, as the execution takes some time to complete.
*/
@NotNull
Ban banPermanent(@Nullable Component reason);
CompletableFuture<Ban> banPermanent(@Nullable Component reason);

/**
* Mutes the user with the given reason and duration. A muted user is not able to send messages in the chat of the server
Expand All @@ -107,7 +108,7 @@ public interface NecrifyUser extends CommandSender {
* @return the mute object representing the mute. The mute may not be active yet, as the execution takes some time to complete.
*/
@NotNull
Mute mute(@Nullable Component reason, @NotNull PunishmentDuration duration);
CompletableFuture<Mute> mute(@Nullable Component reason, @NotNull PunishmentDuration duration);

/**
* Mutes the user permanently with the given reason. A permanently muted user is not able to send messages in the chat of
Expand All @@ -119,7 +120,7 @@ public interface NecrifyUser extends CommandSender {
* @return the mute object representing the mute. The mute may not be active yet, as the execution takes some time to complete.
*/
@NotNull
Mute mutePermanent(@Nullable Component reason);
CompletableFuture<Mute> mutePermanent(@Nullable Component reason);

/**
* Kicks the user with the given reason. A kicked user is removed from the server this system belongs to. They are able to
Expand All @@ -130,7 +131,7 @@ public interface NecrifyUser extends CommandSender {
* @return the kick object representing the kick. The kick may not be active yet, as the execution takes some time to complete.
*/
@NotNull
Kick kick(@Nullable Component reason);
CompletableFuture<Kick> kick(@Nullable Component reason);

/**
* This method queries all punishments with the given {@link UUID} of a player and returns them in a list.
Expand Down Expand Up @@ -188,6 +189,8 @@ default Optional<Punishment> getPunishment(@NotNull UUID punishmentUuid) {
return getPunishments().stream().filter(punishment -> punishment.getPunishmentUuid().equals(punishmentUuid)).findFirst();
}

Locale getLocale();

/* *//**
* Method to add punishments to users. This method is only meant to be used until events are implemented and all
* other components can be used so this method will not be used.
Expand Down
3 changes: 3 additions & 0 deletions necrify-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ dependencies {
exclude(group = "org.slf4j", module = "slf4j-api")
}
api(libs.bundles.cloud)
compileOnly(libs.cloud.brigadier)
compileOnly(libs.brigadier)
annotationProcessor(libs.cloud.annotations)
compileOnly(libs.slf4j.api)
compileOnly("com.google.code.gson:gson:2.10.1")
compileOnly(libs.bundles.adventure)
testImplementation(libs.junit.jupiter.api)
Expand Down
Loading

0 comments on commit 194fd93

Please sign in to comment.