Skip to content

Commit

Permalink
Held Attributes added.
Browse files Browse the repository at this point in the history
  • Loading branch information
minheragon12345 committed Apr 24, 2024
1 parent 06e4043 commit dc5a3b4
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 19 deletions.
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 {
private final Map<Attribute, AttributeModifier> onHeldAttributeModifiers = Maps.newHashMap();

/**
* Used to create a {@link ManasSkillInstance} of this Skill.
* <p>
Expand Down Expand Up @@ -176,6 +181,47 @@ 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);
}

/**
* Applies the attribute modifiers of this skill on the {@link LivingEntity} holding the skill activation button.
*
* @param entity Affected {@link LivingEntity} owning this Skill.
*/
public void addHeldAttributeModifiers(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) {
AttributeModifier attributemodifier = entry.getValue();
attributeinstance.removeModifier(attributemodifier);
attributeinstance.addPermanentModifier(new AttributeModifier(attributemodifier.getId(),
descriptionId, attributemodifier.getAmount(), 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 @@ -10,18 +10,20 @@ public class TickingSkill {
private int duration = 0;
@Getter
private final ManasSkill skill;
public TickingSkill(ManasSkill skill) {
public TickingSkill(ManasSkill skill, LivingEntity entity) {
this.skill = skill;
skill.addHeldAttributeModifiers(entity);
}

public boolean tick(SkillStorage storage, LivingEntity entity) {
Optional<ManasSkillInstance> optional = storage.getSkill(skill);
if (optional.isEmpty()) return false;

ManasSkillInstance instance = optional.get();
if (reachedMaxDuration(instance, entity)) return false;

if (!instance.canInteractSkill(entity)) return false;
if (reachedMaxDuration(instance, entity) || !instance.canInteractSkill(entity)) {
skill.removeHeldAttributeModifiers(entity);
return false;
}
return instance.onHeld(entity, this.duration++);
}

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()))
multimap.get(player.getUUID()).removeIf(tickingSkill -> {
tickingSkill.getSkill().removeHeldAttributeModifiers(player);
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void handle(Supplier<NetworkEvent.Context> ctx) {
if (!skillInstance.canInteractSkill(player)) continue;
if (skillInstance.onCoolDown() && !skillInstance.canIgnoreCoolDown(player)) continue;
skillInstance.onPressed(player);
TickEventListenerHandler.tickingSkills.put(player.getUUID(), new TickingSkill(skillInstance.getSkill()));
TickEventListenerHandler.tickingSkills.put(player.getUUID(), new TickingSkill(skillInstance.getSkill(), player));
}
storage.syncChanges();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public void handle(Supplier<NetworkEvent.Context> ctx) {

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

0 comments on commit dc5a3b4

Please sign in to comment.