SimRework2.0 #216
@ -60,4 +60,14 @@ public class SimulatorTestCommand extends SWCommand {
|
||||
public void runCommand(Player player) {
|
||||
SimulatorExecutor.run(SIMULATOR);
|
||||
}
|
||||
|
||||
@Register("show")
|
||||
public void showCommand(Player player) {
|
||||
SimulatorWatcher.show(SIMULATOR, player);
|
||||
}
|
||||
|
||||
@Register("hide")
|
||||
public void hideCommand(Player player) {
|
||||
SimulatorWatcher.hide(SIMULATOR, player);
|
||||
}
|
||||
}
|
||||
|
@ -20,21 +20,32 @@
|
||||
package de.steamwar.bausystem.features.simulator2;
|
||||
|
||||
import de.steamwar.bausystem.features.simulator2.data.Simulator;
|
||||
import de.steamwar.bausystem.features.simulator2.data.SimulatorElement;
|
||||
import de.steamwar.bausystem.features.simulator2.data.SimulatorGroup;
|
||||
import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement;
|
||||
import de.steamwar.bausystem.shared.Pair;
|
||||
import de.steamwar.entity.REntity;
|
||||
import de.steamwar.entity.REntityServer;
|
||||
import de.steamwar.entity.RFallingBlockEntity;
|
||||
import de.steamwar.linkage.Linked;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@UtilityClass
|
||||
public class SimulatorWatcher {
|
||||
|
||||
private final World WORLD = Bukkit.getWorlds().get(0);
|
||||
private Map<Simulator, REntityServer> entityServers = new HashMap<>();
|
||||
private Map<Player, Pair<Simulator, Runnable>> watchers = new HashMap<>();
|
||||
|
||||
public void watch(Player player, Simulator simulator, Runnable watcher) {
|
||||
@ -46,6 +57,10 @@ public class SimulatorWatcher {
|
||||
}
|
||||
|
||||
public void update(Simulator simulator) {
|
||||
REntityServer rEntityServer = entityServers.get(simulator);
|
||||
rEntityServer.getEntities().forEach(REntity::die);
|
||||
createSim(rEntityServer, simulator);
|
||||
|
||||
new ArrayList<>(watchers.values()).forEach(simulatorRunnablePair -> {
|
||||
if (simulatorRunnablePair.getKey() == simulator) {
|
||||
simulatorRunnablePair.getValue().run();
|
||||
@ -59,6 +74,39 @@ public class SimulatorWatcher {
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
watchers.remove(event.getPlayer());
|
||||
hide(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
private REntityServer createSim(REntityServer server, Simulator simulator) {
|
||||
Map<Vector, Set<Class<?>>> positionCache = new HashMap<>();
|
||||
simulator.getElements().stream().map(SimulatorGroup::getElements).flatMap(List::stream).forEach(simulatorElement -> {
|
||||
boolean wasNotPresent = positionCache.computeIfAbsent(simulatorElement.getPosition(), __ -> new HashSet<>())
|
||||
.add(simulatorElement.getClass());
|
||||
if (!wasNotPresent) return;
|
||||
Material material = simulatorElement.getWorldMaterial();
|
||||
Location location = simulatorElement.getWorldPos().toLocation(WORLD);
|
||||
RFallingBlockEntity rFallingBlockEntity = new RFallingBlockEntity(server, location, material);
|
||||
rFallingBlockEntity.setNoGravity(true);
|
||||
});
|
||||
return server;
|
||||
}
|
||||
|
||||
public void show(Simulator sim, Player player) {
|
||||
entityServers.computeIfAbsent(sim, __ -> createSim(new REntityServer(), sim)).addPlayer(player);
|
||||
}
|
||||
|
||||
public void hide(Player player) {
|
||||
entityServers.replaceAll((simulator, rEntityServer) -> {
|
||||
rEntityServer.removePlayer(player);
|
||||
return rEntityServer.getPlayers().isEmpty() ? null : rEntityServer;
|
||||
});
|
||||
}
|
||||
|
||||
public void hide(Simulator sim, Player player) {
|
||||
entityServers.computeIfPresent(sim, (simulator, rEntityServer) -> {
|
||||
rEntityServer.removePlayer(player);
|
||||
return rEntityServer.getPlayers().isEmpty() ? null : rEntityServer;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -76,6 +76,12 @@ public abstract class SimulatorElement<T extends SimulatorPhase> {
|
||||
position.setZ(position.getZ() + z);
|
||||
}
|
||||
|
||||
public abstract Material getWorldMaterial();
|
||||
|
||||
public Vector getWorldPos() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public SWItem toItem(Player player, InvCallback invCallback) {
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add("§7Phase count§8:§e " + phases.size());
|
||||
|
@ -33,4 +33,14 @@ public class RedstoneElement extends SimulatorElement<RedstonePhase> {
|
||||
public String getName() {
|
||||
return "Redstone";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Material getWorldMaterial() {
|
||||
return Material.REDSTONE_BLOCK;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector getWorldPos() {
|
||||
return position.clone().add(new Vector(0.5, 0, 0.5));
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,10 @@
|
||||
package de.steamwar.bausystem.features.simulator2.data.tnt;
|
||||
|
||||
import de.steamwar.bausystem.features.simulator2.data.SimulatorElement;
|
||||
import de.steamwar.inventory.InvCallback;
|
||||
import de.steamwar.inventory.SWItem;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class TNTElement extends SimulatorElement<TNTPhase> {
|
||||
@ -61,4 +64,17 @@ public class TNTElement extends SimulatorElement<TNTPhase> {
|
||||
position.setZ(position.getBlockZ() + offset.getZ());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SWItem toItem(Player player, InvCallback invCallback) {
|
||||
long sum = phases.stream().mapToInt(TNTPhase::getCount).sum();
|
||||
SWItem swItem = super.toItem(player, invCallback);
|
||||
swItem.getItemStack().setAmount((int) Math.min(64, Math.max(1, sum)));
|
||||
return swItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Material getWorldMaterial() {
|
||||
return Material.TNT;
|
||||
}
|
||||
}
|
||||
|
@ -50,6 +50,7 @@ public class SimulatorGroupGui extends SimulatorPageGui<SimulatorElement<?>> {
|
||||
|
||||
@Override
|
||||
public void headerAndFooter() {
|
||||
simulatorGroup.getElements().removeIf(element -> element.getPhases().isEmpty());
|
||||
if (simulatorGroup.getElements().size() < 2) {
|
||||
back.open();
|
||||
return;
|
||||
@ -63,7 +64,8 @@ public class SimulatorGroupGui extends SimulatorPageGui<SimulatorElement<?>> {
|
||||
|
||||
inventory.setItem(4, simulatorGroup.toItem(player, clickType -> {
|
||||
new SimulatorMaterialGui(player, simulator, simulatorGroup::getMaterial, simulatorGroup::setMaterial, this).open();
|
||||
}, clickType -> {}));
|
||||
}, clickType -> {
|
||||
}));
|
||||
|
||||
inventory.setItem(48, new SWItem(Material.REPEATER, "§eSettings", clickType -> {
|
||||
new SimulatorGroupSettingsGui(player, simulator, simulatorGroup, this).open();
|
||||
|
@ -43,7 +43,7 @@ public class SimulatorGui extends SimulatorPageGui<SimulatorGroup> {
|
||||
|
||||
@Override
|
||||
public void headerAndFooter() {
|
||||
simulator.getElements().removeIf(element -> element.getElements().isEmpty());
|
||||
simulator.getElements().removeIf(element -> element.getElements().isEmpty() || element.getElements().stream().allMatch(simulatorElement -> simulatorElement.getPhases().isEmpty()));
|
||||
|
||||
inventory.setItem(4, simulator.toItem(player, clickType -> {
|
||||
new SimulatorMaterialGui(player, simulator, simulator::getMaterial, simulator::setMaterial, this).open();
|
||||
@ -60,7 +60,7 @@ public class SimulatorGui extends SimulatorPageGui<SimulatorGroup> {
|
||||
}, clickType -> {
|
||||
SimulatorElement<?> element = simulatorGroup.getElements().get(0);
|
||||
if (element instanceof TNTElement) {
|
||||
new SimulatorTNTGui(player, simulator, (TNTElement) element, simulatorGroup,this).open();
|
||||
new SimulatorTNTGui(player, simulator, (TNTElement) element, simulatorGroup, this).open();
|
||||
} else if (element instanceof RedstoneElement) {
|
||||
new SimulatorRedstoneGui(player, simulator, simulatorGroup, (RedstoneElement) element, this).open();
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren