Skip to content

Commit

Permalink
完成 geckolib 的兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Oct 30, 2023
1 parent db40d89 commit ed3cd1c
Show file tree
Hide file tree
Showing 170 changed files with 10,805 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package com.github.tartaricacid.touhoulittlemaid.client.animation.gecko;

import com.github.tartaricacid.touhoulittlemaid.client.animation.gecko.condition.*;
import com.github.tartaricacid.touhoulittlemaid.client.entity.GeckoMaidEntity;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.IAnimatable;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.PlayState;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.builder.AnimationBuilder;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.builder.ILoopType;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.event.predicate.AnimationEvent;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.resource.GeckoLibCache;
import com.google.common.collect.Lists;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;

public final class AnimationManager {
private static AnimationManager MANAGER;
private final Int2ObjectOpenHashMap<LinkedList<AnimationState>> data = new Int2ObjectOpenHashMap<>();

public static AnimationManager getInstance() {
if (MANAGER == null) {
MANAGER = new AnimationManager();
}
return MANAGER;
}

@NotNull
private static <P extends IAnimatable> PlayState playLoopAnimation(AnimationEvent<P> event, String animationName) {
return playAnimation(event, animationName, ILoopType.EDefaultLoopTypes.LOOP);
}

@NotNull
private static <P extends IAnimatable> PlayState playAnimation(AnimationEvent<P> event, String animationName, ILoopType loopType) {
event.getController().setAnimation(new AnimationBuilder().addAnimation(animationName, loopType));
return PlayState.CONTINUE;
}

@NotNull
private static <P extends IAnimatable> PlayState playAnimation(AnimationEvent<P> event, String animationName) {
event.getController().setAnimation(new AnimationBuilder().addAnimation(animationName));
return PlayState.CONTINUE;
}

public void register(AnimationState state) {
if (data.containsKey(state.getPriority())) {
data.get(state.getPriority()).add(state);
} else {
LinkedList<AnimationState> states = Lists.newLinkedList();
states.add(state);
data.put(state.getPriority(), states);
}
}

public PlayState predicateParallel(AnimationEvent<GeckoMaidEntity> event, String animationName) {
if (Minecraft.getInstance().isPaused()) {
return PlayState.STOP;
}
return playLoopAnimation(event, animationName);
}

@NotNull
public PlayState predicateMain(AnimationEvent<GeckoMaidEntity> event) {
EntityMaid maid = event.getAnimatable().getMaid();
if (maid == null) {
return PlayState.STOP;
}
for (int i = Priority.HIGHEST; i <= Priority.LOWEST; i++) {
if (!data.containsKey(i)) {
continue;
}
LinkedList<AnimationState> states = data.get(i);
for (AnimationState state : states) {
if (state.getPredicate().test(maid, event)) {
String animationName = state.getAnimationName();
ILoopType loopType = state.getLoopType();
return playAnimation(event, animationName, loopType);
}
}
}
return PlayState.STOP;
}

public PlayState predicateOffhandHold(AnimationEvent<GeckoMaidEntity> event) {
EntityMaid maid = event.getAnimatable().getMaid();
if (maid == null) {
return PlayState.STOP;
}
if (!maid.getOffhandItem().isEmpty() && checkSwingAndUse(maid, InteractionHand.OFF_HAND)) {
ResourceLocation id = event.getAnimatable().getAnimation();
ConditionalHold conditionalHold = ConditionManager.getHoldOffhand(id);
if (conditionalHold != null) {
String name = conditionalHold.doTest(maid, InteractionHand.OFF_HAND);
if (StringUtils.isNoneBlank(name)) {
return playAnimation(event, name, ILoopType.EDefaultLoopTypes.LOOP);
}
}
}
return PlayState.STOP;
}

public PlayState predicateMainhandHold(AnimationEvent<GeckoMaidEntity> event) {
EntityMaid maid = event.getAnimatable().getMaid();
if (maid == null) {
return PlayState.STOP;
}
if (!maid.swinging && !maid.isUsingItem()) {
ItemStack mainHandItem = maid.getItemInHand(InteractionHand.MAIN_HAND);
if (mainHandItem.is(Items.CROSSBOW) && CrossbowItem.isCharged(mainHandItem)) {
return playAnimation(event, "hold_mainhand:charged_crossbow", ILoopType.EDefaultLoopTypes.LOOP);
}
ItemStack offhandItem = maid.getItemInHand(InteractionHand.OFF_HAND);
if (offhandItem.is(Items.CROSSBOW) && CrossbowItem.isCharged(offhandItem)) {
return playAnimation(event, "hold_offhand:charged_crossbow", ILoopType.EDefaultLoopTypes.LOOP);
}
}

if (!maid.getMainHandItem().isEmpty() && checkSwingAndUse(maid, InteractionHand.MAIN_HAND)) {
ResourceLocation id = event.getAnimatable().getAnimation();
ConditionalHold conditionalHold = ConditionManager.getHoldMainhand(id);
if (conditionalHold != null) {
String name = conditionalHold.doTest(maid, InteractionHand.MAIN_HAND);
if (StringUtils.isNoneBlank(name)) {
return playAnimation(event, name, ILoopType.EDefaultLoopTypes.LOOP);
}
}
}
return PlayState.STOP;
}

public PlayState predicateSwing(AnimationEvent<GeckoMaidEntity> event) {
EntityMaid maid = event.getAnimatable().getMaid();
if (maid == null) {
return PlayState.STOP;
}
if (maid.swinging && !maid.isSleeping()) {
if (maid.swingTime == 0) {
event.getController().shouldResetTick = true;
event.getController().adjustTick(0);
}
ResourceLocation id = event.getAnimatable().getAnimation();
ConditionalSwing conditionalSwing = ConditionManager.getSwing(id);
if (conditionalSwing != null) {
String name = conditionalSwing.doTest(maid, maid.swingingArm);
if (StringUtils.isNoneBlank(name)) {
return playAnimation(event, name, ILoopType.EDefaultLoopTypes.LOOP);
}
}
return playAnimation(event, "swing_hand", ILoopType.EDefaultLoopTypes.LOOP);
}
return PlayState.STOP;
}

public PlayState predicateUse(AnimationEvent<GeckoMaidEntity> event) {
EntityMaid maid = event.getAnimatable().getMaid();
if (maid == null) {
return PlayState.STOP;
}
if (maid.isUsingItem() && !maid.isSleeping()) {
if (maid.getTicksUsingItem() == 1) {
event.getController().shouldResetTick = true;
event.getController().adjustTick(0);
}
if (maid.getUsedItemHand() == InteractionHand.MAIN_HAND) {
ResourceLocation id = event.getAnimatable().getAnimation();
ConditionalUse conditionalUse = ConditionManager.getUseMainhand(id);
if (conditionalUse != null) {
String name = conditionalUse.doTest(maid, InteractionHand.MAIN_HAND);
if (StringUtils.isNoneBlank(name)) {
return playAnimation(event, name, ILoopType.EDefaultLoopTypes.LOOP);
}
}
return playAnimation(event, "use_mainhand", ILoopType.EDefaultLoopTypes.LOOP);
} else {
ResourceLocation id = event.getAnimatable().getAnimation();
ConditionalUse conditionalUse = ConditionManager.getUseOffhand(id);
if (conditionalUse != null) {
String name = conditionalUse.doTest(maid, InteractionHand.OFF_HAND);
if (StringUtils.isNoneBlank(name)) {
return playAnimation(event, name, ILoopType.EDefaultLoopTypes.LOOP);
}
}
return playAnimation(event, "use_offhand", ILoopType.EDefaultLoopTypes.LOOP);
}
}
return PlayState.STOP;
}

public PlayState predicateBeg(AnimationEvent<GeckoMaidEntity> event) {
EntityMaid maid = event.getAnimatable().getMaid();
if (maid == null) {
return PlayState.STOP;
}
if (maid.isBegging()) {
return playAnimation(event, "beg", ILoopType.EDefaultLoopTypes.LOOP);
}
return PlayState.STOP;
}

public PlayState predicateArmor(AnimationEvent<GeckoMaidEntity> event, EquipmentSlot slot) {
EntityMaid maid = event.getAnimatable().getMaid();
if (maid == null) {
return PlayState.STOP;
}
ItemStack itemBySlot = maid.getItemBySlot(slot);
if (itemBySlot.isEmpty()) {
return PlayState.STOP;
}

ResourceLocation id = event.getAnimatable().getAnimation();
ConditionArmor conditionArmor = ConditionManager.getArmor(id);
if (conditionArmor != null) {
String name = conditionArmor.doTest(maid, slot);
if (StringUtils.isNoneBlank(name)) {
return playAnimation(event, name, ILoopType.EDefaultLoopTypes.LOOP);
}
}

ResourceLocation animation = event.getAnimatable().getAnimation();
String defaultName = slot.getName() + ":default";
if (GeckoLibCache.getInstance().getAnimations().get(animation).animations().containsKey(defaultName)) {
return playAnimation(event, defaultName, ILoopType.EDefaultLoopTypes.LOOP);
}
return PlayState.STOP;
}

private boolean checkSwingAndUse(EntityMaid maid, InteractionHand hand) {
if (maid.swinging && maid.swingingArm == hand) {
return false;
}
return !maid.isUsingItem() || maid.getUsedItemHand() != hand;
}
}
Loading

0 comments on commit ed3cd1c

Please sign in to comment.