SteamWar/BauSystem
Archiviert
13
0

Add TNTTracer 15

Dieser Commit ist enthalten in:
jojo 2020-07-07 21:21:00 +02:00
Ursprung 3c840dfde1
Commit 0cf208af2e
14 geänderte Dateien mit 595 neuen und 94 gelöschten Zeilen

Datei anzeigen

@ -1,4 +1,4 @@
package de.steamwar.bausystem.world;
package de.steamwar.bausystem.tracer;
import org.bukkit.Bukkit;
import org.bukkit.Location;

Datei anzeigen

@ -0,0 +1,78 @@
package de.steamwar.bausystem.tracer;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
class TNTTracer_15 {
private TNTTracer_15(){}
private static Location location;
private static BukkitTask task;
private static TracerCallback_15 tracerCallback;
private static final double showRadius = square(50.0);
public static double square(double d) {
return d * d;
}
static void init(Plugin plugin, TracerCallback_15 tracerCallback) {
task = Bukkit.getScheduler().runTaskTimer(plugin, TNTTracer_15::run, 1, 20);
TNTTracer_15.tracerCallback = tracerCallback;
}
private static void run() {
for (Player p : Bukkit.getOnlinePlayers()) {
double[] doubles = tracerCallback.run(p);
if (doubles.length == 0 || doubles.length % 3 != 0) continue;
Location location = p.getLocation();
World world = location.getWorld();
double px = location.getX();
double py = location.getY();
double pz = location.getZ();
for (int i = 0; i < doubles.length; i += 3) {
double x = doubles[i + 0];
double y = doubles[i + 1];
double z = doubles[i + 2];
if (square((x + 0.5) - px) + square((y + 0.5) - py) + square((z + 0.5) - pz) > showRadius) continue;
showCorner(world, p, x, y, z);
}
}
}
private static void showCorner(World world, Player player, double x, double y, double z) {
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 0, y + 0, z + 0), 1, 0F, 0F, 0F, 0.001);
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 1, y + 0, z + 0), 1, 0F, 0F, 0F, 0.001);
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 0, y + 0, z + 1), 1, 0F, 0F, 0F, 0.001);
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 1, y + 0, z + 1), 1, 0F, 0F, 0F, 0.001);
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 0, y + 1, z + 0), 1, 0F, 0F, 0F, 0.001);
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 1, y + 1, z + 0), 1, 0F, 0F, 0F, 0.001);
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 0, y + 1, z + 1), 1, 0F, 0F, 0F, 0.001);
player.spawnParticle(Particle.FLAME, makeLocation(world, x + 1, y + 1, z + 1), 1, 0F, 0F, 0F, 0.001);
}
private static Location makeLocation(World world, double x, double y, double z) {
if (!location.getWorld().equals(world)) {
location.setWorld(world);
}
location.setX(x);
location.setY(y);
location.setZ(z);
return location;
}
}

Datei anzeigen

@ -0,0 +1,10 @@
package de.steamwar.bausystem.tracer;
import org.bukkit.entity.Player;
@FunctionalInterface
public interface TracerCallback_15 {
double[] run(Player p);
}

Datei anzeigen

@ -1,38 +0,0 @@
package de.steamwar.bausystem.world;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.TNTPrimed;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
class TNTTracer_15 {
private TNTTracer_15(){}
static void remove(Map<TNTPrimed, LinkedList<Location>> locations, TNTPrimed tnt){
Material material = tnt.getLocation().getBlock().getType();
if(material == Material.WATER)
locations.remove(tnt);
}
static int show(Map<TNTPrimed, LinkedList<Location>> locations, Set<Location> printedLocs){
for(LinkedList<Location> tntTrace : locations.values()){
for(Location location : tntTrace){
if(location.getBlock().getType() == Material.AIR){
location.getBlock().setType(Material.RED_STAINED_GLASS);
printedLocs.add(location);
}
}
}
locations.clear();
return printedLocs.size();
}
static boolean airOrBrick(Block block){
return block.getType() == Material.RED_STAINED_GLASS;
}
}

Datei anzeigen

@ -1,6 +1,9 @@
package de.steamwar.bausystem;
import de.steamwar.bausystem.commands.*;
import de.steamwar.bausystem.tracer.TNTListener;
import de.steamwar.bausystem.tracer.TNTTracer15;
import de.steamwar.bausystem.tracer.trace.ShowManager;
import de.steamwar.bausystem.world.*;
import de.steamwar.core.CommandRemover;
import de.steamwar.core.Core;
@ -94,6 +97,9 @@ public class BauSystem extends JavaPlugin implements Listener {
Bukkit.getPluginManager().registerEvents(new TNTListener(), this);
Bukkit.getPluginManager().registerEvents(new BauScoreboard(), this);
new AFKStopper();
if (Core.getVersion() == 15) {
TNTTracer15.initTNTTracer_15();
}
autoShutdown = Bukkit.getScheduler().runTaskLater(this, Bukkit::shutdown, 1200);
}
@ -257,14 +263,20 @@ public class BauSystem extends JavaPlugin implements Listener {
attachment.setPermission("worldedit.extinguish", true);
attachment.setPermission("worldedit.calc", true);
attachment.setPermission("worldedit.fill", true);*/
if (Core.getVersion() == 15) ShowManager.add(p);
}
@EventHandler
public void onLeave(PlayerQuitEvent e) {
Player player = e.getPlayer();
SWScoreboard.removeScoreboard(player);
if(Bukkit.getOnlinePlayers().isEmpty() || (Bukkit.getOnlinePlayers().size() == 1 && Bukkit.getOnlinePlayers().contains(player)))
if (Bukkit.getOnlinePlayers().isEmpty() || (Bukkit.getOnlinePlayers().size() == 1 && Bukkit.getOnlinePlayers().contains(player))) {
Bukkit.shutdown();
return;
}
if (Core.getVersion() == 15) ShowManager.remove(e.getPlayer());
}
@EventHandler

Datei anzeigen

@ -2,8 +2,11 @@ package de.steamwar.bausystem.commands;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.TNTTracer;
import de.steamwar.bausystem.tracer.TNTTracer12;
import de.steamwar.bausystem.tracer.TNTTracer15;
import de.steamwar.bausystem.tracer.trace.ShowManager;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.core.Core;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -11,44 +14,115 @@ import org.bukkit.entity.Player;
public class CommandTrace implements CommandExecutor {
private void help(Player player){
private void help12(Player player) {
player.sendMessage("§8/§etrace start §8- §7Startet die Aufnahme aller TNT-Positionen");
player.sendMessage("§8/§etrace show §8- §7Zeigt alle TNT-Positionen");
player.sendMessage("§8/§etrace stop §8- §7Stoppt den TNT-Tracer");
}
private void help15(Player player) {
player.sendMessage("§8/§etrace start §8- §7Startet die Aufnahme aller TNT-Positionen");
player.sendMessage("§8/§etrace stop §8- §7Stoppt den TNT-Tracer");
player.sendMessage("§8/§etrace auto §8- §7Startet und stoppt die Aufnahme automatisch");
player.sendMessage("§8/§etrace show §8- §7Zeigt alle TNT-Positionen");
player.sendMessage("§8/§etrace hide §8- §7Versteckt alle TNT-Positionen");
player.sendMessage("§8/§etrace clear §8- §7Löscht alle TNT-Positionen");
player.sendMessage("§8/§etrace gui §8- §7Zeigt die Trace Oberfläche an");
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(!(sender instanceof Player))
return false;
Player player = (Player) sender;
if(args.length == 0){
help(player);
return false;
switch (Core.getVersion()) {
case 15:
return tracer15(player, args);
default:
return tracer12(player, args);
}
}
private boolean permissionCheck(Player player) {
if(Welt.noPermission(player, Permission.world)){
player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den TNT-Tracer nutzen");
return false;
}
return true;
}
private boolean tracer12(Player player, String[] args) {
if(args.length == 0){
help12(player);
return false;
}
if (!permissionCheck(player)) return false;
switch(args[0].toLowerCase()){
case "start":
TNTTracer.start();
TNTTracer12.start();
player.sendMessage(BauSystem.PREFIX + "§aAufnahme gestartet");
break;
case "show":
int blocks = TNTTracer.show();
int blocks = TNTTracer12.show();
player.sendMessage(BauSystem.PREFIX + "§a" + blocks + " TNT-Positionen angezeigt");
break;
case "stop":
TNTTracer.stop();
TNTTracer12.stop();
player.sendMessage(BauSystem.PREFIX + "§cTNT-Tracer gestoppt");
break;
default:
help(player);
help12(player);
}
return false;
}
private boolean tracer15(Player player, String[] args) {
if(args.length == 0){
help15(player);
return false;
}
if (!permissionCheck(player)) return false;
switch(args[0].toLowerCase()){
case "start":
TNTTracer15.start();
player.sendMessage(BauSystem.PREFIX + "§aAufnahme gestartet");
break;
case "stop":
TNTTracer15.stop();
player.sendMessage(BauSystem.PREFIX + "§cTNT-Tracer gestoppt");
break;
case "auto":
boolean mode = TNTTracer15.toggleAuto();
if (mode) {
player.sendMessage(BauSystem.PREFIX + "§aAutomatischer TNT-Tracer aktiviert");
} else {
player.sendMessage(BauSystem.PREFIX + "§cAutomatischer TNT-Tracer deaktiviert");
}
break;
case "show":
ShowManager.get(player).show();
player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen angezeigt");
break;
case "hide":
ShowManager.get(player).hide();
player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen versteckt");
break;
case "clear":
TNTTracer15.clear();
player.sendMessage(BauSystem.PREFIX + "§cAlle TNT-Positionen gelöscht");
break;
case "gui":
player.sendMessage(BauSystem.PREFIX + "§cNoch in Bau");
break;
default:
help15(player);
}
return false;
}
}

Datei anzeigen

@ -0,0 +1,34 @@
package de.steamwar.bausystem.tracer;
import de.steamwar.core.Core;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
public class TNTListener implements Listener {
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
switch (Core.getVersion()) {
case 15:
onEntityExplode15(event);
break;
default:
onEntityExplode12(event);
}
}
private void onEntityExplode12(EntityExplodeEvent event) {
if(TNTTracer12.getStatus() != TNTTracer12.Status.RECORD) return;
if(!(event.getEntity() instanceof TNTPrimed)) return;
TNTPrimed entity = (TNTPrimed) event.getEntity();
TNTTracer12.remove(entity);
}
private void onEntityExplode15(EntityExplodeEvent event) {
if (TNTTracer15.getStatus() != TNTTracer15.Status.RECORD_AUTO) return;
if(!(event.getEntity() instanceof TNTPrimed)) return;
TNTTracer15.start();
}
}

Datei anzeigen

@ -1,4 +1,4 @@
package de.steamwar.bausystem.world;
package de.steamwar.bausystem.tracer;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.core.Core;
@ -12,7 +12,7 @@ import org.bukkit.scheduler.BukkitTask;
import java.util.*;
public class TNTTracer {
public class TNTTracer12 {
private static BukkitTask task;
private static final Set<Location> printedLocs = new HashSet<>();
@ -26,13 +26,7 @@ public class TNTTracer {
private static Status status = Status.IDLE;
static void remove(TNTPrimed tnt){
switch (Core.getVersion()){
case 15:
TNTTracer_15.remove(locations, tnt);
break;
default:
TNTTracer_12.remove(locations, tnt);
}
TNTTracer_12.remove(locations, tnt);
}
public static void start(){
@ -42,7 +36,7 @@ public class TNTTracer {
locations.clear();
status = Status.RECORD;
task = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), TNTTracer::run, 1, 1);
task = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), TNTTracer12::run, 1, 1);
}
public static int show(){
@ -51,12 +45,7 @@ public class TNTTracer {
stop();
status = Status.SHOW;
switch (Core.getVersion()){
case 15:
return TNTTracer_15.show(locations, printedLocs);
default:
return TNTTracer_12.show(locations, printedLocs);
}
return TNTTracer_12.show(locations, printedLocs);
}
public static void stop(){
@ -73,18 +62,15 @@ public class TNTTracer {
}
private static boolean airOrBrick(Block block){
switch(Core.getVersion()){
case 15:
return TNTTracer_15.airOrBrick(block);
default:
return TNTTracer_12.airOrBrick(block);
}
return TNTTracer_12.airOrBrick(block);
}
private static void run() {
for(TNTPrimed tnt : world.getEntitiesByClass(TNTPrimed.class)){
locations.computeIfAbsent(tnt, k -> new LinkedList<>()).add(tnt.getLocation());
}
world.getEntities()
.stream()
.filter(e -> e instanceof TNTPrimed)
.map(e -> (TNTPrimed)e)
.forEach(tnt -> locations.computeIfAbsent(tnt, k -> new LinkedList<>()).add(tnt.getLocation()));
}
enum Status{

Datei anzeigen

@ -0,0 +1,136 @@
package de.steamwar.bausystem.tracer;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.tracer.trace.ShowManager;
import de.steamwar.bausystem.tracer.trace.ShowStatus;
import de.steamwar.bausystem.tracer.trace.TNTFrame;
import de.steamwar.bausystem.tracer.trace.TNTTrace;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.scheduler.BukkitTask;
import java.util.Iterator;
import java.util.LinkedList;
public class TNTTracer15 {
private static BukkitTask task;
private static long current = System.currentTimeMillis();
private static final World world = Bukkit.getWorlds().get(0);
private static LinkedList<TNTFrame> tntFrames = new LinkedList<>();
public static TNTTracer15.Status getStatus() {
return status;
}
private static TNTTracer15.Status status = Status.IDLE;
private static final double[] noValue = new double[0];
public static void initTNTTracer_15() {
TNTTracer_15.init(BauSystem.getPlugin(), p -> {
ShowStatus showStatus = ShowManager.get(p);
if (showStatus.getStatus() == ShowStatus.Status.NONE) return noValue;
LinkedList<Integer> selection = showStatus.getSelection();
Iterator<TNTFrame> frameIterator = tntFrames.iterator();
LinkedList<TNTTrace> traces = new LinkedList<>();
while (frameIterator.hasNext()) {
traces.addAll(frameIterator.next().getTraces(selection));
}
int size = traces.stream().mapToInt(TNTTrace::length).sum();
double[] doubles = new double[size * 3];
int index = 0;
Iterator<TNTTrace> traceIterator = traces.iterator();
while (traceIterator.hasNext()) {
double[] positions = traceIterator.next().getPositions();
for (int i = 0; i < positions.length; i++) {
if (index >= doubles.length) break;
doubles[index] = positions[i];
index++;
}
if (index >= doubles.length) break;
}
return doubles;
});
}
public static void clear() {
if (status == Status.RECORD || status == Status.RECORD_AUTO) {
stop();
}
tntFrames.clear();
ShowManager.removeAllTraces();
}
public static void start() {
if (status == Status.IDLE || status == Status.IDLE_AUTO) {
if (status == Status.IDLE) {
status = Status.RECORD;
} else {
status = Status.RECORD_AUTO;
}
task = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), TNTTracer15::run, 1, 1);
tntFrames.addLast(new TNTFrame());
}
if (status == Status.RECORD_AUTO) {
current = System.currentTimeMillis();
}
}
public static void stop() {
if (status == Status.IDLE || status == Status.IDLE_AUTO) {
return;
}
if (status == Status.RECORD) {
status = Status.IDLE;
}
if (status == Status.RECORD_AUTO) {
status = Status.IDLE_AUTO;
}
if (task != null) task.cancel();
tntFrames.getLast().finish();
}
public static boolean toggleAuto() {
if (status == Status.IDLE) {
status = Status.IDLE_AUTO;
return true;
}
if (status == Status.RECORD) {
status = Status.RECORD_AUTO;
current = System.currentTimeMillis();
return true;
}
if (status == Status.IDLE_AUTO) {
status = Status.IDLE;
return false;
}
if (status == Status.RECORD_AUTO) {
status = Status.RECORD;
return false;
}
return false;
}
private static void run() {
if (status == Status.RECORD_AUTO && System.currentTimeMillis() - current > 4500) {
stop();
}
tntFrames.getLast().add(world.getEntities()
.stream()
.filter(e -> e instanceof TNTPrimed)
.map(e -> (TNTPrimed)e)
);
}
public enum Status{
RECORD,
RECORD_AUTO,
IDLE,
IDLE_AUTO
}
}

Datei anzeigen

@ -0,0 +1,36 @@
package de.steamwar.bausystem.tracer.trace;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
public class ShowManager {
private static Map<Player, ShowStatus> showMap = new HashMap<>();
public static void add(Player player) {
showMap.put(player, new ShowStatus());
}
public static void remove(Player player) {
showMap.remove(player);
}
public static ShowStatus get(Player player) {
return showMap.get(player);
}
public static void removeTrace(int traceID) {
for (Map.Entry<Player, ShowStatus> entry : showMap.entrySet()) {
entry.getValue().removeSelection(traceID);
}
}
public static void removeAllTraces() {
for (Map.Entry<Player, ShowStatus> entry : showMap.entrySet()) {
entry.getValue().clear();
}
}
}

Datei anzeigen

@ -0,0 +1,68 @@
package de.steamwar.bausystem.tracer.trace;
import java.util.LinkedList;
public class ShowStatus {
private Status status = Status.NONE;
private LinkedList<Integer> selected = new LinkedList<>();
public enum Status {
NONE,
ALL,
SELECTIVE
}
public boolean toggleShow() {
if (status == Status.NONE) {
status = Status.ALL;
return true;
} else {
selected.clear();
status = Status.NONE;
return false;
}
}
public void show() {
status = Status.ALL;
}
public void hide() {
selected.clear();
status = Status.NONE;
}
public void addSelection(int uniqueID) {
if (status == Status.ALL) return;
if (status == Status.NONE) {
status = Status.SELECTIVE;
}
selected.add(uniqueID);
}
public void removeSelection(int uniqueID) {
if (status == Status.NONE) return;
if (status == Status.ALL) {
status = Status.SELECTIVE;
}
selected.remove(uniqueID);
if (selected.isEmpty()) {
status = Status.NONE;
}
}
public void clear() {
if (status == Status.NONE) return;
selected.clear();
}
public Status getStatus() {
return status;
}
public LinkedList<Integer> getSelection() {
return selected;
}
}

Datei anzeigen

@ -0,0 +1,59 @@
package de.steamwar.bausystem.tracer.trace;
import org.bukkit.entity.TNTPrimed;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Stream;
public class TNTFrame {
private static int uniqueIdentifier = 0;
private long time = System.currentTimeMillis();
private Map<Integer, TNTTrace> positions = new HashMap<>();
private Map<TNTPrimed, TNTTrace> aliveTNT = new HashMap<>();
private final TNTFrame instance = this;
public void add(Stream<TNTPrimed> tntPrimedStream) {
if (aliveTNT == null) return;
tntPrimedStream.forEach(tnt -> aliveTNT.computeIfAbsent(tnt, k -> {
synchronized (instance) {
TNTTrace trace = new TNTTrace(uniqueIdentifier, this, tnt);
positions.put(uniqueIdentifier, trace);
uniqueIdentifier++;
return trace;
}
}).addLocation(tnt.getLocation()));
}
void delete(TNTPrimed tntPrimed) {
if (aliveTNT == null) return;
aliveTNT.remove(tntPrimed);
}
public LinkedList<TNTTrace> getTraces(LinkedList<Integer> selection) {
LinkedList<TNTTrace> traces = new LinkedList<>();
Iterator<Integer> iterator = selection.iterator();
while (iterator.hasNext()) {
TNTTrace trace = positions.get(iterator.next());
if (trace != null) {
traces.add(trace);
}
}
return traces;
}
public void finish() {
aliveTNT.clear();
aliveTNT = null;
}
public long getTime() {
return time;
}
}

Datei anzeigen

@ -0,0 +1,66 @@
package de.steamwar.bausystem.tracer.trace;
import org.bukkit.Location;
import org.bukkit.entity.TNTPrimed;
public class TNTTrace {
private long startTime = System.currentTimeMillis();
private long lastUpdate = System.currentTimeMillis();
private int uniqueID;
private int index = 0;
private double[] positions = new double[240];
private TNTFrame frame;
private TNTPrimed tntPrimed;
public TNTTrace(int uniqueID, TNTFrame frame, TNTPrimed tntPrimed) {
this.uniqueID = uniqueID;
this.frame = frame;
this.tntPrimed = tntPrimed;
}
public void addLocation(Location location) {
addLocation(location.getX(), location.getY(), location.getZ());
}
public void addLocation(double x, double y, double z) {
positions[index + 0] = x;
positions[index + 1] = y;
positions[index + 2] = z;
index += 3;
if (index >= positions.length) {
frame.delete(tntPrimed);
tntPrimed = null;
}
lastUpdate = System.currentTimeMillis();
}
public double[] getPositions() {
double[] doubles = new double[index];
for (int i = 0; i < doubles.length; i++) {
doubles[i] = positions[i];
}
return doubles;
}
public int size() {
return index / 3;
}
public int length() {
return size();
}
public long getStartTime() {
return startTime;
}
public long getLastUpdate() {
return lastUpdate;
}
}

Datei anzeigen

@ -1,20 +0,0 @@
package de.steamwar.bausystem.world;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
public class TNTListener implements Listener {
@EventHandler
public void onEntityExplode(EntityExplodeEvent event){
if(TNTTracer.getStatus() != TNTTracer.Status.RECORD)
return;
if(!(event.getEntity() instanceof TNTPrimed))
return;
TNTPrimed entity = (TNTPrimed) event.getEntity();
TNTTracer.remove(entity);
}
}