Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenh2905 authored Jan 21, 2022
1 parent ea7fa75 commit a871dc6
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mrsky</groupId>
<artifactId>Test_1</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20211205</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>All</shadedClassifierName>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mrsky.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
127 changes: 127 additions & 0 deletions src/main/java/com/mrsky/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.mrsky;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.json.JSONObject;

import javax.swing.*;
import java.awt.*;

public class App extends Application {
private static volatile long estimate = -1;
private static volatile boolean alarmCanceled = false;
private static final Toolkit toolkit = Toolkit.getDefaultToolkit();

@Override
public void start(Stage primaryStage) {
try {
final Pane root = new Pane();

final CircularProgressbar circularProgressbar = new CircularProgressbar(200, 10, 3);
circularProgressbar.draw(0);
circularProgressbar.relocate(0, 0);

final Text text = new Text("Status: NaN");
text.relocate(210, 120);

final Button button = new Button("Cancel Alarm");
button.setStyle("-fx-background-color: #A1E7FF;-fx-border-color: #000000;");
button.setOnMouseClicked(this::onMouseClicked);
button.onMousePressedProperty().set(event -> button.setStyle("-fx-background-color: #37C5FF;-fx-border-color: #000000;"));
button.onMouseReleasedProperty().set(event -> button.setStyle("-fx-background-color: #A1E7FF;-fx-border-color: #000000;"));
button.relocate(210, 150);

root.getChildren().addAll(circularProgressbar, text, button);
root.setStyle("-fx-background-color: #BFECFF");
Scene scene = new Scene(root, 350, 200);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.setOnCloseRequest(event -> {
System.exit(0);
});
primaryStage.setTitle("Inventivetalent Hypixel Skyblock DarkAuction Timer");
primaryStage.show();
final Thread updateTimeThread = new Thread(() -> updateTime(text));
updateTimeThread.start();
final Thread thread = new Thread(() -> {
try {
boolean alarm = true;
long time;
while (true) {
if (estimate == -1) {
Thread.sleep(10000);
continue;
}
time = (estimate - System.currentTimeMillis()) / 1000;
if (time < 0) {
estimate = -1;
continue;
}
text.setText("Status: " + (time / 60) + " min " + (time % 60) + " sec");
if ((time / 60) < 5 && alarm) {
alarmCanceled = false;
while (!alarmCanceled) {
toolkit.beep();
Thread.sleep(500);
}
alarm = false;
} else {
if (!((time / 60) < 5)) {
alarm = true;
}
}
circularProgressbar.draw(time / 3600f * 100);
Thread.sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
});
thread.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}

private void onMouseClicked(MouseEvent mouseEvent) {
alarmCanceled = true;
}

private static synchronized void updateTime(final Text text) {
try {
while (true) {
final String response = WebRequest.request();
if (response == null) {
System.out.println("ERROR: \"WebRequest.request()\" returns NULL");
text.setText("Status: failed to get data");
} else {
final JSONObject jsonObject = new JSONObject(response);
final boolean success = (boolean) jsonObject.get("success");
text.setText("Status: success = " + success);
if (success) {
//final long queryTime = (long) jsonObject.get("queryTime");
estimate = (long) jsonObject.get("estimate");
} else {
System.out.println("ERROR: \"success\" is false");
text.setText("Status: \"success\" is false");
}
}
Thread.sleep(30000);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}

public static void main(String[] args) {
launch(args);
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/mrsky/CircularProgressbar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.mrsky;

import javafx.geometry.VPos;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import javafx.scene.text.TextAlignment;

public class CircularProgressbar extends Canvas {
private final Color progressColor;
private final Color centerBorderColor;
private final Color centerColor;
private final Color fontColor;
private final double progressWith;
private final double centerBorderWith;

public CircularProgressbar(double radius, double progressWith, double centerBorderWith) {
super(radius, radius);
this.progressWith = progressWith;
this.centerBorderWith = centerBorderWith;
final GraphicsContext gc = getGraphicsContext2D();
gc.setTextAlign(TextAlignment.CENTER);
gc.setTextBaseline(VPos.CENTER);
progressColor = Color.web("#FFAA00");
centerBorderColor = Color.web("#FF4100");
centerColor = Color.web("#FFFFFF");
fontColor = Color.web("#000000");
}

public void draw(float percent) {
percent = Math.max(Math.min(percent, 100), 0);
percent = Float.parseFloat(String.format("%.1f", percent));
final GraphicsContext gc = getGraphicsContext2D();
gc.clearRect(0, 0, getWidth(), getHeight());

gc.setFill(progressColor);
gc.fillArc(0, 0, getWidth(), getHeight(), 90, -(percent / 100 * 360), ArcType.ROUND);

gc.setFill(centerBorderColor);
gc.fillArc(progressWith, progressWith, getWidth() - (progressWith * 2), getHeight() - (progressWith * 2), 0, -360, ArcType.ROUND);

gc.setFill(centerColor);
gc.fillArc(progressWith + centerBorderWith, progressWith + centerBorderWith, getWidth() - ((progressWith + centerBorderWith) * 2), getHeight() - ((progressWith + centerBorderWith) * 2), 0, -360, ArcType.ROUND);

gc.setFill(fontColor);
gc.fillText(percent + " %", getWidth() / 2, getHeight() / 2);
}
}
77 changes: 77 additions & 0 deletions src/main/java/com/mrsky/WebRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.mrsky;

import javax.net.ssl.SSLSocketFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class WebRequest {
public static synchronized String request() {
try {
// https://hypixel-api.inventivetalent.org/api/skyblock/darkauction/estimate
final Socket socket = SSLSocketFactory.getDefault().createSocket();
socket.setSoTimeout(2000);
socket.connect(new InetSocketAddress("hypixel-api.inventivetalent.org", 443), 2000);
final InputStream inputStream = socket.getInputStream();
final OutputStream outputStream = socket.getOutputStream();
outputStream.write(("GET /api/skyblock/darkauction/estimate HTTP/1.1\nHOST: hypixel-api.inventivetalent.org\n\n").getBytes(StandardCharsets.UTF_8));
outputStream.flush();
final byte[] singleBuffer = new byte[1];
int length;
String lastChar = "";
String current;
final StringBuilder builder = new StringBuilder();
while ((length = inputStream.read(singleBuffer)) > 0) {
current = new String(singleBuffer, 0, length);
if (current.equals("\r")) {
continue;
}
builder.append(current);
if (lastChar.equals("\n") && current.equals("\n")) {
break;
}
lastChar = current;
}
int content_length = 0;
for (String s : builder.toString().split("\n")) {
s = s.trim().toLowerCase();
if (s.startsWith("content-length:")) {
content_length = Integer.parseInt(s.split("content-length:")[1].trim());
break;
}
}
if (content_length == 0) {
System.out.println("Can't get content_length");
return null;
}
final byte[] data = new byte[content_length];
final byte[] buffer = new byte[10];
int all = 0;
while ((length = inputStream.read(buffer)) > 0) {
System.arraycopy(buffer, 0, data, all, length);
all += length;
if (content_length <= all) {
break;
}
}
try {
inputStream.close();
} catch (Exception ignored) {
}
try {
outputStream.close();
} catch (Exception ignored) {
}
try {
socket.close();
} catch (Exception ignored) {
}
return new String(data, 0, data.length, StandardCharsets.UTF_8);
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
return null;
}
}

0 comments on commit a871dc6

Please sign in to comment.