diff options
author | SHsuperCM <shsupercm@gmail.com> | 2022-02-20 10:10:27 +0200 |
---|---|---|
committer | SHsuperCM <shsupercm@gmail.com> | 2022-02-20 10:10:27 +0200 |
commit | 6c8d3aed55abb448422287c1718f4f3d90ec68a9 (patch) | |
tree | 037d3a66b319d92a1487eb1d1e9402b3931029d1 /defaults/src/main/java | |
parent | f90a1bc5859036e6572932fbfa05b006503c1e29 (diff) | |
download | CITResewn-6c8d3aed55abb448422287c1718f4f3d90ec68a9.tar.gz CITResewn-6c8d3aed55abb448422287c1718f4f3d90ec68a9.tar.bz2 CITResewn-6c8d3aed55abb448422287c1718f4f3d90ec68a9.zip |
Cleaned some stuff up
Diffstat (limited to 'defaults/src/main/java')
6 files changed, 117 insertions, 93 deletions
diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java index 5325e28..0405cbc 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java @@ -4,9 +4,6 @@ import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.cit.CITContext; import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.EnumCondition; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; public class ConditionHand extends EnumCondition<ConditionHand.Hand> { @Entrypoint(CITConditionContainer.ENTRYPOINT) @@ -34,23 +31,19 @@ public class ConditionHand extends EnumCondition<ConditionHand.Hand> { } protected enum Hand implements EnumCondition.Aliased { - MAINHAND { - @Override - public String[] getAliases() { - return new String[] { "main", "mainhand", "main_hand" }; - } - }, - OFFHAND { - @Override - public String[] getAliases() { - return new String[] { "off", "offhand", "off_hand" }; - } - }, - ANY { - @Override - public String[] getAliases() { - return new String[] { "any", "either", "*" }; - } + MAINHAND("main", "mainhand", "main_hand"), + OFFHAND("off", "offhand", "off_hand"), + ANY("any", "either", "*"); + + private final String[] aliases; + + Hand(String... aliases) { + this.aliases = aliases; + } + + @Override + public String[] getAliases() { + return this.aliases; } } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 2c431e1..f37fd6b 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -39,6 +39,10 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; +/** + * Do not go through this class, it looks awful because it was ported from a "proof of concept".<br> + * The whole type will be rewritten at some point. + */ public class TypeItem extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) public static final Container CONTAINER = new Container(); diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java index c3f30f5..498f61f 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java @@ -3,7 +3,7 @@ package shcm.shsupercm.fabric.citresewn.defaults.common; import net.minecraft.util.Identifier; /** - * Marks models as cit item models. + * Marks models as cit models. */ public class ResewnItemModelIdentifier extends Identifier { public ResewnItemModelIdentifier(String id) { diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/ModelLoaderMixin.java new file mode 100644 index 0000000..8ed0e20 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/ModelLoaderMixin.java @@ -0,0 +1,87 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.common; + +import com.mojang.datafixers.util.Either; +import net.minecraft.client.render.model.ModelLoader; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.client.render.model.json.ModelOverride; +import net.minecraft.client.util.SpriteIdentifier; +import net.minecraft.resource.Resource; +import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; +import org.apache.commons.io.IOUtils; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +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.cit.CITType; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; +import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item.JsonUnbakedModelAccessor; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Will be rewritten at some point. + */ +@Mixin(ModelLoader.class) +public class ModelLoaderMixin { + @Shadow @Final private ResourceManager resourceManager; + + @Inject(method = "loadModelFromJson", cancellable = true, at = @At("HEAD")) + public void citresewn$forceLiteralResewnModelIdentifier(Identifier id, CallbackInfoReturnable<JsonUnbakedModel> cir) { + if (id instanceof ResewnItemModelIdentifier) { + InputStream is = null; + Resource resource = null; + try { + JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(id)).getInputStream(), StandardCharsets.UTF_8)); + json.id = id.toString(); + json.id = json.id.substring(0, json.id.length() - 5); + + ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { + Optional<SpriteIdentifier> left = original.left(); + if (left.isPresent()) { + String originalPath = left.get().getTextureId().getPath(); + String[] split = originalPath.split("/"); + if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) { + Identifier resolvedIdentifier = CITType.resolveAsset(id, originalPath, "textures", ".png", resourceManager); + if (resolvedIdentifier != null) + return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); + } + } + return original; + }); + + Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId(); + if (parentId != null) { + String[] parentIdPathSplit = parentId.getPath().split("/"); + if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { + parentId = CITType.resolveAsset(id, parentId.getPath(), "models", ".json", resourceManager); + if (parentId != null) + ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); + } + } + + json.getOverrides().replaceAll(override -> { + String[] modelIdPathSplit = override.getModelId().getPath().split("/"); + if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { + Identifier resolvedOverridePath = CITType.resolveAsset(id, override.getModelId().getPath(), "models", ".json", resourceManager); + if (resolvedOverridePath != null) + return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); + } + + return override; + }); + + cir.setReturnValue(json); + } catch (Exception ignored) { + } finally { + IOUtils.closeQuietly(is, resource); + } + } + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java index 9f6063b..49e26af 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java @@ -29,11 +29,15 @@ import java.lang.ref.WeakReference; import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem.CONTAINER; +/** + * Do not go through this class, it looks awful because it was ported from a "proof of concept".<br> + * The whole type will be rewritten at some point. + */ @Mixin(ItemRenderer.class) public class ItemRendererMixin { @Shadow @Final private ItemModels models; - private static WeakReference<BakedModel> mojankCITModel = null; + private WeakReference<BakedModel> citresewn$mojankCITModel = null; @Inject(method = "getModel", cancellable = true, at = @At("HEAD")) private void citresewn$getItemModel(ItemStack stack, World world, LivingEntity entity, int seed, CallbackInfoReturnable<BakedModel> cir) { @@ -55,19 +59,19 @@ public class ItemRendererMixin { if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) return; - mojankCITModel = null; + citresewn$mojankCITModel = null; if (((TypeItem.CITCacheItem) (Object) stack).citresewn$isMojankCITTypeItem()) { boolean bl = renderMode == ModelTransformation.Mode.GUI || renderMode == ModelTransformation.Mode.GROUND || renderMode == ModelTransformation.Mode.FIXED; if (bl) - mojankCITModel = new WeakReference<>(model); + citresewn$mojankCITModel = new WeakReference<>(model); else { // rendered in hand model of trident/spyglass if (stack.isOf(Items.TRIDENT)) - mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:trident_in_hand#inventory"))); + citresewn$mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:trident_in_hand#inventory"))); else if (stack.isOf(Items.SPYGLASS)) - mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:spyglass_in_hand#inventory"))); + citresewn$mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:spyglass_in_hand#inventory"))); } } else - mojankCITModel = null; + citresewn$mojankCITModel = null; } @ModifyVariable(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At(value = "LOAD", ordinal = 0, target = "Lnet/minecraft/client/render/model/BakedModel;getTransformation()Lnet/minecraft/client/render/model/json/ModelTransformation;"), argsOnly = true) @@ -75,8 +79,8 @@ public class ItemRendererMixin { if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) return original; - if (mojankCITModel != null) - return mojankCITModel.get(); + if (citresewn$mojankCITModel != null) + return citresewn$mojankCITModel.get(); return original; } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index 718cbca..c7fa70d 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -1,6 +1,5 @@ package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; -import com.mojang.datafixers.util.Either; import net.minecraft.client.color.block.BlockColors; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.ModelLoader; @@ -9,12 +8,9 @@ import net.minecraft.client.render.model.UnbakedModel; import net.minecraft.client.render.model.json.JsonUnbakedModel; import net.minecraft.client.render.model.json.ModelOverride; import net.minecraft.client.texture.TextureManager; -import net.minecraft.client.util.SpriteIdentifier; -import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; import net.minecraft.util.profiler.Profiler; -import org.apache.commons.io.IOUtils; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -26,22 +22,16 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CIT; -import shcm.shsupercm.fabric.citresewn.cit.CITType; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; -import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.stream.Collectors; import static shcm.shsupercm.fabric.citresewn.CITResewn.info; import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem.CONTAINER; @Mixin(ModelLoader.class) public class ModelLoaderMixin { - @Shadow @Final private ResourceManager resourceManager; @Shadow @Final private Set<Identifier> modelsToLoad; @Shadow @Final private Map<Identifier, UnbakedModel> modelsToBake; @Shadow @Final private Map<Identifier, UnbakedModel> unbakedModels; @@ -99,60 +89,6 @@ public class ModelLoaderMixin { profiler.pop(); } - - @Inject(method = "loadModelFromJson", cancellable = true, at = @At("HEAD")) - public void citresewn$forceLiteralResewnModelIdentifier(Identifier id, CallbackInfoReturnable<JsonUnbakedModel> cir) { - if (id instanceof ResewnItemModelIdentifier) { - InputStream is = null; - Resource resource = null; - try { - JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(id)).getInputStream(), StandardCharsets.UTF_8)); - json.id = id.toString(); - json.id = json.id.substring(0, json.id.length() - 5); - - ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { - Optional<SpriteIdentifier> left = original.left(); - if (left.isPresent()) { - String originalPath = left.get().getTextureId().getPath(); - String[] split = originalPath.split("/"); - if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) { - Identifier resolvedIdentifier = CITType.resolveAsset(id, originalPath, "textures", ".png", resourceManager); - if (resolvedIdentifier != null) - return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); - } - } - return original; - }); - - Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId(); - if (parentId != null) { - String[] parentIdPathSplit = parentId.getPath().split("/"); - if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { - parentId = CITType.resolveAsset(id, parentId.getPath(), "models", ".json", resourceManager); - if (parentId != null) - ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); - } - } - - json.getOverrides().replaceAll(override -> { - String[] modelIdPathSplit = override.getModelId().getPath().split("/"); - if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { - Identifier resolvedOverridePath = CITType.resolveAsset(id, override.getModelId().getPath(), "models", ".json", resourceManager); - if (resolvedOverridePath != null) - return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); - } - - return override; - }); - - cir.setReturnValue(json); - } catch (Exception ignored) { - } finally { - IOUtils.closeQuietly(is, resource); - } - } - } - @ModifyArg(method = "loadModelFromJson", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;")) public Identifier citresewn$fixDuplicatePrefixSuffix(Identifier original) { |