From a00244db892aab03c8b87b8282ec3cc3c133a2b5 Mon Sep 17 00:00:00 2001 From: Mateus Moreira Date: Sun, 12 Aug 2018 14:36:08 +0100 Subject: [PATCH] Click sound + release 2.1 --- pom.xml | 2 +- .../mattmoreira/citizenscmd/CitizensCMD.java | 2 +- .../citizenscmd/Listeners/NPCListener.java | 12 ++- .../citizenscmd/commands/CMDSound.java | 71 ++++++++++++++++ .../commands/base/CommandHandler.java | 6 +- .../citizenscmd/files/DataHandler.java | 76 +++++++++++++++++ .../citizenscmd/files/LangHandler.java | 84 +++++++++++++++++++ .../mattmoreira/citizenscmd/utility/Path.java | 3 + .../mattmoreira/citizenscmd/utility/Util.java | 30 +++++++ src/main/resources/lang/bg.yml | 3 + src/main/resources/lang/ch.yml | 3 + src/main/resources/lang/en.yml | 3 + src/main/resources/lang/no.yml | 3 + src/main/resources/lang/pt.yml | 2 + src/main/resources/lang/ro.yml | 3 + 15 files changed, 299 insertions(+), 4 deletions(-) create mode 100644 src/main/java/me/mattmoreira/citizenscmd/commands/CMDSound.java diff --git a/pom.xml b/pom.xml index 9834f4c..951b283 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.mattmoreira.plugins citizens-cmd - 2.1-SNAPSHOT + 2.1 jar CitizensCMD diff --git a/src/main/java/me/mattmoreira/citizenscmd/CitizensCMD.java b/src/main/java/me/mattmoreira/citizenscmd/CitizensCMD.java index 1cd46ea..4060f04 100644 --- a/src/main/java/me/mattmoreira/citizenscmd/CitizensCMD.java +++ b/src/main/java/me/mattmoreira/citizenscmd/CitizensCMD.java @@ -239,7 +239,7 @@ public final class CitizensCMD extends JavaPlugin { */ private void registerCommands() { getCommand("npcmd").setExecutor(commandHandler); - Stream.of(new CMDHelp(), new CMDAdd(), new CMDCooldown(), new CMDList(), new CMDReload(), new CMDRemove(), new CMDEdit(), new CMDPrice()).forEach(commandHandler::register); + Stream.of(new CMDHelp(), new CMDAdd(), new CMDCooldown(), new CMDList(), new CMDReload(), new CMDRemove(), new CMDEdit(), new CMDPrice(), new CMDSound()).forEach(commandHandler::register); } /** diff --git a/src/main/java/me/mattmoreira/citizenscmd/Listeners/NPCListener.java b/src/main/java/me/mattmoreira/citizenscmd/Listeners/NPCListener.java index 1b9e876..4a3af8c 100644 --- a/src/main/java/me/mattmoreira/citizenscmd/Listeners/NPCListener.java +++ b/src/main/java/me/mattmoreira/citizenscmd/Listeners/NPCListener.java @@ -28,6 +28,7 @@ import net.citizensnpcs.api.event.NPCRemoveEvent; import net.citizensnpcs.api.event.NPCRightClickEvent; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -70,6 +71,11 @@ public class NPCListener implements Listener { } } + if (CitizensCMD.getPlugin().getDataHandler().hasSound(npc)) { + List soundProperties = CitizensCMD.getPlugin().getDataHandler().getNPCSound(npc); + player.playSound(player.getLocation(), Sound.valueOf(soundProperties.get(0)), Float.parseFloat(soundProperties.get(1)), Float.parseFloat(soundProperties.get(2))); + } + if (CitizensCMD.getPlugin().getDataHandler().hasNoCommands(npc, EnumTypes.ClickType.RIGHT)) return; } @@ -159,7 +165,6 @@ public class NPCListener implements Listener { } } - if (!player.hasPermission("citizenscmd.bypass") || CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc) != 0) CitizensCMD.getPlugin().getCooldownHandler().addInteraction(npc, player.getUniqueId().toString(), System.nanoTime()); @@ -185,6 +190,11 @@ public class NPCListener implements Listener { } } + if (CitizensCMD.getPlugin().getDataHandler().hasSound(npc)) { + List soundProperties = CitizensCMD.getPlugin().getDataHandler().getNPCSound(npc); + player.playSound(player.getLocation(), Sound.valueOf(soundProperties.get(0)), Float.parseFloat(soundProperties.get(1)), Float.parseFloat(soundProperties.get(2))); + } + if (CitizensCMD.getPlugin().getDataHandler().hasNoCommands(npc, EnumTypes.ClickType.LEFT)) return; } diff --git a/src/main/java/me/mattmoreira/citizenscmd/commands/CMDSound.java b/src/main/java/me/mattmoreira/citizenscmd/commands/CMDSound.java new file mode 100644 index 0000000..5fb6fb7 --- /dev/null +++ b/src/main/java/me/mattmoreira/citizenscmd/commands/CMDSound.java @@ -0,0 +1,71 @@ +/** + * CitizensCMD - Add-on for Citizens + * Copyright (C) 2018 Mateus Moreira + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *

+ * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.mattmoreira.citizenscmd.commands; + +import me.mattmoreira.citizenscmd.CitizensCMD; +import me.mattmoreira.citizenscmd.commands.base.CommandBase; +import me.mattmoreira.citizenscmd.utility.Path; +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +import static me.mattmoreira.citizenscmd.utility.Util.*; + +public class CMDSound extends CommandBase { + + public CMDSound() { + super("sound", "citizenscmd.sound", false, null, 0, 3); + } + + public void execute(Player player, String[] args) { + + if (npcNotSelected(player)) return; + + int npc = getSelectedNpcId(player); + + if (args.length == 0) { + CitizensCMD.getPlugin().getDataHandler().removeSound(npc, player); + return; + } + + String soundString = args[0]; + Sound sound; + float volume = 1f; + float pitch = 1f; + + if (args.length > 1) + if (isFloat(args[1])) + volume = Float.valueOf(args[1]); + + if (args.length > 2) + if (isFloat(args[2])) + pitch = Float.valueOf(args[2]); + + try { + sound = Sound.valueOf(soundString); + } catch (Exception e) { + player.sendMessage(color(HEADER)); + player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_SOUND)); + return; + } + + CitizensCMD.getPlugin().getDataHandler().setSound(npc, sound, volume, pitch, player); + + } + +} diff --git a/src/main/java/me/mattmoreira/citizenscmd/commands/base/CommandHandler.java b/src/main/java/me/mattmoreira/citizenscmd/commands/base/CommandHandler.java index b2025bd..a945959 100644 --- a/src/main/java/me/mattmoreira/citizenscmd/commands/base/CommandHandler.java +++ b/src/main/java/me/mattmoreira/citizenscmd/commands/base/CommandHandler.java @@ -180,6 +180,10 @@ public class CommandHandler implements CommandExecutor, TabCompleter, IHandler { return getCommandNames(subCMD, args, 5, (Player) sender); } break; + + case "sound": + if (args.length == 2) return getCommandNames(subCMD, args, 1, (Player) sender); + break; } } @@ -203,7 +207,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter, IHandler { if (!args[arg - 1].equals("")) { for (String commandName : argsComplete[arg - 1]) { if (arg + 1 > args.length) break; - if (!commandName.startsWith(args[arg].toLowerCase())) continue; + if (!commandName.toLowerCase().startsWith(args[arg].toLowerCase())) continue; commandNames.add(commandName); } } else { diff --git a/src/main/java/me/mattmoreira/citizenscmd/files/DataHandler.java b/src/main/java/me/mattmoreira/citizenscmd/files/DataHandler.java index 96bd380..fb96a21 100644 --- a/src/main/java/me/mattmoreira/citizenscmd/files/DataHandler.java +++ b/src/main/java/me/mattmoreira/citizenscmd/files/DataHandler.java @@ -21,6 +21,7 @@ package me.mattmoreira.citizenscmd.files; import me.mattmoreira.citizenscmd.CitizensCMD; import me.mattmoreira.citizenscmd.utility.EnumTypes; import me.mattmoreira.citizenscmd.utility.Path; +import org.bukkit.Sound; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -91,6 +92,7 @@ public class DataHandler { data.put("npc-data." + parent + "." + child, dataConfigurator.getInt("npc-data." + parent + "." + child)); break; + case "sound": case "right-click-commands": case "left-click-commands": data.put("npc-data." + parent + "." + child, dataConfigurator.getStringList("npc-data." + parent + "." + child)); @@ -161,6 +163,11 @@ public class DataHandler { dataConfigurator.set("npc-data.npc-" + npc + ".price", 0); } + if (!data.containsKey("npc-data.npc-" + npc + ".sound")) { + data.put("npc-data.npc-" + npc + ".sound", new ArrayList<>()); + dataConfigurator.set("npc-data.npc-" + npc + ".sound", new ArrayList<>()); + } + player.sendMessage(color(HEADER)); player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NPC_ADDED)); @@ -227,6 +234,44 @@ public class DataHandler { }).start(); } + /** + * Sets the sound of the NPC command + * + * @param npc The NPC id + * @param sound The sound to play + * @param volume the volume + * @param pitch the pitch + * @param player The player who run the command + */ + public void setSound(int npc, Sound sound, float volume, float pitch, Player player) { + new Thread(() -> { + try { + createBasics(); + dataConfigurator.load(savesFile); + + List soundProperties = new ArrayList<>(); + + soundProperties.add(sound.name()); + soundProperties.add(String.valueOf(volume)); + soundProperties.add(String.valueOf(pitch)); + + dataConfigurator.set("npc-data.npc-" + npc + ".sound", soundProperties); + + if (data.containsKey("npc-data.npc-" + npc + ".sound")) + data.replace("npc-data.npc-" + npc + ".sound", soundProperties); + else + data.put("npc-data.npc-" + npc + ".sound", soundProperties); + + player.sendMessage(color(HEADER)); + player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.SOUND_ADDED)); + + dataConfigurator.save(savesFile); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + }).start(); + } + /** * Gets the click commands from the saves.yml * @@ -266,6 +311,12 @@ public class DataHandler { return true; } + public boolean hasSound(int npc) { + if (data.containsKey("npc-data.npc-" + npc + ".sound")) + return !(((List) data.get("npc-data.npc-" + npc + ".sound")).isEmpty()); + return false; + } + /** * Gets the cooldown of the NPC * @@ -276,6 +327,10 @@ public class DataHandler { return data.containsKey("npc-data.npc-" + npc + ".cooldown") ? (int) data.get("npc-data.npc-" + npc + ".cooldown") : 0; } + public List getNPCSound(int npc) { + return (List) data.get("npc-data.npc-" + npc + ".sound"); + } + /** * Gets the price of the NPC * @@ -317,6 +372,27 @@ public class DataHandler { }).start(); } + public void removeSound(int npc, Player player) { + new Thread(() -> { + try { + createBasics(); + dataConfigurator.load(savesFile); + + List soundProperties = new ArrayList<>(); + + data.replace("npc-data.npc-" + npc + ".sound", soundProperties); + dataConfigurator.set("npc-data.npc-" + npc + ".sound", soundProperties); + + player.sendMessage(color(HEADER)); + player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.SOUND_REMOVED)); + + dataConfigurator.save(savesFile); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + }).start(); + } + /** * Edits a command or permission from the NPC * diff --git a/src/main/java/me/mattmoreira/citizenscmd/files/LangHandler.java b/src/main/java/me/mattmoreira/citizenscmd/files/LangHandler.java index 6ee4118..5e12cfc 100644 --- a/src/main/java/me/mattmoreira/citizenscmd/files/LangHandler.java +++ b/src/main/java/me/mattmoreira/citizenscmd/files/LangHandler.java @@ -91,6 +91,90 @@ public class LangHandler { if (!langConf.contains(Path.MESSAGE_DISPLAY)) langConf.set(Path.MESSAGE_DISPLAY, "{name}:&r"); + if (!langConf.contains(Path.INVALID_SOUND)) { + switch (lang) { + case "en": + langConf.set(Path.INVALID_SOUND, "&cPlease enter a valid sound!"); + break; + + case "pt": + langConf.set(Path.INVALID_SOUND, "&cSelecione um som válido!"); + break; + + case "ro": + langConf.set(Path.INVALID_SOUND, "&cIntroduceți un sunet valid!"); + break; + + case "bg": + langConf.set(Path.INVALID_SOUND, "&cМоля, въведете валиден звук!"); + break; + + case "no": + langConf.set(Path.INVALID_SOUND, "&cVennligst skriv inn en gyldig lyd!"); + break; + + case "ch": + langConf.set(Path.INVALID_SOUND, "&c请输入有效的声音!"); + break; + } + } + + if (!langConf.contains(Path.SOUND_ADDED)) { + switch (lang) { + case "en": + langConf.set(Path.SOUND_ADDED, "&aSound added successfully!"); + break; + + case "pt": + langConf.set(Path.SOUND_ADDED, "&aSom adicionado com sucesso!"); + break; + + case "ro": + langConf.set(Path.SOUND_ADDED, "&aSunetul a fost adăugat cu succes!"); + break; + + case "bg": + langConf.set(Path.SOUND_ADDED, "&aЗвукът е добавен успешно!"); + break; + + case "no": + langConf.set(Path.SOUND_ADDED, "&aLyd lagt til!"); + break; + + case "ch": + langConf.set(Path.SOUND_ADDED, "&a声音成功添加!"); + break; + } + } + + if (!langConf.contains(Path.SOUND_REMOVED)) { + switch (lang) { + case "en": + langConf.set(Path.SOUND_REMOVED, "&aSound removed successfully!"); + break; + + case "pt": + langConf.set(Path.SOUND_REMOVED, "&aSom removido com sucesso!"); + break; + + case "ro": + langConf.set(Path.SOUND_REMOVED, "&aSunetul a fost eliminat cu succes!"); + break; + + case "bg": + langConf.set(Path.SOUND_REMOVED, "&aЗвукът е премахнат успешно!"); + break; + + case "no": + langConf.set(Path.SOUND_REMOVED, "&aLyden fjernet vellykket!"); + break; + + case "ch": + langConf.set(Path.SOUND_REMOVED, "&a声音已成功删除!"); + break; + } + } + for (String parent : langConf.getConfigurationSection("messages").getKeys(false)) { for (String child : langConf.getConfigurationSection("messages." + parent).getKeys(false)) messages.put("messages." + parent + "." + child, langConf.getString("messages." + parent + "." + child)); diff --git a/src/main/java/me/mattmoreira/citizenscmd/utility/Path.java b/src/main/java/me/mattmoreira/citizenscmd/utility/Path.java index be6f2c5..37ad83b 100644 --- a/src/main/java/me/mattmoreira/citizenscmd/utility/Path.java +++ b/src/main/java/me/mattmoreira/citizenscmd/utility/Path.java @@ -38,6 +38,9 @@ public class Path { public static final String RELOAD = MAIN_PATH_COMMANDS + "reload-command"; public static final String REMOVED_COMMAND = MAIN_PATH_COMMANDS + "removed-command"; public static final String EDITED_COMMAND = MAIN_PATH_COMMANDS + "edit-command"; + public static final String INVALID_SOUND = MAIN_PATH_COMMANDS + "invalid-sound"; + public static final String SOUND_ADDED = MAIN_PATH_COMMANDS + "sound-added"; + public static final String SOUND_REMOVED = MAIN_PATH_COMMANDS + "sound-removed"; /** * WARNINGS diff --git a/src/main/java/me/mattmoreira/citizenscmd/utility/Util.java b/src/main/java/me/mattmoreira/citizenscmd/utility/Util.java index ce30c95..0e0035b 100644 --- a/src/main/java/me/mattmoreira/citizenscmd/utility/Util.java +++ b/src/main/java/me/mattmoreira/citizenscmd/utility/Util.java @@ -22,6 +22,7 @@ import me.mattmoreira.citizenscmd.CitizensCMD; import net.citizensnpcs.api.CitizensAPI; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Sound; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -65,6 +66,19 @@ public class Util { return false; } + /** + * @param str String to check if it is a float number or not + * @return Returns true if it is a number false if it is a string or contains any non numeric character + */ + public static boolean isFloat(String str) { + try { + Float.parseFloat(str); + } catch (NumberFormatException | NullPointerException e) { + return false; + } + return true; + } + /** * Checks if player has or not selected an NPC * @@ -159,10 +173,26 @@ public class Util { argComplete[2] = CitizensCMD.getPlugin().getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.LEFT); argComplete[3] = CitizensCMD.getPlugin().getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.RIGHT); argComplete[4] = new String[]{"console", "none", "permission", "server", "message"}; + break; + + case "sound": + argComplete[0] = getSoundsList(); + break; } return argComplete; } + private static String[] getSoundsList() { + Sound[] sounds = Sound.values(); + String[] soundString = new String[sounds.length]; + + for (int i = 0; i < sounds.length; i++) { + soundString[i] = sounds[i].name(); + } + + return soundString; + } + /** * Gets the difference in seconds between times * diff --git a/src/main/resources/lang/bg.yml b/src/main/resources/lang/bg.yml index 99ebc80..e608adf 100644 --- a/src/main/resources/lang/bg.yml +++ b/src/main/resources/lang/bg.yml @@ -21,6 +21,9 @@ messages: reload-command: "&aВсичко файлове бяха презаредени успешно!" removed-command: "&aКомандата бе премахната успешно!" edit-command: "&a{type} бе променен/а успешно!" + invalid-sound: "&cМоля, въведете валиден звук!" + sound-added: "&aЗвукът е добавен успешно!" + sound-removed: "&aЗвукът е премахнат успешно!" warnings: no-npc-selected: "&cИзбери NPC първо!" invalid-cooldown: "&cВремето за изчакване трябва да бъде число!" diff --git a/src/main/resources/lang/ch.yml b/src/main/resources/lang/ch.yml index 88e74f9..329e8d9 100644 --- a/src/main/resources/lang/ch.yml +++ b/src/main/resources/lang/ch.yml @@ -21,6 +21,9 @@ messages: reload-command: "&a所有檔案已經被重新載入!" removed-command: "&a這個指令已經被成功移除!" edit-command: "&a {type} 已經被編輯!" + invalid-sound: "&c请输入有效的声音!" + sound-added: "&a声音成功添加!" + sound-removed: "&a声音已成功删除!" warnings: no-npc-selected: "&c你一定要選擇NPC來執行那個指令!" invalid-cooldown: "&c冷卻時間並不是數字!" diff --git a/src/main/resources/lang/en.yml b/src/main/resources/lang/en.yml index 5b69db4..842f5ff 100644 --- a/src/main/resources/lang/en.yml +++ b/src/main/resources/lang/en.yml @@ -21,6 +21,9 @@ messages: reload-command: "&aAll files have been reloaded successfully!" removed-command: "&aThe command was removed successfully!" edit-command: "&aThe {type} was edited successfully!" + invalid-sound: "&cPlease enter a valid sound!" + sound-added: "&aSound added successfully!" + sound-removed: "&aSound removed successfully!" warnings: no-npc-selected: "&cYou must have an NPC selected to execute that command!" invalid-cooldown: "&cThe cooldown must be a number!" diff --git a/src/main/resources/lang/no.yml b/src/main/resources/lang/no.yml index 3d3d694..457efd2 100644 --- a/src/main/resources/lang/no.yml +++ b/src/main/resources/lang/no.yml @@ -21,6 +21,9 @@ messages: reload-command: "&aAlle fillene har blitt reloadet!" removed-command: "&aKommandoen ble fjernet!" edit-command: "&a{type} ble endret!" + invalid-sound: "&cVennligst skriv inn en gyldig lyd!" + sound-added: "&aLyd lagt til!" + sound-removed: "&aLyden fjernet vellykket!" warnings: no-npc-selected: "&cDu må velge en NPC for å utføre denne kommandoen!" invalid-cooldown: "&cNedtellingen må være et tall!" diff --git a/src/main/resources/lang/pt.yml b/src/main/resources/lang/pt.yml index 80ea1bd..d48a708 100644 --- a/src/main/resources/lang/pt.yml +++ b/src/main/resources/lang/pt.yml @@ -21,6 +21,8 @@ messages: reload-command: "&aTodos os arquivos foram recarregados com sucesso!" removed-command: "&aO comando foi removido com sucesso!" edit-command: "&aO {type} foi editado com sucesso!" + invalid-sound: "&cSelecione um som válido!" + sound-added: "&aSom adicionado com sucesso!" warnings: no-npc-selected: "&cVocê deve ter um NPC selecionado para executar esse comando!" invalid-cooldown: "&cO cooldown deve ser um número!" diff --git a/src/main/resources/lang/ro.yml b/src/main/resources/lang/ro.yml index af11861..86bbcb5 100644 --- a/src/main/resources/lang/ro.yml +++ b/src/main/resources/lang/ro.yml @@ -21,6 +21,9 @@ messages: reload-command: "&aToate fisierele au fost reincarcate cu succes!" removed-command: "&aComanda a fost eliminata cu succes!" edit-command: "&a{type} a fost editat cu succes!" + invalid-sound: "&cIntroduceți un sunet valid!" + sound-added: "&aSunetul a fost adăugat cu succes!" + sound-removed: "&aSunetul a fost eliminat cu succes!" warnings: no-npc-selected: "&cTrebuie sa ai un NPC selectat pentru a executa aceasta comanda!" invalid-cooldown: "&cCooldown-ul trebuie sa fie un numar!"