forked from SteamWar/SteamWar
Improve RayVisualizerCommand
This commit is contained in:
+29
-20
@@ -30,6 +30,7 @@ import de.steamwar.entity.REntityServer;
|
||||
import de.steamwar.linkage.Linked;
|
||||
import de.steamwar.linkage.MinVersion;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.TNTPrimed;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -38,15 +39,15 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.util.RayTraceResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Linked
|
||||
@MinVersion(20)
|
||||
public class RayVisualizerCommand extends SWCommand implements Listener {
|
||||
|
||||
private BlockData VISIBLE = Material.LIME_CONCRETE.createBlockData();
|
||||
private BlockData BLOCKED = Material.RED_CONCRETE.createBlockData();
|
||||
|
||||
private class CRayData {
|
||||
private CRay[] rays = new CRay[27 * 400];
|
||||
private boolean[] used = new boolean[27 * 400];
|
||||
@@ -108,28 +109,23 @@ public class RayVisualizerCommand extends SWCommand implements Listener {
|
||||
|
||||
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
|
||||
if (server.getPlayers().isEmpty()) return;
|
||||
Map<Integer, List<TNTPrimed>> primedList = WORLD.getEntitiesByClass(TNTPrimed.class)
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(TNTPrimed::getFuseTicks));
|
||||
List<TNTPrimed> primedList = WORLD.getEntitiesByClass(TNTPrimed.class)
|
||||
.stream().toList();
|
||||
rayData.reset();
|
||||
if (primedList.isEmpty()) return;
|
||||
|
||||
List<Integer> fuseTicks = primedList.keySet().stream().sorted().collect(Collectors.toList());
|
||||
List<TNTPrimed> current = new ArrayList<>();
|
||||
for (int i = 0; i < fuseTicks.size(); i++) {
|
||||
List<TNTPrimed> tnts = primedList.get(fuseTicks.get(i));
|
||||
calculateRays(current, tnts);
|
||||
current.addAll(tnts);
|
||||
}
|
||||
calculateRays(primedList, primedList);
|
||||
}, 1, 1);
|
||||
}
|
||||
|
||||
private void calculateRays(List<TNTPrimed> fromTNTs, List<TNTPrimed> toTNTs) {
|
||||
for (TNTPrimed from : fromTNTs) {
|
||||
if (!from.isInWater()) continue;
|
||||
for (TNTPrimed to : toTNTs) {
|
||||
for (TNTPrimed to : toTNTs) {
|
||||
int hitting = 0;
|
||||
int total = 0;
|
||||
for (TNTPrimed from : fromTNTs) {
|
||||
if (from == to) continue;
|
||||
if (from.getFuseTicks() >= to.getFuseTicks()) continue;
|
||||
if (to.getLocation().distanceSquared(from.getLocation()) > 25) continue;
|
||||
if (!from.isInWater()) continue;
|
||||
|
||||
Location fromLoc = from.getLocation();
|
||||
Location toLoc = to.getLocation().clone().add(-0.49, 0, -0.49);
|
||||
@@ -143,16 +139,29 @@ public class RayVisualizerCommand extends SWCommand implements Listener {
|
||||
for (int dz = 0; dz < 3; dz++) {
|
||||
Location end = toLoc.clone().add(minX + dx * spacing, 0 + dy * spacing, minZ + dz * spacing);
|
||||
RayTraceResult result = fromLoc.getWorld().rayTraceBlocks(fromLoc, end.clone().subtract(fromLoc).toVector(), end.distance(fromLoc), FluidCollisionMode.NEVER, true);
|
||||
if (result != null && result.getHitBlock() != null) {
|
||||
continue;
|
||||
}
|
||||
CRay cRay = rayData.get(fromLoc, end);
|
||||
if (cRay == null) continue;
|
||||
cRay.setFrom(fromLoc);
|
||||
cRay.setTo(end);
|
||||
if (result != null && result.getHitBlock() != null) {
|
||||
cRay.setBlock(BLOCKED);
|
||||
} else {
|
||||
cRay.setBlock(VISIBLE);
|
||||
hitting++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
total += 27;
|
||||
}
|
||||
|
||||
if (total == 0) {
|
||||
to.setCustomNameVisible(false);
|
||||
} else {
|
||||
int remainder = hitting % 27;
|
||||
int whole = hitting / 27;
|
||||
to.setCustomNameVisible(true);
|
||||
to.setCustomName("§e" + whole + " " + remainder + "/27");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user