Allow execution animation on other clients
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
package net.fretux.knockedback.client;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ClientExecutionState {
|
||||
private static int timeLeft = 0;
|
||||
@Nullable
|
||||
private static UUID executorId;
|
||||
|
||||
public static void setTimeLeft(int ticks) {
|
||||
public static void setExecution(int ticks, @Nullable UUID executor) {
|
||||
timeLeft = ticks;
|
||||
executorId = ticks > 0 ? executor : null;
|
||||
}
|
||||
|
||||
public static int getTimeLeft() {
|
||||
@@ -14,4 +20,8 @@ public class ClientExecutionState {
|
||||
public static boolean isExecuting() {
|
||||
return timeLeft > 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isExecutor(UUID playerId) {
|
||||
return timeLeft > 0 && executorId != null && executorId.equals(playerId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import dev.kosmx.playerAnim.api.layered.ModifierLayer;
|
||||
import dev.kosmx.playerAnim.minecraftApi.PlayerAnimationAccess;
|
||||
import dev.kosmx.playerAnim.minecraftApi.PlayerAnimationRegistry;
|
||||
import net.fretux.knockedback.KnockedBack;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.player.AbstractClientPlayer;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
@@ -19,8 +20,8 @@ import java.util.UUID;
|
||||
|
||||
@Mod.EventBusSubscriber(modid = KnockedBack.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.FORGE)
|
||||
public class PlayerAnimationHandler {
|
||||
private static final ResourceLocation GRIP_ANIMATION_ID =
|
||||
ResourceLocation.fromNamespaceAndPath(KnockedBack.MOD_ID, "animation.model.gripping");
|
||||
private static final ResourceLocation EXECUTION_ANIMATION_ID =
|
||||
ResourceLocation.fromNamespaceAndPath(KnockedBack.MOD_ID, "animation.model.execution");
|
||||
private static final Map<UUID, ModifierLayer<IAnimation>> layers = new HashMap<>();
|
||||
private static final Map<UUID, Boolean> activeStates = new HashMap<>();
|
||||
|
||||
@@ -29,17 +30,17 @@ public class PlayerAnimationHandler {
|
||||
if (event.phase != TickEvent.Phase.END) return;
|
||||
if (!(event.player instanceof AbstractClientPlayer player)) return;
|
||||
UUID playerId = player.getUUID();
|
||||
boolean shouldGrip = ClientGrippedState.isGripped(playerId);
|
||||
boolean wasGripping = activeStates.getOrDefault(playerId, false);
|
||||
if (shouldGrip == wasGripping) return;
|
||||
activeStates.put(playerId, shouldGrip);
|
||||
boolean shouldPlayExecution = ClientExecutionState.isExecutor(playerId);
|
||||
boolean wasPlaying = activeStates.getOrDefault(playerId, false);
|
||||
if (shouldPlayExecution == wasPlaying) return;
|
||||
activeStates.put(playerId, shouldPlayExecution);
|
||||
ModifierLayer<IAnimation> layer = layers.computeIfAbsent(playerId, id -> {
|
||||
ModifierLayer<IAnimation> newLayer = new ModifierLayer<>();
|
||||
PlayerAnimationAccess.getPlayerAnimLayer(player).addAnimLayer(0, newLayer);
|
||||
return newLayer;
|
||||
});
|
||||
if (shouldGrip) {
|
||||
var animation = PlayerAnimationRegistry.getAnimation(GRIP_ANIMATION_ID);
|
||||
if (shouldPlayExecution) {
|
||||
var animation = PlayerAnimationRegistry.getAnimation(EXECUTION_ANIMATION_ID);
|
||||
if (animation != null) {
|
||||
layer.setAnimation(new KeyframeAnimationPlayer(animation));
|
||||
}
|
||||
@@ -47,4 +48,4 @@ public class PlayerAnimationHandler {
|
||||
layer.setAnimation(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user