Add SimulatorStabGenerator

This commit is contained in:
2025-04-19 17:58:40 +02:00
parent 76bbfd0381
commit 17704487c9
10 changed files with 436 additions and 17 deletions
@@ -32,13 +32,22 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
public class Depth {
private static final Map<Region, List<BiConsumer<Vector, Integer>>> callbacks = new HashMap<>();
public static void addCallback(Region region, BiConsumer<Vector, Integer> callback) {
callbacks.computeIfAbsent(region, k -> new ArrayList<>()).add(callback);
}
public static void removeCallback(Region region, BiConsumer<Vector, Integer> callback) {
callbacks.computeIfAbsent(region, k -> new ArrayList<>()).remove(callback);
}
private Region region;
private Vector minVector = null;
private Vector maxVector = null;
@@ -70,6 +79,10 @@ public class Depth {
player.spigot().sendMessage(getMessage(player, dimensions.getBlockX() + 1, dimensions.getBlockY() + 1, dimensions.getBlockZ() + 1, tntCount));
}
});
new ArrayList<>(callbacks.getOrDefault(region, Collections.emptyList())).forEach(consumer -> {
consumer.accept(dimensions, tntCount);
});
}
private void internalUpdate(Block block) {
@@ -76,7 +76,7 @@ public class SimulatorCommand extends SWCommand {
@Register(value = "start", description = "SIMULATOR_START_HELP")
public void start(@Validator Player p, @ErrorMessage("SIMULATOR_NOT_EXISTS") Simulator simulator) {
SimulatorExecutor.run(simulator);
SimulatorExecutor.run(simulator, () -> {});
}
@Register(value = "rename", description = "SIMULATOR_RENAME_HELP")
@@ -168,9 +168,19 @@ public class SimulatorCursor implements Listener {
}
return;
}
Simulator simulator = SimulatorStorage.getSimulator(player);
SimulatorWatcher.show(simulator, player);
Simulator simulator = SimulatorStorage.getSimulator(player);
if (simulator != null && simulator.getStabGenerator() != null) {
removeCursor(player);
SimulatorWatcher.show(null, player);
SWUtils.sendToActionbar(player, "§cGenerating Stab");
synchronized (calculating) {
calculating.remove(player);
}
return;
}
SimulatorWatcher.show(simulator, player);
List<REntity> entities = SimulatorWatcher.getEntitiesOfSimulator(simulator);
RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), entities);
if (rayTraceResult == null) {
@@ -357,7 +367,7 @@ public class SimulatorCursor implements Listener {
if (simulator == null) {
return;
}
SimulatorExecutor.run(simulator);
SimulatorExecutor.run(simulator, () -> {});
return;
}
@@ -20,6 +20,7 @@
package de.steamwar.bausystem.features.simulator.data;
import de.steamwar.bausystem.features.simulator.execute.SimulatorAction;
import de.steamwar.bausystem.features.simulator.execute.SimulatorStabGenerator;
import de.steamwar.inventory.InvCallback;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
@@ -30,13 +31,13 @@ import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
@Getter
@Setter
@RequiredArgsConstructor
public final class Simulator {
private SimulatorStabGenerator stabGenerator = null;
private Material material = Material.BARREL;
private final String name;
private boolean autoTrace = false;
@@ -46,7 +46,7 @@ public class SimulatorExecutor implements Listener {
private static Map<Long, Map<Integer, List<SimulatorAction>>> tickStartActions = new HashMap<>();
private static Map<Long, List<SimulatorAction>> tickEndActions = new HashMap<>();
public static boolean run(Simulator simulator) {
public static boolean run(Simulator simulator, Runnable onEnd) {
if (currentlyRunning.contains(simulator)) return false;
currentlyRunning.add(simulator);
@@ -69,7 +69,7 @@ public class SimulatorExecutor implements Listener {
public void accept(World world) {
currentlyRunning.remove(simulator);
if (simulator.isAutoTrace()) {
if (simulator.isAutoTrace() && onEnd == null) {
simulator.getGroups()
.stream()
.map(SimulatorGroup::getElements)
@@ -82,10 +82,12 @@ public class SimulatorExecutor implements Listener {
TraceRecorder.instance.stopRecording(region);
});
}
onEnd.run();
}
});
if (simulator.isAutoTrace()) {
if (simulator.isAutoTrace() && onEnd == null) {
simulator.getGroups()
.stream()
.map(SimulatorGroup::getElements)
@@ -0,0 +1,365 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.execute;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.block.BlockTypes;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.cannon.depth.Depth;
import de.steamwar.bausystem.features.simulator.SimulatorWatcher;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.data.SimulatorGroup;
import de.steamwar.bausystem.features.simulator.data.SimulatorPhase;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTElement;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTPhase;
import de.steamwar.bausystem.features.tracer.TNTPoint;
import de.steamwar.bausystem.features.tracer.Trace;
import de.steamwar.bausystem.features.tracer.TraceManager;
import de.steamwar.bausystem.features.tracer.TraceRecorder;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.bausystem.utils.bossbar.BauSystemBossbar;
import de.steamwar.bausystem.utils.bossbar.BossBarService;
import lombok.AllArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.logging.Level;
public class SimulatorStabGenerator {
private static final int MAX_RECORDINGS = 10;
private final Region region;
private final Simulator simulator;
private final TNTElement tntElement;
private final List<TNTPhase> phases;
private final int depthLimit;
private Clipboard clipboard;
private boolean cancel = false;
public SimulatorStabGenerator(Region region, Simulator simulator, TNTElement tntElement, int depthLimit) {
this.region = region;
this.simulator = simulator;
this.tntElement = tntElement;
this.phases = tntElement.getPhases();
this.depthLimit = depthLimit;
setup();
}
private void setup() {
TNTPhase tntPhase = simulator.getGroups().stream()
.map(SimulatorGroup::getElements)
.flatMap(List::stream)
.filter(TNTElement.class::isInstance)
.map(TNTElement.class::cast)
.filter(tntElement -> this.tntElement != tntElement)
.map(tntElement -> tntElement.getPhases().stream().max(Comparator.comparingInt(TNTPhase::getTickOffset)))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(phase -> phase != phases.get(0))
.max(Comparator.comparingInt(TNTPhase::getTickOffset))
.orElse(null);
if (tntPhase == null) {
throw new SecurityException("");
}
TNTPhase phase = phases.get(0);
phases.clear();
phases.add(phase);
phase.setCount(1);
phase.setTickOffset(tntPhase.getTickOffset());
phase.setOrder(100);
TraceRecorder.instance.stopRecording(region);
if (TraceRecorder.instance.isAutoTraceEnabledInRegion(region)) {
TraceRecorder.instance.removeAutoTraceRegion(region);
}
clipboard = FlatteningWrapper.impl.copy(region.getMinPointTestblockExtension(), region.getMaxPointTestblockExtension(), region.getTestBlockPoint());
run();
}
private BlockVector3 toBlockVector3(Point point) {
return BlockVector3.at(point.getX(), point.getY(), point.getZ());
}
private Direction direction = null;
private void removeTestblock() {
try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) {
e.setBlocks((com.sk89q.worldedit.regions.Region) new CuboidRegion(toBlockVector3(region.getMinPointTestblockExtension()), toBlockVector3(region.getMaxPointTestblockExtension())), Objects.requireNonNull(BlockTypes.AIR).getDefaultState().toBaseBlock());
}
}
private void setTestblock() {
try {
PasteBuilder.ClipboardProvider clipboardProvider = new PasteBuilder.ClipboardProviderImpl(clipboard);
PasteBuilder pasteBuilder = new PasteBuilder(clipboardProvider)
.color(region.getPlain(Flag.COLOR, ColorMode.class).getColor());
region.reset(pasteBuilder, RegionType.TESTBLOCK, RegionExtensionType.EXTENSION);
} catch (SecurityException e) {
stop();
throw e;
}
}
private void run() {
if (cancel) return;
showBossbar(false);
Trace trace;
if (direction == null) {
removeTestblock();
trace = TraceRecorder.instance.startRecording(region);
} else {
setTestblock();
trace = null;
}
if (trace == null && currentDepth > 0) {
TNTPhase lastPhase = phases.getLast();
TNTPhase nextPhase = new TNTPhase();
nextPhase.setCount(1);
nextPhase.setTickOffset(lastPhase.getTickOffset());
nextPhase.setOrder(100);
nextPhase.setXJump(lastPhase.isXJump());
nextPhase.setYJump(lastPhase.isYJump());
nextPhase.setZJump(lastPhase.isZJump());
phases.add(nextPhase);
}
SimulatorExecutor.run(simulator, () -> {
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
if (trace != null) TraceRecorder.instance.stopRecording(region);
if (trace == null && currentDepth > 0) phases.removeLast();
next(trace);
}, 20);
});
}
@AllArgsConstructor
private enum Direction {
X(Vector::getBlockX),
Y(Vector::getBlockY),
Z(Vector::getBlockZ);
private final Function<Vector, Integer> extractVelocity;
}
private void calcDirection(Trace trace) {
long tickSinceStart = -1;
List<TNTPoint> points = null;
for (List<TNTPoint> current : trace.getHistories()) {
long ticks = current.get(0).getTicksSinceStart();
if (points == null || ticks > tickSinceStart) {
tickSinceStart = ticks;
points = current;
} else if (ticks == tickSinceStart && points.get(0).getTntId() < current.get(0).getTntId()) {
points = current;
}
}
if (points == null) {
stop();
return;
}
TNTPoint current = points.getLast();
Vector velocity = current.getVelocity();
if (velocity.getX() < 0) velocity.setX(-velocity.getX());
if (velocity.getY() < 0) velocity.setY(-velocity.getY());
if (velocity.getZ() < 0) velocity.setZ(-velocity.getZ());
if (velocity.getX() > velocity.getY() && velocity.getX() > velocity.getZ()) {
direction = Direction.X;
} else if (velocity.getY() > velocity.getX() && velocity.getY() > velocity.getZ()) {
direction = Direction.Y;
} else if (velocity.getZ() > velocity.getX() && velocity.getZ() > velocity.getY()) {
direction = Direction.Z;
} else {
stop();
return;
}
Bukkit.getLogger().log(Level.FINEST, "Direction: {}", direction);
TraceManager.instance.remove(trace);
phases.getFirst().setOrder(SimulatorPhase.ORDER_LIMIT);
phases.getFirst().setCount(10);
Depth.addCallback(region, callback);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), this::run, 20);
}
private int recordings = 0;
private List<Vector> depths = new ArrayList<>();
private int lastDepth = 0;
private int currentDepth = 0;
private void next(Trace trace) {
if (direction == null) {
calcDirection(trace);
return;
}
List<Integer> depths = new ArrayList<>();
for (Vector vector : this.depths) {
depths.add(direction.extractVelocity.apply(vector));
}
// System.out.println(depths);
int depth = depths.stream().max(Integer::compareTo).orElse(0);
currentDepth = depth;
int countWithoutLast = 0;
for (int i = 0; i < phases.size() - 1; i++) {
countWithoutLast += phases.get(i).getCount();
}
TNTPhase lastPhase = phases.getLast();
boolean moreTNTNeeded = depth - countWithoutLast >= lastPhase.getCount() - 5;
if (!depths.isEmpty() && moreTNTNeeded) {
Bukkit.getLogger().log(Level.FINEST, "Increasing tnt count by 10");
lastPhase.setCount(lastPhase.getCount() + 10);
recordings = 0;
depths.clear();
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), this::run, 20);
return;
}
if (recordings++ < MAX_RECORDINGS) {
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), this::run, 20);
return;
}
recordings = 0;
if (depths.isEmpty()) {
Bukkit.getLogger().log(Level.FINEST, "No dimension - Increasing tickOffset to: {}", phases.getFirst().getTickOffset() + 1);
phases.getFirst().setTickOffset(phases.getFirst().getTickOffset() + 1);
phases.getFirst().setOrder(0);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), this::run, 20);
return;
}
depths.clear();
Bukkit.getLogger().log(Level.FINEST, "No more TNT needed on phase adjusting - {} new depth; {} current count", new Object[]{depth - countWithoutLast, lastPhase.getCount()});
lastPhase.setCount(depth - countWithoutLast);
if (lastPhase.getCount() <= 0) {
Bukkit.getLogger().log(Level.FINEST, "Count was 0 or negative - removing last phase");
phases.removeLast();
}
if (depth > depthLimit) {
Bukkit.getLogger().log(Level.FINEST, "Depth is greater than {} - finished", depthLimit);
stop();
return;
}
if (depth <= lastDepth) {
Bukkit.getLogger().log(Level.FINEST, "Depth is equal to last depth recorded {} - finished", depth);
stop();
return;
}
lastDepth = depth;
Bukkit.getLogger().log(Level.FINEST, "Adding new phase in next tick");
TNTPhase nextPhase = new TNTPhase();
nextPhase.setCount(10);
nextPhase.setTickOffset(lastPhase.getTickOffset() + 1);
nextPhase.setXJump(lastPhase.isXJump());
nextPhase.setYJump(lastPhase.isYJump());
nextPhase.setZJump(lastPhase.isZJump());
phases.add(nextPhase);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), this::run, 20);
}
private void showBossbar(boolean finished) {
for (Player player : Bukkit.getOnlinePlayers()) {
BauSystemBossbar bossbar = BossBarService.instance.get(player, region, "simulator_stab_generator");
bossbar.setColor(BarColor.GREEN);
bossbar.setStyle(BarStyle.SEGMENTED_10);
bossbar.setProgress(Math.min(currentDepth / (double) depthLimit, 1.0));
StringBuilder st = new StringBuilder();
if (finished) {
st.append("§eFinished ").append(currentDepth).append("§8/§7").append(depthLimit);
} else if (direction == null) {
st.append("§eCalculating Stab Direction");
} else {
st.append("§7Direction§7 §e" + direction);
st.append(" §e").append(currentDepth).append("§8/§7").append(depthLimit);
if (recordings > 0) {
st.append(" §7Retries§8:§e ").append(recordings).append("§8/§7").append(MAX_RECORDINGS);
}
}
bossbar.setTitle(st.toString());
}
}
private void stop() {
simulator.setStabGenerator(null);
Depth.removeCallback(region, callback);
SimulatorWatcher.update(simulator);
showBossbar(true);
new Thread(() -> {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
for (Player player : Bukkit.getOnlinePlayers()) {
BossBarService.instance.remove(player, region, "simulator_stab_generator");
}
}
}).start();
}
private BiConsumer<Vector, Integer> callback = this::depth;
private void depth(Vector dimension, int tntCount) {
depths.add(dimension.clone().add(new Vector(1, 1, 1)));
}
public void cancel() {
cancel = true;
simulator.setStabGenerator(null);
Depth.removeCallback(region, callback);
for (Player player : Bukkit.getOnlinePlayers()) {
BossBarService.instance.remove(player, region, "simulator_stab_generator");
}
}
}
@@ -24,8 +24,11 @@ import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.data.SimulatorGroup;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTElement;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTPhase;
import de.steamwar.bausystem.features.simulator.execute.SimulatorStabGenerator;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorScrollGui;
import de.steamwar.bausystem.region.Region;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@@ -97,6 +100,14 @@ public class SimulatorTNTGui extends SimulatorScrollGui<TNTPhase> {
tnt.setDisabled(!tnt.isDisabled());
SimulatorWatcher.update(simulator);
}));
inventory.setItem(49, new SWItem(Material.CALIBRATED_SCULK_SENSOR, "§eCreate Stab", click -> {
new SimulatorAnvilGui<>(player, "Depth Limit", "", Integer::parseInt, depthLimit -> {
if (depthLimit <= 0) return false;
simulator.setStabGenerator(new SimulatorStabGenerator(Region.getRegion(player.getLocation()), simulator, tnt, depthLimit));
SimulatorWatcher.update(simulator);
return true;
}, null).open();
}));
inventory.setItem(50, new SWItem(Material.CHEST, parent.getElements().size() == 1 ? "§eMake Group" : "§eAdd another TNT to Group", clickType -> {
TNTElement tntElement = new TNTElement(tnt.getPosition().clone());
tntElement.add(new TNTPhase());
@@ -44,7 +44,7 @@ public class SimulatorAnvilGui<T extends Number> {
if (error.get()) {
anvilInv.open();
} else {
back.open();
if (back != null) back.open();
}
error.set(false);
}, 0);
@@ -22,7 +22,6 @@ package de.steamwar.bausystem.features.simulator.gui.base;
import de.steamwar.bausystem.features.simulator.SimulatorWatcher;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.core.Core;
import de.steamwar.core.TrickyTrialsWrapper;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import org.bukkit.Bukkit;
@@ -64,7 +63,11 @@ public abstract class SimulatorBaseGui {
if (Core.getVersion() > 19) {
player.getOpenInventory().setTitle(title());
}
populate();
if (simulator != null && simulator.getStabGenerator() != null) {
populateStabGenerator();
} else {
populate();
}
if (player.getOpenInventory().getTopInventory() == inv) {
inventory.open();
SimulatorWatcher.watch(player, simulator, this::open);
@@ -84,10 +87,21 @@ public abstract class SimulatorBaseGui {
});
SimulatorWatcher.watch(player, simulator, this::open);
populate();
if (simulator != null && simulator.getStabGenerator() != null) {
populateStabGenerator();
} else {
populate();
}
inventory.open();
}
private void populateStabGenerator() {
inventory.setItem(22, new SWItem(Material.BARRIER, "§cCancel Stab Generator", click -> {
simulator.getStabGenerator().cancel();
SimulatorWatcher.update(simulator);
}));
}
public boolean shouldOpen() {
return true;
}
@@ -102,11 +102,14 @@ public class TraceRecorder implements Listener {
*
* @param region region to be recorded
*/
public void startRecording(Region region) {
if (activeTraces.containsKey(region)) return;
public Trace startRecording(Region region) {
if (activeTraces.containsKey(region)) {
return activeTraces.get(region).getTrace();
}
TraceRecordingWrapper wrappedTrace = new TraceRecordingWrapper(region);
activeTraces.put(region, wrappedTrace);
return wrappedTrace.getTrace();
}
/**