SteamWar/BauSystem2.0
Archiviert
12
0

Fix AxisMovementLimiter
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Fix Simulator19
Update many things for dynamic preview

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2023-09-01 19:03:33 +02:00
Ursprung 6409748d32
Commit 6ebc9b1068
11 geänderte Dateien mit 180 neuen und 109 gelöschten Zeilen

Datei anzeigen

@ -49,14 +49,14 @@ public class AxisMovementLimiter {
this.movement = movement; this.movement = movement;
// Calculate the min and max values for the movement // Calculate the min and max values for the movement
minX = x; minX = x - 0.49;
maxX = x + 0.98; maxX = x + 0.49;
minY = y; minY = y;
maxY = y + 0.98; maxY = y + 0.98;
minZ = z; minZ = z - 0.49;
maxZ = z + 0.98; maxZ = z + 0.49;
switch (axis) { switch (axis) {
case X: case X:

Datei anzeigen

@ -61,6 +61,9 @@ public class Simulator19 implements Simulator {
@Override @Override
public synchronized PreviewRecord run(Pair<Integer, Map<Integer, List<List<Pair<TNTData, Integer>>>>> toCalculate) { public synchronized PreviewRecord run(Pair<Integer, Map<Integer, List<List<Pair<TNTData, Integer>>>>> toCalculate) {
PreviewRecord previewRecord = new PreviewRecord(); PreviewRecord previewRecord = new PreviewRecord();
if (toCalculate == null) {
return previewRecord;
}
int currentTick = 0; int currentTick = 0;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
@ -74,7 +77,7 @@ public class Simulator19 implements Simulator {
pairs.forEach(pair -> { pairs.forEach(pair -> {
if (pair.getValue() > 0) { if (pair.getValue() > 0) {
hasSomeLeft.set(true); hasSomeLeft.set(true);
TNT tnt = new TNT(pair.getKey().location.getX() - 0.49, pair.getKey().location.getY(), pair.getKey().location.getZ() - 0.49); TNT tnt = new TNT(pair.getKey().location.getX(), pair.getKey().location.getY(), pair.getKey().location.getZ());
if (!pair.getKey().xVelocity) tnt.setVx(0.0); if (!pair.getKey().xVelocity) tnt.setVx(0.0);
if (!pair.getKey().yVelocity) tnt.setVy(0.0); if (!pair.getKey().yVelocity) tnt.setVy(0.0);
if (!pair.getKey().zVelocity) tnt.setVz(0.0); if (!pair.getKey().zVelocity) tnt.setVz(0.0);
@ -97,7 +100,6 @@ public class Simulator19 implements Simulator {
} }
tntList.removeAll(remove); tntList.removeAll(remove);
} }
System.out.println(AIR_BLOCKS);
AIR_BLOCKS.forEach(pos -> previewRecord.addAir(new Vector(pos.x, pos.y, pos.z))); AIR_BLOCKS.forEach(pos -> previewRecord.addAir(new Vector(pos.x, pos.y, pos.z)));
System.out.println("Time: " + (System.currentTimeMillis() - time) + "ms " + cacheMisses + "/" + accessed + "/" + aired); System.out.println("Time: " + (System.currentTimeMillis() - time) + "ms " + cacheMisses + "/" + accessed + "/" + aired);

Datei anzeigen

@ -44,7 +44,7 @@ public class SimulatorCursor {
private Map<Player, REntityServer> rEntityServerMap = new HashMap<>(); private Map<Player, REntityServer> rEntityServerMap = new HashMap<>();
public void show(Player player, TNTSimulator tntSimulator, RayTraceUtils.RRayTraceResult result) { public void show(Player player, TNTSimulator tntSimulator, RayTraceUtils.RRayTraceResult result) {
REntityServer cursor = rEntityServerMap.get(player); REntityServer cursor = rEntityServerMap.remove(player);
if (cursor != null) if (cursor != null)
cursor.close(); cursor.close();
@ -140,4 +140,8 @@ public class SimulatorCursor {
return pos; return pos;
} }
public boolean has(Player player) {
return rEntityServerMap.containsKey(player);
}
} }

Datei anzeigen

@ -178,7 +178,7 @@ public class SimulatorStorage implements Enable, Disable {
if (content.isEmpty()) continue; if (content.isEmpty()) continue;
TNTSimulator tntSimulator = new TNTSimulator(); TNTSimulator tntSimulator = new TNTSimulator();
for (YAPIONObject element : content.streamObject().collect(Collectors.toList())) { for (YAPIONObject element : content.streamObject().collect(Collectors.toList())) {
tntSimulator.getTntElementList().add(new TNTElement(element, null, tntSimulator.getEntityServer())); tntSimulator.getTntElementList().add(new TNTElement(element, tntSimulator, null, tntSimulator.getEntityServer()));
} }
tntSimulators.put(newName, tntSimulator); tntSimulators.put(newName, tntSimulator);
} }

Datei anzeigen

@ -23,6 +23,8 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.features.simulator.gui.TNTElementGUI; import de.steamwar.bausystem.features.simulator.gui.TNTElementGUI;
import de.steamwar.bausystem.features.simulator.gui.TNTSimulatorGui; import de.steamwar.bausystem.features.simulator.gui.TNTSimulatorGui;
import de.steamwar.bausystem.features.simulator.preview.PreviewRecord;
import de.steamwar.bausystem.features.simulator.preview.Simulator;
import de.steamwar.bausystem.features.simulator.tnt.SimulatorElement; import de.steamwar.bausystem.features.simulator.tnt.SimulatorElement;
import de.steamwar.bausystem.features.simulator.tnt.TNTElement; import de.steamwar.bausystem.features.simulator.tnt.TNTElement;
import de.steamwar.bausystem.features.simulator.tnt.TNTGroup; import de.steamwar.bausystem.features.simulator.tnt.TNTGroup;
@ -34,7 +36,6 @@ import de.steamwar.bausystem.utils.RayTraceUtils;
import de.steamwar.entity.REntity; import de.steamwar.entity.REntity;
import de.steamwar.entity.REntityServer; import de.steamwar.entity.REntityServer;
import lombok.Getter; import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.TNTPrimed;
@ -57,8 +58,9 @@ public class TNTSimulator {
private List<SimulatorElement> tntElementList = new ArrayList<>(); private List<SimulatorElement> tntElementList = new ArrayList<>();
public TNTSimulator() { private PreviewRecord previewRecord = null;
public TNTSimulator() {
} }
public TNTSimulator(YAPIONObject yapionObject) { public TNTSimulator(YAPIONObject yapionObject) {
@ -66,9 +68,9 @@ public class TNTSimulator {
YAPIONArray yapionArray = yapionObject.getArrayOrDefault("tntElements", new YAPIONArray()); YAPIONArray yapionArray = yapionObject.getArrayOrDefault("tntElements", new YAPIONArray());
for (YAPIONObject element : yapionArray.streamObject().collect(Collectors.toList())) { for (YAPIONObject element : yapionArray.streamObject().collect(Collectors.toList())) {
if (element.containsKey("elements", YAPIONType.ARRAY)) { if (element.containsKey("elements", YAPIONType.ARRAY)) {
tntElementList.add(new TNTGroup(element, entityServer)); tntElementList.add(new TNTGroup(element, this, entityServer));
} else { } else {
tntElementList.add(new TNTElement(element, null, entityServer)); tntElementList.add(new TNTElement(element, this, null, entityServer));
} }
} }
} }
@ -100,10 +102,39 @@ public class TNTSimulator {
entityServer.removePlayer(player); entityServer.removePlayer(player);
players.remove(player); players.remove(player);
} }
hidePreview(player);
} }
void _hide(Player player) { public void showPreview(Player player) {
players.remove(player); if (previewRecord == null) {
calcPreview(true);
}
if (previewRecord != null) {
previewRecord.show(player);
}
}
public void hidePreview(Player player) {
if (previewRecord != null && previewRecord.hide(player)) {
previewRecord = null;
}
}
public void calcPreview(boolean force) {
if (!force && previewRecord == null) {
return;
}
PreviewRecord newRecord = Simulator.impl.run(locations());
PreviewRecord oldRecord = previewRecord;
previewRecord = newRecord;
if (newRecord == null) return;
if (oldRecord != null) {
new HashSet<>(oldRecord.getPlayers()).forEach(player -> {
oldRecord.hide(player);
newRecord.show(player);
});
}
} }
public List<REntity> getEntities() { public List<REntity> getEntities() {
@ -120,6 +151,7 @@ public class TNTSimulator {
public void clear() { public void clear() {
new ArrayList<>(tntElementList).forEach(this::remove); new ArrayList<>(tntElementList).forEach(this::remove);
calcPreview(false);
} }
public void remove(SimulatorElement element) { public void remove(SimulatorElement element) {
@ -142,6 +174,7 @@ public class TNTSimulator {
} }
element.close(); element.close();
tntElementList.remove(element); tntElementList.remove(element);
calcPreview(false);
} }
public void change() { public void change() {
@ -151,6 +184,7 @@ public class TNTSimulator {
((TNTGroup) simulatorElement).getElements().forEach(SimulatorElement::change); ((TNTGroup) simulatorElement).getElements().forEach(SimulatorElement::change);
} }
}); });
calcPreview(false);
} }
public void edit(Player player, RayTraceUtils.RRayTraceResult result) { public void edit(Player player, RayTraceUtils.RRayTraceResult result) {
@ -200,16 +234,15 @@ public class TNTSimulator {
return; return;
} }
TNTElement tntElement = new TNTElement(SimulatorCursor.getPos(player, result), null, entityServer); TNTElement tntElement = new TNTElement(this, SimulatorCursor.getPos(player, result), null, entityServer);
tntElementList.add(tntElement); tntElementList.add(tntElement);
TNTElementGUI.open(player, tntElement, null); TNTElementGUI.open(player, tntElement, null);
} }
public Pair<Integer, Map<Integer, List<List<Pair<TNTData, Integer>>>>> locations(Player p) { public Pair<Integer, Map<Integer, List<List<Pair<TNTData, Integer>>>>> locations() {
Region region = Region.getRegion(p.getLocation());
Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result = new HashMap<>(); Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result = new HashMap<>();
for (SimulatorElement element : tntElementList) { for (SimulatorElement element : tntElementList) {
if (element.locations(result, region, p.getLocation())) { if (element.locations(result)) {
return null; return null;
} }
} }
@ -234,7 +267,7 @@ public class TNTSimulator {
public void start(Player p) { public void start(Player p) {
Region region = Region.getRegion(p.getLocation()); Region region = Region.getRegion(p.getLocation());
Pair<Integer, Map<Integer, List<List<Pair<TNTData, Integer>>>>> result = locations(p); Pair<Integer, Map<Integer, List<List<Pair<TNTData, Integer>>>>> result = locations();
if (result == null) { if (result == null) {
BauSystem.MESSAGE.send("SIMULATOR_REGION_FROZEN", p); BauSystem.MESSAGE.send("SIMULATOR_REGION_FROZEN", p);
return; return;

Datei anzeigen

@ -28,23 +28,24 @@ import de.steamwar.bausystem.shared.Pair;
import de.steamwar.bausystem.utils.ItemUtils; import de.steamwar.bausystem.utils.ItemUtils;
import de.steamwar.bausystem.utils.RayTraceUtils; import de.steamwar.bausystem.utils.RayTraceUtils;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
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.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.*; import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
@Linked @Linked
public class TNTSimulatorListener implements Listener { public class TNTSimulatorListener implements Listener {
private Map<Player, PreviewRecord> previewRecordMap = new HashMap<>();
private Map<Player, TNTSimulator> currentSimulator = new HashMap<>(); private Map<Player, TNTSimulator> currentSimulator = new HashMap<>();
private boolean permissionCheck(Player player) { private boolean permissionCheck(Player player) {
@ -55,114 +56,125 @@ public class TNTSimulatorListener implements Listener {
return true; return true;
} }
private Pair<TNTSimulator, Boolean> getSimulator(Player player) { private Pair<TNTSimulator, Boolean> getSimulator(ItemStack offHand, ItemStack mainHand) {
if (ItemUtils.isItem(player.getInventory().getItemInOffHand(), "simulator")) { if (ItemUtils.isItem(offHand, "simulator")) {
return new Pair<>(SimulatorStorage.getSimulator(player.getInventory().getItemInOffHand()), true); return new Pair<>(SimulatorStorage.getSimulator(offHand), true);
} }
if (ItemUtils.isItem(player.getInventory().getItemInMainHand(), "simulator")) { if (ItemUtils.isItem(mainHand, "simulator")) {
return new Pair<>(SimulatorStorage.getSimulator(player.getInventory().getItemInMainHand()), false); return new Pair<>(SimulatorStorage.getSimulator(mainHand), false);
} }
return new Pair<>(null, false); return new Pair<>(null, false);
} }
private Pair<TNTSimulator, Boolean> getSimulator(Player player) {
return getSimulator(player.getInventory().getItemInOffHand(), player.getInventory().getItemInMainHand());
}
static RayTraceUtils.RRayTraceResult trace(Player player, Location to, TNTSimulator simulator) { static RayTraceUtils.RRayTraceResult trace(Player player, Location to, TNTSimulator simulator) {
return RayTraceUtils.traceREntity(player, to, simulator.getEntities()); return RayTraceUtils.traceREntity(player, to, simulator.getEntities());
} }
private void hideShow(Player player, TNTSimulator simulator) { private void hideShow(Player player, Pair<TNTSimulator, Boolean> simulatorPair) {
TNTSimulator simulator = simulatorPair.getKey();
TNTSimulator tntSimulator = currentSimulator.remove(player); TNTSimulator tntSimulator = currentSimulator.remove(player);
if (tntSimulator == simulator) return; if (tntSimulator == simulator) return;
if (tntSimulator != null) tntSimulator.hide(player); if (tntSimulator != null) tntSimulator.hide(player);
if (simulator != null) { if (simulator == null) return;
currentSimulator.put(player, simulator);
simulator.show(player); simulator.show(player);
SimulatorCursor.show(player, simulator, trace(player, player.getLocation(), simulator)); SimulatorCursor.show(player, simulator, trace(player, player.getLocation(), simulator));
if (simulatorPair.getValue()) {
simulator.showPreview(player);
} else {
simulator.hidePreview(player);
} }
} }
@EventHandler @EventHandler
public void onPlayerMove(PlayerMoveEvent e) { public void onPlayerMove(PlayerMoveEvent e) {
Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer()); Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer());
hideShow(e.getPlayer(), simulatorPair.getKey()); hideShow(e.getPlayer(), simulatorPair);
} }
@EventHandler @EventHandler
public void onPlayerItemHeld(PlayerItemHeldEvent e) { public void onPlayerItemHeld(PlayerItemHeldEvent e) {
Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer()); Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer().getInventory().getItemInOffHand(), e.getPlayer().getInventory().getItem(e.getNewSlot()));
hideShow(e.getPlayer(), simulatorPair.getKey()); hideShow(e.getPlayer(), simulatorPair);
} }
@EventHandler @EventHandler
public void onPlayerDropItem(PlayerDropItemEvent e) { public void onPlayerDropItem(PlayerDropItemEvent e) {
Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer()); Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer().getInventory().getItemInOffHand(), new ItemStack(Material.AIR));
hideShow(e.getPlayer(), simulatorPair.getKey()); hideShow(e.getPlayer(), simulatorPair);
} }
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer()); Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer());
hideShow(e.getPlayer(), simulatorPair.getKey()); hideShow(e.getPlayer(), simulatorPair);
} }
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent e) { public void onPlayerQuit(PlayerQuitEvent e) {
hideShow(e.getPlayer(), null); hideShow(e.getPlayer(), new Pair<>(null, false));
} }
@EventHandler @EventHandler
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent e) { public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent e) {
Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer()); Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getOffHandItem(), e.getMainHandItem());
hideShow(e.getPlayer(), simulatorPair.getKey()); hideShow(e.getPlayer(), simulatorPair);
if (!simulatorPair.getValue()) return;
TNTSimulator simulator = SimulatorStorage.getSimulator(e.getOffHandItem());
if (preview(e.getPlayer(), simulator)) {
e.setCancelled(true);
}
}
private boolean preview(Player player, TNTSimulator simulator) {
if (simulator == null) return false;
Pair<Integer, Map<Integer, List<List<Pair<TNTData, Integer>>>>> toCalculate = simulator.locations(player);
PreviewRecord previewRecord = Simulator.impl.run(toCalculate);
if (previewRecord == null) {
return true;
}
previewRecord.add(player);
previewRecordMap.put(player, previewRecord);
return false;
} }
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent e) {
if (!ItemUtils.isItem(event.getItem(), "simulator")) { if (!ItemUtils.isItem(e.getItem(), "simulator")) {
return; return;
} }
event.setCancelled(true); Pair<TNTSimulator, Boolean> simulatorPair = getSimulator(e.getPlayer());
if (!permissionCheck(event.getPlayer())) { if (!SimulatorCursor.has(e.getPlayer()) && simulatorPair.getValue()) {
return; return;
} }
TNTSimulator simulator = SimulatorStorage.getSimulator(event.getItem()); e.setCancelled(true);
if (!permissionCheck(e.getPlayer())) {
return;
}
TNTSimulator simulator = simulatorPair.getKey();
switch (event.getAction()) { switch (e.getAction()) {
case LEFT_CLICK_BLOCK: case LEFT_CLICK_BLOCK:
case LEFT_CLICK_AIR: case LEFT_CLICK_AIR:
if (simulator == null) { if (simulator == null || simulatorPair.getValue()) {
return; return;
} }
simulator.start(event.getPlayer()); simulator.start(e.getPlayer());
break; break;
case RIGHT_CLICK_BLOCK: case RIGHT_CLICK_BLOCK:
case RIGHT_CLICK_AIR: case RIGHT_CLICK_AIR:
if (simulator == null) { if (simulator == null) {
SimulatorSelectionGUI.open(event.getPlayer(), event.getItem()); SimulatorSelectionGUI.open(e.getPlayer(), e.getItem());
} else { } else {
simulator.edit(event.getPlayer(), trace(event.getPlayer(), event.getPlayer().getLocation(), simulator)); simulator.edit(e.getPlayer(), trace(e.getPlayer(), e.getPlayer().getLocation(), simulator));
} }
break; break;
default: default:
break; break;
} }
} }
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) { // TODO: Optimize this
currentSimulator.values().forEach(simulator -> {
simulator.calcPreview(false);
});
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) { // TODO: Optimize this
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
currentSimulator.values().forEach(simulator -> {
simulator.calcPreview(false);
});
}, 0);
}
} }

Datei anzeigen

@ -310,7 +310,7 @@ public class TNTElementGUI {
tntSimulator.getTntElementList().remove(tntElement); tntSimulator.getTntElementList().remove(tntElement);
Vector vector = tntElement.getOwnPosition().clone(); Vector vector = tntElement.getOwnPosition().clone();
int tickOffset = tntElement.getOwnTickOffset(); int tickOffset = tntElement.getOwnTickOffset();
TNTGroup tntGroup = new TNTGroup(vector); TNTGroup tntGroup = new TNTGroup(tntSimulator, vector);
tntGroup.setTickOffset(tickOffset); tntGroup.setTickOffset(tickOffset);
tntGroup.add(tntElement); tntGroup.add(tntElement);
tntElement.setTickOffset(0); tntElement.setTickOffset(0);
@ -318,7 +318,7 @@ public class TNTElementGUI {
tntSimulator.getTntElementList().add(tntGroup); tntSimulator.getTntElementList().add(tntGroup);
// Add new TNT // Add new TNT
TNTElement newElement = new TNTElement(new Vector(0, 0, 0), tntGroup, tntSimulator.getEntityServer()); TNTElement newElement = new TNTElement(tntSimulator, new Vector(0, 0, 0), tntGroup, tntSimulator.getEntityServer());
newElement.setTickOffset(1); newElement.setTickOffset(1);
tntGroup.add(newElement); tntGroup.add(newElement);
@ -334,7 +334,7 @@ public class TNTElementGUI {
inv.setItem(25, new SWItem(Material.DISPENSER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ADD_TNT", player), clickType -> { inv.setItem(25, new SWItem(Material.DISPENSER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ADD_TNT", player), clickType -> {
if (clickType == ClickType.DOUBLE_CLICK) return; if (clickType == ClickType.DOUBLE_CLICK) return;
Vector vector = tntElement.getOwnPosition().clone(); Vector vector = tntElement.getOwnPosition().clone();
TNTElement newElement = new TNTElement(vector, null, tntSimulator.getEntityServer()); TNTElement newElement = new TNTElement(tntSimulator, vector, null, tntSimulator.getEntityServer());
if (tntElement.hasParent()) { if (tntElement.hasParent()) {
newElement.setTickOffset(tntElement.getOwnTickOffset() + 1); newElement.setTickOffset(tntElement.getOwnTickOffset() + 1);
tntElement.getParent().add(newElement); tntElement.getParent().add(newElement);

Datei anzeigen

@ -21,8 +21,11 @@ package de.steamwar.bausystem.features.simulator.preview;
import de.steamwar.entity.REntityServer; import de.steamwar.entity.REntityServer;
import de.steamwar.entity.RFallingBlockEntity; import de.steamwar.entity.RFallingBlockEntity;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -32,43 +35,45 @@ import java.util.Set;
public class PreviewRecord { public class PreviewRecord {
private static final World WORLD = Bukkit.getWorlds().get(0);
private static final BlockData AIR_BLOCK_DATA = Material.AIR.createBlockData(); private static final BlockData AIR_BLOCK_DATA = Material.AIR.createBlockData();
private Set<Vector> vectorList = new HashSet<>(); private Set<Vector> vectorList = new HashSet<>();
private Set<Vector> destroyedBlocks = new HashSet<>(); private Set<Vector> destroyedBlocks = new HashSet<>();
private REntityServer rEntityServer;
@Getter
private Set<Player> players = new HashSet<>();
private REntityServer rEntityServer = new REntityServer();
public void add(double x, double y, double z) { public void add(double x, double y, double z) {
vectorList.add(new Vector(x + 0.49, y, z + 0.49)); Vector vector = new Vector(x, y, z);
if (vectorList.add(vector)) {
RFallingBlockEntity entity = new RFallingBlockEntity(rEntityServer, vector.toLocation(WORLD), Material.RED_STAINED_GLASS);
entity.setNoGravity(true);
}
} }
public void addAir(Vector destroyed) { public void addAir(Vector destroyed) {
destroyedBlocks.add(destroyed); destroyedBlocks.add(destroyed);
} }
public void add(Player player) { public void show(Player player) {
if (rEntityServer == null) { if (players.add(player)) {
rEntityServer = new REntityServer();
vectorList.forEach(vector -> {
RFallingBlockEntity entity = new RFallingBlockEntity(rEntityServer, vector.toLocation(player.getWorld()), Material.RED_STAINED_GLASS);
entity.setNoGravity(true);
});
}
rEntityServer.addPlayer(player); rEntityServer.addPlayer(player);
destroyedBlocks.forEach(vector -> { destroyedBlocks.forEach(vector -> {
player.sendBlockChange(vector.toLocation(player.getWorld()), AIR_BLOCK_DATA); player.sendBlockChange(vector.toLocation(player.getWorld()), AIR_BLOCK_DATA);
}); });
} }
}
public void close(Player player) { public boolean hide(Player player) {
rEntityServer.close(); if (players.remove(player)) {
rEntityServer = null; rEntityServer.removePlayer(player);
destroyedBlocks.forEach(vector -> { destroyedBlocks.forEach(vector -> {
Location location = vector.toLocation(player.getWorld()); Location location = vector.toLocation(player.getWorld());
player.sendBlockChange(location, location.getBlock().getBlockData()); player.sendBlockChange(location, location.getBlock().getBlockData());
}); });
} }
return players.isEmpty();
}
} }

Datei anzeigen

@ -47,7 +47,7 @@ public interface SimulatorElement {
void remove(TNTElement tntElement); void remove(TNTElement tntElement);
SWItem menu(Player p); SWItem menu(Player p);
boolean locations(Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result, Region region, Location location); // Ticks to subtick order to spawning runnable to count of activations boolean locations(Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result); // Ticks to subtick order to spawning runnable to count of activations
int tntCount(); int tntCount();
int tick(); int tick();

Datei anzeigen

@ -23,6 +23,7 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.simulator.OrderUtils; import de.steamwar.bausystem.features.simulator.OrderUtils;
import de.steamwar.bausystem.features.simulator.SimulatorStorage; import de.steamwar.bausystem.features.simulator.SimulatorStorage;
import de.steamwar.bausystem.features.simulator.TNTData; import de.steamwar.bausystem.features.simulator.TNTData;
import de.steamwar.bausystem.features.simulator.TNTSimulator;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode; import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode;
@ -55,6 +56,7 @@ public class TNTElement implements SimulatorElement {
private RFallingBlockEntity entity; private RFallingBlockEntity entity;
TNTGroup tntGroup = null; TNTGroup tntGroup = null;
private final TNTSimulator tntSimulator;
private final Vector position; private final Vector position;
private int fuseTicks = 80; private int fuseTicks = 80;
private int count = 1; private int count = 1;
@ -72,14 +74,16 @@ public class TNTElement implements SimulatorElement {
private Material material = Material.TNT; private Material material = Material.TNT;
private boolean disabled = false; private boolean disabled = false;
public TNTElement(Vector position, TNTGroup tntGroup, REntityServer entityServer) { public TNTElement(TNTSimulator tntSimulator, Vector position, TNTGroup tntGroup, REntityServer entityServer) {
this.tntSimulator = tntSimulator;
this.entityServer = entityServer; this.entityServer = entityServer;
this.tntGroup = tntGroup; this.tntGroup = tntGroup;
this.position = position; this.position = position;
initEntity(); initEntity();
} }
public TNTElement(YAPIONObject yapionObject, TNTGroup tntGroup, REntityServer entityServer) { public TNTElement(YAPIONObject yapionObject, TNTSimulator tntSimulator, TNTGroup tntGroup, REntityServer entityServer) {
this.tntSimulator = tntSimulator;
this.entityServer = entityServer; this.entityServer = entityServer;
this.tntGroup = tntGroup; this.tntGroup = tntGroup;
this.position = new Vector(yapionObject.getDoubleOrDefault("x", yapionObject.getDoubleOrDefault("positionX", 0)), yapionObject.getDoubleOrDefault("y", yapionObject.getDoubleOrDefault("positionY", 0)), yapionObject.getDoubleOrDefault("z", yapionObject.getDoubleOrDefault("positionZ", 0))); this.position = new Vector(yapionObject.getDoubleOrDefault("x", yapionObject.getDoubleOrDefault("positionX", 0)), yapionObject.getDoubleOrDefault("y", yapionObject.getDoubleOrDefault("positionY", 0)), yapionObject.getDoubleOrDefault("z", yapionObject.getDoubleOrDefault("positionZ", 0)));
@ -147,6 +151,7 @@ public class TNTElement implements SimulatorElement {
this.position.setY(position.getY()); this.position.setY(position.getY());
this.position.setZ(position.getZ()); this.position.setZ(position.getZ());
_updatePosition(); _updatePosition();
tntSimulator.calcPreview(false);
} }
void _updatePosition() { void _updatePosition() {
@ -168,6 +173,7 @@ public class TNTElement implements SimulatorElement {
@Override @Override
public void remove(TNTElement tntElement) { public void remove(TNTElement tntElement) {
entity.die(); entity.die();
tntSimulator.calcPreview(false);
} }
@Override @Override
@ -190,15 +196,9 @@ public class TNTElement implements SimulatorElement {
} }
@Override @Override
public boolean locations(Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result, Region region, Location radius) { public boolean locations(Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result) {
if (disabled) return false; if (disabled) return false;
Location location = getPosition().toLocation(SimulatorStorage.WORLD); Location location = getPosition().toLocation(SimulatorStorage.WORLD);
if (region.isGlobal() && location.distanceSquared(radius) > 10000) {
return false;
}
if (!region.inRegion(location, RegionType.NORMAL, RegionExtensionType.NORMAL)) {
return false;
}
Region thisRegion = Region.getRegion(location); Region thisRegion = Region.getRegion(location);
if (thisRegion.getFlagStorage().get(Flag.FREEZE) == FreezeMode.ACTIVE) { if (thisRegion.getFlagStorage().get(Flag.FREEZE) == FreezeMode.ACTIVE) {
return true; return true;
@ -224,6 +224,7 @@ public class TNTElement implements SimulatorElement {
if (count < 0) count = 0; if (count < 0) count = 0;
if (count > 400) count = 400; if (count > 400) count = 400;
this.count = count; this.count = count;
tntSimulator.calcPreview(false);
} }
public int getOwnTickOffset() { public int getOwnTickOffset() {
@ -240,19 +241,23 @@ public class TNTElement implements SimulatorElement {
public void setTickOffset(int tickOffset) { public void setTickOffset(int tickOffset) {
if (getTickOffset() - this.tickOffset + tickOffset < 0) { if (getTickOffset() - this.tickOffset + tickOffset < 0) {
this.tickOffset = -this.getParentTickOffset(); this.tickOffset = -this.getParentTickOffset();
tntSimulator.calcPreview(false);
return; return;
} }
if (getTickOffset() - this.tickOffset + tickOffset > 400) { if (getTickOffset() - this.tickOffset + tickOffset > 400) {
this.tickOffset = 400 - this.getParentTickOffset(); this.tickOffset = 400 - this.getParentTickOffset();
tntSimulator.calcPreview(false);
return; return;
} }
this.tickOffset = tickOffset; this.tickOffset = tickOffset;
tntSimulator.calcPreview(false);
} }
public void setFuseTicks(int fuseTicks) { public void setFuseTicks(int fuseTicks) {
if (fuseTicks < 0) fuseTicks = 0; if (fuseTicks < 0) fuseTicks = 0;
if (fuseTicks > 160) fuseTicks = 160; if (fuseTicks > 160) fuseTicks = 160;
this.fuseTicks = fuseTicks; this.fuseTicks = fuseTicks;
tntSimulator.calcPreview(false);
} }
public Vector getOwnPosition() { public Vector getOwnPosition() {
@ -261,6 +266,7 @@ public class TNTElement implements SimulatorElement {
public void setOrder(Material material) { public void setOrder(Material material) {
this.order = material; this.order = material;
tntSimulator.calcPreview(false);
} }
public void setMaterial(Material material) { public void setMaterial(Material material) {
@ -278,6 +284,7 @@ public class TNTElement implements SimulatorElement {
public void setDisabled(boolean disabled) { public void setDisabled(boolean disabled) {
this.disabled = disabled; this.disabled = disabled;
_updatePosition(); _updatePosition();
tntSimulator.calcPreview(false);
} }
public void align(Vector offset) { public void align(Vector offset) {

Datei anzeigen

@ -21,6 +21,7 @@ package de.steamwar.bausystem.features.simulator.tnt;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.simulator.TNTData; import de.steamwar.bausystem.features.simulator.TNTData;
import de.steamwar.bausystem.features.simulator.TNTSimulator;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.shared.Pair; import de.steamwar.bausystem.shared.Pair;
import de.steamwar.entity.REntity; import de.steamwar.entity.REntity;
@ -43,24 +44,27 @@ import java.util.stream.Collectors;
@Getter @Getter
public class TNTGroup implements SimulatorElement { public class TNTGroup implements SimulatorElement {
private final TNTSimulator tntSimulator;
private final Vector position; private final Vector position;
private int tickOffset = 0; private int tickOffset = 0;
private Material material = Material.BARREL; private Material material = Material.BARREL;
private boolean disabled = false; private boolean disabled = false;
private List<TNTElement> elements = new ArrayList<>(); private List<TNTElement> elements = new ArrayList<>();
public TNTGroup(Vector position) { public TNTGroup(TNTSimulator tntSimulator, Vector position) {
this.tntSimulator = tntSimulator;
this.position = position; this.position = position;
} }
public TNTGroup(YAPIONObject yapionObject, REntityServer entityServer) { public TNTGroup(YAPIONObject yapionObject, TNTSimulator tntSimulator, REntityServer entityServer) {
this.tntSimulator = tntSimulator;
this.position = new Vector(yapionObject.getDoubleOrDefault("x", 0), yapionObject.getDoubleOrDefault("y", 0), yapionObject.getDoubleOrDefault("z", 0)); this.position = new Vector(yapionObject.getDoubleOrDefault("x", 0), yapionObject.getDoubleOrDefault("y", 0), yapionObject.getDoubleOrDefault("z", 0));
this.tickOffset = yapionObject.getIntOrDefault("tickOffset", 0); this.tickOffset = yapionObject.getIntOrDefault("tickOffset", 0);
this.material = Material.getMaterial(yapionObject.getStringOrDefault("material", "BARREL")); this.material = Material.getMaterial(yapionObject.getStringOrDefault("material", "BARREL"));
this.disabled = yapionObject.getBooleanOrDefault("disabled", false); this.disabled = yapionObject.getBooleanOrDefault("disabled", false);
YAPIONArray elements = yapionObject.getArrayOrDefault("elements", new YAPIONArray()); YAPIONArray elements = yapionObject.getArrayOrDefault("elements", new YAPIONArray());
for (YAPIONObject element : elements.streamObject().collect(Collectors.toList())) { for (YAPIONObject element : elements.streamObject().collect(Collectors.toList())) {
TNTElement tntElement = new TNTElement(element, this, entityServer); TNTElement tntElement = new TNTElement(element, tntSimulator, this, entityServer);
this.elements.add(tntElement); this.elements.add(tntElement);
tntElement._updatePosition(); tntElement._updatePosition();
} }
@ -86,6 +90,7 @@ public class TNTGroup implements SimulatorElement {
public void add(TNTElement tntElement) { public void add(TNTElement tntElement) {
tntElement.tntGroup = this; tntElement.tntGroup = this;
elements.add(tntElement); elements.add(tntElement);
tntSimulator.calcPreview(false);
} }
@Override @Override
@ -113,6 +118,7 @@ public class TNTGroup implements SimulatorElement {
this.position.setY(position.getY()); this.position.setY(position.getY());
this.position.setZ(position.getZ()); this.position.setZ(position.getZ());
elements.forEach(TNTElement::_updatePosition); elements.forEach(TNTElement::_updatePosition);
tntSimulator.calcPreview(false);
} }
@Override @Override
@ -141,10 +147,10 @@ public class TNTGroup implements SimulatorElement {
} }
@Override @Override
public boolean locations(Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result, Region region, Location location) { public boolean locations(Map<Integer, Map<Integer, Set<Pair<TNTData, Integer>>>> result) {
if (disabled) return false; if (disabled) return false;
for (TNTElement element : elements) { for (TNTElement element : elements) {
if (element.locations(result, region, location)) { if (element.locations(result)) {
return true; return true;
} }
} }
@ -174,6 +180,7 @@ public class TNTGroup implements SimulatorElement {
} }
} }
this.tickOffset = tickOffset; this.tickOffset = tickOffset;
tntSimulator.calcPreview(false);
} }
public void setMaterial(Material material) { public void setMaterial(Material material) {
@ -183,5 +190,6 @@ public class TNTGroup implements SimulatorElement {
public void setDisabled(boolean disabled) { public void setDisabled(boolean disabled) {
this.disabled = disabled; this.disabled = disabled;
elements.forEach(TNTElement::_updatePosition); elements.forEach(TNTElement::_updatePosition);
tntSimulator.calcPreview(false);
} }
} }