Skip to content

Commit

Permalink
method moving
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed May 15, 2024
1 parent 88cda84 commit 48ddfcf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 67 deletions.
56 changes: 0 additions & 56 deletions common/src/main/java/dev/rdh/createunlimited/Util.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
package dev.rdh.createunlimited;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.mojang.brigadier.arguments.ArgumentType;

import dev.architectury.injectables.annotations.ExpectPlatform;

import dev.rdh.createunlimited.config.CUConfigs;

import com.simibubi.create.foundation.config.ConfigBase.CValue;
import com.simibubi.create.foundation.config.ConfigBase.ConfigBool;

import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Direction.Axis;

import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;

import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
import net.minecraftforge.fml.config.IConfigSpec;
import net.minecraftforge.fml.config.ModConfig;

import java.util.UUID;
import java.util.function.Supplier;

public abstract class Util {

@ExpectPlatform
Expand Down Expand Up @@ -61,50 +49,6 @@ public static Attribute getReachAttribute() {
throw new AssertionError();
}

public static Supplier<Multimap<Attribute, AttributeModifier>> singleRange() {
AttributeModifier am = new AttributeModifier(UUID.fromString("7f7dbdb2-0d0d-458a-aa40-ac7633691f66"), "Range modifier",
orElse(CUConfigs.server.singleExtendoGripRange, 3), AttributeModifier.Operation.ADDITION);
return Suppliers.memoize(() -> ImmutableMultimap.of(getReachAttribute(), am));
}

public static Supplier<Multimap<Attribute, AttributeModifier>> doubleRange() {
AttributeModifier am = new AttributeModifier(UUID.fromString("8f7dbdb2-0d0d-458a-aa40-ac7633691f66"), "Range modifier",
orElse(CUConfigs.server.doubleExtendoGripRange, 5), AttributeModifier.Operation.ADDITION);
return Suppliers.memoize(() -> ImmutableMultimap.of(getReachAttribute(), am));
}

@SuppressWarnings("SuspiciousNameCombination") // javac doesn't like when we pass a value called "y" to a method that expects a value called "x"
public static double[] intersect(Vec3 p1, Vec3 p2, Vec3 r, Vec3 s, Axis plane) {
if (plane == Axis.X) {
p1 = new Vec3(p1.y, 0, p1.z);
p2 = new Vec3(p2.y, 0, p2.z);
r = new Vec3(r.y, 0, r.z);
s = new Vec3(s.y, 0, s.z);
}

if (plane == Axis.Z) {
p1 = new Vec3(p1.x, 0, p1.y);
p2 = new Vec3(p2.x, 0, p2.y);
r = new Vec3(r.x, 0, r.y);
s = new Vec3(s.x, 0, s.y);
}

Vec3 qminusp = p2.subtract(p1);
double rcs = r.x * s.z - r.z * s.x;
Vec3 rdivrcs = r.scale(1 / rcs);
Vec3 sdivrcs = s.scale(1 / rcs);
double t = qminusp.x * sdivrcs.z - qminusp.z * sdivrcs.x;
double u = qminusp.x * rdivrcs.z - qminusp.z * rdivrcs.x;
return new double[]{t, u};
}

public static ItemStack copyStackWithSize(ItemStack stack, int size) {
if (size == 0) return ItemStack.EMPTY;
ItemStack copy = stack.copy();
copy.setCount(size);
return copy;
}

public static <V, T extends ConfigValue<V>> V orElse(CValue<V, T> value, V orElse) {
try {
return value.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.rdh.createunlimited.asm.mixin;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
Expand All @@ -10,14 +12,16 @@
import net.minecraft.world.entity.ai.attributes.AttributeModifier;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

import dev.architectury.injectables.annotations.PlatformOnly;

import java.util.function.Supplier;
import dev.rdh.createunlimited.Util;
import dev.rdh.createunlimited.config.CUConfigs;

import static dev.rdh.createunlimited.Util.doubleRange;
import static dev.rdh.createunlimited.Util.singleRange;
import java.util.UUID;
import java.util.function.Supplier;

@SuppressWarnings({"unused", "UnresolvedMixinReference", "MixinAnnotationTarget"})
@Mixin(value = ExtendoGripItem.class, remap = false)
Expand All @@ -28,13 +32,12 @@ public abstract class ExtendoGripItemMixin {
@ModifyExpressionValue(method = {
"holdingExtendoGripIncreasesRange(Lnet/minecraft/world/entity/LivingEntity;)V",
"holdingExtendoGripIncreasesRange(Lnet/minecraft/class_1309;)V",

"addReachToJoiningPlayersHoldingExtendo(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/nbt/CompoundTag;)V",
"addReachToJoiningPlayersHoldingExtendo(Lnet/minecraft/class_1297;Lnet/minecraft/class_2487;)V",
}, at = @At(value = "FIELD", target = "Lcom/simibubi/create/content/equipment/extendoGrip/ExtendoGripItem;rangeModifier:Ljava/util/function/Supplier;"))
@PlatformOnly(PlatformOnly.FABRIC)
private static Supplier<Multimap<Attribute, AttributeModifier>> modifySingleFabric(Supplier<?> original) {
return singleRange();
return cu$singleRange();
}

@ModifyExpressionValue(method = {
Expand All @@ -43,7 +46,7 @@ private static Supplier<Multimap<Attribute, AttributeModifier>> modifySingleFabr
}, at = @At(value = "FIELD", target = "Lcom/simibubi/create/content/equipment/extendoGrip/ExtendoGripItem;rangeModifier:Ljava/util/function/Supplier;"))
@PlatformOnly(PlatformOnly.FORGE)
private static Supplier<Multimap<Attribute, AttributeModifier>> modifySingleForge(Supplier<?> original) {
return singleRange();
return cu$singleRange();
}

@ModifyExpressionValue(method = {
Expand All @@ -54,7 +57,7 @@ private static Supplier<Multimap<Attribute, AttributeModifier>> modifySingleForg
}, at = @At(value = "FIELD", target = "Lcom/simibubi/create/content/equipment/extendoGrip/ExtendoGripItem;doubleRangeModifier:Ljava/util/function/Supplier;"))
@PlatformOnly(PlatformOnly.FABRIC)
private static Supplier<Multimap<Attribute, AttributeModifier>> modifyDoubleFabric(Supplier<?> original) {
return doubleRange();
return cu$doubleRange();
}

@ModifyExpressionValue(method = {
Expand All @@ -63,6 +66,20 @@ private static Supplier<Multimap<Attribute, AttributeModifier>> modifyDoubleFabr
}, at = @At(value = "FIELD", target = "Lcom/simibubi/create/content/equipment/extendoGrip/ExtendoGripItem;doubleRangeModifier:Ljava/util/function/Supplier;"))
@PlatformOnly(PlatformOnly.FORGE)
private static Supplier<Multimap<Attribute, AttributeModifier>> modifyDoubleForge(Supplier<?> original) {
return doubleRange();
return cu$doubleRange();
}

@Unique
private static Supplier<Multimap<Attribute, AttributeModifier>> cu$singleRange() {
AttributeModifier am = new AttributeModifier(UUID.fromString("7f7dbdb2-0d0d-458a-aa40-ac7633691f66"), "Range modifier",
Util.orElse(CUConfigs.server.singleExtendoGripRange, 3), AttributeModifier.Operation.ADDITION);
return Suppliers.memoize(() -> ImmutableMultimap.of(Util.getReachAttribute(), am));
}

@Unique
private static Supplier<Multimap<Attribute, AttributeModifier>> cu$doubleRange() {
AttributeModifier am = new AttributeModifier(UUID.fromString("8f7dbdb2-0d0d-458a-aa40-ac7633691f66"), "Range modifier",
Util.orElse(CUConfigs.server.doubleExtendoGripRange, 5), AttributeModifier.Operation.ADDITION);
return Suppliers.memoize(() -> ImmutableMultimap.of(Util.getReachAttribute(), am));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.simibubi.create.content.trains.track.TrackPlacement;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import dev.rdh.createunlimited.Util;
import dev.rdh.createunlimited.asm.Asm;

import net.minecraft.core.Direction.Axis;
Expand All @@ -20,11 +20,37 @@ public abstract class TrackPlacementMixin {

@Redirect(method = "tryConnect", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/foundation/utility/VecHelper;intersect(Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/core/Direction$Axis;)[D", ordinal = 2))
private static double[] modifyIntersect2(Vec3 p1, Vec3 p2, Vec3 r, Vec3 s, Axis plane) {
return Util.intersect(p1, p2, r, s, plane);
return cu$intersect(p1, p2, r, s, plane);
}

@Redirect(method = "tryConnect", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/foundation/utility/VecHelper;intersect(Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/core/Direction$Axis;)[D", ordinal = 3))
private static double[] modifyIntersect3(Vec3 p1, Vec3 p2, Vec3 r, Vec3 s, Axis plane) {
return Util.intersect(p1, p2, r, s, plane);
return cu$intersect(p1, p2, r, s, plane);
}

@Unique
@SuppressWarnings("SuspiciousNameCombination") // javac doesn't like when we pass a value called "y" to a method that expects a value called "x"
private static double[] cu$intersect(Vec3 p1, Vec3 p2, Vec3 r, Vec3 s, Axis plane) {
if (plane == Axis.X) {
p1 = new Vec3(p1.y, 0, p1.z);
p2 = new Vec3(p2.y, 0, p2.z);
r = new Vec3(r.y, 0, r.z);
s = new Vec3(s.y, 0, s.z);
}

if (plane == Axis.Z) {
p1 = new Vec3(p1.x, 0, p1.y);
p2 = new Vec3(p2.x, 0, p2.y);
r = new Vec3(r.x, 0, r.y);
s = new Vec3(s.x, 0, s.y);
}

Vec3 qminusp = p2.subtract(p1);
double rcs = r.x * s.z - r.z * s.x;
Vec3 rdivrcs = r.scale(1 / rcs);
Vec3 sdivrcs = s.scale(1 / rcs);
double t = qminusp.x * sdivrcs.z - qminusp.z * sdivrcs.x;
double u = qminusp.x * rdivrcs.z - qminusp.z * rdivrcs.x;
return new double[]{t, u};
}
}

0 comments on commit 48ddfcf

Please sign in to comment.