From cdfae387f0d0431a28899a2836669c8845998280 Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Thu, 24 Mar 2022 21:08:14 +0200 Subject: [PATCH] Fixed detection of crops when they grow for block limits (#972) --- .../upgrades/type/UpgradeTypeBlockLimits.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeBlockLimits.java b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeBlockLimits.java index 0dc02961c..a70e308d5 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeBlockLimits.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/module/upgrades/type/UpgradeTypeBlockLimits.java @@ -10,15 +10,19 @@ import com.bgsoftware.superiorskyblock.utils.ServerVersion; import com.bgsoftware.superiorskyblock.utils.StringUtils; import com.bgsoftware.superiorskyblock.utils.legacy.Materials; +import org.bukkit.block.BlockState; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockGrowEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.world.StructureGrowEvent; import org.bukkit.inventory.EquipmentSlot; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -120,6 +124,34 @@ public void onBucketEmpty(PlayerBucketEmptyEvent e) { } } + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onBlockGrow(BlockGrowEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getBlock().getLocation()); + + if (island == null) + return; + + Key blockKey = Key.of(e.getNewState()); + + if (island.hasReachedBlockLimit(blockKey)) + e.setCancelled(true); + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onStructureGrow(StructureGrowEvent e) { + Island island = plugin.getGrid().getIslandAt(e.getLocation()); + + if (island == null) + return; + + List blockStates = new ArrayList<>(e.getBlocks()); + + blockStates.forEach(blockState -> { + if (island.hasReachedBlockLimit(Key.of(blockState))) + e.getBlocks().remove(blockState); + }); + } + } }