Skip to content

Commit

Permalink
Fix bug where the auto-pickup feature bypassed the item filter
Browse files Browse the repository at this point in the history
Fixes #48
  • Loading branch information
GeorgH93 committed Oct 16, 2019
1 parent 6d07050 commit 6c93d1a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>Minepacks</artifactId>
<version>2.0.11-RC2</version>
<version>2.0.11</version>

<scm>
<connection>scm:git:git@github.com:GeorgH93/Minepacks.git</connection>
Expand Down Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>Minepacks-API</artifactId>
<version>2.0</version>
<version>2.0.11</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
Expand All @@ -78,7 +78,7 @@
<dependency>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>PluginLib</artifactId>
<version>1.0.16-SNAPSHOT</version>
<version>1.0.17-SNAPSHOT</version>
</dependency>
<!-- BadRabbit -->
<dependency>
Expand Down
4 changes: 4 additions & 0 deletions src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package at.pcgamingfreaks.Minepacks.Bukkit;

import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.ItemFilter;

import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
Expand All @@ -35,12 +36,14 @@ public class ItemsCollector extends BukkitRunnable
private final Minepacks plugin;
private final double radius;
private final BukkitTask task;
private final ItemFilter itemFilter;

public ItemsCollector(Minepacks plugin)
{
this.plugin = plugin;
this.radius = plugin.getConfiguration().getFullInvRadius();
task = runTaskTimer(plugin, plugin.getConfiguration().getFullInvCheckInterval(), plugin.getConfiguration().getFullInvCheckInterval());
itemFilter = plugin.getItemFilter();
}

@Override
Expand All @@ -65,6 +68,7 @@ public void run()
Item item = (Item) entity;
if(!item.isDead() && item.getPickupDelay() <= 0)
{
if(itemFilter != null && itemFilter.isItemBlocked(item.getItemStack())) continue;
HashMap<Integer, ItemStack> full = backpack.getInventory().addItem(item.getItemStack());
backpack.setChanged();
if(!full.isEmpty())
Expand Down
16 changes: 11 additions & 5 deletions src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/ItemFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.Collection;
import java.util.HashSet;

public class ItemFilter extends MinepacksListener
public class ItemFilter extends MinepacksListener implements at.pcgamingfreaks.Minepacks.Bukkit.API.ItemFilter
{
private final Message messageNotAllowedInBackpack;
private final Collection<MinecraftMaterial> blockedMaterials = new HashSet<>();
Expand Down Expand Up @@ -81,7 +81,7 @@ public ItemFilter(final Minepacks plugin)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onItemMove(InventoryMoveItemEvent event)
{
if(event.getDestination().getHolder() instanceof Backpack && blockedMaterials.contains(new MinecraftMaterial(event.getItem())))
if(event.getDestination().getHolder() instanceof Backpack && isItemBlocked(event.getItem()))
{
if(event.getSource().getHolder() instanceof Player)
{
Expand All @@ -99,9 +99,9 @@ public void onItemMove(InventoryClickEvent event)
if(event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD && event.getHotbarButton() != -1)
{
ItemStack item = event.getWhoClicked().getInventory().getItem(event.getHotbarButton());
if(item != null && blockedMaterials.contains(new MinecraftMaterial(item))) event.setCancelled(true);
if(item != null && isItemBlocked(item)) event.setCancelled(true);
}
else if(event.getCurrentItem() != null && blockedMaterials.contains(new MinecraftMaterial(event.getCurrentItem())))
else if(event.getCurrentItem() != null && isItemBlocked(event.getCurrentItem()))
{
if(event.getClickedInventory() != null && event.getClickedInventory().getHolder() instanceof Backpack) return; // Allow user to move blacklisted items out of the backpack

Expand All @@ -114,10 +114,16 @@ else if(event.getCurrentItem() != null && blockedMaterials.contains(new Minecraf
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onItemMove(InventoryDragEvent event)
{
if(event.getInventory().getHolder() instanceof Backpack && event.getOldCursor() != null && blockedMaterials.contains(new MinecraftMaterial(event.getOldCursor())))
if(event.getInventory().getHolder() instanceof Backpack && event.getOldCursor() != null && isItemBlocked(event.getOldCursor()))
{
messageNotAllowedInBackpack.send(event.getView().getPlayer(), itemNameResolver.getName(event.getOldCursor()));
event.setCancelled(true);
}
}

@Override
public boolean isItemBlocked(ItemStack item)
{
return blockedMaterials.contains(new MinecraftMaterial(item));
}
}
14 changes: 13 additions & 1 deletion src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class Minepacks extends JavaPlugin implements MinepacksPlugin
private CommandManager commandManager;
private Collection<GameMode> gameModes;
private CooldownManager cooldownManager = null;
private ItemFilter itemFilter = null;

public static Minepacks getInstance()
{
Expand Down Expand Up @@ -188,7 +189,11 @@ private void load()
PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new BackpackEventListener(this), this);
if(config.getDropOnDeath()) pluginManager.registerEvents(new DropOnDeath(this), this);
if(config.isItemFilterEnabled()) pluginManager.registerEvents(new ItemFilter(this), this);
if(config.isItemFilterEnabled())
{
itemFilter = new ItemFilter(this);
pluginManager.registerEvents(itemFilter, this);
}
if(config.isShulkerboxesDisable()) pluginManager.registerEvents(new DisableShulkerboxes(this), this);
//endregion
if(config.getFullInvCollect()) collector = new ItemsCollector(this);
Expand All @@ -209,6 +214,7 @@ private void unload()
if(cooldownManager != null) cooldownManager.close();
cooldownManager = null;
getServer().getScheduler().cancelTasks(this); // Kill all running task
itemFilter = null;
}

public void reload()
Expand Down Expand Up @@ -347,4 +353,10 @@ public boolean isPlayerGameModeAllowed(Player player)
{
return cooldownManager;
}

@Override
public @Nullable ItemFilter getItemFilter()
{
return itemFilter;
}
}

0 comments on commit 6c93d1a

Please sign in to comment.