Skip to content

Add code format checking #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/check_formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Check Formatting

on: [pull_request]

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 23 for x64
uses: actions/setup-java@v4
with:
java-version: '23'
distribution: 'temurin'
architecture: x64
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
with:
gradle-version: 8.13
- name: Spotless Check
run: gradle spotlessCheck
15 changes: 14 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ Run the gradle task `runClient`
```sh
./gradlew build
```
2. The generated jar is in `build/libs`
2. The generated jar is in `build/libs`

# Formatting
The formatting configuration can be found in the [formatting directory](https://github.com/MinecraftTAS/TASmod/tree/develop/formatter) and can be imported into your IDE

Alternatively you can run the task
```sh
./gradlew spotlessCheck
```
To see if it fails and
```sh
./gradlew spotlessApply
```
to fix any formatting issues.
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
// legacy looming (loom plugin improvements)
id 'legacy-looming' version "${loom_version}"
id 'com.palantir.git-version' version '3.1.0'
id 'com.diffplug.spotless' version '7.0.3'
}


Expand Down Expand Up @@ -100,3 +101,13 @@ tasks.named('test', Test) {
events "passed", "skipped", "failed"
}
}

spotless {
encoding 'UTF-8'
lineEndings 'UNIX'
java {
importOrderFile('formatter/TASmodImportorder.txt')
eclipse().configFile('formatter/TASmodFormatter.xml')
}
enforceCheck false
}
404 changes: 404 additions & 0 deletions formatter/TASmodFormatter.xml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions formatter/TASmodImportorder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Organize Import Order
#Wed Apr 16 21:35:01 CEST 2025
0=java
1=javax
2=org
3=com
4 changes: 2 additions & 2 deletions src/main/java/com/minecrafttas/mctcommon/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import net.minecraft.server.MinecraftServer;

public class CommandRegistry {

public static void registerServerCommand(ICommand command, MinecraftServer server) {
CommandHandler ch = (CommandHandler) server.getCommandManager();
ch.registerCommand(command);
}

}
8 changes: 4 additions & 4 deletions src/main/java/com/minecrafttas/mctcommon/KeybindManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public KeybindManager(IsKeyDownFunc defaultFunction) {
*/
@Override
public void onRunClientGameLoop(Minecraft mc) {
for (Keybind keybind : this.keybindings){
for (Keybind keybind : this.keybindings) {
IsKeyDownFunc keyDown = keybind.isKeyDownFunc != null ? keybind.isKeyDownFunc : defaultFunction;
if(keyDown.isKeyDown(keybind.vanillaKeyBinding)){
if (keyDown.isKeyDown(keybind.vanillaKeyBinding)) {
keybind.onKeyDown.run();
}
}
Expand All @@ -105,10 +105,10 @@ public void registerKeybind(Keybind keybind) {
// add keybinding
options.keyBindings = ArrayUtils.add(options.keyBindings, keyBinding);
}

@FunctionalInterface
public static interface IsKeyDownFunc {

public boolean isKeyDown(KeyBinding keybind);
}
}
}
180 changes: 90 additions & 90 deletions src/main/java/com/minecrafttas/mctcommon/LanguageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,101 +24,101 @@

public class LanguageManager {

private static Set<String> modids = new HashSet<>();
private static final Splitter SPLITTER = Splitter.on('=').limit(2);
private static final Pattern PATTERN = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]");
private static Set<String> modids = new HashSet<>();
private static final Splitter SPLITTER = Splitter.on('=').limit(2);
private static final Pattern PATTERN = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]");

public static void onResourceManagerReload(Map<String, String> original, IResourceManager iResourceManager, List<String> languageList) {
for (String language : languageList) { // Go through all loaded languages
language = language.toLowerCase(); // Set everything to lowercase which prevents headaches in 1.10.2 and below
for (String modid : modids) { // Iterate through all registered modids
HashMap<String, String> newTranslations = new HashMap<>();
if (iResourceManager.getResourceDomains().contains(modid)) {
try {
newTranslations = getFromResourcePack(iResourceManager, modid, language); // Load .json translations from resource pack
} catch (IOException var9) {
}
}
if (newTranslations.isEmpty()) {
try {
newTranslations = loadLang(getFromResources(modid, language, "lang")); // Load .lang files from resources
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (newTranslations.isEmpty()) {
newTranslations = loadJson(getFromResources(modid, language, "json")); // Load .json translations from resources
}
/**
* Making this put if absent here creates the following hirarchy:
* Resourcepack .lang beats
* Resourcepack .json beats
* Resources .lang beats
* Resources .json
*
* Lang is preferred over json and resourcepacks are preferred over json
*/
newTranslations.forEach(original::putIfAbsent);
}
}
}
public static void onResourceManagerReload(Map<String, String> original, IResourceManager iResourceManager, List<String> languageList) {
for (String language : languageList) { // Go through all loaded languages
language = language.toLowerCase(); // Set everything to lowercase which prevents headaches in 1.10.2 and below
for (String modid : modids) { // Iterate through all registered modids
HashMap<String, String> newTranslations = new HashMap<>();
if (iResourceManager.getResourceDomains().contains(modid)) {
try {
newTranslations = getFromResourcePack(iResourceManager, modid, language); // Load .json translations from resource pack
} catch (IOException var9) {
}
}
if (newTranslations.isEmpty()) {
try {
newTranslations = loadLang(getFromResources(modid, language, "lang")); // Load .lang files from resources
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (newTranslations.isEmpty()) {
newTranslations = loadJson(getFromResources(modid, language, "json")); // Load .json translations from resources
}
/**
* Making this put if absent here creates the following hirarchy:
* Resourcepack .lang beats
* Resourcepack .json beats
* Resources .lang beats
* Resources .json
*
* Lang is preferred over json and resourcepacks are preferred over json
*/
newTranslations.forEach(original::putIfAbsent);
}
}
}

private static InputStream getFromResources(String resourceDomain, String language, String fileending) {
return LanguageManager.class.getResourceAsStream(String.format("/assets/%s/lang/%s.%s", resourceDomain, language, fileending));
}
private static InputStream getFromResources(String resourceDomain, String language, String fileending) {
return LanguageManager.class.getResourceAsStream(String.format("/assets/%s/lang/%s.%s", resourceDomain, language, fileending));
}

private static HashMap<String, String> getFromResourcePack(IResourceManager iResourceManager, String resourceDomain, String language) throws IOException {
String languageFile = String.format("lang/%s.json", language);
HashMap<String, String> out = new HashMap<>();
Collection<IResource> allResources = iResourceManager.getAllResources(new ResourceLocation(resourceDomain, languageFile));
for (IResource iResource : allResources) {
InputStream inputStream = iResource.getInputStream();
out.putAll(loadJson(inputStream));
}
return out;
}
private static HashMap<String, String> getFromResourcePack(IResourceManager iResourceManager, String resourceDomain, String language) throws IOException {
String languageFile = String.format("lang/%s.json", language);
HashMap<String, String> out = new HashMap<>();
Collection<IResource> allResources = iResourceManager.getAllResources(new ResourceLocation(resourceDomain, languageFile));
for (IResource iResource : allResources) {
InputStream inputStream = iResource.getInputStream();
out.putAll(loadJson(inputStream));
}
return out;
}

/**
* Registers your mod to be processed by the language manager<br>
* This will allow you to add .json and/or .lang files to assets/modid/lang<br>
* with en_us.lang/en_us.json (<strong>lowercase!</strong>)
*
* @param modid The modid of your mod
*/
public static void registerMod(String modid) {
modids.add(modid);
}
/**
* Registers your mod to be processed by the language manager<br>
* This will allow you to add .json and/or .lang files to assets/modid/lang<br>
* with en_us.lang/en_us.json (<strong>lowercase!</strong>)
*
* @param modid The modid of your mod
*/
public static void registerMod(String modid) {
modids.add(modid);
}

private static HashMap<String, String> loadJson(InputStream inputStream) {
if (inputStream == null) {
return new HashMap<String, String>();
}
Gson gson = new Gson();
HashMap<String, String> template = new HashMap<>();
@SuppressWarnings("unchecked")
private static HashMap<String, String> loadJson(InputStream inputStream) {
if (inputStream == null) {
return new HashMap<String, String>();
}
Gson gson = new Gson();
HashMap<String, String> template = new HashMap<>();

@SuppressWarnings("unchecked")
HashMap<String, String> out = (HashMap<String, String>) gson.fromJson(new InputStreamReader(inputStream), template.getClass());
out.forEach((key, value) -> {
value = PATTERN.matcher(value).replaceAll("%$1s");
});
return out;
}
out.forEach((key, value) -> {
value = PATTERN.matcher(value).replaceAll("%$1s");
});
return out;
}

private static HashMap<String, String> loadLang(InputStream inputStream) throws IOException {
HashMap<String, String> out = new HashMap<>();
if (inputStream == null) {
return out;
}
for (String string : IOUtils.readLines(inputStream, StandardCharsets.UTF_8)) {
if (!string.isEmpty() && string.charAt(0) != '#') {
String[] key_value_pair = Iterables.toArray(SPLITTER.split(string), String.class);
if (key_value_pair != null && key_value_pair.length == 2) {
String key = key_value_pair[0];
String value = PATTERN.matcher(key_value_pair[1]).replaceAll("%$1s");
out.put(key, value);
}
}
}
return out;
}
private static HashMap<String, String> loadLang(InputStream inputStream) throws IOException {
HashMap<String, String> out = new HashMap<>();
if (inputStream == null) {
return out;
}
for (String string : IOUtils.readLines(inputStream, StandardCharsets.UTF_8)) {
if (!string.isEmpty() && string.charAt(0) != '#') {
String[] key_value_pair = Iterables.toArray(SPLITTER.split(string), String.class);
if (key_value_pair != null && key_value_pair.length == 2) {
String key = key_value_pair[0];
String value = PATTERN.matcher(key_value_pair[1]).replaceAll("%$1s");
out.put(key, value);
}
}
}
return out;
}
}
Loading
Loading