Add item filter whitelist mode (closes #44)

This commit is contained in:
GeorgH93 2020-02-08 03:16:05 +01:00
parent c40c6ee812
commit eeb3770de0
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
3 changed files with 27 additions and 11 deletions

View File

@ -126,6 +126,8 @@ Shulkerboxes:
ItemFilter:
# Enables the item filter. Make sure to define items to be filtered.
Enable: false
# Changes the filter mode, either blacklist (only not listed materials are allowed) or whitelist (only listed materials are allowed)
Mode: blacklist
# List off items not allowed in the backpack. Can be name or id (id only for MC versions older than 1.13!).
Blacklist: []
@ -175,4 +177,4 @@ Misc:
UseBungeeCord: false
# Config file version. Don't touch it!
Version: 22
Version: 23

View File

@ -37,7 +37,7 @@
public class Config extends Configuration implements DatabaseConnectionConfiguration
{
private static final int CONFIG_VERSION = 22, UPGRADE_THRESHOLD = 22, PRE_V2_VERSION = 20;
private static final int CONFIG_VERSION = 23, UPGRADE_THRESHOLD = CONFIG_VERSION, PRE_V2_VERSION = 20;
public Config(JavaPlugin plugin)
{
@ -278,14 +278,21 @@ public boolean isShulkerboxesExistingDestroyEnabled()
//endregion
//region Item filter
public boolean isItemFilterEnabledNoShulker()
{
return getConfigE().getBoolean("ItemFilter.Enabled", false);
}
public boolean isItemFilterEnabled()
{
return getConfigE().getBoolean("ItemFilter.Enable", false) || getConfigE().getBoolean("Shulkerboxes.PreventInBackpack", true);
return isItemFilterEnabledNoShulker() || getConfigE().getBoolean("Shulkerboxes.PreventInBackpack", true);
}
public Collection<MinecraftMaterial> getItemFilterBlacklist()
{
if(!isItemFilterEnabledNoShulker()) return new LinkedList<>();
List<String> stringBlacklist = getConfigE().getStringList("ItemFilter.Blacklist", new LinkedList<>());
if(isItemFilterModeWhitelist()) stringBlacklist.add("air");
Collection<MinecraftMaterial> blacklist = new LinkedList<>();
for(String item : stringBlacklist)
{
@ -294,6 +301,11 @@ public Collection<MinecraftMaterial> getItemFilterBlacklist()
}
return blacklist;
}
public boolean isItemFilterModeWhitelist()
{
return getConfigE().getString("ItemFilter.Mode", "blacklist").toLowerCase(Locale.ENGLISH).equals("whitelist") && isItemFilterEnabledNoShulker();
}
//endregion
//region World settings

View File

@ -40,13 +40,15 @@ public class ItemFilter extends MinepacksListener implements at.pcgamingfreaks.M
{
public final Message messageNotAllowedInBackpack;
public final ItemNameResolver itemNameResolver;
private final boolean whitelistMode;
private final Collection<MinecraftMaterial> blockedMaterials = new HashSet<>();
public ItemFilter(final Minepacks plugin)
{
super(plugin);
if(plugin.getConfiguration().isShulkerboxesPreventInBackpackEnabled())
whitelistMode = plugin.getConfiguration().isItemFilterModeWhitelist();
if(plugin.getConfiguration().isShulkerboxesPreventInBackpackEnabled() && !whitelistMode)
{
for(Material mat : DisableShulkerboxes.SHULKER_BOX_MATERIALS)
{
@ -78,6 +80,12 @@ public ItemFilter(final Minepacks plugin)
/*end[STANDALONE]*/
}
@Override
public boolean isItemBlocked(ItemStack item)
{
return whitelistMode ^ blockedMaterials.contains(new MinecraftMaterial(item));
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onItemMove(InventoryMoveItemEvent event)
{
@ -92,7 +100,7 @@ public void onItemMove(InventoryMoveItemEvent event)
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onItemMove(InventoryClickEvent event)
public void onItemClick(InventoryClickEvent event)
{
if(event.getInventory().getHolder() instanceof Backpack)
{
@ -124,10 +132,4 @@ public void onItemMove(InventoryDragEvent event)
event.setCancelled(true);
}
}
@Override
public boolean isItemBlocked(ItemStack item)
{
return blockedMaterials.contains(new MinecraftMaterial(item));
}
}