Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private ClientTickEvents() {}
public static void onClientTickPost(ClientTickEvent.Post event) {
PatternEncodingUploadClient.tick(event);
tickClient();
BladeAttackPlayback.tick(event);
BladeAttackPlayback.tick();
SanctifierCarryPlayback.tick(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import com.fish_dan_.data_energistics.client.input.cannon.CannonChargeInput;
import com.fish_dan_.data_energistics.common.item.connector.RemoteLinkConnectorItem;
import com.fish_dan_.data_energistics.common.item.depot.DigitalStorageDepotBlockItem;
import com.fish_dan_.data_energistics.common.item.powered.blade.BladeWeapon;
import com.fish_dan_.data_energistics.common.item.vacuum.MeVacuumItem;
import com.fish_dan_.data_energistics.common.network.packets.action.ConnectorClipboardAction;
import com.fish_dan_.data_energistics.common.network.packets.action.ConnectorClipboardPayload;
import com.fish_dan_.data_energistics.common.network.packets.action.ConnectorScrollPayload;
import com.fish_dan_.data_energistics.common.network.packets.action.DigitalStorageDepotScrollPayload;
import com.fish_dan_.data_energistics.common.network.packets.action.MeVacuumLaunchPayload;
import com.fish_dan_.data_energistics.common.network.packets.blade.BladeAttackRequestPayload;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.SubscribeEvent;
Expand All @@ -32,21 +37,25 @@ private ClientInputEvents() {}
@SubscribeEvent
public static void onInteractionKeyTriggered(InputEvent.InteractionKeyMappingTriggered event) {
Minecraft minecraft = Minecraft.getInstance();
if (minecraft.player == null || minecraft.screen != null) {
LocalPlayer player = minecraft.player;
if (player == null || minecraft.screen != null || !event.isAttack()) {
return;
}

if (event.isAttack() && CannonChargeInput.cannonHand(minecraft.player) != null) {
if (CannonChargeInput.cannonHand(player) != null || tryAttackWithBlade(minecraft, player) || tryLaunchMeVacuum(minecraft)) {
event.setCanceled(true);
event.setSwingHand(false);
return;
}
if (!event.isAttack() || !tryLaunchMeVacuum(minecraft)) {
return;
}
}

event.setCanceled(true);
event.setSwingHand(false);
private static boolean tryAttackWithBlade(Minecraft minecraft, LocalPlayer player) {
HitResult hit = minecraft.hitResult;
if (player.isSpectator() || hit.getType() == HitResult.Type.BLOCK || BladeWeapon.of(player.getMainHandItem()) == null) return false;

minecraft.gameMode.ensureHasSentCarriedItem();
int targetId = hit instanceof EntityHitResult entityHit ? entityHit.getEntity().getId() : BladeAttackRequestPayload.NO_TARGET;
PacketDistributor.sendToServer(new BladeAttackRequestPayload(minecraft.options.keyShift.isDown(), targetId));
return true;
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.fish_dan_.data_energistics.client.render.item.blade.animation;

import com.fish_dan_.data_energistics.common.item.powered.blade.BladeSwing;
import com.fish_dan_.data_energistics.common.item.powered.blade.BladeWeapon;

import net.minecraft.util.Mth;

import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;

/** Numeric root tracks from FMIC heavy_sabre attack_1/2/3, retargeted to the grip of an upright blade. */
public final class BladeAttackMotion {

public static final float DURATION_SECONDS = 0.7083F;
public static final float DURATION_TICKS = DURATION_SECONDS * 20;
private static final float SOURCE_WINDUP_END = 0.0417F;
private static final float SOURCE_CUT_END = 0.125F;
private static final float SOURCE_END = 0.7083F;
private static final float RADIANS = (float) (Math.PI / 180);
// In the reference rig, -X of the sw bone becomes the blade's +Y after its 11.25 degree cant.
private static final float SOURCE_CANT = 11.25F * RADIANS;
private static final Quaternionf SOURCE_BASIS_INVERSE = new Quaternionf().rotationX(-SOURCE_CANT)
.mul(rotation(new Vector3f(20.62983F, 22.07286F, 77.84381F)).invert());
private static final Clip[] CLIPS = {
new Clip(new Track(new Key[] {
key(0, 20.62983F, 22.07286F, 77.84381F),
Expand Down Expand Up @@ -70,18 +78,57 @@ public final class BladeAttackMotion {
private BladeAttackMotion() {}

/** Returns a grip-local rigid transform; the caller applies its existing, correctly oriented hand pose first. */
public static Matrix4f sample(int combo, float seconds) {
if (combo < 0 || combo >= CLIPS.length || !Float.isFinite(seconds)) throw new IllegalArgumentException("Invalid blade attack sample");
if (seconds <= 0 || seconds >= DURATION_SECONDS) return new Matrix4f();
Clip clip = CLIPS[combo];
Quaternionf neutralInverse = rotation(new Vector3f(20.62983F, 22.07286F, 77.84381F)).invert();
Quaternionf basisInverse = new Quaternionf().rotationX(-SOURCE_CANT).mul(neutralInverse);
public static Matrix4f sample(BladeWeapon weapon, BladeSwing swing, float tick) {
float progress = cutProgress(swing, tick);
if (swing == BladeSwing.HEAVY) return heavy(weapon, progress);

float seconds = sourceTime(progress);
Clip clip = CLIPS[swing.ordinal()];
Vector3f position = clip.position.sample(seconds).sub(-5, 14, -5.775F).div(16);
basisInverse.transform(position);
Quaternionf rotation = new Quaternionf(basisInverse).mul(rotation(clip.rotation.sample(seconds))).rotateX(SOURCE_CANT);
SOURCE_BASIS_INVERSE.transform(position);
Quaternionf rotation = new Quaternionf(SOURCE_BASIS_INVERSE).mul(rotation(clip.rotation.sample(seconds))).rotateX(SOURCE_CANT);
return new Matrix4f().translation(position).rotate(rotation);
}

private static float cutProgress(BladeSwing swing, float tick) {
float windup = swing.impactTick() - 1F;
float cut = swing.impactTick() + 0.5F;
float recovery = swing.repeatTick() - 0.5F;
if (tick < windup) return (float) Mth.smoothstep(tick / windup);
if (tick < cut) return 1 + (tick - windup) / (cut - windup);
// Hold the finish pose before the slower return to the guard.
if (tick < recovery) return 2;
return 2 + (float) Mth.smoothstep((tick - recovery) / (swing.duration() - recovery));
}

private static float sourceTime(float progress) {
if (progress < 1) return SOURCE_WINDUP_END * progress;
if (progress < 2) return Mth.lerp(progress - 1, SOURCE_WINDUP_END, SOURCE_CUT_END);
return Mth.lerp(progress - 2, SOURCE_CUT_END, SOURCE_END);
}

private static Matrix4f heavy(BladeWeapon weapon, float progress) {
float backswing;
float followThrough;
if (progress < 1) {
backswing = progress;
followThrough = 0;
} else if (progress < 2) {
backswing = 2 - progress;
followThrough = progress - 1;
} else {
backswing = 0;
followThrough = 3 - progress;
}
float weight = weapon == BladeWeapon.SANCTIFIER ? 1.0F : 0.78F;
return new Matrix4f()
.translation((0.28F * backswing - 0.65F * followThrough) * weight,
(0.42F * backswing - 0.38F * followThrough) * weight, 0.25F * backswing - 0.26F * followThrough)
.rotateZ((0.85F * backswing - 1.45F * followThrough) * weight)
.rotateX((-0.72F * backswing + 1.15F * followThrough) * weight)
.rotateY(0.36F * backswing + 0.54F * followThrough);
}

private static Quaternionf rotation(Vector3f angles) {
return new Quaternionf().rotationZYX(angles.z * RADIANS, angles.y * RADIANS, angles.x * RADIANS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,118 @@
package com.fish_dan_.data_energistics.client.render.item.blade.animation;

import com.fish_dan_.data_energistics.client.render.item.sanctifier.SanctifierHandPresentation;
import com.fish_dan_.data_energistics.common.data.DEItems;
import com.fish_dan_.data_energistics.client.render.item.blade.effect.BladeSlashRenderer;
import com.fish_dan_.data_energistics.common.item.powered.blade.BladeSwing;
import com.fish_dan_.data_energistics.common.item.powered.blade.BladeWeapon;

import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.client.event.ClientTickEvent;

import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.jspecify.annotations.Nullable;

import java.util.UUID;

/** Client-world attack clocks driven by vanilla swing synchronization, shared by both camera perspectives. */
/** Both camera perspectives sample the same server-accepted swings. */
public final class BladeAttackPlayback {

private static final Object2ObjectMap<UUID, BladeAttackPlayback> PLAYERS = new Object2ObjectOpenHashMap<>();
private static final Int2ObjectOpenHashMap<BladeAttackPlayback> SWINGS = new Int2ObjectOpenHashMap<>();
private static @Nullable ClientLevel world;
private final Item item;
private int combo = -1;
private int previousSwingTime;
private boolean previouslySwinging;
private long started = Long.MIN_VALUE;

private BladeAttackPlayback(Item item) {
this.item = item;
private final Player player;
private final BladeWeapon weapon;
private final BladeSwing swing;
private final int started;
private final @Nullable Matrix4f precedingPose;
private boolean impacted;

private BladeAttackPlayback(Player player, BladeWeapon weapon, BladeSwing swing, @Nullable Matrix4f precedingPose) {
this.player = player;
this.weapon = weapon;
this.swing = swing;
this.started = player.tickCount;
this.precedingPose = precedingPose;
}

public static void tick(ClientTickEvent.Post event) {
ClientLevel level = Minecraft.getInstance().level;
public static void begin(Player player, BladeWeapon weapon, BladeSwing swing) {
ClientLevel level = (ClientLevel) player.level();
if (world != level) {
clear();
world = level;
}
if (level == null) return;
for (Player player : level.players()) {
ItemStack stack = player.getMainHandItem();
if (!stack.is(DEItems.DATA_SANCTIFIER.get()) && !stack.is(DEItems.DATA_LIGHT_SABER.get())) {
PLAYERS.remove(player.getUUID());
continue;
}
BladeAttackPlayback playback = PLAYERS.get(player.getUUID());
if (playback == null || playback.item != stack.getItem()) {
playback = new BladeAttackPlayback(stack.getItem());
PLAYERS.put(player.getUUID(), playback);
}
boolean swinging = player.swinging && player.swingingArm == InteractionHand.MAIN_HAND;
if (swinging && (!playback.previouslySwinging || player.swingTime < playback.previousSwingTime) && SanctifierHandPresentation.execution(player) == null) {
long now = level.getGameTime();
playback.combo = playback.combo < 0 || now - playback.started > 30 ? 0 : (playback.combo + 1) % 3;
playback.started = now;
BladeAttackPlayback previous = active(player);
Matrix4f precedingPose = previous == null ? null : previous.sample(player.tickCount - previous.started);
SWINGS.put(player.getId(), new BladeAttackPlayback(player, weapon, swing, precedingPose));
}

public static void tick() {
Minecraft minecraft = Minecraft.getInstance();
if (world != minecraft.level) {
clear();
world = minecraft.level;
}
BladeSlashRenderer.tick();
if (SWINGS.isEmpty()) return;
var entries = Int2ObjectMaps.fastIterator(SWINGS);
while (entries.hasNext()) {
BladeAttackPlayback playback = entries.next().getValue();
Player player = playback.player;
int elapsed = player.tickCount - playback.started;
if (!player.isAlive() || player.isUsingItem() || elapsed >= playback.swing.duration()) {
entries.remove();
} else if (!playback.impacted && elapsed >= playback.swing.impactTick()) {
playback.impacted = true;
if (playback.weapon.matches(player.getMainHandItem())) {
BladeSlashRenderer.start(player, playback.weapon, playback.swing);
if (player == minecraft.player) player.resetAttackStrengthTicker();
}
}
playback.previouslySwinging = swinging;
playback.previousSwingTime = player.swingTime;
}
PLAYERS.object2ObjectEntrySet().removeIf(entry -> level.getPlayerByUUID(entry.getKey()) == null);
}

public static boolean isSwinging(Player player) {
return active(player) != null;
}

public static @Nullable Matrix4f motion(LivingEntity player, float partialTick) {
if (player.level() != world) return null;
BladeAttackPlayback playback = PLAYERS.get(player.getUUID());
if (playback == null || playback.combo < 0 || playback.item != player.getMainHandItem().getItem()) return null;
float elapsed = player.level().getGameTime() - playback.started + partialTick;
return elapsed < 0 || elapsed >= BladeAttackMotion.DURATION_TICKS ? null : BladeAttackMotion.sample(playback.combo, elapsed / 20);
if (player.isUsingItem()) return null;
BladeAttackPlayback playback = active(player);
if (playback != null) return playback.sample(player.tickCount - playback.started + partialTick);
// Vanilla still owns block mining and supplies its main-hand swing.
if (player.swinging && player.swingingArm == InteractionHand.MAIN_HAND) {
BladeWeapon weapon = BladeWeapon.of(player.getMainHandItem());
if (weapon != null) {
return BladeAttackMotion.sample(weapon, BladeSwing.FOREHAND, player.getAttackAnim(partialTick) * BladeSwing.FOREHAND.duration());
}
}
return null;
}

private static @Nullable BladeAttackPlayback active(LivingEntity player) {
BladeAttackPlayback playback = SWINGS.get(player.getId());
return playback != null && playback.player == player && playback.weapon.matches(player.getMainHandItem()) ? playback : null;
}

private @Nullable Matrix4f sample(float elapsed) {
if (elapsed >= swing.duration()) return null;
Matrix4f motion = BladeAttackMotion.sample(weapon, swing, elapsed);
float windup = swing.impactTick() - 1F;
if (precedingPose == null || elapsed >= windup) return motion;

// A quick follow-up starts from the previous grip and joins the new backswing before the cut.
float blend = elapsed / windup;
Vector3f position = precedingPose.getTranslation(new Vector3f()).lerp(motion.getTranslation(new Vector3f()), blend);
Quaternionf rotation = precedingPose.getNormalizedRotation(new Quaternionf()).slerp(motion.getNormalizedRotation(new Quaternionf()), blend);
return motion.translation(position).rotate(rotation);
}

public static void clear() {
PLAYERS.clear();
SWINGS.clear();
BladeSlashRenderer.clear();
world = null;
}
}
Loading
Loading