Move ItemFilter logic into PCGF_PluginLib

This commit is contained in:
GeorgH93 2021-03-10 19:02:38 +01:00
parent eba9027677
commit c1cf6e6316
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8

View File

@ -32,27 +32,24 @@
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 org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.Collection;
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
{ {
public final Message messageNotAllowedInBackpack; public final Message messageNotAllowedInBackpack;
public final ItemNameResolver itemNameResolver; public final ItemNameResolver itemNameResolver;
private final boolean whitelistMode; private final at.pcgamingfreaks.Bukkit.ItemFilter filter;
private final Collection<MinecraftMaterial> filteredMaterials = new HashSet<>();
private final Set<String> filteredNames, filteredLore;
public ItemFilter(final Minepacks plugin) public ItemFilter(final Minepacks plugin)
{ {
super(plugin); super(plugin);
whitelistMode = plugin.getConfiguration().isItemFilterModeWhitelist(); boolean whitelistMode = plugin.getConfiguration().isItemFilterModeWhitelist();
filter = new at.pcgamingfreaks.Bukkit.ItemFilter(whitelistMode);
Collection<MinecraftMaterial> filteredMaterials = plugin.getConfiguration().getItemFilterMaterials();
if(plugin.getConfiguration().isShulkerboxesPreventInBackpackEnabled() && !whitelistMode) if(plugin.getConfiguration().isShulkerboxesPreventInBackpackEnabled() && !whitelistMode)
{ {
for(Material mat : DisableShulkerboxes.SHULKER_BOX_MATERIALS) for(Material mat : DisableShulkerboxes.SHULKER_BOX_MATERIALS)
@ -60,9 +57,9 @@ public ItemFilter(final Minepacks plugin)
filteredMaterials.add(new MinecraftMaterial(mat, (short) -1)); filteredMaterials.add(new MinecraftMaterial(mat, (short) -1));
} }
} }
filteredMaterials.addAll(plugin.getConfiguration().getItemFilterMaterials()); filter.addFilteredMaterials(filteredMaterials);
filteredNames = plugin.getConfiguration().getItemFilterNames(); filter.addFilteredNames(plugin.getConfiguration().getItemFilterNames());
filteredLore = plugin.getConfiguration().getItemFilterLore(); filter.addFilteredLore(plugin.getConfiguration().getItemFilterLore());
messageNotAllowedInBackpack = plugin.getLanguage().getMessage("Ingame.NotAllowedInBackpack").replaceAll("\\{ItemName}", "%s"); messageNotAllowedInBackpack = plugin.getLanguage().getMessage("Ingame.NotAllowedInBackpack").replaceAll("\\{ItemName}", "%s");
@ -90,27 +87,7 @@ public ItemFilter(final Minepacks plugin)
@Override @Override
public boolean isItemBlocked(final @Nullable ItemStack item) public boolean isItemBlocked(final @Nullable ItemStack item)
{ {
if(item == null) return false; return filter.isItemBlocked(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;
if(meta.hasLore() && !filteredLore.isEmpty())
{
StringBuilder loreBuilder = new StringBuilder();
//noinspection ConstantConditions
for(String loreLine : meta.getLore())
{
if(filteredLore.contains(loreLine)) return !whitelistMode;
if(loreBuilder.length() > 0) loreBuilder.append("\n");
loreBuilder.append(loreLine);
}
if(filteredLore.contains(loreBuilder.toString())) return !whitelistMode;
}
}
return whitelistMode;
} }
@Override @Override