Allow items to be dropped into the backpack without opening it

This commit is contained in:
GeorgH93 2020-02-06 15:21:46 +01:00
parent 1b73ba2b2f
commit 5897082f76
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
2 changed files with 33 additions and 10 deletions

View File

@ -38,9 +38,9 @@
public class ItemFilter extends MinepacksListener implements at.pcgamingfreaks.Minepacks.Bukkit.API.ItemFilter public class ItemFilter extends MinepacksListener implements at.pcgamingfreaks.Minepacks.Bukkit.API.ItemFilter
{ {
private final Message messageNotAllowedInBackpack; public final Message messageNotAllowedInBackpack;
public final ItemNameResolver itemNameResolver;
private final Collection<MinecraftMaterial> blockedMaterials = new HashSet<>(); private final Collection<MinecraftMaterial> blockedMaterials = new HashSet<>();
private final ItemNameResolver itemNameResolver;
public ItemFilter(final Minepacks plugin) public ItemFilter(final Minepacks plugin)
{ {

View File

@ -20,6 +20,7 @@
import at.pcgamingfreaks.Bukkit.HeadUtils; import at.pcgamingfreaks.Bukkit.HeadUtils;
import at.pcgamingfreaks.Bukkit.MCVersion; import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.Bukkit.Message.Message; import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
@ -41,6 +42,7 @@
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class ItemShortcut implements Listener public class ItemShortcut implements Listener
@ -150,38 +152,59 @@ public void onItemFrameInteract(PlayerInteractEntityEvent event)
} }
//endregion //endregion
//region Handle inventory acctions //region Handle inventory actions
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onItemClick(InventoryClickEvent event) public void onItemClick(InventoryClickEvent event)
{ {
if(event.getWhoClicked() instanceof Player) if(event.getWhoClicked() instanceof Player)
{ {
final Player player = (Player) event.getWhoClicked();
if(isItemShortcut(event.getCurrentItem())) if(isItemShortcut(event.getCurrentItem()))
{ {
if(event.getClick() == ClickType.RIGHT || event.getClick() == ClickType.SHIFT_RIGHT) if(event.getAction() == InventoryAction.SWAP_WITH_CURSOR)
{ {
((Player) event.getWhoClicked()).performCommand("backpack open"); if(plugin.isDisabled(player) != WorldBlacklistMode.None || !player.hasPermission("backpack.use")) return;
Backpack backpack = plugin.getBackpackCachedOnly(player);
if(backpack != null)
{
//TODO right click should place only one
final ItemStack stack = event.getCursor();
if(plugin.getItemFilter() == null || !plugin.getItemFilter().isItemBlocked(stack))
{
Map<Integer, ItemStack> full = backpack.getInventory().addItem(stack);
stack.setAmount((full.isEmpty()) ? 0 : full.get(0).getAmount());
event.setCancelled(true);
}
else
{
plugin.getItemFilter().messageNotAllowedInBackpack.send(player, plugin.getItemFilter().itemNameResolver.getName(stack));
}
}
}
else if(event.getClick() == ClickType.RIGHT || event.getClick() == ClickType.SHIFT_RIGHT)
{
player.performCommand("backpack open");
event.setCancelled(true); event.setCancelled(true);
} }
else if(event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY) else if(event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY)
{ {
event.setCancelled(true); event.setCancelled(true);
messageDoNotRemoveItem.send(event.getWhoClicked()); messageDoNotRemoveItem.send(player);
} }
} }
else if((event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD || event.getAction() == InventoryAction.HOTBAR_SWAP) && event.getHotbarButton() != -1) else if((event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD || event.getAction() == InventoryAction.HOTBAR_SWAP) && event.getHotbarButton() != -1)
{ {
ItemStack item = event.getWhoClicked().getInventory().getItem(event.getHotbarButton()); ItemStack item = player.getInventory().getItem(event.getHotbarButton());
if(isItemShortcut(item)) if(isItemShortcut(item))
{ {
event.setCancelled(true); event.setCancelled(true);
messageDoNotRemoveItem.send(event.getWhoClicked()); messageDoNotRemoveItem.send(player);
} }
} }
else if(isItemShortcut(event.getCursor()) && !event.getWhoClicked().getInventory().equals(event.getClickedInventory())) else if(isItemShortcut(event.getCursor()) && !player.getInventory().equals(event.getClickedInventory()))
{ {
event.setCancelled(true); event.setCancelled(true);
messageDoNotRemoveItem.send(event.getWhoClicked()); messageDoNotRemoveItem.send(player);
} }
} }
} }