Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Held Attributes added. #33

Merged
merged 4 commits into from
Apr 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ forgeVersion=43.3.5
# Parchment Version
parchmentVersion=2022.11.27
# Mod Information see https://mcforge.readthedocs.io/en/1.18.x/gettingstarted/versioning/#examples
modVersion=2.1.3.4
modVersion=2.1.3.5
modId=manascore
# Mixin Extras
mixinExtrasVersion=0.3.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
import com.github.manasmods.manascore.api.skills.capability.SkillStorage;
import com.github.manasmods.manascore.api.skills.event.SkillDamageEvent;
import com.github.manasmods.manascore.api.skills.event.UnlockSkillEvent;
import com.google.common.collect.Maps;
import net.minecraft.Util;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.*;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.Event;
import org.jetbrains.annotations.ApiStatus;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
* This is the Registry Object for Skills.
Expand All @@ -39,6 +42,8 @@
*/
@ApiStatus.AvailableSince("1.0.2.0")
public class ManasSkill {
protected final Map<Attribute, AttributeModifier> onHeldAttributeModifiers = Maps.newHashMap();

/**
* Used to create a {@link ManasSkillInstance} of this Skill.
* <p>
Expand Down Expand Up @@ -176,6 +181,59 @@ public void addMasteryPoint(ManasSkillInstance instance, LivingEntity entity) {
if (isMastered(instance, entity)) onSkillMastered(instance, entity);
}

/**
* Adds an attribute modifier to this skill. This method can be called for more than one attribute.
* The attributes are applied to an entity when the skill is held and removed when it stops being held.
* </p>
*/
public void addHeldAttributeModifier(Attribute pAttribute, String pUuid, double pAmount, AttributeModifier.Operation pOperation) {
AttributeModifier attributemodifier = new AttributeModifier(UUID.fromString(pUuid),
Util.makeDescriptionId("skill", this.getRegistryName()), pAmount, pOperation);
this.onHeldAttributeModifiers.put(pAttribute, attributemodifier);
}

/**
* @return the amplifier for each attribute modifier that this skill applies.
* </p>
* @param entity Affected {@link LivingEntity} owning this Skill.
* @param instance Affected {@link ManasSkillInstance}
* @param modifier Affected {@link AttributeModifier} that this skill provides.
*/
public double getAttributeModifieraAmplifier(ManasSkillInstance instance, LivingEntity entity, AttributeModifier modifier) {
return 1;
}

/**
* Applies the attribute modifiers of this skill on the {@link LivingEntity} holding the skill activation button.
*
* @param entity Affected {@link LivingEntity} owning this Skill.
* @param instance Affected {@link ManasSkillInstance}
*/
public void addHeldAttributeModifiers(ManasSkillInstance instance, LivingEntity entity) {
String descriptionId = Util.makeDescriptionId("skill", this.getRegistryName());
for(Map.Entry<Attribute, AttributeModifier> entry : this.onHeldAttributeModifiers.entrySet()) {
AttributeInstance attributeinstance = entity.getAttributes().getInstance(entry.getKey());
if (attributeinstance != null && !attributeinstance.hasModifier(entry.getValue())) {
AttributeModifier attributemodifier = entry.getValue();
attributeinstance.removeModifier(attributemodifier);
attributeinstance.addPermanentModifier(new AttributeModifier(attributemodifier.getId(),
descriptionId, instance.getAttributeModifierAmplifier(entity, attributemodifier), attributemodifier.getOperation()));
}
}
}

/**
* Removes the attribute modifiers of this skill from the {@link LivingEntity} holding the skill activation button.
*
* @param entity Affected {@link LivingEntity} owning this Skill.
*/
public void removeHeldAttributeModifiers(LivingEntity entity) {
for(Map.Entry<Attribute, AttributeModifier> entry : this.onHeldAttributeModifiers.entrySet()) {
AttributeInstance attributeinstance = entity.getAttributes().getInstance(entry.getKey());
if (attributeinstance != null) attributeinstance.removeModifier(entry.getValue());
}
}

/**
* Called when the {@link LivingEntity} owing this Skill toggles it on.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
Expand Down Expand Up @@ -366,6 +367,25 @@ public void setTag(@Nullable CompoundTag tag) {
markDirty();
}

/**
* @return the amplifier for each attribute modifier that this instance applies.
* </p>
* @param entity Affected {@link LivingEntity} owning this Skill.
* @param modifier Affected {@link AttributeModifier} that this skill provides.
*/
public double getAttributeModifierAmplifier(LivingEntity entity, AttributeModifier modifier) {
return this.getSkill().getAttributeModifieraAmplifier(this, entity, modifier);
}

/**
* Applies the attribute modifiers of this instance on the {@link LivingEntity} holding the skill activation button.
*
* @param entity Affected {@link LivingEntity} owning this Skill.
*/
public void addHeldAttributeModifiers(LivingEntity entity) {
this.getSkill().addHeldAttributeModifiers(this, entity);
}

/**
* Called when the {@link LivingEntity} owning this Skill toggles this {@link ManasSkill} type of this instance on.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.monster.Creeper;
import net.minecraft.world.entity.monster.Spider;
import net.minecraft.world.entity.player.Player;
Expand All @@ -16,24 +18,23 @@
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.*;
import net.minecraftforge.event.entity.player.PlayerEvent;
import org.jetbrains.annotations.ApiStatus.Internal;

@Internal
@Log4j2
public class TestSkill extends ManasSkill {
protected static final String TEST = "e83b2e47-ef49-4af5-b5da-4fd14a5c8777";
public TestSkill(){
MinecraftForge.EVENT_BUS.addListener(this::unlock);
this.addHeldAttributeModifier(Attributes.MOVEMENT_SPEED, TEST, 1.0F, AttributeModifier.Operation.MULTIPLY_TOTAL);
}

public boolean canTick(ManasSkillInstance instance, LivingEntity entity) {
return instance.isToggled();
}

public void onToggleOn(ManasSkillInstance instance, LivingEntity entity) {
log.debug("Toggled On");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ public static void onDeath(final LivingDeathEvent e) {
public static void onLogOut(final PlayerEvent.PlayerLoggedOutEvent e) {
Player player = e.getEntity();
Multimap<UUID, TickingSkill> multimap = TickEventListenerHandler.tickingSkills;
if (multimap.containsKey(player.getUUID())) multimap.removeAll(player.getUUID());
if (multimap.containsKey(player.getUUID())) {
for (TickingSkill tickingSkill : multimap.get(player.getUUID()))
tickingSkill.getSkill().removeHeldAttributeModifiers(player);
multimap.removeAll(player.getUUID());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ public void handle(Supplier<NetworkEvent.Context> ctx) {

Optional<ManasSkillInstance> optional = storage.getSkill(manasSkill);
if (optional.isEmpty()) continue;
ManasSkillInstance skillInstance = optional.get();
ManasSkillInstance instance = optional.get();

if (!skillInstance.canInteractSkill(player)) continue;
if (skillInstance.onCoolDown() && !skillInstance.canIgnoreCoolDown(player)) continue;
skillInstance.onPressed(player);
TickEventListenerHandler.tickingSkills.put(player.getUUID(), new TickingSkill(skillInstance.getSkill()));
if (!instance.canInteractSkill(player)) continue;
if (instance.onCoolDown() && !instance.canIgnoreCoolDown(player)) continue;

instance.onPressed(player);
instance.addHeldAttributeModifiers(player);
TickEventListenerHandler.tickingSkills.put(player.getUUID(), new TickingSkill(instance.getSkill()));
}
storage.syncChanges();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public void handle(Supplier<NetworkEvent.Context> ctx) {

if (!skillInstance.canInteractSkill(player)) continue;
if (skillInstance.onCoolDown() && !skillInstance.canIgnoreCoolDown(player)) continue;

skillInstance.onRelease(player, this.heldTick);
skillInstance.getSkill().removeHeldAttributeModifiers(player);

Multimap<UUID, TickingSkill> multimap = TickEventListenerHandler.tickingSkills;
if (multimap.containsKey(player.getUUID())) {
multimap.get(player.getUUID()).removeIf(tickingSkill -> tickingSkill.getSkill() == skillInstance.getSkill());
}
if (multimap.containsKey(player.getUUID())) multimap.get(player.getUUID()).removeIf(tickingSkill -> tickingSkill.getSkill() == skillInstance.getSkill());
}
storage.syncChanges();
}
Expand Down
Loading