Skip to content

Commit

Permalink
Added support for method=cycle in the enchantment type
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
SHsuperCM committed Jan 15, 2022
1 parent 9bd8a6c commit 3e35989
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ public void setEnchantmentAppliedContextCached(ItemStack stack, World world, Liv
return;
}

for (CITEnchantment cit : citEnchantments)
cit.applyMethod(stack);
if (effectiveGlobalProperties.method != null)
effectiveGlobalProperties.method.applyMethod(citEnchantments, stack);

CITEnchantment.appliedContext = citEnchantments;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package shcm.shsupercm.fabric.citresewn.mixin.citenchantment;

import net.minecraft.client.MinecraftClient;
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 static shcm.shsupercm.fabric.citresewn.pack.cits.CITEnchantment.MergeMethod.ticks;

@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {
@Inject(method = "tick", at = @At("HEAD"))
public void onTick(CallbackInfo ci) {
ticks++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Vec3f;
import org.lwjgl.opengl.GL11;
import shcm.shsupercm.fabric.citresewn.CITResewn;
import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig;
import shcm.shsupercm.fabric.citresewn.ex.CITParseException;
import shcm.shsupercm.fabric.citresewn.mixin.citenchantment.BufferBuilderStorageAccessor;
Expand All @@ -28,6 +27,7 @@

import static org.lwjgl.opengl.GL11.*;
import static com.mojang.blaze3d.systems.RenderSystem.*;
import static shcm.shsupercm.util.logic.Loops.statelessFadingLoop;

public class CITEnchantment extends CIT {
public static List<CITEnchantment> appliedContext = null;
Expand All @@ -40,7 +40,6 @@ public class CITEnchantment extends CIT {
public final Blend blend;

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

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

Expand Down Expand Up @@ -77,8 +76,6 @@ 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 @@ -93,17 +90,6 @@ 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 @@ -195,10 +181,10 @@ public RenderLayer build(CITEnchantment enchantment) {
}

public VertexConsumer tryApply(VertexConsumer base, RenderLayer baseLayer, VertexConsumerProvider provider) {
if (!shouldApply || appliedContext == null)
if (!shouldApply || appliedContext == null || appliedContext.size() == 0)
return null;

VertexConsumer[] layers = new VertexConsumer[Math.min(appliedContext.size(), CITResewn.INSTANCE.activeCITs.effectiveGlobalProperties.cap)];
VertexConsumer[] layers = new VertexConsumer[Math.min(appliedContext.size(), appliedContext.get(0).pack.cap)];

for (int i = 0; i < layers.length; i++)
layers[i] = provider.getBuffer(appliedContext.get(i).renderLayers.get(GlintRenderLayer.this));
Expand Down Expand Up @@ -305,51 +291,74 @@ public BlendFormatException() {
public enum MergeMethod {
AVERAGE {
@Override
public float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit) {
public void applyIntensity(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;
if (enchantment == null) {
cit.methodIntensity.intensity = 0f;
} else {
float sum = 0f;
for (Integer value : stackEnchantments.values())
sum += value;

return (float) stackEnchantments.get(enchantment) / sum;
cit.methodIntensity.intensity = (float) stackEnchantments.get(enchantment) / sum;
}
}
},
LAYERED {
@Override
public float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit) {
public void applyIntensity(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;
if (enchantment == null) {
cit.methodIntensity.intensity = 0f;
return;
}

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

return (float) stackEnchantments.get(enchantment) / max;
cit.methodIntensity.intensity = (float) stackEnchantments.get(enchantment) / max;
}
},
CYCLE {
@Override
public float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit) {
return 1f;
public void applyMethod(List<CITEnchantment> citEnchantments, ItemStack stack) {
List<Map.Entry<CITEnchantment, Float>> durations = new ArrayList<>();
for (CITEnchantment cit : citEnchantments)
durations.add(new HashMap.SimpleEntry<>(cit, cit.duration));

for (Map.Entry<CITEnchantment, Float> intensity : statelessFadingLoop(durations, citEnchantments.get(0).pack.fade, ticks, 20).entrySet())
intensity.getKey().methodIntensity.intensity = intensity.getValue();
}
};

public abstract float getIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit);
public static int ticks = 0;

public void applyIntensity(Map<Identifier, Integer> stackEnchantments, CITEnchantment cit) {
cit.methodIntensity.intensity = 1f;
}

public void applyMethod(List<CITEnchantment> citEnchantments, ItemStack stack) {
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));

for (CITEnchantment cit : citEnchantments)
if (!cit.enchantmentsAny)
applyIntensity(stackEnchantments, cit);
}
}

private static class WrappedMethodIntensity {
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/shcm/shsupercm/util/logic/Loops.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package shcm.shsupercm.util.logic;

import java.util.*;

/**
* This class(or class portion) is a part of SHCM's utilities. Feel free to use without credit.
*/
public class Loops {
/**
* Creates a loop of T with linked intensities allowing for fading between the elements.
* @param items list of items and pause durations(in time units) ordered as they are in the loop
* @param fade time in units to fade between each item
* @param ticks positive raising counter
* @param tpu the amount of ticks per time unit
* @param <T> element type
* @return map of elements and their respective intensities(between 0.0f and 1.0f)
*/
public static <T> Map<T, Float> statelessFadingLoop(List<Map.Entry<T, Float>> items, float fade, int ticks, int tpu) {
Map<T, Float> itemValues = new HashMap<>();

if (items == null || items.size() == 0)
return itemValues;

if (items.size() == 1) {
itemValues.put(items.get(0).getKey(), 1f);
return itemValues;
}

float totalUnitsInLoop = 0f;
for (Map.Entry<T, Float> item : items) {
itemValues.put(item.getKey(), 0f);
totalUnitsInLoop += item.getValue() + fade;
}

float unitInLoop = (ticks % (tpu * totalUnitsInLoop)) / tpu;

for (int i = 0; i < items.size(); i++) {
Map.Entry<T, Float> item = items.get(i);
if (unitInLoop < item.getValue()) {
itemValues.put(item.getKey(), 1f);
break;
} else
unitInLoop -= item.getValue();

if (unitInLoop < fade) {
Map.Entry<T, Float> nextItem = items.get(i + 1 >= items.size() ? 0 : i + 1);

unitInLoop /= fade;

itemValues.put(item.getKey(), 1f - unitInLoop);
itemValues.put(nextItem.getKey(), unitInLoop);

break;
} else
unitInLoop -= fade;
}

return itemValues;
}
}
1 change: 1 addition & 0 deletions src/main/resources/citresewn.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"citenchantment.ItemRendererMixin",
"citenchantment.ItemStackMixin",
"citenchantment.RenderPhaseAccessor",
"citenchantment.MinecraftClientMixin",
"cititem.ItemRendererMixin",
"cititem.ItemStackMixin",
"cititem.JsonUnbakedModelAccessor",
Expand Down

0 comments on commit 3e35989

Please sign in to comment.