Skip to content

Commit efc7229

Browse files
committed
Added better dispenser handling
1 parent d92c1d0 commit efc7229

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

Diff for: src/main/java/me/rayzr522/permissionitems/listeners/PlayerListener.java

+10-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.bukkit.Location;
88
import org.bukkit.Material;
99
import org.bukkit.block.BlockFace;
10-
import org.bukkit.entity.Entity;
1110
import org.bukkit.entity.LivingEntity;
1211
import org.bukkit.entity.Player;
1312
import org.bukkit.event.EventHandler;
@@ -29,6 +28,7 @@
2928
import java.util.Optional;
3029
import java.util.UUID;
3130
import java.util.function.Function;
31+
import java.util.stream.Collectors;
3232

3333
public class PlayerListener implements Listener {
3434
private final PermissionItems plugin;
@@ -199,26 +199,20 @@ public void onArmorEquip(BlockDispenseEvent e) {
199199
BlockFace facing = state.getFacing();
200200
Location location = e.getBlock().getRelative(facing).getLocation();
201201

202-
LivingEntity target = findDispenserTarget(location);
202+
boolean anyPrevent = findDispenserTargets(location).stream()
203+
.filter(target -> target instanceof Player)
204+
.anyMatch(target -> isPreventedFor((Player) target, e.getItem(), PreventOptions::isEquippingPrevented, "equipping"));
203205

204-
if (!(target instanceof Player)) {
205-
return;
206-
}
207-
208-
if (isPreventedFor((Player) target, e.getItem(), PreventOptions::isEquippingPrevented, "equipping")) {
206+
if (anyPrevent) {
209207
e.setCancelled(true);
210208
}
211209
}
212210

213-
private LivingEntity findDispenserTarget(Location location) {
214-
for (Entity entity : location.getWorld().getNearbyEntities(location, 1.5, 1.5, 1.5)) {
215-
if (!(entity instanceof LivingEntity)) {
216-
continue;
217-
}
218-
219-
return (LivingEntity) entity;
220-
}
221-
return null;
211+
private List<LivingEntity> findDispenserTargets(Location location) {
212+
return location.getWorld().getNearbyEntities(location, 1, 1, 1).stream()
213+
.filter(entity -> entity instanceof LivingEntity)
214+
.map(entity -> (LivingEntity) entity)
215+
.collect(Collectors.toList());
222216
}
223217

224218
private enum ArmorType {

0 commit comments

Comments
 (0)