Skip to content

Commit

Permalink
Fix alternative armor and item groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jun 6, 2024
1 parent 2157fa0 commit 4a30a20
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 59 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ allprojects {
// JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used.
// We'll use that if it's available, but otherwise we'll use the older option.

it.options.release = 17
it.options.release = 21
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public final class PolymerImpl {

public static final boolean IS_CLIENT = CommonImpl.IS_CLIENT;

public static final boolean USE_ALT_ARMOR_HANDLER;
public static final boolean DISPLAY_DEBUG_INFO_CLIENT;
public static final boolean ADD_NON_POLYMER_CREATIVE_TABS;
public static final boolean RESEND_BLOCKS_AROUND_CLICK;
Expand Down Expand Up @@ -47,13 +46,11 @@ public final class PolymerImpl {

if (PolymerImpl.IS_CLIENT) {
var clientConfig = CommonImpl.loadConfig("client", ClientConfig.class);
USE_ALT_ARMOR_HANDLER = CompatStatus.REQUIRE_ALT_ARMOR_HANDLER || clientConfig.useAlternativeArmorRenderer;
DISPLAY_DEBUG_INFO_CLIENT = clientConfig.displayF3Info;
LOG_INVALID_SERVER_IDS_CLIENT = clientConfig.logInvalidServerEntryIds;
CHANGING_QOL_CLIENT = !clientConfig.disableNonVisualQualityOfLifeChanges;
USE_UNSAFE_ITEMS_CLIENT = clientConfig.experimentalModdedContainerSupport;
} else {
USE_ALT_ARMOR_HANDLER = false;
DISPLAY_DEBUG_INFO_CLIENT = false;
LOG_INVALID_SERVER_IDS_CLIENT = false;
CHANGING_QOL_CLIENT = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import eu.pb4.polymer.core.impl.PolymerImpl;

public class ClientConfig {
public String _c3 = "Enables alternative armor (texture) renderer. Always enabled with Iris/Canvas installed";
public boolean useAlternativeArmorRenderer = false;
public String _c4 = "Toggles visibility of F3 debug info";
public boolean displayF3Info = true;
public String _c5 = "Enables logging of invalid registry ids (BlockStates, Blocks, Items, etc) sent by server";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
@Environment(EnvType.CLIENT)
@Mixin(value = ItemGroup.class, priority = 1200)
public abstract class ItemGroupMixin implements ClientItemGroupExtension {
@Shadow private ItemStack icon;

@Shadow private Collection<ItemStack> displayStacks;
@Shadow private Set<ItemStack> searchTabStacks;

Expand All @@ -51,6 +49,17 @@ public abstract class ItemGroupMixin implements ClientItemGroupExtension {
return entry instanceof InternalClientItemGroup ? ItemGroups.getDefaultTab() : entry;
}

@Inject(method = "updateEntries", at = @At("HEAD"), cancellable = true)
private void polymer$injectEntriesCustom(ItemGroup.DisplayContext arg, CallbackInfo ci) {
if (((Object) this) instanceof InternalClientItemGroup) {
this.displayStacks.clear();
this.searchTabStacks.clear();
this.displayStacks.addAll(this.polymer$itemsGroup);
this.searchTabStacks.addAll(this.polymer$itemsSearch);
ci.cancel();
}
}

@Inject(method = "updateEntries", at = @At("TAIL"))
private void polymer$injectEntriesVanilla(ItemGroup.DisplayContext arg, CallbackInfo ci) {
if (this.type == ItemGroup.Type.CATEGORY && ClientUtils.isClientThread()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
import com.llamalad7.mixinextras.sugar.Local;
import eu.pb4.polymer.core.impl.other.PolymerTooltipType;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

@Mixin(ItemStack.class)
public class ItemStackMixin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static class Config {

public String _c3 = "Value of CustomModelData offset when enabled";
public int offsetValue = 100000;
public String _c4 = "Enables usage of alternative armor rendering for increased mod compatibility";
public String _c4 = "Enables usage of alternative armor rendering for increased mod compatibility. (Always on with Iris or Canvas present)";
@SerializedName("use_alternative_armor_rendering")
public boolean useAlternativeArmorHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.Style;
Expand All @@ -19,14 +20,19 @@
import net.minecraft.util.Util;
import org.jetbrains.annotations.ApiStatus;

import java.util.List;

import static net.minecraft.server.command.CommandManager.literal;


@ApiStatus.Internal
public class PolymerResourcePackMod implements ModInitializer, ClientModInitializer {
public static boolean alreadyGeneration = false;
public static final Int2ObjectMap<Identifier> ARMOR_TEXTURES_1 = new Int2ObjectOpenHashMap<>();
public static final Int2ObjectMap<Identifier> ARMOR_TEXTURES_2 = new Int2ObjectOpenHashMap<>();
public static final Int2ObjectMap<List<ArmorMaterial.Layer>> ARMOR_TEXTURES = new Int2ObjectOpenHashMap<>();
public static final List<ArmorMaterial.Layer> LEATHER_OVERRIDE = List.of(
new ArmorMaterial.Layer(Identifier.ofVanilla("vanilla_leather"), "", true),
new ArmorMaterial.Layer(Identifier.ofVanilla("vanilla_leather"), "_overlay", false)
);
public static boolean hasArmorTextures = false;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import eu.pb4.polymer.resourcepack.impl.PolymerResourcePackMod;
import eu.pb4.polymer.resourcepack.mixin.client.ArmorFeatureRendererAccessor;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceReloader;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
Expand All @@ -27,48 +29,35 @@ public record PolymerResourceReloader(TextureManager manager) implements Resourc
public CompletableFuture<Void> reload(Synchronizer synchronizer, ResourceManager manager, Profiler prepareProfiler, Profiler applyProfiler, Executor prepareExecutor, Executor applyExecutor) {
return CompletableFuture.supplyAsync(() -> {
if (PolymerResourcePackImpl.USE_ALT_ARMOR_HANDLER) {
PolymerResourcePackMod.ARMOR_TEXTURES_1.clear();
PolymerResourcePackMod.ARMOR_TEXTURES_2.clear();
PolymerResourcePackMod.ARMOR_TEXTURES.clear();
var polymerArmor = manager.getResource(POLYMER_ARMOR_ID);
if (polymerArmor.isPresent()) {
PolymerResourcePackMod.hasArmorTextures = true;
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_1.png", Identifier.of("textures/models/armor/vanilla_leather_layer_1.png"));
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_1_overlay.png", Identifier.of("textures/models/armor/vanilla_leather_layer_1_overlay.png"));
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_2.png", Identifier.of("textures/models/armor/vanilla_leather_layer_2.png"));
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_2_overlay.png", Identifier.of("textures/models/armor/vanilla_leather_layer_2_overlay.png"));


try {
HashMap<String, String> data = GSON.fromJson(new String(polymerArmor.get().getInputStream().readAllBytes()), HashMap.class);

for (var entry : data.entrySet()) {
var id = Identifier.of(entry.getValue());
var key = Integer.parseInt(entry.getKey());
var tex1 = Identifier.of(id.getNamespace(), "textures/models/armor/" + id.getPath() + "_layer_1.png");
var tex2 = Identifier.of(id.getNamespace(), "textures/models/armor/" + id.getPath() + "_layer_2.png");
PolymerResourcePackMod.ARMOR_TEXTURES_1.put(key, tex1);
PolymerResourcePackMod.ARMOR_TEXTURES_2.put(key, tex2);
PolymerResourcePackMod.ARMOR_TEXTURES.put(key, List.of(
new ArmorMaterial.Layer(id, "", false)
));
}
} catch (Exception e) {
CommonImpl.LOGGER.warn("Invalid armors.json file!", e);
}
} else {
PolymerResourcePackMod.hasArmorTextures = false;
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_1.png", Identifier.of("textures/models/armor/leather_layer_1.png"));
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_1_overlay.png", Identifier.of("textures/models/armor/leather_layer_1_overlay.png"));
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_2.png", Identifier.of("textures/models/armor/leather_layer_2.png"));
ArmorFeatureRendererAccessor.getARMOR_TEXTURE_CACHE().put("textures/models/armor/leather_layer_2_overlay.png", Identifier.of("textures/models/armor/leather_layer_2_overlay.png"));

}
}
return null;
}, prepareExecutor).thenCompose(synchronizer::whenPrepared).thenAcceptAsync(v -> {
if (PolymerResourcePackImpl.USE_ALT_ARMOR_HANDLER) {
for (var id : PolymerResourcePackMod.ARMOR_TEXTURES_1.values()) {
this.manager.registerTexture(id, new PolymerArmorResourceTexture(id));
}
for (var id : PolymerResourcePackMod.ARMOR_TEXTURES_2.values()) {
this.manager.registerTexture(id, new PolymerArmorResourceTexture(id));
for (var id : PolymerResourcePackMod.ARMOR_TEXTURES.values()) {
var first = id.getFirst().getTexture(false);
var second = id.getFirst().getTexture(true);
this.manager.registerTexture(first, new PolymerArmorResourceTexture(first));
this.manager.registerTexture(second, new PolymerArmorResourceTexture(second));
}
}
}, (runnable) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,33 @@
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.feature.ArmorFeatureRenderer;
import net.minecraft.client.render.entity.model.BipedEntityModel;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.ArmorMaterials;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.List;

@Environment(EnvType.CLIENT)
@Mixin(ArmorFeatureRenderer.class)
public class armor_ArmorFeatureRendererMixin<T extends LivingEntity, M extends BipedEntityModel<T>, A extends BipedEntityModel<T>> {
// Todo
/*@WrapOperation(method = "renderArmor", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/entity/feature/ArmorFeatureRenderer;renderArmorParts(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ILnet/minecraft/client/render/entity/model/BipedEntityModel;FFFLnet/minecraft/util/Identifier;)V"), require = 0)
private void polymer$changeArmorTexture(ArmorFeatureRenderer instance, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, A model, float red, float green, float blue, Identifier texture, Operation<Void> original, @Local ItemStack stack) {
if (PolymerResourcePackMod.hasArmorTextures) {
@WrapOperation(method = "renderArmor", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ArmorMaterial;layers()Ljava/util/List;"))
private List<ArmorMaterial.Layer> polymer$changeArmorTexture(ArmorMaterial instance, Operation<List<ArmorMaterial.Layer>> original, @Local ItemStack stack) {
if (PolymerResourcePackMod.hasArmorTextures && instance == ArmorMaterials.LEATHER.value()) {
var color = stack.get(DataComponentTypes.DYED_COLOR);
if (color != null && PolymerResourcePackMod.ARMOR_TEXTURES_1.containsKey(color.rgb())) {
boolean usesSecondLayer = texture.getPath().endsWith("_2");
VertexConsumer vertexConsumer = ItemRenderer.getArmorGlintConsumer(vertexConsumers, RenderLayer.getArmorCutoutNoCull((usesSecondLayer ? PolymerResourcePackMod.ARMOR_TEXTURES_2 : PolymerResourcePackMod.ARMOR_TEXTURES_1).get(color)), false, stack.hasGlint());
model.render(matrices, vertexConsumer, light, OverlayTexture.DEFAULT_UV, 1f, 1f, 1f, 1.0F);
return;
if (color != null) {
return PolymerResourcePackMod.ARMOR_TEXTURES.getOrDefault(color.rgb(), PolymerResourcePackMod.LEATHER_OVERRIDE);
}

return PolymerResourcePackMod.LEATHER_OVERRIDE;
}

original.call(instance, matrices, vertexConsumers, light, model, red, green, blue, texture);
}*/
return original.call(instance);
}
}

0 comments on commit 4a30a20

Please sign in to comment.