From 89d8435e80c8955d8299fe60d9a2169067f8ac75 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 28 Aug 2021 18:40:09 +0300 Subject: Added config as well as support for modmenu/cloth --- .../shcm/shsupercm/fabric/citresewn/CITResewn.java | 6 +++ .../fabric/citresewn/config/CITResewnConfig.java | 51 ++++++++++++++++++++++ .../config/CITResewnConfigScreenFactory.java | 36 +++++++++++++++ .../fabric/citresewn/config/CITResewnModMenu.java | 15 +++++++ .../mixin/ElytraFeatureRendererMixin.java | 6 ++- .../fabric/citresewn/mixin/ItemRendererMixin.java | 3 +- .../fabric/citresewn/mixin/ModelLoaderMixin.java | 10 +++++ .../resources/assets/citresewn/lang/en_us.json | 6 +++ src/main/resources/fabric.mod.json | 3 ++ 9 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java create mode 100644 src/main/resources/assets/citresewn/lang/en_us.json (limited to 'src/main') diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java index 1c45468..cbc7342 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java @@ -5,6 +5,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; @Environment(EnvType.CLIENT) public class CITResewn implements ClientModInitializer { @@ -13,9 +14,14 @@ public class CITResewn implements ClientModInitializer { public ActiveCITs activeCITs = null; + public CITResewnConfig config = null; + @Override public void onInitializeClient() { INSTANCE = this; + + config = CITResewnConfig.read(); + /*net.minecraft.client.render.entity.feature.ArmorFeatureRenderer#renderArmor take entity.getEquippedStack(armorSlot) eval override diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java new file mode 100644 index 0000000..38995ca --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java @@ -0,0 +1,51 @@ +package shcm.shsupercm.fabric.citresewn.config; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; +import org.apache.commons.io.IOUtils; +import shcm.shsupercm.fabric.citresewn.CITResewn; + +import java.io.*; + +public class CITResewnConfig { + public boolean enabled = true; + + private static final File FILE = new File("config/citresewn.json"); + public static CITResewnConfig INSTANCE() { + return CITResewn.INSTANCE.config; + } + + public static CITResewnConfig read() { + if (!FILE.exists()) + return new CITResewnConfig().write(); + + Reader reader = null; + try { + return new Gson().fromJson(reader = new FileReader(FILE), CITResewnConfig.class); + } catch (Exception e) { + CITResewn.LOG.error("Couldn't load config"); + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(reader); + } + } + + public CITResewnConfig write() { + Gson gson = new Gson(); + JsonWriter writer = null; + try { + writer = gson.newJsonWriter(new FileWriter(FILE)); + writer.setIndent(" "); + + gson.toJson(gson.toJsonTree(this, CITResewnConfig.class), writer); + } catch (Exception e) { + CITResewn.LOG.error("Couldn't save config"); + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(writer); + } + return this; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java new file mode 100644 index 0000000..bc00d2b --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java @@ -0,0 +1,36 @@ +package shcm.shsupercm.fabric.citresewn.config; + +import me.shedaniel.clothconfig2.api.ConfigBuilder; +import me.shedaniel.clothconfig2.api.ConfigCategory; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.LiteralText; +import net.minecraft.text.TranslatableText; + +public class CITResewnConfigScreenFactory { + public static Screen create(Screen parent) { + CITResewnConfig currentConfig = CITResewnConfig.INSTANCE(), defaultConfig = new CITResewnConfig(); + + ConfigBuilder builder = ConfigBuilder.create() + .setParentScreen(parent) + .setTitle(new TranslatableText("config.citresewn.title")) + .setSavingRunnable(currentConfig::write); + + ConfigCategory category = builder.getOrCreateCategory(new LiteralText("")); + ConfigEntryBuilder entryBuilder = builder.entryBuilder(); + + category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.citsEnabled.title"), currentConfig.enabled) + .setTooltip(new TranslatableText("config.citresewn.citsEnabled.tooltip")) + .setSaveConsumer(newConfig -> { + if (currentConfig.enabled != newConfig) { + currentConfig.enabled = newConfig; + MinecraftClient.getInstance().reloadResources(); + } + }) + .setDefaultValue(defaultConfig.enabled) + .build()); + + return builder.build(); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java new file mode 100644 index 0000000..4c3f20a --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java @@ -0,0 +1,15 @@ +package shcm.shsupercm.fabric.citresewn.config; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import net.fabricmc.loader.api.FabricLoader; + +public class CITResewnModMenu implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + if (FabricLoader.getInstance().isModLoaded("cloth-config2")) + return CITResewnConfigScreenFactory::create; + + return parent -> null; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ElytraFeatureRendererMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ElytraFeatureRendererMixin.java index 39dfa73..37f0dd9 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ElytraFeatureRendererMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ElytraFeatureRendererMixin.java @@ -14,6 +14,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import java.lang.ref.WeakReference; @@ -24,7 +25,7 @@ public class ElytraFeatureRendererMixin { @Inject(method = "render", cancellable = true, at = @At("HEAD")) public void injectCIT(MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, LivingEntity livingEntity, float f, float g, float h, float j, float k, float l, CallbackInfo ci) { - if (CITResewn.INSTANCE.activeCITs == null) + if (!CITResewnConfig.INSTANCE().enabled || CITResewn.INSTANCE.activeCITs == null) return; this.elytraItemCached = new WeakReference<>(livingEntity.getEquippedStack(EquipmentSlot.CHEST)); @@ -33,6 +34,9 @@ public class ElytraFeatureRendererMixin { @Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/RenderLayer;getArmorCutoutNoCull(Lnet/minecraft/util/Identifier;)Lnet/minecraft/client/render/RenderLayer;")) public RenderLayer getArmorCutoutNoCull(Identifier originalIdentifier) { + if (!CITResewnConfig.INSTANCE().enabled || CITResewn.INSTANCE.activeCITs == null) + return RenderLayer.getArmorCutoutNoCull(originalIdentifier); + ItemStack itemStack = this.elytraItemCached.get(); LivingEntity livingEntity = livingEntityCached.get(); if (itemStack != null && livingEntity != null) { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java index f2c15ec..58b5fe2 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java @@ -10,12 +10,13 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; @Mixin(ItemRenderer.class) public class ItemRendererMixin { @Inject(method = "getHeldItemModel", cancellable = true, at = @At("RETURN")) public void injectCIT(ItemStack stack, World world, LivingEntity entity, int seed, CallbackInfoReturnable cir) { - if (CITResewn.INSTANCE.activeCITs == null) + if (!CITResewnConfig.INSTANCE().enabled || CITResewn.INSTANCE.activeCITs == null) return; BakedModel citModel = CITResewn.INSTANCE.activeCITs.getItemModel(stack, cir.getReturnValue(), world, entity); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java index c6681d3..f0fa82d 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java @@ -16,6 +16,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.ActiveCITs; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import shcm.shsupercm.fabric.citresewn.pack.CITParser; import shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier; import shcm.shsupercm.fabric.citresewn.pack.cits.CIT; @@ -41,6 +42,9 @@ public abstract class ModelLoaderMixin { CITResewn.INSTANCE.activeCITs = null; } + if (!CITResewnConfig.INSTANCE().enabled) + return; + Collection parsed = CITParser.parse(resourceManager.streamResourcePacks().collect(Collectors.toCollection(ArrayList::new))); for (CIT cit : parsed) @@ -65,11 +69,17 @@ public abstract class ModelLoaderMixin { @Inject(method = "bake", at = @At("RETURN")) public void onBake(Identifier id, ModelBakeSettings settings, CallbackInfoReturnable cir) { + if (CITResewn.INSTANCE.activeCITs == null) + return; + this.citOverrideCacheMap.put(id, cir.getReturnValue()); } @Inject(method = "upload", at = @At("RETURN")) public void linkBakedModels(TextureManager textureManager, Profiler profiler, CallbackInfoReturnable cir) { + if (CITResewn.INSTANCE.activeCITs == null) + return; + profiler.push("citresewn_linking"); if (CITResewn.INSTANCE.activeCITs != null) { diff --git a/src/main/resources/assets/citresewn/lang/en_us.json b/src/main/resources/assets/citresewn/lang/en_us.json new file mode 100644 index 0000000..84fc464 --- /dev/null +++ b/src/main/resources/assets/citresewn/lang/en_us.json @@ -0,0 +1,6 @@ +{ + "config.citresewn.title": "CIT Resewn Configuration", + + "config.citresewn.citsEnabled.title": "Enabled", + "config.citresewn.citsEnabled.tooltip": "Should CITResewn load and inject cits from resourcepacks" +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 5e2421e..45c4721 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -14,6 +14,9 @@ "entrypoints": { "client": [ "shcm.shsupercm.fabric.citresewn.CITResewn" + ], + "modmenu": [ + "shcm.shsupercm.fabric.citresewn.config.CITResewnModMenu" ] }, "mixins": [ -- cgit