Skip to content

Commit

Permalink
🎨 Java formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Dec 28, 2024
1 parent 2127e4a commit a42d869
Show file tree
Hide file tree
Showing 30 changed files with 8,905 additions and 6,630 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/java-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Java Format Check

on:
pull_request:
paths:
- '**/*.java'

jobs:
format-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install google-java-format
run: |
curl -Lo google-java-format.jar https://github.com/google/google-java-format/releases/download/v1.17.0/google-java-format-1.17.0-all-deps.jar
- name: Check formatting
run: |
java -jar google-java-format.jar --dry-run --set-exit-if-changed $(find . -name '*.java')
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ https://dejavu-fonts.github.io/License.html
# Formatting
## Install formatters
```zsh
brew install ktlint clang-format
brew install ktlint clang-format google-java-format
```
```bash
find . \( -iname '*.h' -o -iname '*.cpp' -o -iname '*.mm' \) | xargs clang-format -i
ktlint '!src/craftground/MinecraftEnv/src/main/java/com/kyhsgeekcode/minecraftenv/proto/**'
find . -name '*.java' -print0 | xargs -0 -P 4 google-java-format -i
```
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.kyhsgeekcode.minecraftenv;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public interface AddListenerInterface {
List<EntityRenderListener> listeners = new ArrayList<>();
List<EntityRenderListener> listeners = new ArrayList<>();

default void addRenderListener(@NotNull EntityRenderListener listener) {
listeners.add(listener);
}
default void addRenderListener(@NotNull EntityRenderListener listener) {
listeners.add(listener);
}

default List<EntityRenderListener> getRenderListeners() {
return listeners;
}
default List<EntityRenderListener> getRenderListeners() {
return listeners;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.List;

public interface GetMessagesInterface {
ArrayList<String> lastDeathMessage = new ArrayList<>();
ArrayList<String> lastDeathMessage = new ArrayList<>();

default List<String> getLastDeathMessage() {
return lastDeathMessage;
}
}
default List<String> getLastDeathMessage() {
return lastDeathMessage;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.kyhsgeekcode.minecraftenv;

public record Point3D(int x, int y, int z) {

}
public record Point3D(int x, int y, int z) {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import net.fabricmc.api.ClientModInitializer;

public class Minecraft_envClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
System.out.println("Hello Fabric world! client");
var ld_preload = System.getenv("LD_PRELOAD");
if (ld_preload != null) {
System.out.println("LD_PRELOAD is set: " + ld_preload);
} else {
System.out.println("LD_PRELOAD is not set");
}
@Override
public void onInitializeClient() {
System.out.println("Hello Fabric world! client");
var ld_preload = System.getenv("LD_PRELOAD");
if (ld_preload != null) {
System.out.println("LD_PRELOAD is set: " + ld_preload);
} else {
System.out.println("LD_PRELOAD is not set");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.kyhsgeekcode.minecraftenv.mixin;

import java.util.List;
import net.minecraft.client.gui.hud.ChatHudLine;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.List;

@Mixin(net.minecraft.client.gui.hud.ChatHud.class)
public interface ChatVisibleMessageAccessor {
@Accessor("visibleMessages")
List<ChatHudLine.Visible> getVisibleMessages();
@Accessor("visibleMessages")
List<ChatHudLine.Visible> getVisibleMessages();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;


@Mixin(net.minecraft.client.network.ClientCommonNetworkHandler.class)
public interface ClientCommonNetworkHandlerClientAccessor {
@Accessor("client")
MinecraftClient getClient();
}
@Accessor("client")
MinecraftClient getClient();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

@Mixin(net.minecraft.client.MinecraftClient.class)
public interface ClientEntityRenderDispatcherAccessor {
@Accessor("entityRenderDispatcher")
EntityRenderDispatcher getEntityRenderDispatcher();
@Accessor("entityRenderDispatcher")
EntityRenderDispatcher getEntityRenderDispatcher();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

@Mixin(MinecraftClient.class)
public class ClientIsWindowFocusedMixin {
@Overwrite
public boolean isWindowFocused() {
return true;
}
@Overwrite
public boolean isWindowFocused() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

@Mixin(ClientPlayNetworkHandler.class)
public class ClientPlayNetworkHandlerMixin implements GetMessagesInterface {
@Shadow
private ClientWorld world;
@Shadow private ClientWorld world;

@Inject(method = "onDeathMessage", at = @At("HEAD"), cancellable = false)
public void onDeathMessage(DeathMessageS2CPacket packet, CallbackInfo ci) {
Entity entity = this.world.getEntityById(packet.playerId());
if (entity == ((ClientCommonNetworkHandlerClientAccessor) this).getClient().player) {
var message = packet.message();
this.lastDeathMessage.clear();
this.lastDeathMessage.add(message.getString());
}
@Inject(method = "onDeathMessage", at = @At("HEAD"), cancellable = false)
public void onDeathMessage(DeathMessageS2CPacket packet, CallbackInfo ci) {
Entity entity = this.world.getEntityById(packet.playerId());
if (entity == ((ClientCommonNetworkHandlerClientAccessor) this).getClient().player) {
var message = packet.message();
this.lastDeathMessage.clear();
this.lastDeathMessage.add(message.getString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;


@Mixin(MinecraftClient.class)
public interface ClientRenderInvoker {
@Invoker("render")
void invokeRender(boolean tick);
}
@Invoker("render")
void invokeRender(boolean tick);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@

@Mixin(WorldRenderer.class)
public class ClientWorldMixin {
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/world/ClientWorld;getEntities()Ljava/lang/Iterable;"))
private Iterable<Entity> getEntities(net.minecraft.client.world.ClientWorld clientWorld) {
var listeners = ((AddListenerInterface) this).getRenderListeners();
for (var listener : listeners) {
listener.clear();
}
return clientWorld.getEntities();
@Redirect(
method = "render",
at =
@At(
value = "INVOKE",
target = "Lnet/minecraft/client/world/ClientWorld;getEntities()Ljava/lang/Iterable;"))
private Iterable<Entity> getEntities(net.minecraft.client.world.ClientWorld clientWorld) {
var listeners = ((AddListenerInterface) this).getRenderListeners();
for (var listener : listeners) {
listener.clear();
}
return clientWorld.getEntities();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

@Mixin(ReloadableResourceManagerImpl.class)
public class DisableRegionalComplianceMixin {
@Inject(method = "registerReloader", at = @At("HEAD"), cancellable = true)
private void registerReloader(ResourceReloader reloader, CallbackInfo ci) {
if (reloader instanceof PeriodicNotificationManager) {
ci.cancel(); // cancel the reload
}
@Inject(method = "registerReloader", at = @At("HEAD"), cancellable = true)
private void registerReloader(ResourceReloader reloader, CallbackInfo ci) {
if (reloader instanceof PeriodicNotificationManager) {
ci.cancel(); // cancel the reload
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,24 @@
@Mixin(SimpleOption.class)
public class GammaMixin<T> {

@Shadow
@Final
Text text;
@Shadow @Final Text text;

@Shadow
T value;
@Shadow T value;

/**
* Mixin to allow saving "invalid" gamma values into the options file
*/
@Inject(method = "getCodec", at = @At("HEAD"), cancellable = true)
private void returnFakeCodec(CallbackInfoReturnable<Codec<Double>> info) {
if (text.getString().equals(I18n.translate("options.gamma"))) {
info.setReturnValue(Codec.DOUBLE);
}
/** Mixin to allow saving "invalid" gamma values into the options file */
@Inject(method = "getCodec", at = @At("HEAD"), cancellable = true)
private void returnFakeCodec(CallbackInfoReturnable<Codec<Double>> info) {
if (text.getString().equals(I18n.translate("options.gamma"))) {
info.setReturnValue(Codec.DOUBLE);
}
}

/**
* Mixin to allow setting "invalid" gamma values
*/
@Inject(method = "setValue", at = @At("HEAD"), cancellable = true)
private void setRealValue(T value, CallbackInfo info) {
if (text.getString().equals(I18n.translate("options.gamma"))) {
this.value = value;
info.cancel();
}
/** Mixin to allow setting "invalid" gamma values */
@Inject(method = "setValue", at = @At("HEAD"), cancellable = true)
private void setRealValue(T value, CallbackInfo info) {
if (text.getString().equals(I18n.translate("options.gamma"))) {
this.value = value;
info.cancel();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,34 @@

@Mixin(net.minecraft.client.util.InputUtil.class)
public class InputUtilMixin {
@Overwrite
public static boolean isKeyPressed(long handle, int code) {
return KeyboardInfo.INSTANCE.isKeyPressed(code);
}
@Overwrite
public static boolean isKeyPressed(long handle, int code) {
return KeyboardInfo.INSTANCE.isKeyPressed(code);
}

@Overwrite
public static void setMouseCallbacks(long handle, GLFWCursorPosCallbackI cursorPosCallback, GLFWMouseButtonCallbackI mouseButtonCallback, GLFWScrollCallbackI scrollCallback, GLFWDropCallbackI dropCallback) {
MouseInfo.INSTANCE.setCursorPosCallback(cursorPosCallback);
MouseInfo.INSTANCE.setMouseButtonCallback(mouseButtonCallback);
MouseInfo.INSTANCE.setHandle(handle);
}
@Overwrite
public static void setMouseCallbacks(
long handle,
GLFWCursorPosCallbackI cursorPosCallback,
GLFWMouseButtonCallbackI mouseButtonCallback,
GLFWScrollCallbackI scrollCallback,
GLFWDropCallbackI dropCallback) {
MouseInfo.INSTANCE.setCursorPosCallback(cursorPosCallback);
MouseInfo.INSTANCE.setMouseButtonCallback(mouseButtonCallback);
MouseInfo.INSTANCE.setHandle(handle);
}

@Overwrite
public static void setCursorParameters(long handler, int inputModeValue, double x, double y) {
MouseInfo.INSTANCE.setCursorPos(x, y);
MouseInfo.INSTANCE.setCursorShown(inputModeValue == GLFW.GLFW_CURSOR_NORMAL);
}
@Overwrite
public static void setCursorParameters(long handler, int inputModeValue, double x, double y) {
MouseInfo.INSTANCE.setCursorPos(x, y);
MouseInfo.INSTANCE.setCursorShown(inputModeValue == GLFW.GLFW_CURSOR_NORMAL);
}

@Overwrite
public static void setKeyboardCallbacks(long handle, GLFWKeyCallbackI keyCallback, GLFWCharModsCallbackI charModsCallback) {
KeyboardInfo.INSTANCE.setKeyCallback(keyCallback);
KeyboardInfo.INSTANCE.setCharModsCallback(charModsCallback);
KeyboardInfo.INSTANCE.setHandle(handle);
}
@Overwrite
public static void setKeyboardCallbacks(
long handle, GLFWKeyCallbackI keyCallback, GLFWCharModsCallbackI charModsCallback) {
KeyboardInfo.INSTANCE.setKeyCallback(keyCallback);
KeyboardInfo.INSTANCE.setCharModsCallback(charModsCallback);
KeyboardInfo.INSTANCE.setHandle(handle);
}
}
Loading

0 comments on commit a42d869

Please sign in to comment.