Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.skriptdev.skript.api.command;

import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.npc.commands.NPCCommand;

import java.util.Map;
import java.util.TreeMap;

/**
* Registration shortcuts for string to ArgumentType mappings.
*/
public class ArgUtils {

private static final Map<String, ArgumentType<?>> TYPES_MAP = new TreeMap<>();

public static void init() {
// BASIC
register(ArgTypes.BOOLEAN, "boolean", "bool");
register(ArgTypes.STRING, "string", "text");
register(ArgTypes.UUID, "uuid");

// NUMBERS
register(ArgTypes.DOUBLE, "double");
register(ArgTypes.FLOAT, "float");
register(ArgTypes.INTEGER, "integer", "int");

// ENTITY
register(NPCCommand.NPC_ROLE, "role", "npcrole", "npc_role");
register(ArgTypes.PLAYER_REF, "player_ref", "playerref");

// WORLD
register(ArgTypes.ROTATION, "rotation", "vector3f");
register(ArgTypes.VECTOR3I, "vector3i");
register(ArgTypes.WORLD, "world");
}

private static void register(ArgumentType<?> type, String... names) {
for (String name : names) {
TYPES_MAP.put(name, type);
}
}

/**
* Get an argument type by its name.
*
* @param name Name of the argument type.
* @return The argument type if found, otherwise null.
*/
public static ArgumentType<?> getType(String name) {
return TYPES_MAP.get(name);
}

}
100 changes: 100 additions & 0 deletions src/main/java/com/github/skriptdev/skript/api/command/CommandArg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.github.skriptdev.skript.api.command;

import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;

/**
* Represents a command argument which contains a name, argument type, description, and optional flag.
*/
public class CommandArg {

private final String name;
private final String description;
private final ArgumentType<?> type;
private final boolean optional;

/**
* @param name Name of the argument
* @param description Description of the argument
* @param type Type of the argument
* @param optional Whether the argument is optional
*/
private CommandArg(String name, String description, ArgumentType<?> type, boolean optional) {
this.name = name;
this.description = description;
this.type = type;
this.optional = optional;
}

public String getName() {
return this.name;
}

public String getDescription() {
return this.description;
}

public ArgumentType<?> getType() {
return this.type;
}

public boolean isOptional() {
return this.optional;
}

@Override
public String toString() {
return "CommandArg{" +
"name='" + this.name + '\'' +
", description='" + this.description + '\'' +
", type=" + this.type +
", optional=" + this.optional +
'}';
}

/** Parse a string into a CommandArg.
* @param a String to parse in the format of [name:type:desc] or <name:type:desc>
* @return CommandArg
*/
public static CommandArg parseArg(String a) {
if (a.startsWith("[<") && a.endsWith(">]")) {
a = a.substring(2, a.length() - 2);
return parseArg(a, true);
} else if (a.startsWith("<") && a.endsWith(">")) {
a = a.substring(1, a.length() - 1);
return parseArg(a, false);
} else {
return null;
}
}

private static CommandArg parseArg(String a, boolean optional) {
String name;
String description = "";
ArgumentType<?> type;
if (a.contains(":")) {
String[] split = a.split(":");
if (split.length == 2) {
if (split[1].startsWith("\"")) {
name = split[0];
type = ArgUtils.getType(split[0]);
description = split[1];
} else {
name = split[0];
type = ArgUtils.getType(split[1]);
}
} else {
name = split[0];
type = ArgUtils.getType(split[1]);
description = split[2];
}
} else {
name = a;
type = ArgUtils.getType(a);
}
if (description.startsWith("\"")) description = description.substring(1);
if (description.endsWith("\"")) description = description.substring(0, description.length() - 1);
if (type == null) return null;
return new CommandArg(name, description, type, optional);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.skriptdev.skript.plugin.elements;

import com.github.skriptdev.skript.plugin.Skript;
import com.github.skriptdev.skript.plugin.elements.command.ScriptCommand;
import com.github.skriptdev.skript.plugin.elements.conditions.ConditionHandler;
import com.github.skriptdev.skript.plugin.elements.effects.EffectHandler;
import com.github.skriptdev.skript.plugin.elements.events.EventHandler;
Expand Down Expand Up @@ -43,6 +44,9 @@ public void registerElements() {

// EVENTS
EventHandler.register(this.registration);

// COMMAND
ScriptCommand.register(this.registration);
}

public ListenerHandler getListenerHandler() {
Expand Down
Loading
Loading