Skip to content

Commit

Permalink
Fixed issues with closing and output rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mircokroon committed Feb 14, 2021
1 parent 4a45756 commit 114e96b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/main/java/game/data/RenderDistanceExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public void updateDistance(CoordinateDim2D location) {
}

int dist = location.blockDistance(playerChunk);
if (dist > 32) {
System.out.println(location);
}
if (dist > maxDistance && maxDistance < 32 && dist < 32) {
maxDistance = dist;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/game/data/coordinates/Coordinate2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,11 @@ public CoordinateDim2D addDimension(Dimension dimension) {
public int blockDistance(Coordinate2D globalToChunk) {
return Math.max(Math.abs(this.x - globalToChunk.x), Math.abs(this.z - globalToChunk.z));
}

public Coordinate2D divide(int size) {
if (size == 0) {
return this;
}
return new Coordinate2D(this.x / size, this.z / size);
}
}
3 changes: 1 addition & 2 deletions src/main/java/gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ public void start(Stage stage) throws Exception {
// when in GUI mode, close the application when the main stage is closed.
if (Config.inGuiMode()) {
this.stage.setOnCloseRequest(e -> {
WorldManager.getInstance().save();
Platform.exit();
System.exit(0);
});
}

Expand Down
32 changes: 22 additions & 10 deletions src/main/java/gui/GuiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ void computeBounds(boolean force) {
double gridWidth = width.get() / bounds.getWidth();
double gridHeight = height.get() / bounds.getHeight();

gridSize = (int) Math.round(Math.min(gridWidth, gridHeight));

gridSize = (int) Math.max(1, Math.round(Math.min(gridWidth, gridHeight)));
redrawCanvas();
}

Expand Down Expand Up @@ -270,14 +269,16 @@ private Bounds getOverviewBounds(Collection<Coordinate2D> coordinates) {
* @return the set of chunks actually in range.
*/
private Collection<Coordinate2D> getChunksInRange(Collection<Coordinate2D> coords, int rangeX, int rangeZ) {
if (this.playerPos == null) {
drawableChunks = chunkMap.keySet();
return drawableChunks;
Coordinate2D center;
if (this.playerPos != null) {
center = this.playerPos.discretize().globalToChunk();
} else {
// if no player position is known, calculate the average coordinate
center = coords.stream().reduce(Coordinate2D::add).orElse(new Coordinate2D(0, 0)).divide(coords.size());
}
Coordinate2D player = this.playerPos.discretize().globalToChunk();

return coords.parallelStream()
.filter(coordinate2D -> coordinate2D.isInRange(player, rangeX, rangeZ))
.filter(coordinate2D -> coordinate2D.isInRange(center, rangeX, rangeZ))
.collect(Collectors.toSet());
}

Expand Down Expand Up @@ -317,10 +318,10 @@ void redrawPlayer() {
}

private void drawChunk(Coordinate2D pos) {
drawChunk(chunkCanvas.getGraphicsContext2D(), pos, this.bounds, true);
drawChunk(chunkCanvas.getGraphicsContext2D(), pos, this.bounds, this.gridSize, true);
}

private void drawChunk(GraphicsContext graphics, Coordinate2D pos, Bounds bounds, boolean drawBlack) {
private void drawChunk(GraphicsContext graphics, Coordinate2D pos, Bounds bounds, int gridSize, boolean drawBlack) {
ChunkImage chunkImage = chunkMap.get(pos);

int drawX = (pos.getX() - bounds.getMinX()) * gridSize;
Expand Down Expand Up @@ -356,16 +357,27 @@ private void drawChunkAsync(Coordinate2D pos) {
public void export() {
Bounds fullBounds = getOverviewBounds(chunkMap.keySet());

// set size limit so that we don't have memory issues with the output image
int MAX_SIZE = 2 << 12;

int width = fullBounds.getWidth() * 16;
int height = fullBounds.getHeight() * 16;

// half the size of the output so we keep nice round numbers, until the grid size is down to 1px/chunk
int gridSize = 16;
while (gridSize > 1 && (width > MAX_SIZE || height > MAX_SIZE)) {
width /= 2;
height /= 2;
gridSize /= 2;
}

Canvas temp = new Canvas(width, height);
GraphicsContext graphics = temp.getGraphicsContext2D();
graphics.setImageSmoothing(false);

// draw each chunk
for (Map.Entry<Coordinate2D, ChunkImage> entry : chunkMap.entrySet()) {
drawChunk(graphics, entry.getKey(), fullBounds, false);
drawChunk(graphics, entry.getKey(), fullBounds, gridSize, false);
}

SnapshotParameters snapshotParameters = new SnapshotParameters();
Expand Down
48 changes: 43 additions & 5 deletions src/main/java/gui/GuiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@


import config.Config;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.util.Duration;
import proxy.auth.AuthStatus;
import proxy.auth.ClientAuthenticator;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -43,7 +42,9 @@ public class GuiSettings {
public TextField minecraftUsername;
public TextField accessToken;
public Hyperlink authHelpLink;
public Label portVerifyLabel;
Config config;
private boolean portInUse;

public GuiSettings() {
this.config = GuiManager.getConfig();
Expand Down Expand Up @@ -101,9 +102,8 @@ void initialize() {
private void handleDataValidation() {
// disable button when field is empty
server.textProperty().addListener((ov, oldV, newV) -> {
saveButton.setDisable(newV.length() == 0);
updateSaveButtonState();
});
saveButton.setDisable(server.getText() == null || server.getText().length() == 0);

// verify auth details on focus loss
minecraftDir.focusedProperty().addListener((ov, oldVal, newVal) -> {
Expand All @@ -112,6 +112,17 @@ private void handleDataValidation() {
}
});
verifyAuthDetails();

// verify port on focus loss
portLocal.focusedProperty().addListener((ov, oldVal, newVal) -> {
verifyLocalPort();
});
verifyLocalPort();
}

private void updateSaveButtonState() {
int len = server.getText() == null ? 0 : server.getText().length();
saveButton.setDisable(len == 0 || portInUse);
}


Expand All @@ -132,6 +143,17 @@ private void handleResizing() {
});
}

private void verifyLocalPort() {
if (!isPortAvailable(Integer.parseInt(portLocal.getText()))) {
portVerifyLabel.setText("Port in use!");
portInUse = true;
} else {
portVerifyLabel.setText("");
portInUse = false;
}
updateSaveButtonState();
}

private void verifyAuthDetails() {
String path = minecraftDir.getText();
AuthStatus status = ClientAuthenticator.authDetailsValid(path);
Expand Down Expand Up @@ -230,7 +252,23 @@ public void saveSettings(ActionEvent actionEvent) {
config.username = minecraftUsername.getText();
config.accessToken = accessToken.getText();

if (!config.isStarted()) {
if (!isPortAvailable(config.portLocal)) {
System.err.println("Port in use");
return;
}
}
config.settingsComplete();
GuiManager.closeSettings();
}

public boolean isPortAvailable(int port) {
if (config.isStarted()) { return true; }

try (ServerSocket ss = new ServerSocket(port)) {
return true;
} catch (IOException e) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/gui/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public RightClickMenu(GuiMap handler) {

menu.add(construct("Save & Exit", e -> {
WorldManager.getInstance().save();
Platform.exit();
System.exit(0);
}));

if (Config.isInDevMode()) {
Expand Down
15 changes: 10 additions & 5 deletions src/main/resources/ui/Settings.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@
<Label text="Minecraft location" GridPane.rowIndex="4" />
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="4">
<children>
<Label fx:id="authDetailsVerifyLabel" layoutX="22.0" layoutY="6.0" prefHeight="17.0" prefWidth="90.0" text="xxx" textAlignment="CENTER" AnchorPane.leftAnchor="20.0" />
<Label fx:id="authDetailsVerifyLabel" layoutX="22.0" layoutY="6.0" prefHeight="17.0" prefWidth="90.0" textAlignment="CENTER" AnchorPane.leftAnchor="20.0" />
</children>
</AnchorPane>
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2">
<children>
<Label styleClass="label-err" fx:id="portVerifyLabel" layoutX="22.0" layoutY="6.0" prefHeight="17.0" prefWidth="90.0" textAlignment="CENTER" AnchorPane.leftAnchor="20.0" />
</children>
</AnchorPane>
</children>
</GridPane>
</children>
Expand Down Expand Up @@ -167,11 +172,11 @@
</rowConstraints>
<children>
<Label text="Minecraft username" />
<TextField fx:id="minecraftUsername" GridPane.columnIndex="2"/>
<TextField fx:id="minecraftUsername" GridPane.columnIndex="2" />

<Label text="Access token" GridPane.rowIndex="1"/>
<Hyperlink fx:id="authHelpLink" text="?" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField fx:id="accessToken" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Label text="Access token" GridPane.rowIndex="1" />
<Hyperlink fx:id="authHelpLink" text="?" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="accessToken" GridPane.columnIndex="2" GridPane.rowIndex="1" />
</children>
</GridPane>
</AnchorPane>
Expand Down

0 comments on commit 114e96b

Please sign in to comment.