Last active
March 24, 2023 12:24
-
-
Save hamza-cskn/9d36272ade9ebddd8903ed6241064cf1 to your computer and use it in GitHub Desktop.
Coordinate rounder for arenas of Bedwars1058. (mostly written by ChatGPT)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Usage:
javac CoordinateRounder.java
java CoordinateRounder
Your output files will be stored at
./out/