Add sound effects for auto collect and drag and drop (fixes #106)

This commit is contained in:
GeorgH93 2020-08-04 18:04:26 +02:00
parent 26f61cc1f6
commit 87b47e93d0
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
7 changed files with 86 additions and 41 deletions

View File

@ -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
Version: 33

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<Material> 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)
{

View File

@ -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";
}

View File

@ -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);
}