Skip to content

Commit 4cebcef

Browse files
authored
Add code format checking (#235)
- Adds spotless - Adds my current code formatter to the repo - Adds the import groupings - Add workflow for checking the formatting - Add notice to CONTRIBUTING.md - [Savestates] Move SavestateMotionStorage into builtin package
2 parents da2429c + 26c5b6f commit 4cebcef

File tree

82 files changed

+2041
-1575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2041
-1575
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This workflow will build a Java project with Gradle
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
3+
4+
name: Check Formatting
5+
6+
on: [pull_request]
7+
8+
jobs:
9+
check:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up JDK 23 for x64
14+
uses: actions/setup-java@v4
15+
with:
16+
java-version: '23'
17+
distribution: 'temurin'
18+
architecture: x64
19+
- name: Setup Gradle
20+
uses: gradle/actions/setup-gradle@v3
21+
with:
22+
gradle-version: 8.13
23+
- name: Spotless Check
24+
run: gradle spotlessCheck

CONTRIBUTING.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ Run the gradle task `runClient`
2828
```sh
2929
./gradlew build
3030
```
31-
2. The generated jar is in `build/libs`
31+
2. The generated jar is in `build/libs`
32+
33+
# Formatting
34+
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
35+
36+
Alternatively you can run the task
37+
```sh
38+
./gradlew spotlessCheck
39+
```
40+
To see if it fails and
41+
```sh
42+
./gradlew spotlessApply
43+
```
44+
to fix any formatting issues.

build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
// legacy looming (loom plugin improvements)
55
id 'legacy-looming' version "${loom_version}"
66
id 'com.palantir.git-version' version '3.1.0'
7+
id 'com.diffplug.spotless' version '7.0.3'
78
}
89

910

@@ -100,3 +101,13 @@ tasks.named('test', Test) {
100101
events "passed", "skipped", "failed"
101102
}
102103
}
104+
105+
spotless {
106+
encoding 'UTF-8'
107+
lineEndings 'UNIX'
108+
java {
109+
importOrderFile('formatter/TASmodImportorder.txt')
110+
eclipse().configFile('formatter/TASmodFormatter.xml')
111+
}
112+
enforceCheck false
113+
}

formatter/TASmodFormatter.xml

Lines changed: 404 additions & 0 deletions
Large diffs are not rendered by default.

formatter/TASmodImportorder.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Organize Import Order
2+
#Wed Apr 16 21:35:01 CEST 2025
3+
0=java
4+
1=javax
5+
2=org
6+
3=com

src/main/java/com/minecrafttas/mctcommon/CommandRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import net.minecraft.server.MinecraftServer;
66

77
public class CommandRegistry {
8-
8+
99
public static void registerServerCommand(ICommand command, MinecraftServer server) {
1010
CommandHandler ch = (CommandHandler) server.getCommandManager();
1111
ch.registerCommand(command);
1212
}
13-
13+
1414
}

src/main/java/com/minecrafttas/mctcommon/KeybindManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public KeybindManager(IsKeyDownFunc defaultFunction) {
7979
*/
8080
@Override
8181
public void onRunClientGameLoop(Minecraft mc) {
82-
for (Keybind keybind : this.keybindings){
82+
for (Keybind keybind : this.keybindings) {
8383
IsKeyDownFunc keyDown = keybind.isKeyDownFunc != null ? keybind.isKeyDownFunc : defaultFunction;
84-
if(keyDown.isKeyDown(keybind.vanillaKeyBinding)){
84+
if (keyDown.isKeyDown(keybind.vanillaKeyBinding)) {
8585
keybind.onKeyDown.run();
8686
}
8787
}
@@ -105,10 +105,10 @@ public void registerKeybind(Keybind keybind) {
105105
// add keybinding
106106
options.keyBindings = ArrayUtils.add(options.keyBindings, keyBinding);
107107
}
108-
108+
109109
@FunctionalInterface
110110
public static interface IsKeyDownFunc {
111111

112112
public boolean isKeyDown(KeyBinding keybind);
113113
}
114-
}
114+
}

src/main/java/com/minecrafttas/mctcommon/LanguageManager.java

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -24,101 +24,101 @@
2424

2525
public class LanguageManager {
2626

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

31-
public static void onResourceManagerReload(Map<String, String> original, IResourceManager iResourceManager, List<String> languageList) {
32-
for (String language : languageList) { // Go through all loaded languages
33-
language = language.toLowerCase(); // Set everything to lowercase which prevents headaches in 1.10.2 and below
34-
for (String modid : modids) { // Iterate through all registered modids
35-
HashMap<String, String> newTranslations = new HashMap<>();
36-
if (iResourceManager.getResourceDomains().contains(modid)) {
37-
try {
38-
newTranslations = getFromResourcePack(iResourceManager, modid, language); // Load .json translations from resource pack
39-
} catch (IOException var9) {
40-
}
41-
}
42-
if (newTranslations.isEmpty()) {
43-
try {
44-
newTranslations = loadLang(getFromResources(modid, language, "lang")); // Load .lang files from resources
45-
} catch (IOException e) {
46-
throw new RuntimeException(e);
47-
}
48-
}
49-
if (newTranslations.isEmpty()) {
50-
newTranslations = loadJson(getFromResources(modid, language, "json")); // Load .json translations from resources
51-
}
52-
/**
53-
* Making this put if absent here creates the following hirarchy:
54-
* Resourcepack .lang beats
55-
* Resourcepack .json beats
56-
* Resources .lang beats
57-
* Resources .json
58-
*
59-
* Lang is preferred over json and resourcepacks are preferred over json
60-
*/
61-
newTranslations.forEach(original::putIfAbsent);
62-
}
63-
}
64-
}
31+
public static void onResourceManagerReload(Map<String, String> original, IResourceManager iResourceManager, List<String> languageList) {
32+
for (String language : languageList) { // Go through all loaded languages
33+
language = language.toLowerCase(); // Set everything to lowercase which prevents headaches in 1.10.2 and below
34+
for (String modid : modids) { // Iterate through all registered modids
35+
HashMap<String, String> newTranslations = new HashMap<>();
36+
if (iResourceManager.getResourceDomains().contains(modid)) {
37+
try {
38+
newTranslations = getFromResourcePack(iResourceManager, modid, language); // Load .json translations from resource pack
39+
} catch (IOException var9) {
40+
}
41+
}
42+
if (newTranslations.isEmpty()) {
43+
try {
44+
newTranslations = loadLang(getFromResources(modid, language, "lang")); // Load .lang files from resources
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
}
49+
if (newTranslations.isEmpty()) {
50+
newTranslations = loadJson(getFromResources(modid, language, "json")); // Load .json translations from resources
51+
}
52+
/**
53+
* Making this put if absent here creates the following hirarchy:
54+
* Resourcepack .lang beats
55+
* Resourcepack .json beats
56+
* Resources .lang beats
57+
* Resources .json
58+
*
59+
* Lang is preferred over json and resourcepacks are preferred over json
60+
*/
61+
newTranslations.forEach(original::putIfAbsent);
62+
}
63+
}
64+
}
6565

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

70-
private static HashMap<String, String> getFromResourcePack(IResourceManager iResourceManager, String resourceDomain, String language) throws IOException {
71-
String languageFile = String.format("lang/%s.json", language);
72-
HashMap<String, String> out = new HashMap<>();
73-
Collection<IResource> allResources = iResourceManager.getAllResources(new ResourceLocation(resourceDomain, languageFile));
74-
for (IResource iResource : allResources) {
75-
InputStream inputStream = iResource.getInputStream();
76-
out.putAll(loadJson(inputStream));
77-
}
78-
return out;
79-
}
70+
private static HashMap<String, String> getFromResourcePack(IResourceManager iResourceManager, String resourceDomain, String language) throws IOException {
71+
String languageFile = String.format("lang/%s.json", language);
72+
HashMap<String, String> out = new HashMap<>();
73+
Collection<IResource> allResources = iResourceManager.getAllResources(new ResourceLocation(resourceDomain, languageFile));
74+
for (IResource iResource : allResources) {
75+
InputStream inputStream = iResource.getInputStream();
76+
out.putAll(loadJson(inputStream));
77+
}
78+
return out;
79+
}
8080

81-
/**
82-
* Registers your mod to be processed by the language manager<br>
83-
* This will allow you to add .json and/or .lang files to assets/modid/lang<br>
84-
* with en_us.lang/en_us.json (<strong>lowercase!</strong>)
85-
*
86-
* @param modid The modid of your mod
87-
*/
88-
public static void registerMod(String modid) {
89-
modids.add(modid);
90-
}
81+
/**
82+
* Registers your mod to be processed by the language manager<br>
83+
* This will allow you to add .json and/or .lang files to assets/modid/lang<br>
84+
* with en_us.lang/en_us.json (<strong>lowercase!</strong>)
85+
*
86+
* @param modid The modid of your mod
87+
*/
88+
public static void registerMod(String modid) {
89+
modids.add(modid);
90+
}
9191

92-
private static HashMap<String, String> loadJson(InputStream inputStream) {
93-
if (inputStream == null) {
94-
return new HashMap<String, String>();
95-
}
96-
Gson gson = new Gson();
97-
HashMap<String, String> template = new HashMap<>();
98-
99-
@SuppressWarnings("unchecked")
92+
private static HashMap<String, String> loadJson(InputStream inputStream) {
93+
if (inputStream == null) {
94+
return new HashMap<String, String>();
95+
}
96+
Gson gson = new Gson();
97+
HashMap<String, String> template = new HashMap<>();
98+
99+
@SuppressWarnings("unchecked")
100100
HashMap<String, String> out = (HashMap<String, String>) gson.fromJson(new InputStreamReader(inputStream), template.getClass());
101-
out.forEach((key, value) -> {
102-
value = PATTERN.matcher(value).replaceAll("%$1s");
103-
});
104-
return out;
105-
}
101+
out.forEach((key, value) -> {
102+
value = PATTERN.matcher(value).replaceAll("%$1s");
103+
});
104+
return out;
105+
}
106106

107-
private static HashMap<String, String> loadLang(InputStream inputStream) throws IOException {
108-
HashMap<String, String> out = new HashMap<>();
109-
if (inputStream == null) {
110-
return out;
111-
}
112-
for (String string : IOUtils.readLines(inputStream, StandardCharsets.UTF_8)) {
113-
if (!string.isEmpty() && string.charAt(0) != '#') {
114-
String[] key_value_pair = Iterables.toArray(SPLITTER.split(string), String.class);
115-
if (key_value_pair != null && key_value_pair.length == 2) {
116-
String key = key_value_pair[0];
117-
String value = PATTERN.matcher(key_value_pair[1]).replaceAll("%$1s");
118-
out.put(key, value);
119-
}
120-
}
121-
}
122-
return out;
123-
}
107+
private static HashMap<String, String> loadLang(InputStream inputStream) throws IOException {
108+
HashMap<String, String> out = new HashMap<>();
109+
if (inputStream == null) {
110+
return out;
111+
}
112+
for (String string : IOUtils.readLines(inputStream, StandardCharsets.UTF_8)) {
113+
if (!string.isEmpty() && string.charAt(0) != '#') {
114+
String[] key_value_pair = Iterables.toArray(SPLITTER.split(string), String.class);
115+
if (key_value_pair != null && key_value_pair.length == 2) {
116+
String key = key_value_pair[0];
117+
String value = PATTERN.matcher(key_value_pair[1]).replaceAll("%$1s");
118+
out.put(key, value);
119+
}
120+
}
121+
}
122+
return out;
123+
}
124124
}

0 commit comments

Comments
 (0)