Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command Rework - Clean up existing code #7033

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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;
@@ -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"
@@ -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
@@ -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,
@@ -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);
@@ -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;
}

@@ -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);
}

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;
@@ -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 :
@@ -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);
}

@@ -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);
}
@@ -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);
@@ -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;
@@ -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;
}

@@ -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
}

/**
@@ -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;
}

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;

@@ -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();
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;
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;
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;
@@ -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;
@@ -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;
@@ -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);
Loading