-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for commands.yml aliases in command cooldowns #3744
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
Merged
mdcfe
merged 13 commits into
EssentialsX:2.x
from
FrankHeijden:fix/alias-command-cooldown
Feb 20, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
60db00a
Add support for command aliases in PlayerCommandPreProcessEvent
FrankHeijden dbcf797
Merge branch '2.x' into fix/alias-command-cooldown
FrankHeijden e637e66
Move new files into src/main/java folders (as a result of refactor up…
FrankHeijden b6c5cc3
Refactor FormattedCommandAliasProvider
FrankHeijden ad8ce92
Merge branch '2.x' into fix/alias-command-cooldown
FrankHeijden 58fab76
Merge branch '2.x' into fix/alias-command-cooldown
FrankHeijden cd4007b
Merge branch '2.x' into fix/alias-command-cooldown
FrankHeijden c27c8e0
Update Essentials/src/main/java/com/earth2me/essentials/Essentials.java
mdcfe e850e72
Merge branch '2.x' into fix/alias-command-cooldown
mdcfe 32c65ea
Remove reflection call for FormattedCommandAlias class
FrankHeijden 345e425
Revert back to initial reflection call
FrankHeijden b432c30
Use MethodHandles instead of Method invocations + Make FormattedComma…
FrankHeijden 736802d
Merge branch '2.x' into fix/alias-command-cooldown
mdcfe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
15 changes: 15 additions & 0 deletions
15
providers/BaseProviders/src/main/java/net/ess3/provider/FormattedCommandAliasProvider.java
This file contains hidden or 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,15 @@ | ||
package net.ess3.provider; | ||
|
||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.FormattedCommandAlias; | ||
|
||
import java.util.List; | ||
|
||
public interface FormattedCommandAliasProvider extends Provider { | ||
|
||
List<String> createCommands(FormattedCommandAlias command, CommandSender sender, String[] args); | ||
|
||
String[] getFormatStrings(FormattedCommandAlias command); | ||
|
||
String buildCommand(FormattedCommandAlias command, CommandSender sender, String formatString, String[] args); | ||
} |
92 changes: 92 additions & 0 deletions
92
...Provider/src/main/java/net/ess3/nms/refl/providers/ReflFormattedCommandAliasProvider.java
This file contains hidden or 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,92 @@ | ||
package net.ess3.nms.refl.providers; | ||
|
||
import net.ess3.nms.refl.ReflUtil; | ||
import net.ess3.provider.FormattedCommandAliasProvider; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.FormattedCommandAlias; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasProvider { | ||
|
||
private final boolean paper; | ||
private final Field formatStringsField; | ||
private final MethodHandle buildCommandMethodHandle; | ||
|
||
public ReflFormattedCommandAliasProvider(boolean paper) { | ||
this.paper = paper; | ||
|
||
final Class<? extends FormattedCommandAlias> formattedCommandAliasClass; | ||
Field formatStringsField = null; | ||
MethodHandle buildCommandMethodHandle = null; | ||
try { | ||
formattedCommandAliasClass = FormattedCommandAlias.class; | ||
formatStringsField = ReflUtil.getFieldCached(formattedCommandAliasClass, "formatStrings"); | ||
|
||
final Class<?>[] parameterTypes; | ||
if (paper) { | ||
parameterTypes = new Class[] {CommandSender.class, String.class, String[].class}; | ||
} else { | ||
parameterTypes = new Class[] {String.class, String[].class}; | ||
} | ||
|
||
final Method buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", parameterTypes); | ||
buildCommandMethod.setAccessible(true); | ||
buildCommandMethodHandle = MethodHandles.lookup().unreflect(buildCommandMethod); | ||
} catch (final Exception ex) { | ||
ex.printStackTrace(); | ||
} finally { | ||
this.formatStringsField = formatStringsField; | ||
this.buildCommandMethodHandle = buildCommandMethodHandle; | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> createCommands(FormattedCommandAlias command, CommandSender sender, String[] args) { | ||
final List<String> commands = new ArrayList<>(); | ||
for (String formatString : getFormatStrings(command)) { | ||
final String cmd; | ||
try { | ||
cmd = buildCommand(command, sender, formatString, args); | ||
} catch (Throwable th) { | ||
continue; // Ignore, let server handle this. | ||
} | ||
|
||
if (cmd == null) continue; | ||
commands.add(cmd.trim()); | ||
} | ||
return commands; | ||
} | ||
|
||
@Override | ||
public String[] getFormatStrings(FormattedCommandAlias command) { | ||
try { | ||
return (String[]) formatStringsField.get(command); | ||
} catch (ReflectiveOperationException ex) { | ||
throw new RuntimeException(ex); // If this happens we have bigger problems... | ||
} | ||
} | ||
|
||
@Override | ||
public String buildCommand(FormattedCommandAlias command, CommandSender sender, String formatString, String[] args) { | ||
try { | ||
if (paper) { | ||
return (String) buildCommandMethodHandle.invoke(command, sender, formatString, args); | ||
} else { | ||
return (String) buildCommandMethodHandle.invoke(command, formatString, args); | ||
} | ||
} catch (Throwable ex) { | ||
throw new RuntimeException(ex); // If this happens we have bigger problems... | ||
} | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "NMS Reflection Provider for FormattedCommandAlias methods"; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.