From b8d348445076bfc44c1c489dcca0f12c416d86e5 Mon Sep 17 00:00:00 2001 From: Yanis48 Date: Thu, 29 Jul 2021 22:10:49 +0200 Subject: [PATCH] Resource pack send command - 1.3.0 --- README.md | 1 + .../pacpi/command/ResourcePackCommand.java | 74 +++++++++++++++++++ src/fr/peaceandcube/pacpi/pAcPI.java | 4 +- .../pacpi/player/PlayerMessages.java | 4 + src/plugin.yml | 10 ++- 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/fr/peaceandcube/pacpi/command/ResourcePackCommand.java diff --git a/README.md b/README.md index bf1ea40..35349b0 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ An API to help creating plugins for PeaceAndCube Various useful commands - ``/removealltags []`` to remove all scoreboard tags from the target. The tag specified in ``except`` will be ignored. - ``/chrono (start|end) `` to start or end a player timer. +- ``/resourcepack send `` to send a resource pack to a target. ### Date - ``Chrono`` provides a player-specific timer that can be started and ended. diff --git a/src/fr/peaceandcube/pacpi/command/ResourcePackCommand.java b/src/fr/peaceandcube/pacpi/command/ResourcePackCommand.java new file mode 100644 index 0000000..46f928a --- /dev/null +++ b/src/fr/peaceandcube/pacpi/command/ResourcePackCommand.java @@ -0,0 +1,74 @@ +package fr.peaceandcube.pacpi.command; + +import fr.peaceandcube.pacpi.player.PlayerMessages; +import fr.peaceandcube.pacpi.player.PlayerSuggestionProviders; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class ResourcePackCommand implements CommandExecutor, TabExecutor { + public static final Plugin PLUGIN = Bukkit.getPluginManager().getPlugin("pAcPI"); + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender.hasPermission("pacpi.resourcepack")) { + switch (args[0]) { + case "send": + if (args.length == 3) { + Player player = Bukkit.getPlayer(args[1]); + String url = args[2]; + + try { + new URL(url); + } catch (MalformedURLException e) { + sender.sendMessage(PlayerMessages.PACK_URL_INVALID); + return true; + } + + if (!Bukkit.getOnlinePlayers().contains(player)) { + sender.sendMessage(PlayerMessages.PLAYER_NOT_FOUND); + return true; + } + + player.sendMessage(PlayerMessages.PACK_SENT); + Bukkit.getScheduler().runTaskLaterAsynchronously(PLUGIN, new Runnable() { + @Override + public void run() { + player.setResourcePack(url); + } + }, 2 * 20); + + return true; + } + } + } + + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { + List options = new ArrayList<>(); + + if (sender.hasPermission("pacpi.resourcepack")) { + switch (args.length) { + case 1: + options.add("send"); + break; + case 2: + return PlayerSuggestionProviders.getOnlinePlayers(args[1]); + } + } + + return options; + } +} diff --git a/src/fr/peaceandcube/pacpi/pAcPI.java b/src/fr/peaceandcube/pacpi/pAcPI.java index 5b23f3e..cbd950c 100644 --- a/src/fr/peaceandcube/pacpi/pAcPI.java +++ b/src/fr/peaceandcube/pacpi/pAcPI.java @@ -1,5 +1,6 @@ package fr.peaceandcube.pacpi; +import fr.peaceandcube.pacpi.command.ResourcePackCommand; import fr.peaceandcube.pacpi.event.PlayerEventHandler; import org.bukkit.plugin.java.JavaPlugin; @@ -7,11 +8,12 @@ import fr.peaceandcube.pacpi.command.RemoveAllTagsCommand; public class pAcPI extends JavaPlugin { - + @Override public void onEnable() { this.getCommand("removealltags").setExecutor(new RemoveAllTagsCommand()); this.getCommand("chrono").setExecutor(new ChronoCommand()); + this.getCommand("resourcepack").setExecutor(new ResourcePackCommand()); this.getServer().getPluginManager().registerEvents(new PlayerEventHandler(), this); } diff --git a/src/fr/peaceandcube/pacpi/player/PlayerMessages.java b/src/fr/peaceandcube/pacpi/player/PlayerMessages.java index 07caba2..2e46736 100644 --- a/src/fr/peaceandcube/pacpi/player/PlayerMessages.java +++ b/src/fr/peaceandcube/pacpi/player/PlayerMessages.java @@ -16,6 +16,10 @@ public class PlayerMessages { public static final String CHRONO_ENDED = info("Tu as fait un temps de %s !"); public static final String CHRONO_NOT_STARTED = error("%s n'a pas de chrono démarré."); + // resourcepack command + public static final String PACK_URL_INVALID = error("L'adresse du pack de ressources est invalide."); + public static final String PACK_SENT = info("Un pack de ressources va être téléchargé !"); + public static String error(String msg) { return ChatColor.RED + msg; } diff --git a/src/plugin.yml b/src/plugin.yml index 7bcaa23..223180c 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ main: fr.peaceandcube.pacpi.pAcPI name: pAcPI -version: 1.2.1 +version: 1.3.0 author: YanisBft website: https://github.com/PeaceAndCubeMC/pAcPI api-version: 1.17 @@ -16,6 +16,11 @@ commands: permission: pacpi.chrono permission-message: §cYou do not have the permission to use this command. usage: "§eUsage: §r/chrono (start|end) " + resourcepack: + description: Allows you to send resource packs to a player + permission: pacpi.resourcepack + permission-message: §cYou do not have the permission to use this command. + usage: "§eUsage: §r/resourcepack send " permissions: pacpi.removealltags: @@ -24,3 +29,6 @@ permissions: pacpi.chrono: description: Permission to use the /chrono command default: op + pacpi.resourcepack: + description: Permission to use the /resourcepack command + default: op