Skip to content

Commit

Permalink
style: add checkstyle and corresponding ci check
Browse files Browse the repository at this point in the history
  • Loading branch information
YanWQ-monad committed Aug 9, 2022
1 parent ed7bfbc commit 2236f1c
Show file tree
Hide file tree
Showing 11 changed files with 644 additions and 184 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ jobs:
with:
name: Artifacts
path: build/libs/

checkstyle:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: checkstyle
uses: nikitasavinov/checkstyle-action@0.6.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
checkstyle_config: "checkstyle.xml"
reporter: ${{ github.event_name == 'pull_request' && 'github-pr-check' || 'github-check' }}
431 changes: 431 additions & 0 deletions checkstyle.xml

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version = 1.19
yarn_mappings = 1.19+build.1
loader_version = 0.14.6
# check these on https://fabricmc.net/develop
minecraft_version = 1.19
yarn_mappings = 1.19+build.1
loader_version = 0.14.6

# Mod Properties
mod_version = 1.1.1
maven_group = com.imyvm
archives_base_name = hoki
mod_version = 1.1.1
maven_group = com.imyvm
archives_base_name = hoki

# Dependencies
fabric_version = 0.55.1+1.19
typesafe_version = 1.4.2
fabric_version = 0.55.1+1.19
typesafe_version = 1.4.2
12 changes: 6 additions & 6 deletions src/main/java/com/imyvm/hoki/HokiMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import org.slf4j.LoggerFactory;

public class HokiMod implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("Hoki");
public static final Logger LOGGER = LoggerFactory.getLogger("Hoki");

@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register(PlayerUtil::initialize);
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register(PlayerUtil::initialize);

LOGGER.info("Hoki mod initialize");
}
LOGGER.info("Hoki mod initialize");
}
}
21 changes: 13 additions & 8 deletions src/main/java/com/imyvm/hoki/config/Option.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.imyvm.hoki.config;

import com.typesafe.config.*;
import com.typesafe.config.*; // CHECKSTYLE SUPPRESS: AvoidStarImport
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import org.jetbrains.annotations.NotNull;
Expand All @@ -21,21 +21,26 @@ public class Option<T> {
@NotNull private final BiFunction<Config, String, T> deserializer;
@NotNull private final Function<T, ConfigValue> serializer;

@NotNull public final Event<OptionChangeCallback<T>> changeEvents = EventFactory.createArrayBacked(OptionChangeCallback.class,
(listeners) -> (option, oldValue, newValue) -> {
for (OptionChangeCallback<T> listener : listeners)
listener.onChange(option, oldValue, newValue);
});
// CHECKSTYLE SUPPRESS: DeclarationOrder
@NotNull public final Event<OptionChangeCallback<T>> changeEvents =
EventFactory.createArrayBacked(
OptionChangeCallback.class,
(listeners) -> (option, oldValue, newValue) -> {
for (OptionChangeCallback<T> listener : listeners)
listener.onChange(option, oldValue, newValue);
});

public Option(@NotNull String key, @NotNull T defaultValue, @Nullable String comment, @NotNull BiFunction<Config, String, T> deserializer, @NotNull Function<T, ConfigValue> serializer) {
public Option(@NotNull String key, @NotNull T defaultValue, @Nullable String comment,
@NotNull BiFunction<Config, String, T> deserializer, @NotNull Function<T, ConfigValue> serializer) {
this.key = key;
this.defaultValue = defaultValue;
this.comments = comment == null ? null : Arrays.asList(comment.split("\n"));
this.deserializer = deserializer;
this.serializer = serializer;
}

public Option(@NotNull String key, @NotNull T defaultValue, @Nullable String comment, @NotNull BiFunction<Config, String, T> deserializer) {
public Option(@NotNull String key, @NotNull T defaultValue, @Nullable String comment,
@NotNull BiFunction<Config, String, T> deserializer) {
this(key, defaultValue, comment, deserializer, ConfigValueFactory::fromAnyRef);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/imyvm/hoki/i18n/HokiTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// CHECKSTYLE SUPPRESS: HideUtilityClassConstructor
public class HokiTranslator {
private static final String FORMAT_PLACEHOLDER = "(?<!\\{)\\{(\\d+)}";
private static final Pattern FORMAT_PATTERN = Pattern.compile(FORMAT_PLACEHOLDER);
Expand Down
141 changes: 2 additions & 139 deletions src/main/java/com/imyvm/hoki/nbt/NbtPersistent.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package com.imyvm.hoki.nbt;

import com.google.common.collect.ImmutableMap;
import net.minecraft.nbt.*;
import org.apache.logging.log4j.util.TriConsumer;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

public interface NbtPersistent {
@Nullable
default NbtElement serialize() {
Expand All @@ -24,133 +17,3 @@ default void deserialize(@Nullable NbtElement element) {
}
}
}

class NbtPersistentHelper {
private static final Map<Class<?>, ValueType> simpleTypes = buildValueTypes();

private static Map<Class<?>, ValueType> buildValueTypes() {
ImmutableMap.Builder<Class<?>, ValueType> map = ImmutableMap.builder();

TriConsumer<Class<?>, Class<?>, ValueType> putPrimitiveType = (primitive, wrapper, valueType) -> {
map.put(primitive, valueType);
map.put(wrapper, valueType);
};
putPrimitiveType.accept(byte.class, Byte.class, ValueType.of(NbtByte::of, (element) -> ((AbstractNbtNumber) element).byteValue()));
putPrimitiveType.accept(short.class, Short.class, ValueType.of(NbtShort::of, (element) -> ((AbstractNbtNumber) element).shortValue()));
putPrimitiveType.accept(char.class, Character.class, ValueType.of((c) -> NbtShort.of((short) (char) c), (element) -> (char) ((AbstractNbtNumber) element).shortValue()));
putPrimitiveType.accept(int.class, Integer.class, ValueType.of(NbtInt::of, (element) -> ((AbstractNbtNumber) element).intValue()));
putPrimitiveType.accept(long.class, Long.class, ValueType.of(NbtLong::of, (element) -> ((AbstractNbtNumber) element).longValue()));
putPrimitiveType.accept(float.class, Float.class, ValueType.of(NbtFloat::of, (element) -> ((AbstractNbtNumber) element).floatValue()));
putPrimitiveType.accept(double.class, Double.class, ValueType.of(NbtDouble::of, (element) -> ((AbstractNbtNumber) element).doubleValue()));
putPrimitiveType.accept(boolean.class, Boolean.class, ValueType.of(NbtByte::of, (element) -> ((AbstractNbtNumber) element).byteValue() != 0));

map.put(String.class, ValueType.of(NbtString::of, NbtElement::asString));
map.put(UUID.class, ValueType.of(NbtHelper::fromUuid, NbtHelper::toUuid));
map.put(int[].class, ValueType.of(NbtIntArray::new, (element) -> ((NbtIntArray) element).getIntArray()));
map.put(long[].class, ValueType.of(NbtLongArray::new, (element) -> ((NbtLongArray) element).getLongArray()));
map.put(byte[].class, ValueType.of(NbtByteArray::new, (element) -> ((NbtByteArray) element).getByteArray()));

return map.build();
}

public static NbtCompound serialize(NbtPersistent obj) {
NbtCompound nbt = new NbtCompound();

for (Field field : obj.getClass().getDeclaredFields()) {
if (!field.isAnnotationPresent(NbtPersistentValue.class))
continue;

Object rawValue = fieldGet(field, obj);
if (rawValue == null)
continue;

String key = getKey(field);
ValueType type = simpleTypes.get(field.getType());

NbtElement element;
if (type != null)
element = type.serializer.apply(rawValue);
else if (NbtPersistent.class.isAssignableFrom(field.getType()))
element = ((NbtPersistent) rawValue).serialize();
else
throw new RuntimeException("Cannot serialize field \"" + field.getType().getName() + " " + field.getName() + "\", in class \"" + obj.getClass().getName() + "\"");
nbt.put(key, element);
}

return nbt;
}

public static void deserialize(NbtPersistent obj, NbtCompound nbt) {
for (Field field : obj.getClass().getDeclaredFields()) {
if (!field.isAnnotationPresent(NbtPersistentValue.class))
continue;

String key = getKey(field);
NbtElement element = nbt.get(key);
ValueType type = simpleTypes.get(field.getType());
if (NbtPersistent.class.isAssignableFrom(field.getType())) {
Object value = fieldGet(field, obj);
if (value == null) {
if (element == null)
continue;
value = getEmptyInstance(field.getType());
fieldSet(field, obj, value);
}
((NbtPersistent) value).deserialize(element);
}
else if (element != null && type != null) {
Object value = type.deserializer.apply(element);
fieldSet(field, obj, value);
}
else if (element != null)
throw new RuntimeException("Cannot deserialize field \"" + field.getType().getName() + " " + field.getName() + "\", in class \"" + obj.getClass().getName() + "\"");
}
}

private static String getKey(Field field) {
String key = field.getAnnotation(NbtPersistentValue.class).key();
return key.isEmpty() ? field.getName() : key;
}

private static Object fieldGet(Field field, Object obj) {
boolean canAccess = field.canAccess(obj);
field.setAccessible(true);
try {
return field.get(obj);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} finally {
field.setAccessible(canAccess);
}
}

private static void fieldSet(Field field, Object obj, Object value) {
boolean canAccess = field.canAccess(obj);
field.setAccessible(true);
try {
field.set(obj, value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
field.setAccessible(canAccess);
}

private static Object getEmptyInstance(Class<?> cls) {
try {
Method method = cls.getMethod("emptyForDeserialize");
return method.invoke(null);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

private record ValueType(
Function<Object, NbtElement> serializer,
Function<NbtElement, Object> deserializer
) {
@SuppressWarnings("unchecked")
private static <T> ValueType of(Function<T, NbtElement> serializer, Function<NbtElement, T> deserializer) {
return new ValueType((obj) -> serializer.apply((T) obj), deserializer::apply);
}
}
}
Loading

0 comments on commit 2236f1c

Please sign in to comment.