-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
examples/bukkit/src/main/java/dev/rollczi/example/bukkit/command/CatCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.rollczi.example.bukkit.command; | ||
|
||
import dev.rollczi.litecommands.annotations.argument.Arg; | ||
import dev.rollczi.litecommands.annotations.command.Command; | ||
import dev.rollczi.litecommands.annotations.context.Context; | ||
import dev.rollczi.litecommands.annotations.execute.Execute; | ||
import org.bukkit.Location; | ||
import org.bukkit.entity.Cat; | ||
import org.bukkit.entity.EntityType; | ||
|
||
@Command(name = "cat") | ||
public class CatCommand { | ||
|
||
@Execute | ||
void executeCat(@Context Location currentLocation, @Arg Cat.Type type) { | ||
Cat cat = (Cat) currentLocation.getWorld().spawnEntity(currentLocation, EntityType.CAT); | ||
cat.setCatType(type); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/OldEnumAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package dev.rollczi.litecommands.bukkit.argument; | ||
|
||
import dev.rollczi.litecommands.reflect.LiteCommandsReflectException; | ||
import dev.rollczi.litecommands.reflect.ReflectUtil; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class OldEnumAccessor { | ||
|
||
private static final Map<Class<?>, Method> VALUE_OF_METHODS = new HashMap<>(); | ||
private static final Map<Class<?>, Method> NAME_METHODS = new HashMap<>(); | ||
private static final Map<Class<?>, Method> VALUES_METHODS = new HashMap<>(); | ||
|
||
public static boolean isAvailable() { | ||
return getType().isPresent(); | ||
} | ||
|
||
public static Class<?> getTypeOrThrow() { | ||
return getType().orElseThrow(() -> new IllegalStateException("OldEnum is not available")); | ||
} | ||
|
||
public static Optional<Class<?>> getType() { | ||
try { | ||
return Optional.of(Class.forName("org.bukkit.util.OldEnum")); | ||
} catch (ClassNotFoundException classNotFoundException) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
public static Object invokeValueOf(Class<?> type, String source) { | ||
if (!isInstanceOfOldEnum(type)) { | ||
throw new IllegalArgumentException("Type is not an instance of OldEnum"); | ||
} | ||
|
||
Method valueOfMethod = VALUE_OF_METHODS.computeIfAbsent(type, key -> ReflectUtil.getMethod(type, "valueOf", String.class)); | ||
try { | ||
return ReflectUtil.invokeStaticMethod(valueOfMethod, source); | ||
} catch (LiteCommandsReflectException exception) { | ||
throw exception.toRuntimeException(); | ||
} | ||
} | ||
|
||
public static String invokeName(Object source) { | ||
Class<?> type = source.getClass(); | ||
if (!isInstanceOfOldEnum(type)) { | ||
throw new IllegalArgumentException("Type is not an instance of OldEnum"); | ||
} | ||
|
||
Method nameMethod = NAME_METHODS.computeIfAbsent(type, key -> ReflectUtil.getMethod(type, "name")); | ||
try { | ||
return ReflectUtil.invokeMethod(nameMethod, source); | ||
} catch (LiteCommandsReflectException exception) { | ||
throw exception.toRuntimeException(); | ||
} | ||
} | ||
|
||
public static Object[] invokeValues(Class<?> type) { | ||
if (!isInstanceOfOldEnum(type)) { | ||
throw new IllegalArgumentException("Type is not an instance of OldEnum"); | ||
} | ||
|
||
Method valuesMethod = VALUES_METHODS.computeIfAbsent(type, key -> ReflectUtil.getMethod(type, "values")); | ||
try { | ||
return ReflectUtil.invokeStaticMethod(valuesMethod); | ||
} catch (LiteCommandsReflectException exception) { | ||
throw exception.toRuntimeException(); | ||
} | ||
} | ||
|
||
private static @NotNull Boolean isInstanceOfOldEnum(Class<?> type) { | ||
return getType() | ||
.map(oldEnum -> oldEnum.isAssignableFrom(type)) | ||
.orElse(false); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/OldEnumArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dev.rollczi.litecommands.bukkit.argument; | ||
|
||
import dev.rollczi.litecommands.argument.Argument; | ||
import dev.rollczi.litecommands.argument.parser.ParseResult; | ||
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver; | ||
import dev.rollczi.litecommands.invalidusage.InvalidUsage; | ||
import dev.rollczi.litecommands.invocation.Invocation; | ||
import dev.rollczi.litecommands.suggestion.SuggestionContext; | ||
import dev.rollczi.litecommands.suggestion.SuggestionResult; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.bukkit.command.CommandSender; | ||
|
||
public class OldEnumArgument extends ArgumentResolver<CommandSender, Object> { | ||
|
||
private final Map<Class<?>, SuggestionResult> cachedOldEnumSuggestions = new HashMap<>(); | ||
|
||
@Override | ||
public boolean canParse(Argument<Object> argument) { | ||
return OldEnumAccessor.getType().map(type -> type.isAssignableFrom(argument.getType().getRawType())) | ||
.orElseThrow(() -> new IllegalStateException("OldEnumArgument can't be used without on old bukkit version")); | ||
} | ||
|
||
@Override | ||
protected ParseResult<Object> parse(Invocation<CommandSender> invocation, Argument<Object> context, String argument) { | ||
try { | ||
return ParseResult.success(OldEnumAccessor.invokeValueOf(context.getType().getRawType(), argument)); | ||
} catch (IllegalArgumentException ignored) { | ||
return ParseResult.failure(InvalidUsage.Cause.INVALID_ARGUMENT); | ||
} | ||
} | ||
|
||
@Override | ||
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<Object> argument, SuggestionContext context) { | ||
Class<?> oldEnumClass = argument.getType().getRawType(); | ||
|
||
return cachedOldEnumSuggestions.computeIfAbsent(oldEnumClass, key -> { | ||
Object[] oldEnums = OldEnumAccessor.invokeValues(oldEnumClass); | ||
if (oldEnums.length == 0) { | ||
return SuggestionResult.empty(); | ||
} | ||
|
||
return Arrays.stream(oldEnums) | ||
.map(oldEnum -> OldEnumAccessor.invokeName(oldEnum)) | ||
.collect(SuggestionResult.collector()); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters