Skip to content

Instantly share code, notes, and snippets.

@hamza-cskn
Last active March 24, 2023 12:24
Show Gist options
  • Save hamza-cskn/9d36272ade9ebddd8903ed6241064cf1 to your computer and use it in GitHub Desktop.
Save hamza-cskn/9d36272ade9ebddd8903ed6241064cf1 to your computer and use it in GitHub Desktop.
Coordinate rounder for arenas of Bedwars1058. (mostly written by ChatGPT)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CoordinateRounder {
// Define regular expression to match coordinate string
private static final String COORD_PATTERN = "(-?\\d+\\.\\d+),(-?\\d+\\.\\d+),(-?\\d+\\.\\d+),(-?\\d+\\.\\d+),(-?\\d+\\.\\d+)";
// Define function to round pitch and yaw
private static String roundCoords(String coords) {
// Split the coordinates into individual values
String[] coordValues = coords.split(",");
// Convert string values to floating point numbers
double x = Double.parseDouble(coordValues[0]);
double y = Double.parseDouble(coordValues[1]);
double z = Double.parseDouble(coordValues[2]);
double yaw = Double.parseDouble(coordValues[3]);
double pitch = Double.parseDouble(coordValues[4]);
double roundedPitch = 0; //fix 0 degree
double roundedYaw = Math.round(yaw / 90) * 90;
// round x, y, and z to the nearest integer or half-integer
x = Math.round(x * 2) / 2.0;
y = Math.round(y * 2) / 2.0;
z = Math.round(z * 2) / 2.0;
// Combine the rounded coordinates into a string
String roundedCoords = String.format("%s,%s,%s,%s,%s", x, y, z, roundedYaw, roundedPitch);
return roundedCoords;
}
public static void main(String[] args) {
File cur = new File(".");
new File("out").mkdir();
for (File file : cur.listFiles()) {
if (!file.getName().endsWith(".yml")) continue;
write(file.getName(), "out/" + file.getName());
System.out.println(file.getName());
}
}
private static void write(String input, String output) {
// Open input file and create output file
File inputFile = new File("./" + input);
File outputFile = new File("./" + output);
try {
Scanner scanner = new Scanner(inputFile);
FileWriter writer = new FileWriter(outputFile);
// Read input file and extract coordinate strings using regular expression
Pattern pattern = Pattern.compile(COORD_PATTERN);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String coords = matcher.group();
String roundedCoords = roundCoords(coords);
// Write rounded coordinates to output file
writer.write(line.replace(coords, roundedCoords));
} else {
// If no match found, write original line to output file
writer.write(line);
}
writer.write("\n");
}
scanner.close();
writer.close();
} catch (FileNotFoundException e) {
System.out.println("Input file not found.");
} catch (IOException e) {
System.out.println("Error writing to output file.");
}
}
}
@hamza-cskn
Copy link
Author

hamza-cskn commented Mar 16, 2023

This code rounds all location datas of your arena file.
X, Y, Z values will be come as integers or half-integers.
Yaw values will be turned to 0,90,180 or 270.
Pitch values always be turned to 0.

Example Diff

129,132c129,132
<   - 12.340948487171008,27.0,-10.582272370916685,5.15997314453125,89.6462631225586
<   - -11.552414741776811,23.0,-10.309985330054463,180.26593017578125,-14.352709770202637
<   - -11.434891461353175,27.0,13.338894300126581,182.9833984375,86.46250915527344
<   - 12.431011421701102,23.0,13.478522354500523,274.95721435546875,89.29252624511719
---
>   - 12.5,27.0,-10.5,0.0,0.0
>   - -11.5,23.0,-10.5,180.0,0.0
>   - -11.5,27.0,13.5,180.0,0.0
>   - 12.5,23.0,13.5,270.0,0.0

Usage:

  1. Go you arenas folder.
  2. Write the code into a file named 'CoordinateRounder.java'
  3. use javac CoordinateRounder.java
  4. use java CoordinateRounder
    Your output files will be stored at ./out/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment