Skip to content

Commit

Permalink
Fix step assist not working with other mods
Browse files Browse the repository at this point in the history
Closes #246
  • Loading branch information
rubensworks committed Feb 5, 2024
1 parent d437067 commit 6be3254
Showing 1 changed file with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package org.cyclops.everlastingabilities.ability;

import com.google.common.collect.Maps;
import com.mojang.serialization.Codec;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Rarity;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.common.crafting.conditions.ICondition;
import org.cyclops.everlastingabilities.Reference;
import org.cyclops.everlastingabilities.RegistryEntries;
import org.cyclops.everlastingabilities.api.AbilityTypeAdapter;
import org.cyclops.everlastingabilities.api.IAbilityType;

import java.util.Map;
import java.util.Objects;

/**
Expand All @@ -17,14 +22,19 @@
*/
public class AbilityTypeSpecialStepAssist extends AbilityTypeAdapter {

private static final String PLAYER_NBT_KEY = Reference.MOD_ID + ":" + "stepAssist";
private final boolean forceDefaultStepHeight;
@Deprecated
private final boolean forceDefaultStepHeight; // TODO: rm in next MC version
private final Map<Integer, AttributeModifier> attributeModifiers;

public AbilityTypeSpecialStepAssist(ICondition condition, String name, Rarity rarity, int maxLevel, int baseXpPerLevel,
boolean obtainableOnPlayerSpawn, boolean obtainableOnMobSpawn, boolean obtainableOnCraft, boolean obtainableOnLoot,
boolean forceDefaultStepHeight) {
super(condition, name, rarity, maxLevel, baseXpPerLevel, obtainableOnPlayerSpawn, obtainableOnMobSpawn, obtainableOnCraft, obtainableOnLoot);
this.forceDefaultStepHeight = forceDefaultStepHeight;
this.attributeModifiers = Maps.newHashMap();
for (int i = 1; i <= maxLevel; i++) {
this.attributeModifiers.put(i, new AttributeModifier(Reference.MOD_ID + ":stepHeightModifier" + i, i, AttributeModifier.Operation.ADDITION));
}
}

@Override
Expand All @@ -36,24 +46,16 @@ public boolean isForceDefaultStepHeight() {
return forceDefaultStepHeight;
}

@Override
public void onTick(Player player, int level) {
player.setMaxUpStep(player.isCrouching() ? 0.5F : level);
}

@Override
public void onChangedLevel(Player player, int oldLevel, int newLevel) {
if (oldLevel > 0 && newLevel == 0) {
float stepHeight = 0.6F;
if(player.getPersistentData().contains(PLAYER_NBT_KEY)) {
if (!this.isForceDefaultStepHeight()) {
stepHeight = player.getPersistentData().getFloat(PLAYER_NBT_KEY);
}
player.getPersistentData().remove(PLAYER_NBT_KEY);
AttributeInstance attribute = player.getAttribute(ForgeMod.STEP_HEIGHT_ADDITION.get());
if (attribute != null) {
if (oldLevel > 0) {
attribute.removeModifier(this.attributeModifiers.get(oldLevel));
}
if (newLevel > 0) {
attribute.addPermanentModifier(this.attributeModifiers.get(newLevel));
}
player.setMaxUpStep(stepHeight);
} else if (oldLevel == 0 && newLevel > 0) {
player.getPersistentData().putFloat(PLAYER_NBT_KEY, player.maxUpStep());
}
}
}

0 comments on commit 6be3254

Please sign in to comment.