From 4f9df8f3f0ceebb55cc8f552b628f3a550cbe4df Mon Sep 17 00:00:00 2001 From: CarmJos Date: Fri, 3 Dec 2021 20:12:41 +0800 Subject: [PATCH] =?UTF-8?q?1.0.0=20=E5=88=9D=E5=A7=8B=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 33 +++ .github/workflows/maven.yml | 38 ++++ .gitignore | 4 + README.md | 27 +++ pom.xml | 195 ++++++++++++++++++ .../cc/carm/plugin/protalcommands/Main.java | 90 ++++++++ .../protalcommands/listener/UserListener.java | 42 ++++ .../protalcommands/util/ColorParser.java | 33 +++ .../protalcommands/util/MessageUtil.java | 74 +++++++ src/main/resources/config.yml | 5 + src/main/resources/plugin.yml | 9 + 11 files changed, 550 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 .github/workflows/maven.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/cc/carm/plugin/protalcommands/Main.java create mode 100644 src/main/java/cc/carm/plugin/protalcommands/listener/UserListener.java create mode 100644 src/main/java/cc/carm/plugin/protalcommands/util/ColorParser.java create mode 100644 src/main/java/cc/carm/plugin/protalcommands/util/MessageUtil.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..506585f --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,33 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Deploy + +on: + # 支持手动触发构建 + workflow_dispatch: + release: + # 创建release的时候触发 + types: [ published ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: "Set up JDK" + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + cache: maven + server-id: github + server-username: MAVEN_USERNAME + server-password: MAVEN_TOKEN + - name: "Deploy" + run: mvn -B deploy --file pom.xml -DskipTests + env: + MAVEN_USERNAME: ${{ github.repository_owner }} + MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}} \ No newline at end of file diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..b7ae340 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,38 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Build + +on: + # 支持手动触发构建 + workflow_dispatch: + push: + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: "Set up JDK" + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + cache: maven + server-id: github + server-username: MAVEN_USERNAME + server-password: MAVEN_TOKEN + - name: "Package" + run: mvn -B package --file pom.xml + env: + MAVEN_USERNAME: ${{ github.repository_owner }} + MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}} + - name: "Target Stage" + run: mkdir staging && cp target/*.jar staging + - name: "Upload artifact" + uses: actions/upload-artifact@v2 + with: + name: Artifact + path: staging \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..445f9d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.idea/ +/target/ +./*.iml +*.iml diff --git a/README.md b/README.md new file mode 100644 index 0000000..e66d3d8 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +```text + ___ _ _ ___ _ +( _`\ ( )_ (_ ) ( _`\ ( ) +| |_) ) _ _ __ | ,_) _ _ | | | ( (_) _ ___ ___ ___ ___ _ _ ___ _| | ___ +| ,__/'/'_`\ ( '__)| | /'_` ) | | | | _ /'_`\ /' _ ` _ `\/' _ ` _ `\ /'_` )/' _ `\ /'_` |/',__) +| | ( (_) )| | | |_ ( (_| | | | | (_( )( (_) )| ( ) ( ) || ( ) ( ) |( (_| || ( ) |( (_| |\__, \ +(_) `\___/'(_) `\__)`\__,_)(___) (____/'`\___/'(_) (_) (_)(_) (_) (_)`\__,_)(_) (_)`\__,_)(____/ +``` + +# PortalCommands + +当玩家进入传送门时执行对应指令。 +~~~~ +## 依赖 + +- **[必须]** 插件本体基于 [Spigot-API](https://hub.spigotmc.org/stash/projects/SPIGOT) 、[BukkitAPI](http://bukkit.org/) 实现。~~~~ +- **[推荐]** 变量部分基于 [PlaceholderAPI](https://www.spigotmc.org/resources/6245/) 实现。 + +详细依赖列表可见 [Dependencies](https://github.com/CarmJos/PortalCommands/network/dependencies) 。 + +## 支持与捐赠 + +若您觉得本插件做的不错,您可以捐赠支持我! + +感谢您成为开源项目的支持者! + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5683b17 --- /dev/null +++ b/pom.xml @@ -0,0 +1,195 @@ + + + 4.0.0 + + cc.carm.plugom + portalcommands + 1.0.0 + + PortalCommands + 当玩家进入传送门时执行对应指令。 + https://github.com/CarmJos/PortalCommands + + + GitHub Issues + ${project.url}/issues + + + + GitHub Actions + ${project.url}/actions/workflows/maven.yml + + + + CarmJos + Carm Jos + carm@carm.cc + https://work.carm.cc + + Main Developer + + + + + + 8 + 8 + UTF-8 + UTF-8 + + + + + + carm-repo + Carm's Repo + https://repo.carm.cc/repository/maven-public/ + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + placeholder-api-repo + https://repo.extendedclip.com/content/repositories/placeholderapi/ + + + + oss-repo + https://oss.sonatype.org/content/groups/public/ + + + + nexus + https://mvn.lumine.io/repository/maven-public/ + + + + maven-central + https://repo1.maven.org/maven2/ + + + + github + GitHub Packages + https://maven.pkg.github.com/CarmJos/${project.name} + + + + + ${project.url}/releases + + + + + + org.spigotmc + spigot-api + 1.17-R0.1-SNAPSHOT + provided + + + + me.clip + placeholderapi + 2.10.9 + provided + + + + junit + junit + 4.13.2 + test + + + + org.jetbrains + annotations + 22.0.0 + provided + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + UTF-8 + -parameters + + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.0 + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + package + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.3 + + + package + + shade + + + false + + + + + + + *:* + + META-INF/MANIFEST.MF + META-INF/*.txt + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + false + + + + + + src/main/resources + true + + + + + \ No newline at end of file diff --git a/src/main/java/cc/carm/plugin/protalcommands/Main.java b/src/main/java/cc/carm/plugin/protalcommands/Main.java new file mode 100644 index 0000000..2586ba0 --- /dev/null +++ b/src/main/java/cc/carm/plugin/protalcommands/Main.java @@ -0,0 +1,90 @@ +package cc.carm.plugin.protalcommands; + +import cc.carm.plugin.protalcommands.listener.UserListener; +import cc.carm.plugin.protalcommands.util.ColorParser; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabCompleter; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class Main extends JavaPlugin { + + private static Main instance; + public static boolean debugMode = true; + + @Override + public void onEnable() { + instance = this; + log(getName() + " " + getDescription().getVersion() + " &7开始加载..."); + long startTime = System.currentTimeMillis(); + + log("加载配置文件..."); + saveDefaultConfig(); + reloadConfig(); + + log("注册监听器..."); + regListener(new UserListener()); + + log("加载完成 ,共耗时 " + (System.currentTimeMillis() - startTime) + " ms 。"); + + } + + @Override + public void onDisable() { + log(getName() + " " + getDescription().getVersion() + " 开始卸载..."); + long startTime = System.currentTimeMillis(); + + log("卸载监听器..."); + Bukkit.getServicesManager().unregisterAll(this); + + log("卸载完成 ,共耗时 " + (System.currentTimeMillis() - startTime) + " ms 。"); + } + + /** + * 注册监听器 + * + * @param listener 监听器 + */ + public static void regListener(Listener listener) { + Bukkit.getPluginManager().registerEvents(listener, getInstance()); + } + + public static void log(String message) { + Bukkit.getConsoleSender().sendMessage(ColorParser.parse("[" + getInstance().getName() + "] " + message)); + } + + public static void debug(String message) { + if (debugMode) { + log("[DEBUG] " + message); + } + } + + public static Main getInstance() { + return instance; + } + + public static void registerCommand(String commandName, + @NotNull CommandExecutor executor) { + registerCommand(commandName, executor, null); + } + + public static void registerCommand(String commandName, + @NotNull CommandExecutor executor, + @Nullable TabCompleter tabCompleter) { + PluginCommand command = Bukkit.getPluginCommand(commandName); + if (command == null) return; + command.setExecutor(executor); + if (tabCompleter != null) command.setTabCompleter(tabCompleter); + } + + public static List getCommands() { + return getInstance().getConfig().getStringList("commands"); + } + +} diff --git a/src/main/java/cc/carm/plugin/protalcommands/listener/UserListener.java b/src/main/java/cc/carm/plugin/protalcommands/listener/UserListener.java new file mode 100644 index 0000000..48a6312 --- /dev/null +++ b/src/main/java/cc/carm/plugin/protalcommands/listener/UserListener.java @@ -0,0 +1,42 @@ +package cc.carm.plugin.protalcommands.listener; + +import cc.carm.plugin.protalcommands.Main; +import me.clip.placeholderapi.PlaceholderAPI; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerTeleportEvent; + +import java.util.List; + +public class UserListener implements Listener { + + + @EventHandler + public void onTeleport(PlayerTeleportEvent event) { + Location to = event.getTo(); + if (to == null || to.getWorld() == null) { + return; + } + World.Environment environment = to.getWorld().getEnvironment(); + if (environment == World.Environment.NETHER) { + event.setCancelled(true); //取消传送 + executeCommands(event.getPlayer(), Main.getCommands()); + } + } + + private static void executeCommands(Player player, List commands) { + if (commands == null || commands.isEmpty()) return; + for (String command : commands) { + try { + player.chat(PlaceholderAPI.setPlaceholders(player, command)); + } catch (Exception exception) { + exception.printStackTrace(); + } + } + } + + +} diff --git a/src/main/java/cc/carm/plugin/protalcommands/util/ColorParser.java b/src/main/java/cc/carm/plugin/protalcommands/util/ColorParser.java new file mode 100644 index 0000000..d8792af --- /dev/null +++ b/src/main/java/cc/carm/plugin/protalcommands/util/ColorParser.java @@ -0,0 +1,33 @@ + +package cc.carm.plugin.protalcommands.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ColorParser { + + public static String parse(String text) { + text = parseHexColor(text); + return parseColor(text); + } + + public static String parseColor(final String text) { + return text.replaceAll("&", "§").replace("§§", "&"); + } + + public static String parseHexColor(String text) { + Pattern pattern = Pattern.compile("&\\((&?#[0-9a-fA-F]{6})\\)"); + Matcher matcher = pattern.matcher(text); + while (matcher.find()) { + String hexColor = text.substring(matcher.start() + 2, matcher.end() - 1); + hexColor = hexColor.replace("&", ""); + StringBuilder bukkitColorCode = new StringBuilder('§' + "x"); + for (int i = 1; i < hexColor.length(); i++) { + bukkitColorCode.append('§').append(hexColor.charAt(i)); + } + text = text.replaceAll("&\\(" + hexColor + "\\)", bukkitColorCode.toString().toLowerCase()); + matcher.reset(text); + } + return text; + } +} diff --git a/src/main/java/cc/carm/plugin/protalcommands/util/MessageUtil.java b/src/main/java/cc/carm/plugin/protalcommands/util/MessageUtil.java new file mode 100644 index 0000000..95ea77f --- /dev/null +++ b/src/main/java/cc/carm/plugin/protalcommands/util/MessageUtil.java @@ -0,0 +1,74 @@ +package cc.carm.plugin.protalcommands.util; + +import me.clip.placeholderapi.PlaceholderAPI; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.*; + +public class MessageUtil { + + public static boolean hasPlaceholderAPI() { + return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null; + } + + public static void send(CommandSender sender, List messages) { + for (String s : messages) { + sender.sendMessage(ColorParser.parse(s)); + } + } + + public static void send(CommandSender sender, String... messages) { + send(sender, Arrays.asList(messages)); + } + + public static void sendWithPlaceholders(CommandSender sender, String... messages) { + sendWithPlaceholders(sender, Arrays.asList(messages)); + } + + public static void sendWithPlaceholders(CommandSender sender, List messages) { + if (messages == null || messages.isEmpty()) return; + if (hasPlaceholderAPI() && sender instanceof Player) { + send(sender, PlaceholderAPI.setPlaceholders((Player) sender, messages)); + } else { + send(sender, messages); + } + } + + public static void sendWithPlaceholders(CommandSender sender, List messages, String param, Object value) { + sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value}); + } + + public static void sendWithPlaceholders(CommandSender sender, List messages, String[] params, Object[] values) { + sendWithPlaceholders(sender, setCustomParams(messages, params, values)); + } + + public static List setCustomParams(List messages, String param, Object value) { + return setCustomParams(messages, new String[]{param}, new Object[]{value}); + } + + public static List setCustomParams(List messages, String[] params, Object[] values) { + if (params.length != values.length) return messages; + HashMap paramsMap = new HashMap<>(); + for (int i = 0; i < params.length; i++) { + paramsMap.put(params[i], values[i]); + } + return setCustomParams(messages, paramsMap); + } + + + public static List setCustomParams(List messages, HashMap params) { + List list = new ArrayList<>(); + for (String message : messages) { + String afterMessage = message; + for (Map.Entry entry : params.entrySet()) { + afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString()); + } + list.add(afterMessage); + } + return list; + } + + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..22fff83 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,5 @@ +version: ${project.version} + +commands: + - "/say Hello I'm %player_name%" + - "如果不加斜杠,则会让玩家以文本形式发送出来。" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..30d8a65 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,9 @@ +main: cc.carm.plugin.protalcommands.Main +name: ${project.name} +version: ${project.version} +author: CarmJos +website: ${project.url} +description: ${project.description} + +softdepend: + - PlaceholderAPI \ No newline at end of file