Add SimulatorExecutor

Dieser Commit ist enthalten in:
yoyosource 2023-10-22 14:25:54 +02:00
Ursprung 834039c4d4
Commit 3506083416
11 geänderte Dateien mit 217 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -25,9 +25,11 @@ import de.steamwar.bausystem.features.simulator2.data.redstone.RedstoneElement;
import de.steamwar.bausystem.features.simulator2.data.redstone.RedstonePhase;
import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement;
import de.steamwar.bausystem.features.simulator2.data.tnt.TNTPhase;
import de.steamwar.bausystem.features.simulator2.execute.SimulatorExecutor;
import de.steamwar.bausystem.features.simulator2.gui.SimulatorGui;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import jdk.jfr.Registered;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
@ -39,18 +41,23 @@ public class SimulatorTestCommand extends SWCommand {
public SimulatorTestCommand() {
super("simtest");
SimulatorGroup group1 = new SimulatorGroup().add(new TNTElement(new Vector(0, 0, 0)).add(new TNTPhase())).add(new TNTElement(new Vector(0, 0, 0)).add(new TNTPhase()));
SimulatorGroup group1 = new SimulatorGroup().add(new TNTElement(new Vector(-477.5, 46, 311.5)).add(new TNTPhase()));
SIMULATOR.getElements().add(group1);
SimulatorGroup group2 = new SimulatorGroup().add(new TNTElement(new Vector(0, 0, 0)).add(new TNTPhase()));
SimulatorGroup group2 = new SimulatorGroup().add(new TNTElement(new Vector(-477.5, 47, 312.5)).add(new TNTPhase()));
SIMULATOR.getElements().add(group2);
SimulatorGroup group3 = new SimulatorGroup().add(new RedstoneElement(new Vector(0, 0, 0)).add(new RedstonePhase()));
SimulatorGroup group3 = new SimulatorGroup().add(new RedstoneElement(new Vector(-484, 47, 307)).add(new RedstonePhase()));
SIMULATOR.getElements().add(group3);
}
@Register
public void genericCommand(Player player, String... args) {
@Register("open")
public void openCommand(Player player) {
new SimulatorGui(player, SIMULATOR).open();
}
@Register("run")
public void runCommand(Player player) {
SimulatorExecutor.run(SIMULATOR);
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bausystem.features.simulator2.data;
import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction;
import de.steamwar.inventory.InvCallback;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
@ -29,6 +30,7 @@ import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@ -48,4 +50,10 @@ public class Simulator {
public SWItem toItem(Player player, InvCallback invCallback) {
return new SWItem(material, "§e" + name, invCallback);
}
public void toSimulatorActions(Map<Integer, List<SimulatorAction>> actions) {
elements.forEach(simulatorGroup -> {
simulatorGroup.toSimulatorActions(actions);
});
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bausystem.features.simulator2.data;
import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction;
import de.steamwar.inventory.InvCallback;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
@ -30,6 +31,7 @@ import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@ -88,4 +90,11 @@ public abstract class SimulatorElement<T extends SimulatorPhase> {
}
return new SWItem(material, "§e" + getName(), lore, disabled, invCallback);
}
public void toSimulatorActions(Map<Integer, List<SimulatorAction>> actions) {
if (disabled) return;
phases.forEach(phase -> {
phase.toSimulatorActions(actions, position.clone());
});
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bausystem.features.simulator2.data;
import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction;
import de.steamwar.inventory.InvCallback;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
@ -29,6 +30,7 @@ import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@ -79,4 +81,11 @@ public class SimulatorGroup {
return new SWItem(material, "§eGroup", lore, disabled, groupCallback);
}
}
public void toSimulatorActions(Map<Integer, List<SimulatorAction>> actions) {
if (disabled) return;
elements.forEach(simulatorElement -> {
simulatorElement.toSimulatorActions(actions);
});
}
}

Datei anzeigen

@ -19,8 +19,13 @@
package de.steamwar.bausystem.features.simulator2.data;
import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.util.Vector;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@ -30,4 +35,6 @@ public abstract class SimulatorPhase {
protected int tickOffset = 0;
protected int lifetime = 80;
protected int order = 1;
public abstract void toSimulatorActions(Map<Integer, List<SimulatorAction>> actions, Vector position);
}

Datei anzeigen

@ -20,8 +20,18 @@
package de.steamwar.bausystem.features.simulator2.data.redstone;
import de.steamwar.bausystem.features.simulator2.data.SimulatorPhase;
import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction;
import lombok.NoArgsConstructor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@NoArgsConstructor
public class RedstonePhase extends SimulatorPhase {
@ -33,4 +43,26 @@ public class RedstonePhase extends SimulatorPhase {
{
this.lifetime = 1;
}
@Override
public void toSimulatorActions(Map<Integer, List<SimulatorAction>> actions, Vector position) {
AtomicReference<BlockState> previousBlockState = new AtomicReference<>();
actions.computeIfAbsent(tickOffset, __ -> new ArrayList<>())
.add(new SimulatorAction(0, 1) {
@Override
public void accept(World world) {
Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
previousBlockState.set(block.getState());
block.setType(Material.REDSTONE_BLOCK);
}
});
actions.computeIfAbsent(tickOffset + lifetime, __ -> new ArrayList<>())
.add(new SimulatorAction(0, 1) {
@Override
public void accept(World world) {
previousBlockState.get().update(true, true);
}
});
}
}

Datei anzeigen

@ -20,9 +20,17 @@
package de.steamwar.bausystem.features.simulator2.data.tnt;
import de.steamwar.bausystem.features.simulator2.data.SimulatorPhase;
import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.bukkit.World;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@ -46,4 +54,18 @@ public class TNTPhase extends SimulatorPhase {
yJump = jump;
zJump = jump;
}
@Override
public void toSimulatorActions(Map<Integer, List<SimulatorAction>> actions, Vector position) {
actions.computeIfAbsent(tickOffset, __ -> new ArrayList<>()).add(new SimulatorAction(order, count) {
@Override
public void accept(World world) {
TNTPrimed tnt = world.spawn(position.toLocation(world), TNTPrimed.class);
if (!xJump) tnt.setVelocity(tnt.getVelocity().setX(0));
if (!yJump) tnt.setVelocity(tnt.getVelocity().setY(0));
if (!zJump) tnt.setVelocity(tnt.getVelocity().setZ(0));
tnt.setFuseTicks(lifetime);
}
});
}
}

Datei anzeigen

@ -0,0 +1,36 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.simulator2.execute;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.World;
import java.util.function.Consumer;
@Getter
@AllArgsConstructor
public abstract class SimulatorAction implements Consumer<World> {
private int order;
@Setter
private int count;
}

Datei anzeigen

@ -0,0 +1,81 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.simulator2.execute;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.simulator2.data.Simulator;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@UtilityClass
public class SimulatorExecutor {
private static final World WORLD = Bukkit.getWorlds().get(0);
private static Set<Simulator> currentlyRunning = new HashSet<>();
public boolean run(Simulator simulator) {
if (currentlyRunning.contains(simulator)) return false;
Map<Integer, List<SimulatorAction>> map = new HashMap<>();
simulator.toSimulatorActions(map);
Map<Integer, Map<Integer, List<SimulatorAction>>> actions = new HashMap<>();
map.forEach((integer, simulatorActions) -> {
simulatorActions.forEach(simulatorAction -> {
actions.computeIfAbsent(integer, __ -> new HashMap<>()).computeIfAbsent(simulatorAction.getOrder(), __ -> new ArrayList<>()).add(simulatorAction);
});
});
AtomicInteger tick = new AtomicInteger();
BauSystem.runTaskTimer(BauSystem.getInstance(), bukkitTask -> {
if (actions.isEmpty()) {
bukkitTask.cancel();
currentlyRunning.remove(simulator);
return;
}
int currentTick = tick.getAndIncrement();
Map<Integer, List<SimulatorAction>> actionsToRun = actions.remove(currentTick);
if (actionsToRun == null) return;
List<Integer> keys = new ArrayList<>(actionsToRun.keySet());
keys.sort(null);
for (int actionKey : keys) {
List<SimulatorAction> keyedActions = actionsToRun.get(actionKey);
while (!keyedActions.isEmpty()) {
Collections.shuffle(keyedActions);
for (int i = keyedActions.size() - 1 ; i >= 0; i--) {
SimulatorAction action = keyedActions.get(i);
action.accept(WORLD);
action.setCount(action.getCount() - 1);
if (action.getCount() == 0) {
keyedActions.remove(i);
}
}
}
}
}, 0, 1);
return true;
}
}

Datei anzeigen

@ -136,7 +136,7 @@ public class SimulatorTNTPhaseSettingsGui extends SimulatorBaseGui {
orderItem.getItemStack().setAmount(Math.max(1, Math.min(order, 30)));
inventory.setItem(22, orderItem);
inventory.setItem(31, SWItem.getDye(order > -30 ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
inventory.setItem(31, SWItem.getDye(order > -SimulatorPhase.ORDER_LIMIT ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> {
tnt.setOrder(Math.max(-30, order - (clickType.isShiftClick() ? 5 : 1)));
SimulatorWatcher.update(simulator);
});