Skip to content

Commit

Permalink
New Version
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Apr 22, 2024
1 parent 84a90f4 commit efbad03
Show file tree
Hide file tree
Showing 34 changed files with 1,128 additions and 1,013 deletions.
2 changes: 1 addition & 1 deletion bungee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'io.github.mqzn'
version '1.1.5'
version '1.1.6'

repositories {
mavenCentral()
Expand Down
14 changes: 1 addition & 13 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {


group 'io.github.mqzn'
version '1.1.5'
version '1.1.6'

repositories {
mavenCentral()
Expand All @@ -35,7 +35,6 @@ dependencies {
compileOnly 'org.jetbrains:annotations:24.0.1'
compileOnly "net.kyori:adventure-api:4.13.1"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

java {
Expand Down Expand Up @@ -64,17 +63,6 @@ kotlin {
}


compileKotlin {
kotlinOptions {
jvmTarget = "17"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "17"
}
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public <E extends Enum<E>, C, CO> void parse(CO annotatedCommand) {
//default Execution
builder
.defaultExecutor(
(sender, context) -> invokeMethod(annotatedCommand, method, sender, CommandArgs.create(context)));
(sender, context) -> invokeMethod(annotatedCommand, method, context, CommandArgs.create(context)));
}

continue;
Expand All @@ -148,8 +148,8 @@ public <E extends Enum<E>, C, CO> void parse(CO annotatedCommand) {
assert executionMetaMeta != null;

var loadedData = loadMethodParameters(manager, cmdAnnotation.name(), executionMetaMeta, annotatedCommand.getClass(), method);
var arguments = loadedData.right;
var flags = loadedData.left;
var arguments = loadedData.getRight();
var flags = loadedData.getLeft();


if (executionMetaMeta.senderType() != Object.class)
Expand All @@ -167,7 +167,7 @@ public <E extends Enum<E>, C, CO> void parse(CO annotatedCommand) {
.flags(flags)
.execute((sender, context) -> {
Object[] valuesToUse = readValues(method, sender, context);
invokeMethod(annotatedCommand, method, valuesToUse);
invokeMethod(annotatedCommand, method, context, valuesToUse);
});

builder.syntax(syntaxBuilder.build());
Expand Down Expand Up @@ -261,7 +261,7 @@ private <C> SubCommandBuilder<S, C> loadSubCommandBuilder(Command cmd,

subBuilder = subBuilder
.defaultExecution(
(sender, context) -> invokeMethod(subCommandInstance, defaultExecutionMethod, sender));
(sender, context) -> invokeMethod(subCommandInstance, defaultExecutionMethod, context));
}

Method executeMethod = Arrays.stream(subClass.getDeclaredMethods())
Expand Down Expand Up @@ -296,7 +296,7 @@ private <C> SubCommandBuilder<S, C> loadSubCommandBuilder(Command cmd,

subBuilder = subBuilder.execute((sender, context) -> {
Object[] valuesToUse = readValues(executeMethod, sender, context);
invokeMethod(subCommandInstance, executeMethod, valuesToUse);
invokeMethod(subCommandInstance, executeMethod, context, valuesToUse);
});
}

Expand Down Expand Up @@ -574,15 +574,15 @@ private boolean isParamFlag(@NotNull Parameter parameter) {
if (parent.equals(Object.class)) {

var data = loadMethodParameters(manager, commandName, executionMeta, subCommandClass, method);
var args = data.right;
var args = data.getRight();

Argument<?>[] modifiedArgs = new Argument[args.length + 1];
modifiedArgs[0] = Argument.literal(info.name())
.aliases(info.aliases());

if (args.length - 1 >= 0) System.arraycopy(args, 0, modifiedArgs, 1, args.length - 1);

return new ResolvedSubCommandMethod(args, modifiedArgs, data.left);
return new ResolvedSubCommandMethod(args, modifiedArgs, data.getLeft());
}

String[] split = executionMeta.syntax().isEmpty() ? new String[0] : executionMeta.syntax().split(Pattern.quote(" "));
Expand Down Expand Up @@ -669,7 +669,7 @@ public String permission() {
};

var data = loadMethodParameters(manager, commandName, newExecutionMeta, subCommandClass, method);
return new ResolvedSubCommandMethod(arguments, data.right, data.left);
return new ResolvedSubCommandMethod(arguments, data.getRight(), data.getLeft());
}


Expand Down Expand Up @@ -823,7 +823,7 @@ else if (flag != null && !manager.flagRegistry().flagExists(flag))
}


return Pair.Companion.of(flags, args);
return Pair.of(flags, args);
}


Expand Down Expand Up @@ -863,11 +863,11 @@ private String annotationNotPresent(Class<?> subClass, Class<? extends Annotatio
return String.format("Subcommand class '%s' is NOT annotated with @%s", subClass.getName(), annotation.getSimpleName());
}

private <T> void invokeMethod(T instance, Method method, Object... objects) {
private <T> void invokeMethod(T instance, Method method, Context<S> context, Object... objects) {
try {
method.invoke(instance, objects);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
} catch (Exception ex) {
manager.exceptionHandler().handleException(ex, context.sender(), context);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.mqzn.commands.base.caption;

import io.github.mqzn.commands.base.context.Context;
import io.github.mqzn.commands.exceptions.CommandException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.jetbrains.annotations.NotNull;
Expand All @@ -16,13 +15,13 @@ public interface Caption<S> {

@NotNull CaptionKey key();

@NotNull <E extends CommandException> TextComponent message(S sender, Context<S> context, E exception);
@NotNull TextComponent message(S sender, Context<S> context, Throwable exception);

interface CaptionResult<S> {

@NotNull TextComponent messageResult(@NotNull S sender,
@NotNull Context<S> context,
@Nullable CommandException exception);
@Nullable Throwable exception);

}

Expand Down Expand Up @@ -52,9 +51,9 @@ record ImmutableCaption<S>(CaptionKey key, CaptionResult<S> messageCreator) impl


@Override
public @NotNull <E extends CommandException> TextComponent message(@NotNull S sender,
@NotNull Context<S> context,
@Nullable E exception) {
public @NotNull TextComponent message(@NotNull S sender,
@NotNull Context<S> context,
@Nullable Throwable exception) {
return messageCreator.messageResult(sender, context, exception);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.github.mqzn.commands.base.context.Context;
import io.github.mqzn.commands.base.manager.CommandManager;
import io.github.mqzn.commands.exceptions.CommandException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -34,10 +33,10 @@ public Caption<S> getCaption(CaptionKey key) {
return captions.get(key);
}

public <E extends CommandException> void sendCaption(@NotNull S sender,
@NotNull Context<S> commandContext,
@Nullable E exception,
@NotNull CaptionKey key) {
public void sendCaption(@NotNull S sender,
@NotNull Context<S> commandContext,
@Nullable Throwable exception,
@NotNull CaptionKey key) {
var caption = getCaption(key);

if (caption == null) return;
Expand All @@ -48,12 +47,13 @@ public void sendCaption(S sender, Context<S> commandContext, CaptionKey key) {
this.sendCaption(sender, commandContext, null, key);
}

public <E extends CommandException> void sendCaption(@NotNull S sender,
@NotNull Context<S> commandContext,
@Nullable E exception,
@NotNull Caption<S> caption) {
public void sendCaption(@NotNull S sender,
@NotNull Context<S> commandContext,
@Nullable Throwable exception,
@NotNull Caption<S> caption) {
var text = caption.message(sender, commandContext, exception);
manager.getSenderWrapper().sendMessage(sender, text);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.mqzn.commands.base.cooldown;

import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class CommandCooldown {

public static final CommandCooldown EMPTY = new CommandCooldown(0, TimeUnit.SECONDS);
private final long value;
private final TimeUnit unit;
private final Duration duration;

public CommandCooldown(long value, TimeUnit unit) {
this.value = value;
this.unit = unit;
this.duration = Duration.of(value, unit.toChronoUnit());
}

public boolean isEmpty() {
return value <= 0;
}

public long toMillis() {
return duration.toMillis();
}

public long getValue() {
return value;
}

public TimeUnit getUnit() {
return unit;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof CommandCooldown that)) return false;
return value == that.value && unit == that.unit;
}

@Override
public int hashCode() {
return Objects.hash(value, unit);
}
}


This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.github.mqzn.commands.base.cooldown;

import io.github.mqzn.commands.base.caption.Caption;
import io.github.mqzn.commands.base.caption.CaptionKey;
import io.github.mqzn.commands.base.caption.Message;
import io.github.mqzn.commands.base.context.Context;
import io.github.mqzn.commands.utilities.TimeParser;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

public final class CooldownCaption<S> implements Caption<S> {

public static String formatUnit(TimeUnit unit) {
var unitName = unit.name().toLowerCase(Locale.getDefault());
unitName = unitName.substring(0, unitName.length() - 1);
return unitName + "(s)";
}

public static long calculateRemainingTime(long lastTime, CommandCooldown commandCooldown) {
var diff = System.currentTimeMillis() - lastTime;
return commandCooldown.toMillis() - diff;
}

@Override
public @NotNull CaptionKey key() {
return CaptionKey.COMMAND_IN_COOLDOWN;
}

@Override
public @NotNull TextComponent message(S sender, Context<S> context, Throwable exception) {
var command = context.commandUsed();
var manager = command.manager();
var cooldown = command.cooldown();
var lastTimeCommandExecuted = manager.getCommandCooldown(manager.getSenderWrapper().senderName(sender));
if (lastTimeCommandExecuted == null) lastTimeCommandExecuted = 0L;
// Send a caption telling the user that he's in a cooldown
// Calculating remaining time
var parser = TimeParser.parse(calculateRemainingTime(lastTimeCommandExecuted, cooldown));
var timeData = parser.highestLogicalUnitValue();

return cooldownMessage(timeData.getLeft(), timeData.getRight(), context);
}

public TextComponent cooldownMessage(long time, TimeUnit unit, Context<S> context) {
return Message.prefixed(Message.EXECUTION_ERROR)
.append(
Component.text(
String.format(
"Command '" + context.commandUsed().name()
+ "' is in cooldown for %d %s", time, formatUnit(unit)
), NamedTextColor.YELLOW
)
);
}
}


Loading

0 comments on commit efbad03

Please sign in to comment.