From 72e2423741f48813f748e82ee3c276255807281d Mon Sep 17 00:00:00 2001 From: PretzelJohn <58197328+PretzelJohn@users.noreply.github.com> Date: Sun, 10 Apr 2022 21:32:26 -0400 Subject: [PATCH] Version 1.5.3-pre2: * Add support for other plugins that cancel the PlayerInteractEntityEvent. No longer runs the interaction code when the event has already been cancelled. * Add support for other plugins that use special items. For example, SafariNet captures villagers when a player uses a custom spawn egg on them. No longer runs the interaction code when the player is holding one of the items listed in config.yml --- README.md | 21 +++++++++++++------ .../listeners/PlayerListener.java | 18 +++++++++++++--- .../nms/utils/MinecraftVersion.java | 7 ------- src/main/resources/config.yml | 3 +++ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 33bdd1e..e64bad1 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
by PretzelJohn

Description:

-

This Minecraft plugin limits the villager trade deals that players can get.
Click here to see this plugin on Spigot.

+

This Minecraft plugin limits the villager trade deals that players can get.
Supports Spigot, Paper, and Purpur servers from 1.14.1 to the current version.
Click here to see this plugin on Spigot.

Some information has moved to the Wiki!


@@ -59,6 +59,15 @@ IgnoreShopkeepers: Whether to ignore Shopkeepers NPCs from the Shopkeepers plugin. If set to true, Shopkeepers NPCs won't be affected by this plugin. + + IgnoreHeldItems: + A list of item types where, when the player interacts with a villager while holding one of these items, VTL doesn't affect the interaction. This is used for compatibility with other plugins, like the custom spawn eggs in SafariNet.
Options: + + + DisableTrading: Whether to disable all villager trading for all worlds, some worlds, or no worlds.
Options: @@ -110,7 +119,7 @@
  • -

    Per-item settings: (Overrides:)

    +

    Per-item override settings: (Overrides:)

    @@ -138,19 +147,19 @@ - + - + - +
    Setting
    .Cooldown:Sets the time between restocks for the trade, and applies to ALL villagers. Once the player reaches the MaxUses, the cooldown begins. The trade is disabled for all villagers until the cooldown expires.
    FORMAT: <Number><interval>
    EXAMPLE: 30s = 30 seconds, 5m = 5 minutes, 4h = 4 hours, 7d = 7 days
    Sets the time between restocks for the trade, and applies to ALL villagers. Once the player reaches the MaxUses, the cooldown begins. The trade is disabled for all villagers until the cooldown expires.
    Format: <Number><interval>
    Examples: 30s = 30 seconds, 5m = 5 minutes, 4h = 4 hours, 7d = 7 days
    .Item1.Material:
    .Item2.Material:
    .Result.Material:
    Sets the material of the 1st or 2nd item in the trade
    WARNING: This cannot be undone!
    Sets the material of the 1st, 2nd, or result item in the trade
    WARNING: This cannot be undone!
    .Item1.Amount:
    .Item2.Amount:
    .Result.Amount:
    Sets the amount of the 1st or 2nd item in the trade
    WARNING: This cannot be undone!
    Sets the amount of the 1st, 2nd, or result item in the trade
    WARNING: This cannot be undone!
  • -

    For the default config.yml, see: src/main/resources/config.yml

    +

    For the default config.yml, see src/main/resources/config.yml

  • diff --git a/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java b/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java index f775247..f2016d6 100644 --- a/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java +++ b/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java @@ -5,6 +5,7 @@ import com.pretzel.dev.villagertradelimiter.data.Cooldown; import com.pretzel.dev.villagertradelimiter.data.PlayerData; import com.pretzel.dev.villagertradelimiter.settings.Settings; import com.pretzel.dev.villagertradelimiter.wrappers.*; +import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; @@ -12,6 +13,7 @@ import org.bukkit.entity.Villager; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; @@ -35,12 +37,22 @@ public class PlayerListener implements Listener { /** Handles when a player begins trading with a villager */ @EventHandler public void onPlayerBeginTrading(final PlayerInteractEntityEvent event) { - if(!(event.getRightClicked() instanceof Villager)) return; + if(event.isCancelled()) return; //Skips when event is already cancelled + if(!(event.getRightClicked() instanceof Villager)) return; //Skips non-villager entities + final Player player = event.getPlayer(); final Villager villager = (Villager)event.getRightClicked(); + + //Skips when player is holding an ignored item + Material heldItemType = player.getInventory().getItem(event.getHand()).getType(); + for(String ignoredType : instance.getCfg().getStringList("IgnoredHeldItems")) { + if(heldItemType.equals(Material.matchMaterial(ignoredType))) { + event.setCancelled(true); + return; + } + } if(settings.shouldSkipNPC(event.getPlayer()) || settings.shouldSkipNPC(villager)) return; //Skips NPCs - if(villager.getProfession() == Villager.Profession.NONE || villager.getProfession() == Villager.Profession.NITWIT) return; //Skips non-trading villagers - if(villager.getRecipeCount() == 0) return; //Skips non-trading villagers + if(villager.getProfession() == Villager.Profession.NONE || villager.getProfession() == Villager.Profession.NITWIT || villager.getRecipeCount() == 0) return; //Skips non-trading villagers //DisableTrading feature if(instance.getCfg().isBoolean("DisableTrading")) { diff --git a/src/com/pretzel/dev/villagertradelimiter/nms/utils/MinecraftVersion.java b/src/com/pretzel/dev/villagertradelimiter/nms/utils/MinecraftVersion.java index 693444b..46ae957 100644 --- a/src/com/pretzel/dev/villagertradelimiter/nms/utils/MinecraftVersion.java +++ b/src/com/pretzel/dev/villagertradelimiter/nms/utils/MinecraftVersion.java @@ -117,13 +117,6 @@ public enum MinecraftVersion { } private static void init() { - try { - if (hasGsonSupport() && !bStatsDisabled) - new ApiMetricsLite(); - } catch (Exception ex) { - logger.log(Level.WARNING, "[NBTAPI] Error enabling Metrics!", ex); - } - if (hasGsonSupport() && !updateCheckDisabled) new Thread(() -> { try { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index fb8fb12..0e50876 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -23,6 +23,9 @@ database: IgnoreCitizens: true IgnoreShopkeepers: true +# Ignore interactions when the player is holding one of these item types (e.g. spawn_egg) +IgnoreHeldItems: [] + # Add world names for worlds that you want to completely disable ALL villager trading. Set to [] to disable this feature. DisableTrading: - world_nether