aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorSHsuperCM <shsupercm@gmail.com>2022-01-15 16:04:31 +0200
committerSHsuperCM <shsupercm@gmail.com>2022-01-15 16:04:31 +0200
commit3e359895c516576050ce9b5c1a4e159c1535a2f6 (patch)
treec252c6c327066dd849958901a89d656119f7a7eb /src/main/java
parent9bd8a6c2166925ece10ae0ded252da03f3a02102 (diff)
downloadCITResewn-3e359895c516576050ce9b5c1a4e159c1535a2f6.tar.gz
CITResewn-3e359895c516576050ce9b5c1a4e159c1535a2f6.tar.bz2
CITResewn-3e359895c516576050ce9b5c1a4e159c1535a2f6.zip
Added support for method=cycle in the enchantment type
Fixes #15
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java4
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/mixin/citenchantment/MinecraftClientMixin.java17
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITEnchantment.java71
-rw-r--r--src/main/java/shcm/shsupercm/util/logic/Loops.java60
4 files changed, 119 insertions, 33 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
index 6362764..8d56a35 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
@@ -179,8 +179,8 @@ public class ActiveCITs {
return;
}
- for (CITEnchantment cit : citEnchantments)
- cit.applyMethod(stack);
+ if (effectiveGlobalProperties.method != null)
+ effectiveGlobalProperties.method.applyMethod(citEnchantments, stack);
CITEnchantment.appliedContext = citEnchantments;
}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/citenchantment/MinecraftClientMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/citenchantment/MinecraftClientMixin.java
new file mode 100644
index 0000000..a9bea56
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/citenchantment/MinecraftClientMixin.java
@@ -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++;
+ }
+}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITEnchantment.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITEnchantment.java
index 759d6cc..7c4fa94 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITEnchantment.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITEnchantment.java
@@ -15,7 +15,6 @@ import net.minecraft.util.Util;
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;
@@ -28,6 +27,7 @@ import java.util.function.Supplier;
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;
@@ -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);
@@ -77,8 +76,6 @@ public class CITEnchantment extends CIT {
};
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());
}
@@ -93,17 +90,6 @@ public class CITEnchantment extends CIT {
}
}
- 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;
@@ -195,10 +181,10 @@ public class CITEnchantment extends CIT {
}
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));
@@ -305,51 +291,74 @@ public class CITEnchantment extends CIT {
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 {
diff --git a/src/main/java/shcm/shsupercm/util/logic/Loops.java b/src/main/java/shcm/shsupercm/util/logic/Loops.java
new file mode 100644
index 0000000..6c18244
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/util/logic/Loops.java
@@ -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;
+ }
+} \ No newline at end of file