More comments for the command API, Arguments methods are NotNull since id should not be decided at runtime

This commit is contained in:
themode 2020-10-30 18:43:14 +01:00
parent 3b2f17c826
commit 6d29f955e4
4 changed files with 41 additions and 38 deletions

View File

@ -11,7 +11,6 @@ import net.minestom.server.utils.math.FloatRange;
import net.minestom.server.utils.math.IntRange;
import net.minestom.server.utils.time.UpdateOption;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -20,9 +19,15 @@ import java.util.List;
import java.util.Map;
/**
* Class used to retrieve argument data.
* Class used to retrieve argument data in a {@link CommandExecutor}.
* <p>
* All id are the one specified in the {@link net.minestom.server.command.builder.arguments.Argument} constructor.
* <p>
* All methods are @{@link NotNull} in the sense that you should not have to verify their validity since if the syntax
* is called, it means that all of its arguments are correct. Be aware that trying to retrieve an argument not present
* in the syntax will result in a {@link NullPointerException}.
*/
public class Arguments {
public final class Arguments {
private Map<String, Object> args = new HashMap<>();
@ -46,84 +51,87 @@ public class Arguments {
return (float) getObject(id);
}
@Nullable
@NotNull
public String getString(@NotNull String id) {
return (String) getObject(id);
}
@Nullable
@NotNull
public String getWord(@NotNull String id) {
return getString(id);
}
@Nullable
@NotNull
public String[] getStringArray(@NotNull String id) {
return (String[]) getObject(id);
}
@Nullable
@NotNull
public ChatColor getColor(@NotNull String id) {
return (ChatColor) getObject(id);
}
@Nullable
@NotNull
public UpdateOption getTime(@NotNull String id) {
return (UpdateOption) getObject(id);
}
@Nullable
@NotNull
public Enchantment getEnchantment(@NotNull String id) {
return (Enchantment) getObject(id);
}
@Nullable
@NotNull
public Particle getParticle(@NotNull String id) {
return (Particle) getObject(id);
}
@Nullable
@NotNull
public PotionEffect getPotionEffect(@NotNull String id) {
return (PotionEffect) getObject(id);
}
@Nullable
@NotNull
public EntityType getEntityType(@NotNull String id) {
return (EntityType) getObject(id);
}
@Nullable
@NotNull
public IntRange getIntRange(@NotNull String id) {
return (IntRange) getObject(id);
}
@Nullable
@NotNull
public FloatRange getFloatRange(@NotNull String id) {
return (FloatRange) getObject(id);
}
@Nullable
@NotNull
public List<Entity> getEntities(@NotNull String id) {
return (List<Entity>) getObject(id);
}
@Nullable
@NotNull
public ItemStack getItemStack(@NotNull String id) {
return (ItemStack) getObject(id);
}
@Nullable
@NotNull
public NBTCompound getNbtCompound(@NotNull String id) {
return (NBTCompound) getObject(id);
}
@Nullable
@NotNull
public NBT getNBT(@NotNull String id) {
return (NBT) getObject(id);
}
@Nullable
@NotNull
public Object getObject(@NotNull String id) {
return args.getOrDefault(id, null);
return args.computeIfAbsent(id, s -> {
throw new NullPointerException(
"The argument with the id " + id + " has no value assigned, be sure to check your arguments id, your syntax, and that you do not change the argument id dynamically.");
});
}
protected void setArg(@NotNull String id, Object value) {

View File

@ -14,7 +14,7 @@ import java.util.Collection;
import java.util.List;
/**
* Represents a command which have suggestion/auto-completion
* Represents a command which have suggestion/auto-completion.
* <p>
* The command works using a list of valid syntaxes.
* For instance we could build the command

View File

@ -10,5 +10,13 @@ import org.jetbrains.annotations.NotNull;
*/
@FunctionalInterface
public interface CommandExecutor {
/**
* Executes the command callback once the syntax has been called (or the default executor)
*
* @param source the sender of the command
* @param args contains all the parsed arguments,
* the id is the one initialized when creating the argument object
*/
void apply(@NotNull CommandSender source, @NotNull Arguments args);
}

View File

@ -1,10 +1,6 @@
package net.minestom.server.extensions;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import lombok.extern.slf4j.Slf4j;
import net.minestom.server.extras.selfmodification.MinestomOverwriteClassLoader;
import net.minestom.server.utils.validate.Check;
@ -13,27 +9,18 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.Mixins;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.zip.ZipFile;
@Slf4j
public class ExtensionManager {
public final class ExtensionManager {
private final static String INDEV_CLASSES_FOLDER = "minestom.extension.indevfolder.classes";
private final static String INDEV_RESOURCES_FOLDER = "minestom.extension.indevfolder.resources";