forked from SteamWar/SteamWar
Improve SelectAdjacent
This commit is contained in:
+12
-2
@@ -42,6 +42,7 @@ import java.util.HashMap;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
@Linked
|
@Linked
|
||||||
@MinVersion(20)
|
@MinVersion(20)
|
||||||
@@ -79,7 +80,12 @@ public class SelectAdjacent implements Listener {
|
|||||||
if (event.getAction() != Action.LEFT_CLICK_BLOCK) return;
|
if (event.getAction() != Action.LEFT_CLICK_BLOCK) return;
|
||||||
Selector selector = selectors.get(event.getPlayer());
|
Selector selector = selectors.get(event.getPlayer());
|
||||||
if (selector != null) selector.cancel();
|
if (selector != null) selector.cancel();
|
||||||
selector = new Selector(event.getClickedBlock(), event.getPlayer());
|
Material material = event.getPlayer().getInventory().getItemInOffHand().getType();
|
||||||
|
if (material.isAir()) {
|
||||||
|
selector = new Selector(event.getClickedBlock(), event.getPlayer(), __ -> true);
|
||||||
|
} else {
|
||||||
|
selector = new Selector(event.getClickedBlock(), event.getPlayer(), type -> type == material);
|
||||||
|
}
|
||||||
selectors.put(event.getPlayer(), selector);
|
selectors.put(event.getPlayer(), selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,10 +107,12 @@ public class SelectAdjacent implements Listener {
|
|||||||
private int maxZ;
|
private int maxZ;
|
||||||
|
|
||||||
private BukkitTask bukkitTask;
|
private BukkitTask bukkitTask;
|
||||||
|
private Predicate<Material> predicate;
|
||||||
private Set<Location> seen = new HashSet<>();
|
private Set<Location> seen = new HashSet<>();
|
||||||
private Set<Location> toCalc = new HashSet<>();
|
private Set<Location> toCalc = new HashSet<>();
|
||||||
|
|
||||||
public Selector(Block block, Player player) {
|
public Selector(Block block, Player player, Predicate<Material> predicate) {
|
||||||
|
this.predicate = predicate;
|
||||||
toCalc.add(block.getLocation());
|
toCalc.add(block.getLocation());
|
||||||
minX = block.getX();
|
minX = block.getX();
|
||||||
minY = block.getY();
|
minY = block.getY();
|
||||||
@@ -148,6 +156,7 @@ public class SelectAdjacent implements Listener {
|
|||||||
for (Location location : current) {
|
for (Location location : current) {
|
||||||
Block block = location.getBlock();
|
Block block = location.getBlock();
|
||||||
if (block.isEmpty() || block.isLiquid()) continue;
|
if (block.isEmpty() || block.isLiquid()) continue;
|
||||||
|
if (!predicate.test(block.getType())) continue;
|
||||||
seen.add(location);
|
seen.add(location);
|
||||||
|
|
||||||
minX = Math.min(minX, location.getBlockX());
|
minX = Math.min(minX, location.getBlockX());
|
||||||
@@ -160,6 +169,7 @@ public class SelectAdjacent implements Listener {
|
|||||||
for (Vector face : FACES) {
|
for (Vector face : FACES) {
|
||||||
Block next = block.getRelative(face.getBlockX(), face.getBlockY(), face.getBlockZ());
|
Block next = block.getRelative(face.getBlockX(), face.getBlockY(), face.getBlockZ());
|
||||||
if (next.isEmpty() || next.isLiquid()) continue;
|
if (next.isEmpty() || next.isLiquid()) continue;
|
||||||
|
if (!predicate.test(next.getType())) continue;
|
||||||
Location loc = next.getLocation();
|
Location loc = next.getLocation();
|
||||||
if (seen.contains(loc)) continue;
|
if (seen.contains(loc)) continue;
|
||||||
toCalc.add(loc);
|
toCalc.add(loc);
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import org.bukkit.scheduler.BukkitTask;
|
|||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class SelectAdjacent implements Listener {
|
public class SelectAdjacent implements Listener {
|
||||||
|
|
||||||
@@ -76,7 +77,12 @@ public class SelectAdjacent implements Listener {
|
|||||||
if (event.getAction() != Action.LEFT_CLICK_BLOCK) return;
|
if (event.getAction() != Action.LEFT_CLICK_BLOCK) return;
|
||||||
Selector selector = selectors.get(event.getPlayer());
|
Selector selector = selectors.get(event.getPlayer());
|
||||||
if (selector != null) selector.cancel();
|
if (selector != null) selector.cancel();
|
||||||
selector = new Selector(event.getClickedBlock(), event.getPlayer());
|
Material material = event.getPlayer().getInventory().getItemInOffHand().getType();
|
||||||
|
if (material.isAir()) {
|
||||||
|
selector = new Selector(event.getClickedBlock(), event.getPlayer(), __ -> true);
|
||||||
|
} else {
|
||||||
|
selector = new Selector(event.getClickedBlock(), event.getPlayer(), type -> type == material);
|
||||||
|
}
|
||||||
selectors.put(event.getPlayer(), selector);
|
selectors.put(event.getPlayer(), selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,10 +107,12 @@ public class SelectAdjacent implements Listener {
|
|||||||
private int maxZ;
|
private int maxZ;
|
||||||
|
|
||||||
private BukkitTask bukkitTask;
|
private BukkitTask bukkitTask;
|
||||||
|
private Predicate<Material> predicate;
|
||||||
private Set<Location> seen = new HashSet<>();
|
private Set<Location> seen = new HashSet<>();
|
||||||
private Set<Location> toCalc = new HashSet<>();
|
private Set<Location> toCalc = new HashSet<>();
|
||||||
|
|
||||||
public Selector(Block block, Player player) {
|
public Selector(Block block, Player player, Predicate<Material> predicate) {
|
||||||
|
this.predicate = predicate;
|
||||||
toCalc.add(block.getLocation());
|
toCalc.add(block.getLocation());
|
||||||
minX = block.getX();
|
minX = block.getX();
|
||||||
minY = block.getY();
|
minY = block.getY();
|
||||||
@@ -145,6 +153,7 @@ public class SelectAdjacent implements Listener {
|
|||||||
for (Location location : current) {
|
for (Location location : current) {
|
||||||
Block block = location.getBlock();
|
Block block = location.getBlock();
|
||||||
if (block.isEmpty() || block.isLiquid()) continue;
|
if (block.isEmpty() || block.isLiquid()) continue;
|
||||||
|
if (!predicate.test(block.getType())) continue;
|
||||||
seen.add(location);
|
seen.add(location);
|
||||||
|
|
||||||
minX = Math.min(minX, location.getBlockX());
|
minX = Math.min(minX, location.getBlockX());
|
||||||
@@ -157,6 +166,7 @@ public class SelectAdjacent implements Listener {
|
|||||||
for (Vector face : FACES) {
|
for (Vector face : FACES) {
|
||||||
Block next = block.getRelative(face.getBlockX(), face.getBlockY(), face.getBlockZ());
|
Block next = block.getRelative(face.getBlockX(), face.getBlockY(), face.getBlockZ());
|
||||||
if (next.isEmpty() || next.isLiquid()) continue;
|
if (next.isEmpty() || next.isLiquid()) continue;
|
||||||
|
if (!predicate.test(next.getType())) continue;
|
||||||
Location loc = next.getLocation();
|
Location loc = next.getLocation();
|
||||||
if (seen.contains(loc)) continue;
|
if (seen.contains(loc)) continue;
|
||||||
toCalc.add(loc);
|
toCalc.add(loc);
|
||||||
|
|||||||
Reference in New Issue
Block a user