From 7e145ee0b3086a0c7d06ab2f17f6e9445bc4f6c9 Mon Sep 17 00:00:00 2001 From: carm Date: Tue, 19 Dec 2023 20:19:17 +0800 Subject: [PATCH] feat(proj): Finished functions. --- README.md | 41 +++++--------- pom.xml | 25 ++++----- .../plugin/template/conf/PluginMessages.java | 6 --- .../plugin/{template => togglepvp}/Main.java | 31 ++++------- .../togglepvp/command/TogglePVPCommand.java | 32 +++++++++++ .../conf/PluginConfig.java | 14 ++--- .../plugin/togglepvp/conf/PluginMessages.java | 27 ++++++++++ .../plugin/togglepvp/handler/PVPHandler.java | 53 +++++++++++++++++++ src/main/resources/plugin.yml | 12 +++++ 9 files changed, 162 insertions(+), 79 deletions(-) delete mode 100644 src/main/java/com/artformgames/plugin/template/conf/PluginMessages.java rename src/main/java/com/artformgames/plugin/{template => togglepvp}/Main.java (74%) create mode 100644 src/main/java/com/artformgames/plugin/togglepvp/command/TogglePVPCommand.java rename src/main/java/com/artformgames/plugin/{template => togglepvp}/conf/PluginConfig.java (61%) create mode 100644 src/main/java/com/artformgames/plugin/togglepvp/conf/PluginMessages.java create mode 100644 src/main/java/com/artformgames/plugin/togglepvp/handler/PVPHandler.java diff --git a/README.md b/README.md index 29ad958..5c0b263 100644 --- a/README.md +++ b/README.md @@ -2,41 +2,28 @@ ``` -README LANGUAGES [ [**English**](README.md) | [中文](README_CN.md) ] +README LANGUAGES [ [**English**](README.md) ] -![CodeSize](https://img.shields.io/github/languages/code-size/ArtformGames/TemplateSinglePlugin) -[![Download](https://img.shields.io/github/downloads/ArtformGames/TemplateSinglePlugin/total)](https://github.com/ArtformGames/TemplateSinglePlugin/releases) -[![Java CI with Maven](https://github.com/ArtformGames/TemplateSinglePlugin/actions/workflows/maven.yml/badge.svg?branch=master)](https://github.com/ArtformGames//actions/workflows/maven.yml) +![CodeSize](https://img.shields.io/github/languages/code-size/ArtformGames/TogglePVP) +[![Download](https://img.shields.io/github/downloads/ArtformGames/TogglePVP/total)](https://github.com/ArtformGames/TogglePVP/releases) +[![Java CI with Maven](https://github.com/ArtformGames/TogglePVP/actions/workflows/maven.yml/badge.svg?branch=master)](https://github.com/ArtformGames//actions/workflows/maven.yml) ![Support](https://img.shields.io/badge/Minecraft-Java%201.16--Latest-green) -# **TemplateSinglePlugin** +# **TogglePVP** -> Slogan - -Descriptions - -## Features & Advantages - -## Screenshots +SLet players decide whether to enable PVP! ## Dependencies +- **[Necessary]** Develop based on [ArtCore](https://github.com/ArtformGames/ArtCore) . - **[Recommend]** Placeholders based on [PlaceholderAPI](https://www.spigotmc.org/resources/6245/) . For development dependencies, please -see [Dependencies](https://github.com/ArtformGames/TemplateSinglePlugin/network/dependencies) . +see [Dependencies](https://github.com/ArtformGames/TogglePVP/network/dependencies) . ## Commands -The main command is `/TemplateSinglePlugin` or `/tsp`. - -```text - -# subcommand -@ Player command -- Description - -``` +Use `/pvp` or `/TogglePVP` to toggle pvp status. ## Configurations @@ -51,14 +38,10 @@ Will be generated on the first boot up. ## Permissions ```text -# TemplateSinglePlugin.admin -- The permissions for all admin commands and functions. +# TogglePVP.use +- The permissions to use toggle command. ``` -## Statistics - -[![bStats](https://bstats.org/signatures/bukkit/TemplatePlugin.svg)](https://bstats.org/plugin/bukkit/TemplatePlugin/18946) - ## Open Source Licence The source code of this project adopts the [GNU General Public License v3.0](https://opensource.org/licenses/GPL-3.0). @@ -68,5 +51,5 @@ The source code of this project adopts the [GNU General Public License v3.0](htt This project is mainly developed by the [Artfrom Games](https://github.com/ArtformGames/) . Many thanks to Jetbrains for kindly providing a license for us to work on this and other open-source projects. -[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/ArtformGames/TemplateSinglePlugin) +[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/ArtformGames/TogglePVP) diff --git a/pom.xml b/pom.xml index b37f827..21806e7 100644 --- a/pom.xml +++ b/pom.xml @@ -10,19 +10,17 @@ UTF-8 UTF-8 - ${project.groupId}.plugin.template + ${project.groupId}.plugin.${project.groupId} com.artformgames - TemplateSinglePlugin + togglepvp 1.0.0 - TemplateSinglePlugin + TogglePVP - An efficient and fair verification plugin for whitelisted servers, - which supports players to submit whitelist requests by themselves - and be reviewed by all players who have passed. + Let players decide whether to enable PVP! - https://github.com/ArtformGames/TemplateSinglePlugin + https://github.com/ArtformGames/TogglePVP @@ -51,20 +49,20 @@ GitHub Actions - https://github.com/ArtformGames/TemplateSinglePlugin/actions/workflows/maven.yml + https://github.com/ArtformGames/TogglePVP/actions/workflows/maven.yml - https://github.com/ArtformGames/TemplateSinglePlugin/releases + https://github.com/ArtformGames/TogglePVP/releases github GitHub Packages - https://maven.pkg.github.com/ArtformGames/TemplateSinglePlugin + https://maven.pkg.github.com/ArtformGames/TogglePVP github GitHub Packages - https://maven.pkg.github.com/ArtformGames/TemplateSinglePlugin + https://maven.pkg.github.com/ArtformGames/TogglePVP @@ -101,7 +99,7 @@ com.artformgames artcore-api - 1.0-SNAPSHOT + 1.0.0 provided @@ -112,7 +110,6 @@ provided - me.clip placeholderapi @@ -211,7 +208,7 @@ - ${project.name}-${project.parent.version} + ${project.name}-${project.version} ${project.basedir}/.asset/ false diff --git a/src/main/java/com/artformgames/plugin/template/conf/PluginMessages.java b/src/main/java/com/artformgames/plugin/template/conf/PluginMessages.java deleted file mode 100644 index 9b524fa..0000000 --- a/src/main/java/com/artformgames/plugin/template/conf/PluginMessages.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.artformgames.plugin.template.conf; - -import cc.carm.lib.configuration.core.ConfigurationRoot; - -public class PluginMessages extends ConfigurationRoot { -} diff --git a/src/main/java/com/artformgames/plugin/template/Main.java b/src/main/java/com/artformgames/plugin/togglepvp/Main.java similarity index 74% rename from src/main/java/com/artformgames/plugin/template/Main.java rename to src/main/java/com/artformgames/plugin/togglepvp/Main.java index 1005ba0..3e6fbe2 100644 --- a/src/main/java/com/artformgames/plugin/template/Main.java +++ b/src/main/java/com/artformgames/plugin/togglepvp/Main.java @@ -1,11 +1,13 @@ -package com.artformgames.plugin.template; +package com.artformgames.plugin.togglepvp; import cc.carm.lib.easyplugin.EasyPlugin; import cc.carm.lib.mineconfiguration.bukkit.MineConfiguration; import com.artformgames.core.utils.GHUpdateChecker; -import com.artformgames.plugin.template.conf.PluginConfig; -import com.artformgames.plugin.template.conf.PluginMessages; -import org.bstats.bukkit.Metrics; +import com.artformgames.plugin.togglepvp.command.TogglePVPCommand; +import com.artformgames.plugin.togglepvp.conf.PluginConfig; +import com.artformgames.plugin.togglepvp.conf.PluginMessages; +import com.artformgames.plugin.togglepvp.handler.PVPHandler; +import dev.rollczi.litecommands.bukkit.LiteCommandsBukkit; public class Main extends EasyPlugin { @@ -16,29 +18,23 @@ public Main() { } protected MineConfiguration configuration; + protected LiteCommandsBukkit commands; @Override - protected void load() { + protected boolean initialize() { log("Loading plugin configurations..."); configuration = new MineConfiguration(this); configuration.initializeConfig(PluginConfig.class); configuration.initializeMessage(PluginMessages.class); - } - - @Override - protected boolean initialize() { log("Register listeners..."); + registerListener(new PVPHandler()); log("Register commands..."); + registerCommand(getName(), new TogglePVPCommand()); - if (PluginConfig.METRICS.getNotNull()) { - log("Initializing bStats..."); - new Metrics(this, 0); - } - if (PluginConfig.CHECK_UPDATE.getNotNull()) { log("Start to check the plugin versions..."); getScheduler().runAsync(GHUpdateChecker.runner(this)); @@ -49,13 +45,6 @@ protected boolean initialize() { return true; } - @Override - protected void shutdown() { - - log("Shutting down..."); - - } - @Override public boolean isDebugging() { return PluginConfig.DEBUG.getNotNull(); diff --git a/src/main/java/com/artformgames/plugin/togglepvp/command/TogglePVPCommand.java b/src/main/java/com/artformgames/plugin/togglepvp/command/TogglePVPCommand.java new file mode 100644 index 0000000..98d8e73 --- /dev/null +++ b/src/main/java/com/artformgames/plugin/togglepvp/command/TogglePVPCommand.java @@ -0,0 +1,32 @@ +package com.artformgames.plugin.togglepvp.command; + +import com.artformgames.plugin.togglepvp.handler.PVPHandler; +import com.artformgames.plugin.togglepvp.conf.PluginMessages; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class TogglePVPCommand implements CommandExecutor { + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, + @NotNull String label, @NotNull String[] args) { + if (!(sender instanceof Player player)) { + sender.sendMessage("You must be a player to use this command."); + return true; + } + + boolean current = PVPHandler.isPVPEnabled(player); + PVPHandler.setPVPStatus(player, !current); + if (!current) { + PluginMessages.ENABLED.send(player); + } else { + PluginMessages.DISABLED.send(player); + } + + return true; + } + +} diff --git a/src/main/java/com/artformgames/plugin/template/conf/PluginConfig.java b/src/main/java/com/artformgames/plugin/togglepvp/conf/PluginConfig.java similarity index 61% rename from src/main/java/com/artformgames/plugin/template/conf/PluginConfig.java rename to src/main/java/com/artformgames/plugin/togglepvp/conf/PluginConfig.java index 0a5bbd1..0af6714 100644 --- a/src/main/java/com/artformgames/plugin/template/conf/PluginConfig.java +++ b/src/main/java/com/artformgames/plugin/togglepvp/conf/PluginConfig.java @@ -1,21 +1,14 @@ -package com.artformgames.plugin.template.conf; +package com.artformgames.plugin.togglepvp.conf; import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.core.annotation.HeaderComment; +import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.type.ConfiguredValue; public class PluginConfig extends ConfigurationRoot { public static final ConfiguredValue DEBUG = ConfiguredValue.of(Boolean.class, false); - @HeaderComment({ - "Statistics Settings", - "This option is used to help developers count plug-in versions and usage, and it will never affect performance and user experience.", - "Of course, you can also choose to turn it off here for this plugin,", - "or turn it off for all plugins in the configuration file under \"plugins/bStats\"." - }) - public static final ConfiguredValue METRICS = ConfiguredValue.of(Boolean.class, true); - @HeaderComment({ "Check update settings", "This option is used by the plug-in to determine whether to check for updates.", @@ -24,4 +17,7 @@ public class PluginConfig extends ConfigurationRoot { }) public static final ConfiguredValue CHECK_UPDATE = ConfiguredValue.of(Boolean.class, true); + @HeaderComment("Default status of player's pvp settings") + public static final ConfigValue DEFAULTS = ConfiguredValue.of(Boolean.class, false); + } diff --git a/src/main/java/com/artformgames/plugin/togglepvp/conf/PluginMessages.java b/src/main/java/com/artformgames/plugin/togglepvp/conf/PluginMessages.java new file mode 100644 index 0000000..1a0d178 --- /dev/null +++ b/src/main/java/com/artformgames/plugin/togglepvp/conf/PluginMessages.java @@ -0,0 +1,27 @@ +package com.artformgames.plugin.togglepvp.conf; + +import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList; +import com.artformgames.core.conf.MessagesRoot; +import net.md_5.bungee.api.chat.BaseComponent; + +public class PluginMessages extends MessagesRoot { + + public static final ConfiguredMessageList SELF_DISABLED = list() + .defaults("&7 You have disabled PVP so you can't attack other players.") + .build(); + + public static final ConfiguredMessageList TARGET_DISABLED = list() + .defaults("&fPlayer &c%(player) &fPVP is disabled, so it can't be attacked.") + .params("player") + .build(); + + public static final ConfiguredMessageList ENABLED = list() + .defaults("&fYou have &a&lenabled &fPVP, now you can fight with other players.") + .build(); + + public static final ConfiguredMessageList DISABLED = list() + .defaults("&fYou have &c&ldisabled &fPVP, now you can't attack other players, and you can't be attacked by other players.") + .build(); + +} + diff --git a/src/main/java/com/artformgames/plugin/togglepvp/handler/PVPHandler.java b/src/main/java/com/artformgames/plugin/togglepvp/handler/PVPHandler.java new file mode 100644 index 0000000..c455ea9 --- /dev/null +++ b/src/main/java/com/artformgames/plugin/togglepvp/handler/PVPHandler.java @@ -0,0 +1,53 @@ +package com.artformgames.plugin.togglepvp.handler; + +import com.artformgames.core.ArtCore; +import com.artformgames.core.function.settings.SettingsType; +import com.artformgames.core.function.settings.UserSettingsData; +import com.artformgames.core.function.settings.values.BooleanSettingsType; +import com.artformgames.plugin.togglepvp.conf.PluginConfig; +import com.artformgames.plugin.togglepvp.conf.PluginMessages; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; + +public class PVPHandler implements Listener { + + public static final SettingsType PVP = new BooleanSettingsType(10, PluginConfig.DEFAULTS.getNotNull()); + + + @EventHandler + public void onAttack(EntityDamageByEntityEvent e) { + Player attacker = null; + + if (e.getDamager() instanceof Player player) { + attacker = player; + } else if (e.getDamager() instanceof Projectile proj && proj.getShooter() instanceof Player shooter) { + attacker = shooter; + } + + if (attacker == null) return; + if (!(e.getEntity() instanceof Player player)) return; + + if (!isPVPEnabled(attacker)) { + // 当玩家想要攻击别人时,先判断自己是否允许攻击 + PluginMessages.SELF_DISABLED.send(attacker); + e.setCancelled(true); + } else if (!isPVPEnabled(player)) { + //再判断被攻击者是否允许被攻击 + PluginMessages.TARGET_DISABLED.send(attacker, player.getName()); + e.setCancelled(true); + } + } + + public static boolean isPVPEnabled(Player player) { + return ArtCore.getUserManager().get(player).getHandler(UserSettingsData.class).get(PVP); + } + + public static void setPVPStatus(Player player, boolean status) { + ArtCore.getUserManager().get(player).getHandler(UserSettingsData.class).set(PVP, status); + } + + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8c04ddb..bb17d6d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -15,3 +15,15 @@ depend: softdepend: - PlaceholderAPI + +permissions: + "togglepvp.use": + description: "Allows the player to toggle PVP status" + default: true + +commands: + "TogglePVP": + description: "Toggle PVP status" + usage: "/togglepvp" + permission: "togglepvp.use" + aliases: [ "pvp" ] \ No newline at end of file