SteamWar/BauSystem2.0
Archiviert
12
0

Add SimulatorWatcher.show, SimulatorWatcher.hide

Dieser Commit ist enthalten in:
yoyosource 2023-10-22 18:05:13 +02:00
Ursprung 3506083416
Commit 4e6ee3b95b
7 geänderte Dateien mit 98 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -60,4 +60,14 @@ public class SimulatorTestCommand extends SWCommand {
public void runCommand(Player player) { public void runCommand(Player player) {
SimulatorExecutor.run(SIMULATOR); 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);
}
} }

Datei anzeigen

@ -20,21 +20,32 @@
package de.steamwar.bausystem.features.simulator2; package de.steamwar.bausystem.features.simulator2;
import de.steamwar.bausystem.features.simulator2.data.Simulator; 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.bausystem.shared.Pair;
import de.steamwar.entity.REntity;
import de.steamwar.entity.REntityServer;
import de.steamwar.entity.RFallingBlockEntity;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import lombok.experimental.UtilityClass; 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.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.util.Vector;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.Map;
@UtilityClass @UtilityClass
public class SimulatorWatcher { 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<>(); private Map<Player, Pair<Simulator, Runnable>> watchers = new HashMap<>();
public void watch(Player player, Simulator simulator, Runnable watcher) { public void watch(Player player, Simulator simulator, Runnable watcher) {
@ -46,6 +57,10 @@ public class SimulatorWatcher {
} }
public void update(Simulator simulator) { public void update(Simulator simulator) {
REntityServer rEntityServer = entityServers.get(simulator);
rEntityServer.getEntities().forEach(REntity::die);
createSim(rEntityServer, simulator);
new ArrayList<>(watchers.values()).forEach(simulatorRunnablePair -> { new ArrayList<>(watchers.values()).forEach(simulatorRunnablePair -> {
if (simulatorRunnablePair.getKey() == simulator) { if (simulatorRunnablePair.getKey() == simulator) {
simulatorRunnablePair.getValue().run(); simulatorRunnablePair.getValue().run();
@ -59,6 +74,39 @@ public class SimulatorWatcher {
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
watchers.remove(event.getPlayer()); 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;
});
}
} }

Datei anzeigen

@ -76,6 +76,12 @@ public abstract class SimulatorElement<T extends SimulatorPhase> {
position.setZ(position.getZ() + z); position.setZ(position.getZ() + z);
} }
public abstract Material getWorldMaterial();
public Vector getWorldPos() {
return position;
}
public SWItem toItem(Player player, InvCallback invCallback) { public SWItem toItem(Player player, InvCallback invCallback) {
List<String> lore = new ArrayList<>(); List<String> lore = new ArrayList<>();
lore.add("§7Phase count§8:§e " + phases.size()); lore.add("§7Phase count§8:§e " + phases.size());

Datei anzeigen

@ -33,4 +33,14 @@ public class RedstoneElement extends SimulatorElement<RedstonePhase> {
public String getName() { public String getName() {
return "Redstone"; 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));
}
} }

Datei anzeigen

@ -20,7 +20,10 @@
package de.steamwar.bausystem.features.simulator2.data.tnt; package de.steamwar.bausystem.features.simulator2.data.tnt;
import de.steamwar.bausystem.features.simulator2.data.SimulatorElement; 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.Material;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class TNTElement extends SimulatorElement<TNTPhase> { public class TNTElement extends SimulatorElement<TNTPhase> {
@ -61,4 +64,17 @@ public class TNTElement extends SimulatorElement<TNTPhase> {
position.setZ(position.getBlockZ() + offset.getZ()); 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;
}
} }

Datei anzeigen

@ -50,6 +50,7 @@ public class SimulatorGroupGui extends SimulatorPageGui<SimulatorElement<?>> {
@Override @Override
public void headerAndFooter() { public void headerAndFooter() {
simulatorGroup.getElements().removeIf(element -> element.getPhases().isEmpty());
if (simulatorGroup.getElements().size() < 2) { if (simulatorGroup.getElements().size() < 2) {
back.open(); back.open();
return; return;
@ -63,7 +64,8 @@ public class SimulatorGroupGui extends SimulatorPageGui<SimulatorElement<?>> {
inventory.setItem(4, simulatorGroup.toItem(player, clickType -> { inventory.setItem(4, simulatorGroup.toItem(player, clickType -> {
new SimulatorMaterialGui(player, simulator, simulatorGroup::getMaterial, simulatorGroup::setMaterial, this).open(); new SimulatorMaterialGui(player, simulator, simulatorGroup::getMaterial, simulatorGroup::setMaterial, this).open();
}, clickType -> {})); }, clickType -> {
}));
inventory.setItem(48, new SWItem(Material.REPEATER, "§eSettings", clickType -> { inventory.setItem(48, new SWItem(Material.REPEATER, "§eSettings", clickType -> {
new SimulatorGroupSettingsGui(player, simulator, simulatorGroup, this).open(); new SimulatorGroupSettingsGui(player, simulator, simulatorGroup, this).open();

Datei anzeigen

@ -43,7 +43,7 @@ public class SimulatorGui extends SimulatorPageGui<SimulatorGroup> {
@Override @Override
public void headerAndFooter() { 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 -> { inventory.setItem(4, simulator.toItem(player, clickType -> {
new SimulatorMaterialGui(player, simulator, simulator::getMaterial, simulator::setMaterial, this).open(); new SimulatorMaterialGui(player, simulator, simulator::getMaterial, simulator::setMaterial, this).open();