Fix sponge absorbing water across plot borders (#3191)

* Fix sponge absorbing water across plot borders

* Shorten for-loops by using removeIf

* Use 'Location' instead of 'var'

Co-authored-by: NotMyFault <mc.cache@web.de>
This commit is contained in:
Traks 2021-08-03 21:07:02 +02:00 committed by GitHub
parent e7fa057abe
commit fd4cec39c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -101,6 +101,7 @@ import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.block.CauldronLevelChangeEvent;
import org.bukkit.event.block.EntityBlockFormEvent;
import org.bukkit.event.block.LeavesDecayEvent;
import org.bukkit.event.block.SpongeAbsorbEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.material.Directional;
import org.bukkit.projectiles.BlockProjectileSource;
@ -1183,4 +1184,29 @@ public class BlockEventListener implements Listener {
}
@EventHandler(ignoreCancelled = true)
public void onSpongeAbsorb(SpongeAbsorbEvent event) {
Block sponge = event.getBlock();
Location location = BukkitUtil.adapt(sponge.getLocation());
PlotArea area = location.getPlotArea();
List<org.bukkit.block.BlockState> blocks = event.getBlocks();
if (area == null) {
blocks.removeIf(block -> BukkitUtil.adapt(block.getLocation()).isPlotArea());
} else {
Plot origin = area.getOwnedPlot(location);
blocks.removeIf(block -> {
Location blockLocation = BukkitUtil.adapt(block.getLocation());
if (!area.contains(blockLocation.getX(), blockLocation.getZ())) {
return true;
}
Plot plot = area.getOwnedPlot(blockLocation);
return !Objects.equals(plot, origin);
});
}
if (blocks.isEmpty()) {
// Cancel event so the sponge block doesn't turn into a wet sponge
// if no water is being absorbed
event.setCancelled(true);
}
}
}