From 87b47e93d090c3de159d4d1de859cd111ebcae4f Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Tue, 4 Aug 2020 18:04:26 +0200 Subject: [PATCH] Add sound effects for auto collect and drag and drop (fixes #106) --- Minepacks/resources/config.yml | 10 ++- .../Minepacks/Bukkit/Database/Config.java | 12 ++- .../Minepacks/Bukkit/ItemsCollector.java | 11 ++- .../Listener/BackpackEventListener.java | 2 +- .../Bukkit/Listener/ItemShortcut.java | 88 +++++++++++-------- .../Minepacks/Bukkit/MagicValues.java | 2 +- .../Minepacks/Bukkit/Minepacks.java | 2 +- 7 files changed, 86 insertions(+), 41 deletions(-) diff --git a/Minepacks/resources/config.yml b/Minepacks/resources/config.yml index 0041723..dc063c0 100644 --- a/Minepacks/resources/config.yml +++ b/Minepacks/resources/config.yml @@ -197,6 +197,14 @@ Sound: # disabled or false to disable the sound effect # auto will play the chest sound for minecraft versions older than 1.11, and the shulker-box sound for newer MC versions CloseSound: auto + # The sound effect that should be played when the auto-pickup feature collects an item into the backpack + # disabled or false to disable the sound effect + # auto will play the item pickup sound + AutoCollectSound: auto + # The sound effect that should be played when an item is deposited into the backpack through drag and drop + # disabled or false to disable the sound effect + # auto will play the item pickup sound + DragAndDropSound: auto # Settings for the inventory management InventoryManagement: @@ -213,4 +221,4 @@ Misc: UseBungeeCord: false # Config file version. Don't touch it! -Version: 32 \ No newline at end of file +Version: 33 \ No newline at end of file diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java index 5988307..583d2b5 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java @@ -37,7 +37,7 @@ public class Config extends Configuration implements DatabaseConnectionConfiguration { - private static final int CONFIG_VERSION = 32, UPGRADE_THRESHOLD = CONFIG_VERSION, PRE_V2_VERSION = 20; + private static final int CONFIG_VERSION = 33, UPGRADE_THRESHOLD = CONFIG_VERSION, PRE_V2_VERSION = 20; public Config(JavaPlugin plugin) { @@ -482,6 +482,16 @@ public boolean isItemShortcutPlayerDisableItemEnabled() { return getSound("CloseSound", MCVersion.isNewerOrEqualThan(MCVersion.MC_1_11) ? "BLOCK_SHULKER_BOX_CLOSE" : (MCVersion.isNewerOrEqualThan(MCVersion.MC_1_9_2) ? "BLOCK_CHEST_CLOSE" : "CHEST_CLOSE")); } + + public @Nullable Sound getAutoCollectSound() + { + return getSound("AutoCollectSound", MCVersion.isNewerOrEqualThan(MCVersion.MC_1_9_2) ? "ENTITY_ITEM_PICKUP" : "ITEM_PICKUP"); + } + + public @Nullable Sound getDragAndDropSound() + { + return getSound("DragAndDropSound", MCVersion.isNewerOrEqualThan(MCVersion.MC_1_9_2) ? "ENTITY_ITEM_PICKUP" : "ITEM_PICKUP"); + } //endregion //region InventoryManagement settings diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java index 4171b02..03bd739 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java @@ -21,12 +21,14 @@ import at.pcgamingfreaks.Minepacks.Bukkit.Listener.ItemFilter; import org.bukkit.Bukkit; +import org.bukkit.Sound; import org.bukkit.entity.Entity; import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.Map; @@ -37,12 +39,14 @@ public class ItemsCollector extends BukkitRunnable private final double radius; private final BukkitTask task; private final ItemFilter itemFilter; + private final Sound collectSound; - public ItemsCollector(Minepacks plugin) + public ItemsCollector(final @NotNull Minepacks plugin) { this.plugin = plugin; this.radius = plugin.getConfiguration().getFullInvRadius(); task = runTaskTimer(plugin, plugin.getConfiguration().getFullInvCheckInterval(), plugin.getConfiguration().getFullInvCheckInterval()); + collectSound = plugin.getConfiguration().getAutoCollectSound(); itemFilter = plugin.getItemFilter(); } @@ -73,11 +77,16 @@ public void run() backpack.setChanged(); if(!full.isEmpty()) { + if(collectSound != null && item.getItemStack().getAmount() != full.get(0).getAmount()) + { // Play sound for partially collected item stacks + player.getWorld().playSound(player.getLocation(), collectSound, 1, 0); + } item.setItemStack(full.get(0)); } else { item.remove(); + if(collectSound != null) player.getWorld().playSound(player.getLocation(), collectSound, 1, 0); } } } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java index 6c8d27a..713a8cd 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java @@ -67,7 +67,7 @@ public void onClose(InventoryCloseEvent event) } if(closeSound != null) { - closer.getWorld().playSound(closer.getLocation(), closeSound, 1, 0); + closer.getWorld().playSound(closer.getEyeLocation(), closeSound, 1, 0); } } } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/ItemShortcut.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/ItemShortcut.java index ac684f9..c1104bd 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/ItemShortcut.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/ItemShortcut.java @@ -19,7 +19,6 @@ import at.pcgamingfreaks.Bukkit.MCVersion; import at.pcgamingfreaks.Bukkit.Message.Message; -import at.pcgamingfreaks.Bukkit.Util.HeadUtils; import at.pcgamingfreaks.Bukkit.Util.InventoryUtils; import at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack; import at.pcgamingfreaks.Minepacks.Bukkit.API.Events.InventoryClearedEvent; @@ -30,6 +29,7 @@ import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -56,6 +56,7 @@ public class ItemShortcut extends MinepacksListener private final int preferredSlotId; private final Set containerMaterials = new HashSet<>(); private final ItemConfig itemConfig; + private final Sound dragAndDropSound; public ItemShortcut(final @NotNull Minepacks plugin) { @@ -65,6 +66,7 @@ public ItemShortcut(final @NotNull Minepacks plugin) allowRightClickOnContainers = plugin.getConfiguration().isItemShortcutRightClickOnContainerAllowed(); preferredSlotId = plugin.getConfiguration().getItemShortcutPreferredSlotId(); blockItemFromMoving = plugin.getConfiguration().getItemShortcutBlockItemFromMoving(); + dragAndDropSound = plugin.getConfiguration().getDragAndDropSound(); openCommand = plugin.getLanguage().getCommandAliases("Backpack", "backpack")[0] + ' ' + plugin.getLanguage().getCommandAliases("Open", "open")[0]; messageDoNotRemoveItem = plugin.getLanguage().getMessage("Ingame.DontRemoveShortcut"); @@ -219,6 +221,55 @@ public void onBlockPlace(BlockPlaceEvent event) //endregion //region Handle inventory actions + private void handleDragAndDrop(final @NotNull InventoryClickEvent event) + { + final Player player = (Player) event.getWhoClicked(); + if(plugin.isDisabled(player) != WorldBlacklistMode.None || !player.hasPermission(Permissions.USE) || !plugin.isPlayerGameModeAllowed(player)) return; + Backpack backpack = plugin.getBackpackCachedOnly(player); + if(backpack != null) + { + final ItemStack stack = event.getCursor(); + if(stack != null && stack.getAmount() > 0) + { + if(plugin.getItemFilter() == null || !plugin.getItemFilter().isItemBlocked(stack)) + { + if(event.getClick() == ClickType.RIGHT) + { // right click should place only one + ItemStack place = stack.clone(); + place.setAmount(1); + ItemStack full = backpack.addItem(place); + if(full == null) + { + stack.setAmount(stack.getAmount() - 1); + event.setCursor(stack); + if(dragAndDropSound != null) player.playSound(player.getEyeLocation(), dragAndDropSound, 1, 0); + } + } + else + { + ItemStack full = backpack.addItem(stack); + if(full == null) + { + stack.setAmount(0); + if(dragAndDropSound != null) player.playSound(player.getEyeLocation(), dragAndDropSound, 1, 0); + } + else + { + if(dragAndDropSound != null && stack.getAmount() != full.getAmount()) player.playSound(player.getEyeLocation(), dragAndDropSound, 1, 0); + stack.setAmount(full.getAmount()); + } + event.setCursor(stack); + } + event.setCancelled(true); + } + else + { + plugin.getItemFilter().messageNotAllowedInBackpack.send(player, plugin.getItemFilter().itemNameResolver.getName(stack)); + } + } + } + } + @EventHandler(priority = EventPriority.LOWEST) public void onItemClick(InventoryClickEvent event) { @@ -229,40 +280,7 @@ public void onItemClick(InventoryClickEvent event) { if(event.getAction() == InventoryAction.SWAP_WITH_CURSOR) { - if(plugin.isDisabled(player) != WorldBlacklistMode.None || !player.hasPermission(Permissions.USE) || !plugin.isPlayerGameModeAllowed(player)) return; - Backpack backpack = plugin.getBackpackCachedOnly(player); - if(backpack != null) - { - final ItemStack stack = event.getCursor(); - if(stack != null && stack.getAmount() > 0) - { - if(plugin.getItemFilter() == null || !plugin.getItemFilter().isItemBlocked(stack)) - { - if(event.getClick() == ClickType.RIGHT) - { // right click should place only one - ItemStack place = stack.clone(); - place.setAmount(1); - ItemStack full = backpack.addItem(place); - if(full == null) - { - stack.setAmount(stack.getAmount() - 1); - event.setCursor(stack); - } - } - else - { - ItemStack full = backpack.addItem(stack); - stack.setAmount((full == null) ? 0 : full.getAmount()); - event.setCursor(stack); - } - event.setCancelled(true); - } - else - { - plugin.getItemFilter().messageNotAllowedInBackpack.send(player, plugin.getItemFilter().itemNameResolver.getName(stack)); - } - } - } + handleDragAndDrop(event); } else if(event.getClick() == ClickType.RIGHT || event.getClick() == ClickType.SHIFT_RIGHT) { diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/MagicValues.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/MagicValues.java index 45c8c74..de9374b 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/MagicValues.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/MagicValues.java @@ -19,5 +19,5 @@ public class MagicValues { - public static final String MIN_PCGF_PLUGIN_LIB_VERSION = "1.0.28-SNAPSHOT"; + public static final String MIN_PCGF_PLUGIN_LIB_VERSION = "1.0.30-SNAPSHOT"; } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java index 0eb4e9b..6a270fa 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java @@ -343,7 +343,7 @@ public void openBackpack(@NotNull Player opener, @Nullable Backpack backpack, bo if(opener.getOpenInventory().getTopInventory().getHolder() == backpack) return; // == is fine as there is only one instance of each backpack if(openSound != null) { - opener.getWorld().playSound(opener.getLocation(), openSound, 1, 0); + opener.getWorld().playSound(opener.getEyeLocation(), openSound, 1, 0); } backpack.open(opener, editable); }