Skip to content

Commit

Permalink
Added support for average and layered enchantment merging methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SHsuperCM committed Jan 11, 2022
1 parent 3281ac9 commit b6267ae
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public void setEnchantmentAppliedContextCached(ItemStack stack, World world, Liv
return;
}

for (CITEnchantment cit : citEnchantments)
cit.applyMethod(stack);

CITEnchantment.appliedContext = citEnchantments;
}
}
10 changes: 3 additions & 7 deletions src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import net.minecraft.resource.ResourcePack;
import shcm.shsupercm.fabric.citresewn.pack.cits.CIT;
import shcm.shsupercm.fabric.citresewn.pack.cits.CITEnchantment;

import java.util.*;

public class CITPack {
public final ResourcePack resourcePack;
public final Collection<CIT> cits = new ArrayList<>();

public EnchantmentMergeMethod method = EnchantmentMergeMethod.AVERAGE;
public CITEnchantment.MergeMethod method = CITEnchantment.MergeMethod.AVERAGE;
public Integer cap = 8;
public Float fade = 0.5f;
public Boolean useGlint = true;
Expand All @@ -20,7 +21,7 @@ public CITPack(ResourcePack resourcePack) {

public void loadGlobalProperties(Properties properties) throws Exception {
try {
this.method = properties.containsKey("method") ? CITPack.EnchantmentMergeMethod.valueOf(properties.getProperty("method").toUpperCase(Locale.ENGLISH)) : null;
this.method = properties.containsKey("method") ? CITEnchantment.MergeMethod.valueOf(properties.getProperty("method").toUpperCase(Locale.ENGLISH)) : null;

if (properties.containsKey("cap")) {
this.cap = Integer.parseInt(properties.getProperty("cap"));
Expand Down Expand Up @@ -61,9 +62,4 @@ public void loadGlobalProperties(CITPack properties) {
this.useGlint = properties.useGlint;
}

public enum EnchantmentMergeMethod {
AVERAGE,
LAYERED,
CYCLE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.*;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.EnchantedBookItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
Expand Down Expand Up @@ -33,6 +39,9 @@ public class CITEnchantment extends CIT {
public final boolean useGlint, blur;
public final Blend blend;

private final WrappedMethodIntensity methodIntensity = new WrappedMethodIntensity();
private final MergeMethod method;

public final Map<GlintRenderLayer, RenderLayer> renderLayers = new EnumMap<>(GlintRenderLayer.class);

public CITEnchantment(CITPack pack, Identifier identifier, Properties properties) throws CITParseException {
Expand Down Expand Up @@ -68,6 +77,8 @@ public CITEnchantment(CITPack pack, Identifier identifier, Properties properties
};

blend = Blend.getBlend(properties.getProperty("blend", "add"));

method = !enchantmentsAny && this.enchantments.size() > 0 ? pack.method : null;
} catch (Exception e) {
throw new CITParseException(pack.resourcePack, identifier, (e.getClass() == Exception.class ? "" : e.getClass().getSimpleName() + ": ") + e.getMessage());
}
Expand All @@ -82,6 +93,17 @@ public void activate() {
}
}

public void applyMethod(ItemStack stack) {
if (this.method != null) {
Map<Identifier, Integer> stackEnchantments = new LinkedHashMap<>();
for (NbtElement nbtElement : stack.isOf(Items.ENCHANTED_BOOK) ? EnchantedBookItem.getEnchantmentNbt(stack) : stack.getEnchantments())
stackEnchantments.put(EnchantmentHelper.getIdFromNbt((NbtCompound) nbtElement), EnchantmentHelper.getLevelFromNbt((NbtCompound) nbtElement));

this.methodIntensity.intensity = this.method.getIntensity(stackEnchantments, this);
} else
this.methodIntensity.intensity = 1f;
}

@Override
public void dispose() {
appliedContext = null;
Expand Down Expand Up @@ -142,6 +164,7 @@ public enum GlintRenderLayer {

public RenderLayer build(CITEnchantment enchantment) {
final float speed = enchantment.speed, rotation = enchantment.rotation, r = enchantment.r, g = enchantment.g, b = enchantment.b, a = enchantment.a;
final WrappedMethodIntensity methodIntensity = enchantment.methodIntensity;
//noinspection ConstantConditions
RenderLayer.MultiPhaseParameters.Builder layer = RenderLayer.MultiPhaseParameters.builder()
.texture(new RenderPhase.Texture(enchantment.textureIdentifier, enchantment.blur, false))
Expand All @@ -154,7 +177,7 @@ public RenderLayer build(CITEnchantment enchantment) {
matrix4f.multiply(Matrix4f.scale(scale, scale, scale));
setTextureMatrix(matrix4f);

setShaderColor(r, g, b, a);
setShaderColor(r, g, b, a * methodIntensity.intensity);
}, () -> {
RenderSystem.resetTextureMatrix();

Expand Down Expand Up @@ -279,6 +302,60 @@ public BlendFormatException() {
}
}

public enum MergeMethod {
AVERAGE {
@Override
public float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit) {
Identifier enchantment = null;
for (Identifier enchantmentMatch : cit.enchantments)
if (stackEnchantments.containsKey(enchantmentMatch)) {
enchantment = enchantmentMatch;
break;
}
if (enchantment == null)
return 0f;

float sum = 0f;
for (Integer value : stackEnchantments.values())
sum += value;

return (float) stackEnchantments.get(enchantment) / sum;
}
},
LAYERED {
@Override
public float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit) {
Identifier enchantment = null;
for (Identifier enchantmentMatch : cit.enchantments)
if (stackEnchantments.containsKey(enchantmentMatch)) {
enchantment = enchantmentMatch;
break;
}
if (enchantment == null)
return 0f;

float max = 0f;
for (Integer value : stackEnchantments.values())
if (value > max)
max = value;

return (float) stackEnchantments.get(enchantment) / max;
}
},
CYCLE {
@Override
public float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit) {
return 1f;
}
};

public abstract float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit);
}

private static class WrappedMethodIntensity {
public float intensity = 1f;
}

public interface Cached {
List<CITEnchantment> citresewn_getCachedCITEnchantment(Supplier<List<CITEnchantment>> realtime);
}
Expand Down

0 comments on commit b6267ae

Please sign in to comment.