Add option to filter by name #33

This commit is contained in:
GeorgH93 2020-02-12 02:50:09 +01:00
parent 0070d5bb84
commit d25e6c5b1e
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
3 changed files with 24 additions and 3 deletions

View File

@ -129,11 +129,14 @@ Shulkerboxes:
ItemFilter: ItemFilter:
# Enables the item filter. Make sure to define items to be filtered. # Enables the item filter. Make sure to define items to be filtered.
Enable: false Enabled: false
# Changes the filter mode, either blacklist (only not listed materials are allowed) or whitelist (only listed materials are allowed) # Changes the filter mode, either blacklist (only not listed materials are allowed) or whitelist (only listed materials are allowed)
Mode: blacklist Mode: blacklist
# List off materials that should be filtered. Can be name or id (id only for MC versions older than 1.13!). # Filter lists bellow. An item will be blocked (in blacklist mode) or allowed (in whitelist mode) if it matches on of the given filters.
# List of materials that should be filtered. Can be name or id (id only for MC versions older than 1.13!).
Materials: [] Materials: []
# List of names that should be filtered. Must match the display name of the item exactly. & color codes will be converted automatically.
Names: []
# This settings allow control over how the plugin behave in different worlds # This settings allow control over how the plugin behave in different worlds
WorldSettings: WorldSettings:

View File

@ -304,6 +304,13 @@ public Collection<MinecraftMaterial> getItemFilterMaterials()
return blacklist; return blacklist;
} }
public Set<String> getItemFilterNames()
{
Set<String> names = new HashSet<>();
getConfigE().getStringList("ItemFilter.Names", new LinkedList<>()).forEach(name -> names.add(ChatColor.translateAlternateColorCodes('&', name)));
return names;
}
public boolean isItemFilterModeWhitelist() public boolean isItemFilterModeWhitelist()
{ {
return getConfigE().getString("ItemFilter.Mode", "blacklist").toLowerCase(Locale.ENGLISH).equals("whitelist") && isItemFilterEnabledNoShulker(); return getConfigE().getString("ItemFilter.Mode", "blacklist").toLowerCase(Locale.ENGLISH).equals("whitelist") && isItemFilterEnabledNoShulker();

View File

@ -32,9 +32,11 @@
import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
public class ItemFilter extends MinepacksListener implements at.pcgamingfreaks.Minepacks.Bukkit.API.ItemFilter public class ItemFilter extends MinepacksListener implements at.pcgamingfreaks.Minepacks.Bukkit.API.ItemFilter
{ {
@ -42,6 +44,7 @@ public class ItemFilter extends MinepacksListener implements at.pcgamingfreaks.M
public final ItemNameResolver itemNameResolver; public final ItemNameResolver itemNameResolver;
private final boolean whitelistMode; private final boolean whitelistMode;
private final Collection<MinecraftMaterial> filteredMaterials = new HashSet<>(); private final Collection<MinecraftMaterial> filteredMaterials = new HashSet<>();
private final Set<String> filteredNames;
public ItemFilter(final Minepacks plugin) public ItemFilter(final Minepacks plugin)
{ {
@ -56,6 +59,7 @@ public ItemFilter(final Minepacks plugin)
} }
} }
filteredMaterials.addAll(plugin.getConfiguration().getItemFilterMaterials()); filteredMaterials.addAll(plugin.getConfiguration().getItemFilterMaterials());
filteredNames = plugin.getConfiguration().getItemFilterNames();
messageNotAllowedInBackpack = plugin.getLanguage().getMessage("Ingame.NotAllowedInBackpack").replaceAll("\\{ItemName}", "%s"); messageNotAllowedInBackpack = plugin.getLanguage().getMessage("Ingame.NotAllowedInBackpack").replaceAll("\\{ItemName}", "%s");
@ -83,7 +87,14 @@ public ItemFilter(final Minepacks plugin)
@Override @Override
public boolean isItemBlocked(ItemStack item) public boolean isItemBlocked(ItemStack item)
{ {
return whitelistMode ^ filteredMaterials.contains(new MinecraftMaterial(item)); if(filteredMaterials.contains(new MinecraftMaterial(item))) return !whitelistMode;
if(item.hasItemMeta())
{
ItemMeta meta = item.getItemMeta();
assert meta != null; //TODO remove after testing
if(meta.hasDisplayName() && filteredNames.contains(meta.getDisplayName())) return !whitelistMode;
}
return whitelistMode;
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)