Skip to content

Commit

Permalink
Command Rework - Clean up existing code (#7033)
Browse files Browse the repository at this point in the history
* initial changes to allow pr creation

* cleanup 1
  • Loading branch information
sovdeeth authored Sep 10, 2024
1 parent c9634b9 commit 17acf67
Show file tree
Hide file tree
Showing 20 changed files with 119 additions and 517 deletions.
18 changes: 0 additions & 18 deletions src/main/java/org/skriptlang/skript/commands/CommandModule.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.commands;

import ch.njol.skript.Skript;
Expand Down
40 changes: 10 additions & 30 deletions src/main/java/org/skriptlang/skript/commands/api/Argument.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.commands.api;

import ch.njol.skript.Skript;
Expand All @@ -26,23 +8,21 @@
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.log.RetainingLogHandler;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.WeakHashMap;

public class Argument<T> {

@Nullable
private final String name;
private final @Nullable String name;
private final Class<T> type;

private final boolean optional;
private final boolean single;

@Nullable
private final Expression<? extends T> defaultValue;
private final @Nullable Expression<? extends T> defaultValue;

// TODO in the future this map should be replaced and argument values
// should be stored on a argument-value map on each execution's "TriggerContext"
Expand All @@ -61,9 +41,7 @@ private Argument(
}

@ApiStatus.Internal
@Nullable
@SuppressWarnings("unchecked")
public static <T> Argument<T> of(
public static <T> @Nullable Argument<T> of(
@Nullable String name, Class<T> type,
boolean optional, boolean single,
@Nullable String defaultExpression
Expand All @@ -81,6 +59,7 @@ public static <T> Argument<T> of(
if (defaultExpression.startsWith("%") && defaultExpression.endsWith("%")) {
// attempt to parse this as an expression

//noinspection unchecked
parsedDefaultExpression = new SkriptParser(
defaultExpression.substring(1, defaultExpression.length() - 1),
SkriptParser.PARSE_EXPRESSIONS,
Expand All @@ -92,14 +71,17 @@ public static <T> Argument<T> of(

if (type == String.class) { // this is a string literal
if (defaultExpression.startsWith("\"") && defaultExpression.endsWith("\"")) {
//noinspection unchecked
parsedDefaultExpression =
(Expression<? extends T>) VariableString.newInstance(defaultExpression.substring(1, defaultExpression.length() - 1));
} else {
//noinspection unchecked
parsedDefaultExpression =
(Expression<? extends T>) new SimpleLiteral<>(defaultExpression, false);
}
} else { // any other kind of literal
// TODO is ParseContext.DEFAULT correct?
//noinspection unchecked
parsedDefaultExpression = new SkriptParser(
defaultExpression, SkriptParser.PARSE_LITERALS, ParseContext.DEFAULT
).parseExpression(type);
Expand All @@ -120,8 +102,7 @@ public static <T> Argument<T> of(
return new Argument<>(name, type, optional, single, parsedDefaultExpression);
}

@Nullable
public String getName() {
public @Nullable String getName() {
return name;
}

Expand All @@ -141,8 +122,7 @@ public void setValues(ScriptCommandEvent event, T[] values) {
valueMap.put(event, values);
}

@Nullable
public T[] getValues(ScriptCommandEvent event) {
public T @Nullable [] getValues(ScriptCommandEvent event) {
return valueMap.getOrDefault(event, defaultValue != null ? defaultValue.getArray(event) : null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.commands.api;

import ch.njol.skript.lang.VariableString;
import ch.njol.skript.localization.Language;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Timespan.TimePeriod;
import ch.njol.skript.variables.Variables;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -39,13 +23,11 @@ public class CommandCooldown {
private final Map<UUID, Date> cooldownEndDates = new HashMap<>();

private final Timespan cooldown;
private final VariableString cooldownMessage;
private final @UnknownNullability VariableString cooldownMessage;

private final String cooldownBypassPermission;
@Nullable
private final VariableString cooldownStorageVariableName;
private final @Nullable VariableString cooldownStorageVariableName;

@SuppressWarnings("ConstantConditions")
public CommandCooldown(Timespan cooldown, @Nullable VariableString cooldownMessage, String cooldownBypassPermission, @Nullable VariableString cooldownStorageVariableName) {
this.cooldown = cooldown;
this.cooldownMessage = cooldownMessage != null ? cooldownMessage :
Expand Down Expand Up @@ -92,14 +74,16 @@ public void applyCooldown(ScriptCommandSender sender, Event event) {
* @param startDate The retroactive start date of the cooldown. Must be in the past.
* @see #setRemainingDuration(ScriptCommandSender, Event, Timespan)
* @see #setEndDate(ScriptCommandSender, Event, Date)
* @throws IllegalArgumentException if start date is in the future.
*/
public void applyCooldown(ScriptCommandSender sender, Event event, Date startDate) {
if (startDate.isAfter(new Date()))
Date now = new Date();
if (startDate.isAfter(now))
throw new IllegalArgumentException("Start date must be in the past.");
if (hasBypassPermission(sender))
return;
Timespan elapsed = new Date().difference(startDate);
Timespan remaining = new Timespan(cooldown.getMilliSeconds() - elapsed.getMilliSeconds());
Timespan elapsed = now.difference(startDate);
Timespan remaining = new Timespan(cooldown.getAs(TimePeriod.MILLISECOND) - elapsed.getAs(TimePeriod.MILLISECOND));
setRemainingDuration(sender, event, remaining);
}

Expand Down Expand Up @@ -154,7 +138,7 @@ public Timespan getRemainingDuration(ScriptCommandSender sender, Event event) {
*/
public void setRemainingDuration(ScriptCommandSender sender, Event event, Timespan newDuration) {
Date endDate = null;
if (newDuration.getMilliSeconds() > 0) {
if (newDuration.getAs(TimePeriod.MILLISECOND) > 0) {
endDate = new Date();
endDate.add(newDuration);
}
Expand All @@ -168,8 +152,7 @@ public void setRemainingDuration(ScriptCommandSender sender, Event event, Timesp
* @param event The event used to evaluate the cooldown storage variable name, if one exists.
* @return The end date of the cooldown. Will be {@code null} if the {@link ScriptCommandSender} is not on cooldown.
*/
@Nullable
public Date getEndDate(ScriptCommandSender sender, Event event) {
public @UnknownNullability Date getEndDate(ScriptCommandSender sender, Event event) {
// prefer the cooldown storage variable if it exists
if (cooldownStorageVariableName != null)
return getEndDateVariable(event);
Expand All @@ -196,8 +179,7 @@ public Date getEndDate(ScriptCommandSender sender, Event event) {
* @param event The event used to evaluate the cooldown storage variable.
* @return The end date of the cooldown. Will be {@code null} if the {@link ScriptCommandSender} is not on cooldown.
*/
@Nullable
private Date getEndDateVariable(Event event) {
private @UnknownNullability Date getEndDateVariable(Event event) {
String variableName = getStorageVariableName(event);
Object variable = Variables.getVariable(variableName, null, false);
Date endDate = null;
Expand Down Expand Up @@ -262,8 +244,8 @@ private void setEndDateVariable(Event event, @Nullable Date newEndDate) {
* @param event The event used to evaluate the cooldown storage variable name, if one exists.
* @return The elapsed cooldown duration, or {@code null} if the {@link ScriptCommandSender} is not on cooldown.
*/
@Nullable
public Timespan getElapsedDuration(ScriptCommandSender sender, Event event) {
public @Nullable Timespan getElapsedDuration(ScriptCommandSender sender, Event event) {
// TODO: implement
return null;
}

Expand All @@ -277,6 +259,7 @@ public Timespan getElapsedDuration(ScriptCommandSender sender, Event event) {
* current cooldown, the {@link ScriptCommandSender} will be taken off cooldown.
*/
public void setElapsedDuration(ScriptCommandSender sender, Event event, Timespan newElapsedDuration) {
// TODO: implement
}

/**
Expand All @@ -301,16 +284,14 @@ public VariableString getCooldownMessage() {
/**
* @return The permission required to bypass the cooldown.
*/
@Nullable
public String getCooldownBypassPermission() {
public @Nullable String getCooldownBypassPermission() {
return cooldownBypassPermission;
}

/**
* @return The name of the variable used to store the remaining cooldown duration.
*/
@Nullable
public VariableString getCooldownStorageVariableName() {
public @Nullable VariableString getCooldownStorageVariableName() {
return cooldownStorageVariableName;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.commands.api;

import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

Expand All @@ -28,8 +10,7 @@ public interface CommandHandler {

boolean unregisterCommand(ScriptCommand scriptCommand);

@Nullable
ScriptCommand getScriptCommand(String label);
@Nullable ScriptCommand getScriptCommand(String label);

Collection<String> getScriptCommands();
Collection<String> getServerCommands();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.commands.api;

import org.bukkit.event.Event;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.commands.api;

import org.bukkit.event.Cancellable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.commands.api;

import ch.njol.skript.Skript;
Expand All @@ -38,7 +20,7 @@
import ch.njol.skript.variables.Variables;
import ch.njol.util.StringUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.commands.api.ScriptCommandSender.CommandSenderType;

import java.util.List;
Expand All @@ -59,12 +41,10 @@ public abstract class ScriptCommand {
private final String description;
private final CommandUsage usage;
private final List<String> aliases;
@Nullable
private final String permission;
private final @Nullable String permission;
private final VariableString permissionMessage;
private final List<CommandSenderType> executableBy;
@Nullable
private final CommandCooldown cooldown;
private final @Nullable CommandCooldown cooldown;
private final Trigger trigger;
private final List<Argument<?>> arguments;
private final SkriptPattern pattern;
Expand Down Expand Up @@ -271,6 +251,8 @@ private void setArgumentVariables(ScriptCommandEvent event) {
String name = argument.getName();
if (name != null) {
Object[] values = argument.getValues(event);
if (values == null)
continue;
if (argument.isSingle()) {
if (values.length > 0)
Variables.setVariable(name, values[0], event, true);
Expand Down
Loading

0 comments on commit 17acf67

Please sign in to comment.