This commit is contained in:
BradBot_1 2023-10-09 02:03:49 +01:00
commit 71c096c2a7
14 changed files with 831 additions and 0 deletions

201
LICENSE.txt Normal file
View file

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2023 BradBot_1
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

93
README.md Normal file
View file

@ -0,0 +1,93 @@
# Shortcut
A Minecraft plugin for creating command shortcuts without needing another plugin
> *Reload ready! This plugin has no adverse effects when /reload is executed: commands will be disabled and added according to the changes to the configuration*
## Configuration
Each command can be individually configured
The basic yaml structure is as follows:
```yml
# This is the command name/label that you will type ingame (think /command_name)
command_name:
# An OPTIONAL permission to use, this can be anything
# If the sender doesn't have this permission they cannot run this shortcut
permision: example.permission
# If the command can only be executed by players (this is also optional)
# For none players the execution method returns instantly
playerOnly: false
# An OPTIONAL way to describe how the command should be used
usage: "/command_name <value>"
# What is actually used to control what the shortcut does
executions:
- {
# This is the command to run
# %%0 is replaced with the first argument provided, %%1 is the second and so on
# Placeholders can be used if you so choose
run: say %%0,
# Who to run the command as
# Must be either "sender" OR "console"
as: sender
}
- {
# This is a message that is to be sent to the user
# Formatting and placeholders can be used here
display: Example message with §aFormatting!
}
```
### Example
The following is an example for a "gmc" command shortcut:
> Placeholders are utilised in this example, ensure you have PlaceHolderApi if you wish to use it
```yml
# the command label to use
gmc:
# permission required to run this shortcut
# this doesn't bypass the check on the original command if proxying!
# placeholders do NOT work here
permission: command.gamemode
# if the command can only be ran by a player
# placeholders do NOT work here
playerOnly: true
# displayed when an invalid version of this command is issued
# placeholders do NOT work here
usage: /gmc
executions:
# runs the command "gamemode creative" as the sender
# placeholders can be used here
- { run: gamemode creative %player_name%, as: console }
# displays this message to the executor
# placeholders can be used here
- { display: "§0[§a✓§0]§r You are now in creative mode" }
```
Below is each element explained:
| Component | Used Value | Description |
| --- | --- | --- |
| Permission | command.gamemode | Specifies the permission required to run the "gmc" command. Users attempting to execute this command must have the "command.gamemode" permission. |
| Player-Only | true | When set to `true`, this configuration ensures that only players (as opposed to console or other entities) can execute the "gmc" command. |
| Usage | /gmc | The "usage" field specifies the command syntax that users should follow to invoke the "gmc" command. |
Execution Step 1
| Action | Command to Run | As Sender |
| --- | --- | --- |
| Run a command | `gamemode creative %player_name%` | `console` |
This step instructs the system to set the executor's game mode to creative. The `%player_name%` placeholder is replaced with the executor's name. The command is executed as the console.
Execution Step 2
| Action | Message to Display |
| --- | --- |
| Display a message | "§0[§a✓§0]§r You are now in creative mode" |
This step displays a message to the executor indicating that they are now in creative mode. The message includes formatting for visual appeal.

39
configs/GAMEMODES.yml Normal file
View file

@ -0,0 +1,39 @@
#
# Thank you for using ShortCut!
# Make sure you have the most upto date version from modrinth:
# https://modrinth.com/mod/shortcut
#
# Placeholders are supported!
#
gmc:
permission: command.gamemode
playerOnly: true
usage: /gmc
executions:
- { run: gamemode creative %player_name%, as: console }
- { display: "§0[§a✓§0]§r You are now in creative mode" }
gms:
permission: command.gamemode
playerOnly: true
usage: /gms
executions:
- { run: gamemode survival %player_name%, as: console }
- { display: "§0[§a✓§0]§r You are now in survival mode" }
gma:
permission: command.gamemode
playerOnly: true
usage: /gma
executions:
- { run: gamemode survival %player_name%, as: console }
- { display: "§0[§a✓§0]§r You are now in adventure mode" }
gmsp:
permission: command.gamemode
playerOnly: true
usage: /gmsp
executions:
- { run: gamemode spectator %player_name%, as: sender }
- { display: "§0[§a✓§0]§r You are now in spectator mode" }

1
configs/README.md Normal file
View file

@ -0,0 +1 @@
In here are a list of config's that can be used as either an example or to get you started

61
pom.xml Normal file
View file

@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fun.bb1.spigot</groupId>
<artifactId>command-shortcuts</artifactId>
<version>1.0.0</version>
<name>ShortCut</name>
<description>Create simple shortcuts</description>
<packaging>jar</packaging>
<url>https://git.bb1.fun/Spigot/ShortCut</url>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean package</defaultGoal>
<finalName>Shortcut</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>repo</id>
<url>https://repo.bb1.fun/releases</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,56 @@
package fun.bb1.spigot.shortcut;
import java.util.List;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;
import fun.bb1.spigot.shortcut.config.Config;
/**
* Copyright 2023 BradBot_1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final class EntryPoint extends JavaPlugin {
private @Nullable List<Shortcut> shortcuts;
@Override
public void onLoad() {
this.saveDefaultConfig();
super.onLoad();
}
@Override
public void onEnable() {
this.getLogger().info("Thank you for using shortcut!");
if (this.shortcuts != null) {
this.getLogger().warning("Commands being reregistered without disabling them! This will cause issues");
}
this.shortcuts = new Config(this).getData().stream().map(Shortcut::new).toList();
this.shortcuts.forEach(shortcut -> {
shortcut.register();
this.getLogger().info("Registered the shortcut \"" + shortcut.getLabel() + '"');
});
}
@Override
public void onDisable() {
if (this.shortcuts != null) {
this.shortcuts.forEach(Shortcut::unregister);
this.shortcuts = null;
}
}
}

View file

@ -0,0 +1,98 @@
package fun.bb1.spigot.shortcut;
import java.lang.reflect.Field;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import fun.bb1.spigot.shortcut.config.ShortcutData;
/**
* Copyright 2023 BradBot_1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@Internal
final class Shortcut extends BukkitCommand {
private @NotNull ShortcutData data;
private boolean disabled = false;
@Internal
Shortcut(@NotNull final ShortcutData data) {
super(data.alias());
this.update(data);
}
@Override
public final boolean execute(final @NotNull CommandSender arg0, final @NotNull String arg1, final @NotNull String[] arg2) {
if (this.disabled) return false;
if (!this.data.canBeExecutedBy(arg0)) return false;
this.data.executeFor(arg0, arg1, arg2);
return true;
}
@Override
public final @NotNull List<String> tabComplete(final @NotNull CommandSender sender, final @NotNull String alias, final @NotNull String[] args) throws IllegalArgumentException {
if (this.disabled) return List.of();
return List.of(); // maybe?
}
@Internal
final void register() {
try {
Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
bukkitCommandMap.setAccessible(true);
CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
@Nullable Command cmd = commandMap.getCommand(this.data.alias());
if (cmd != null && cmd instanceof final Shortcut shortcut) {
shortcut.update(this.data);
this.disabled = false;
return;
}
cmd = commandMap.getCommand("shortcut:" + this.data.alias());
if (cmd != null && cmd instanceof final Shortcut shortcut) {
shortcut.update(this.data);
this.disabled = false;
return;
}
commandMap.register("shortcut", this);
this.disabled = false;
} catch (Exception e) {
e.printStackTrace();
}
}
@Internal
final void update(@NotNull final ShortcutData data) {
this.data = data;
this.setLabel(this.data.alias());
if (this.data.permission() != null) this.setPermission(this.data.permission());
this.setUsage(this.data.getUsage());
}
@Internal
final void unregister() {
this.setPermission(UUID.randomUUID().toString().replace('-', '.')); // make inaccessible
this.disabled = true;
}
}

View file

@ -0,0 +1,55 @@
package fun.bb1.spigot.shortcut.config;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
import fun.bb1.spigot.shortcut.config.executable.IExecutableElement;
/**
* Copyright 2023 BradBot_1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@Internal
public final class Config {
private final @NotNull Plugin plugin;
@Internal
public Config(final @NotNull Plugin plugin) {
this.plugin = plugin;
}
public final @NotNull List<ShortcutData> getData() {
final List<ShortcutData> datas = new ArrayList<ShortcutData>();
for (final String key : this.plugin.getConfig().getKeys(false)) {
final ConfigurationSection commandSection = this.plugin.getConfig().getConfigurationSection(key);
final List<IExecutableElement> executables = new ArrayList<IExecutableElement>();
if (commandSection.contains("executions")) {
for (final Map<?, ?> executionMap : commandSection.getMapList("executions")) {
if (executionMap.isEmpty()) continue;
executables.add(IExecutableElement.forSect(executionMap));
}
}
datas.add(new ShortcutData(key, commandSection.getString("usage"), commandSection.getString("permission"), commandSection.getBoolean("playerOnly", false), executables.toArray(IExecutableElement[]::new)));
}
return datas;
}
}

View file

@ -0,0 +1,44 @@
package fun.bb1.spigot.shortcut.config;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import fun.bb1.spigot.shortcut.config.executable.IExecutableElement;
/**
* Copyright 2023 BradBot_1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final record ShortcutData(@NotNull String alias, @Nullable String usage, @Nullable String permission, boolean playerOnly, @NotNull IExecutableElement[] executions) {
public final @NotNull String getUsage() {
if (this.usage != null) return this.usage;
return "/" + alias;
}
public final boolean canBeExecutedBy(@NotNull final CommandSender sender) {
if (this.playerOnly && !(sender instanceof Player)) return false;
if (this.permission == null) return true;
return sender.hasPermission(this.permission);
}
public final void executeFor(@NotNull final CommandSender sender, @NotNull final String aliasUsed, @NotNull final String[] arguments) {
for (final IExecutableElement execution : this.executions) {
execution.executeFor(sender, aliasUsed, arguments);
}
}
}

View file

@ -0,0 +1,45 @@
package fun.bb1.spigot.shortcut.config.executable;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
import me.clip.placeholderapi.PlaceholderAPI;
/**
* Copyright 2023 BradBot_1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@Internal
final class DisplayExecutable implements IExecutableElement {
private static final @NotNull String MESSAGE_KEY = "display";
private final @NotNull String message;
@Internal
DisplayExecutable(@NotNull final Map<String, Object> section) {
this.message = section.getOrDefault(MESSAGE_KEY, "Nothing to say, maybe configure this?").toString();
}
@Override
public final void executeFor(@NotNull final CommandSender sender, @NotNull final String aliasUsed, @NotNull final String[] arguments) {
sender.sendMessage((sender instanceof final Player player && Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) ? PlaceholderAPI.setPlaceholders(player, this.message) : this.message);
}
}

View file

@ -0,0 +1,47 @@
package fun.bb1.spigot.shortcut.config.executable;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
/**
* Copyright 2023 BradBot_1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@FunctionalInterface
public interface IExecutableElement extends CommandExecutor {
@Internal
public static @NotNull IExecutableElement forSect(@NotNull final Map<?, ?> section) {
@NotNull final Map<String, Object> newMap = new HashMap<String, Object>();
section.entrySet().forEach(e -> newMap.put(e.getKey().toString(), e.getValue()));
if (section.containsKey("run")) return new RunExecutable(newMap);
if (section.containsKey("display")) return new DisplayExecutable(newMap);
return (sender, alias, arguments) -> {};
}
public void executeFor(@NotNull final CommandSender sender, @NotNull final String aliasUsed, @NotNull final String[] arguments);
@Override
default boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
this.executeFor(arg0, arg2, arg3);
return true;
}
}

View file

@ -0,0 +1,66 @@
package fun.bb1.spigot.shortcut.config.executable;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
import me.clip.placeholderapi.PlaceholderAPI;
/**
* Copyright 2023 BradBot_1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@Internal
final class RunExecutable implements IExecutableElement {
private static final @NotNull String COMMAND_KEY = "run";
private static final @NotNull String SENDER_KEY = "as";
private static final @NotNull String SUBSTITUTION = "%%";
private final @NotNull String command;
private final boolean performSubstitution;
private final @NotNull ExecuteAs executeAs;
@Internal
RunExecutable(@NotNull final Map<String, Object> section) {
this.command = section.getOrDefault(COMMAND_KEY, "say You need to provide a command to run!").toString();
this.performSubstitution = this.command.contains(SUBSTITUTION);
this.executeAs = section.getOrDefault(SENDER_KEY, "sender").toString().toLowerCase().equals("console") ? ExecuteAs.CONSOLE : ExecuteAs.SENDER;
}
@Override
public final void executeFor(@NotNull final CommandSender sender, @NotNull final String aliasUsed, @NotNull final String[] arguments) {
String toRun = this.command;
if (this.performSubstitution) {
for (int i = 0; i < arguments.length; i++) {
toRun = toRun.replaceAll(SUBSTITUTION + i, arguments[i]);
}
}
if (sender instanceof final Player player && Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
toRun = PlaceholderAPI.setPlaceholders(player, toRun);
}
Bukkit.dispatchCommand(this.executeAs == ExecuteAs.CONSOLE ? Bukkit.getConsoleSender() : sender, toRun);
}
private static enum ExecuteAs {
SENDER,
CONSOLE,
;
}
}

View file

@ -0,0 +1,14 @@
#
# Thank you for using ShortCut!
# Make sure you have the most upto date version from modrinth:
# https://modrinth.com/mod/shortcut
#
# Placeholders are supported!
#
# Stuck? Checkout the configuration guide at:
# https://git.bb1.fun/Spigot/ShortCut
#
shortcuts:
executions:
- { display: Set up your own shortcuts by editing the config! }

View file

@ -0,0 +1,11 @@
main: fun.bb1.spigot.shortcut.EntryPoint
name: ShortCut
version: '1.0.0'
description: 'A command linking plugin'
api-version: '1.20'
api: '1.20'
authors: [ BradBot_1#2042 ]
prefix: ShortCut
website: https://modrinth.com/plugin/shortcut
load: POSTWORLD
softdepend: [ PlaceholderAPI ]