Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Jan 20, 2024
2 parents b4680ec + c4760e2 commit d294495
Show file tree
Hide file tree
Showing 27 changed files with 302 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public abstract class ClientConfigManager {
public static Config getConfig() {
if(!ConfigManager.loaded()) {
if (!ConfigManager.loaded()) {
BetterTrims.LOGGER.warn("Attempted to access configs before they were loaded, loading configs now");
ConfigManager.loadConfigs();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ private static Collection<ConfigCategory> generateCategories() {
Collection<ConfigCategory> categories = new ArrayList<>();
Groups groups = Reflection.getAnnotation(ClientConfigManager.getConfig(), Groups.class);
List<String> groupNames = new ArrayList<>(Arrays.asList(groups.value()));
Reflection.forEachAnnotatedField(ClientConfigManager.getConfig(), field -> ConfigOptionReference.readGroup(field).ifPresent(group -> {
if(!groupNames.contains(group)) {
throw new IllegalStateException("Group \"" + group + "\" does not exist. It is referenced by field \"" + field.getName() + "\".");
}
}));
Reflection.forEachAnnotatedField(ClientConfigManager.getConfig(), field -> ConfigOptionReference.readGroup(field)
.ifPresent(group -> {
if (!groupNames.contains(group)) {
throw new IllegalStateException("Group \"" + group + "\" does not exist. It is referenced by field \"" + field.getName() + "\".");
}
}));
for (String group : groupNames) {
categories.add(generateCategoryForGroup(group));
}
Expand Down Expand Up @@ -157,7 +158,10 @@ private static Option<Integer> integerOption(ConfigOptionReference reference) {

private static OptionDescription imagedDescription(ConfigOptionReference reference) {
Identifier textureLocation = reference.findTexture();
if (textureLocation == null || MinecraftClient.getInstance().getResourceManager().getResource(textureLocation).isEmpty()) {
if (textureLocation == null || MinecraftClient.getInstance()
.getResourceManager()
.getResource(textureLocation)
.isEmpty()) {
return OptionDescription.of(description(reference.getFormattedName()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public abstract class GameRendererMixin {
@WrapOperation(method = "getNightVisionStrength", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/effect/StatusEffectInstance;isDurationBelow(I)Z"))
private static boolean letsNotCrashThanks(StatusEffectInstance instance, int duration, Operation<Boolean> original) {
if(instance == null) return false;
if (instance == null) return false;
return original.call(instance, duration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Debug(export = true)
@Mixin(value = GameRenderer.class, priority = 1500)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public static void init() {
isConnectedToDedicated = buf.readBoolean();
String serialized = buf.readString();
ConfigManager.deserializeConfig(serialized);
if(!isConnectedToDedicated) return;
if (!isConnectedToDedicated) return;

if (displayNewConfigToast) {
ToastManager toastManager = client.getToastManager();
toastManager.add(SystemToast.create(client, SystemToast.Type.WORLD_ACCESS_FAILURE, Text.translatable("bettertrims.new_config.title"), Text.translatable("bettertrims.new_config.desc")));
} else displayNewConfigToast = true;

if(refreshScreen) {
if (refreshScreen) {
client.execute(EventHandler::onRecievedConfig);
refreshScreen = false;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ public static void trySendConfigToServer() {

public static void sendConfigToServer() {
ClientPlayNetworking.send(Networking.CONFIG_SYNC, PacketByteBufs.create()
.writeString(ConfigManager.serializeConfig()));
.writeString(ConfigManager.serializeConfig()));
}

private static void requestConfigFromServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class BetterTrimsMixinPlugin implements IMixinConfigPlugin {
public static boolean testClass(String className) {
try {
List<AnnotationNode> annotationNodes = MixinService.getService()
.getBytecodeProvider()
.getClassNode(className).visibleAnnotations;
.getBytecodeProvider()
.getClassNode(className).visibleAnnotations;
if (annotationNodes == null) return true;

for (AnnotationNode node : annotationNodes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.bawnorton.bettertrims;

import com.bawnorton.bettertrims.config.ConfigManager;
import com.bawnorton.mixinsquared.api.MixinCanceller;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;

import java.util.List;

public class BetterTrimsPreLaunch implements PreLaunchEntrypoint {

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bawnorton/bettertrims/compat/Compat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

public abstract class Compat {
public static boolean isStackedTrimsLoaded() {
return FabricLoader.getInstance().isModLoaded("stacked_trims") || FabricLoader.getInstance().isModLoaded("stackable_trims");
return FabricLoader.getInstance().isModLoaded("stacked_trims") || FabricLoader.getInstance()
.isModLoaded("stackable_trims");
}

public static boolean isYaclLoaded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
import net.fabricmc.loader.api.FabricLoader;

import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;

public abstract class ConfigManager {
private static final Gson GSON = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.create();
.setPrettyPrinting()
.create();
private static final Path localConfigPath = FabricLoader.getInstance()
.getConfigDir()
.resolve(BetterTrims.MOD_ID + ".json");
.getConfigDir()
.resolve(BetterTrims.MOD_ID + ".json");
private static final Path serverConfigPath = FabricLoader.getInstance()
.getConfigDir()
.resolve(BetterTrims.MOD_ID + "-server.json");
.getConfigDir()
.resolve(BetterTrims.MOD_ID + "-server.json");

private static boolean loaded = false;

Expand Down Expand Up @@ -106,7 +105,7 @@ private static void validateNestedFields(Object instance) {
}

private static void setIfNull(ConfigOptionReference reference, Object value) {
if(reference.isValueNull()) {
if (reference.isValueNull()) {
reference.setConfigValue(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.bawnorton.bettertrims.config.annotation;

import java.lang.annotation.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public static Optional<String> readGroup(Field field) {

private void validateAnnotations() {
Annotation[] annotations = field.getAnnotations();
if (annotations.length == 0) throw new IllegalStateException("Field \"" + field.getName() + "\" has no annotations.");
if (annotations.length == 0)
throw new IllegalStateException("Field \"" + field.getName() + "\" has no annotations.");
boolean hasType = false;
for (Annotation annotation : annotations) {
if (annotation instanceof BooleanOption || annotation instanceof IntOption || annotation instanceof FloatOption || annotation instanceof NestedOption) {
if (hasType) throw new IllegalStateException("Field \"" + field.getName() + "\" has multiple type annotations.");
if (hasType)
throw new IllegalStateException("Field \"" + field.getName() + "\" has multiple type annotations.");
hasType = true;
}
}
Expand All @@ -61,15 +63,16 @@ private void validateOptionType() {
FieldType fieldType = getType();
Class<? extends Annotation> expectedAnnotation = fieldType.expectedAnnotation();
Annotation annotation = field.getAnnotation(expectedAnnotation);
if (annotation == null) throw new IllegalStateException("Field \"" + field.getName() + "\" has invalid type annotation. Expected @" + expectedAnnotation.getSimpleName());
if (annotation == null)
throw new IllegalStateException("Field \"" + field.getName() + "\" has invalid type annotation. Expected @" + expectedAnnotation.getSimpleName());
}

private void validateValueType(Class<?> clazz) {
if (value == null) return;
if (clazz == null) throw new IllegalArgumentException("Class cannot be null.");
if (!clazz.isAssignableFrom(value.getClass())) {
throw new IllegalArgumentException("Invalid type " + clazz.getName() + " for " + value.getClass()
.getName());
.getName());
}
}

Expand Down Expand Up @@ -185,29 +188,40 @@ public Integer maxIntValue() {
}

public @Nullable Identifier findTexture() {
if(!field.isAnnotationPresent(TextureLocation.class)) return null;
if (!field.isAnnotationPresent(TextureLocation.class)) return null;

TextureLocation location = field.getAnnotation(TextureLocation.class);
if(!location.effectLookup()) {
if (!location.effectLookup()) {
String locationString = location.value();
if(locationString.equals("none")) return null;
if (locationString.equals("none")) return null;

return new Identifier(locationString);
}

String searchString = location.value();
return TEXTURE_CACHE.computeIfAbsent(searchString, id -> {
Identifier itemId = Registries.ITEM.getIds().stream().filter(identifier -> identifier.getPath().contains(searchString)).findFirst().orElseGet(() -> {
BetterTrims.LOGGER.debug("Could not find item for identifier \"%s\", trying \"%s_ingot\"".formatted(searchString, searchString));
String ingotSearchString = searchString + "_ingot";
return Registries.ITEM.getIds().stream().filter(identifier -> identifier.getPath().contains(ingotSearchString)).findFirst().orElseGet(() -> {
BetterTrims.LOGGER.debug("Could not find item for identifier \"%s_ingot\"".formatted(ingotSearchString));
return null;
});
});
if(itemId == null) return null;

return ModelIds.getItemModelId(Registries.ITEM.get(itemId)).withPrefixedPath("textures/").withSuffixedPath(".png");
Identifier itemId = Registries.ITEM.getIds()
.stream()
.filter(identifier -> identifier.getPath().contains(searchString))
.findFirst()
.orElseGet(() -> {
BetterTrims.LOGGER.debug("Could not find item for identifier \"%s\", trying \"%s_ingot\"".formatted(searchString, searchString));
String ingotSearchString = searchString + "_ingot";
return Registries.ITEM.getIds()
.stream()
.filter(identifier -> identifier.getPath()
.contains(ingotSearchString))
.findFirst()
.orElseGet(() -> {
BetterTrims.LOGGER.debug("Could not find item for identifier \"%s_ingot\"".formatted(ingotSearchString));
return null;
});
});
if (itemId == null) return null;

return ModelIds.getItemModelId(Registries.ITEM.get(itemId))
.withPrefixedPath("textures/")
.withSuffixedPath(".png");
});
}

Expand All @@ -234,6 +248,14 @@ public enum FieldType {
FLOAT,
NESTED;

public static FieldType of(Class<?> clazz) {
if (Boolean.class.isAssignableFrom(clazz)) return BOOLEAN;
if (Integer.class.isAssignableFrom(clazz)) return INTEGER;
if (Float.class.isAssignableFrom(clazz)) return FLOAT;
if (Object.class.isAssignableFrom(clazz)) return NESTED;
throw new IllegalArgumentException("Unknown type " + clazz.getName());
}

private Class<? extends Annotation> expectedAnnotation() {
return switch (this) {
case BOOLEAN -> BooleanOption.class;
Expand All @@ -242,13 +264,5 @@ private Class<? extends Annotation> expectedAnnotation() {
case NESTED -> NestedOption.class;
};
}

public static FieldType of(Class<?> clazz) {
if (Boolean.class.isAssignableFrom(clazz)) return BOOLEAN;
if (Integer.class.isAssignableFrom(clazz)) return INTEGER;
if (Float.class.isAssignableFrom(clazz)) return FLOAT;
if (Object.class.isAssignableFrom(clazz)) return NESTED;
throw new IllegalArgumentException("Unknown type " + clazz.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public static ParentedConfigOptionReference of(ConfigOptionReference parent, Obj

@Override
public boolean isOf(String type) {
if(getOptionType().equals("inherit")) return parent.isOf(type);
if (getOptionType().equals("inherit")) return parent.isOf(type);
return super.isOf(type);
}

@Override
public @Nullable Identifier findTexture() {
Identifier texture = super.findTexture();
if(texture == null) return parent.findTexture();
if (texture == null) return parent.findTexture();
return texture;
}

Expand Down
Loading

0 comments on commit d294495

Please sign in to comment.