From 6d00c691193a01c6a0112bbd4f9707601a2255c0 Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Sat, 10 Aug 2024 13:31:28 +0200 Subject: [PATCH 01/12] Fix bug with read time out on projectlombok --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index e848d830..e20b6ebc 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ plugins { id "architectury-plugin" version "3.4-SNAPSHOT" id "dev.architectury.loom" version "1.6-SNAPSHOT" apply false - id "io.freefair.lombok" version "8.4" apply false + id "io.freefair.lombok" version "8.7.1" apply false id "org.sonarqube" version "4.4.1.3373" } From 1c5164d93520c8959183911318be2af8d795ec9c Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Sat, 10 Aug 2024 19:06:42 +0200 Subject: [PATCH 02/12] Change AttributeHelper API Most significant change is the usage of Holder for Attributes and ResourceLocation instead of UUIDs --- .../attribute/ManasCoreAttributeHelper.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/com/github/manasmods/manascore/api/attribute/ManasCoreAttributeHelper.java b/common/src/main/java/com/github/manasmods/manascore/api/attribute/ManasCoreAttributeHelper.java index 1ef8be60..62057c51 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/attribute/ManasCoreAttributeHelper.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/attribute/ManasCoreAttributeHelper.java @@ -1,6 +1,8 @@ package com.github.manasmods.manascore.api.attribute; import lombok.experimental.UtilityClass; +import net.minecraft.core.Holder; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.AttributeInstance; @@ -19,13 +21,13 @@ public class ManasCoreAttributeHelper { * @param attribute Target Attribute * @param modifier Attribute modifier */ - public static void setModifier(final LivingEntity entity, final Attribute attribute, final AttributeModifier modifier) { + public static void setModifier(final LivingEntity entity, final Holder attribute, final AttributeModifier modifier) { AttributeInstance instance = entity.getAttribute(attribute); if (instance == null) return; double oldMaxHealth = attribute == Attributes.MAX_HEALTH ? entity.getMaxHealth() : 0; // Store old max health or 0 - Optional.ofNullable(instance.getModifier(modifier.getId())) //Get old modifier if present - .ifPresent(modifier1 -> instance.removeModifier(modifier1.getId())); //Remove old modifier + Optional.ofNullable(instance.getModifier(modifier.id())) //Get old modifier if present + .ifPresent(modifier1 -> instance.removeModifier(modifier1.id())); //Remove old modifier instance.addPermanentModifier(modifier); //Add modifier @@ -43,27 +45,26 @@ public static void setModifier(final LivingEntity entity, final Attribute attrib } /** - * @param entity Target Entity - * @param attribute Target Attribute - * @param attributeModifierId A unique id to identify this {@link AttributeModifier} - * @param attributeModifierName A unique name which is used to store the {@link AttributeModifier} - * @param amount Will be used to calculate the final value of the {@link Attribute} - * @param attributeOperation Mathematical operation type which is used to calculate the final value of the {@link Attribute} + * @param entity Target Entity + * @param attribute Target Attribute + * @param attributeModifierLocation A unique ResourceLocation to identify this {@link AttributeModifier} + * @param amount Will be used to calculate the final value of the {@link Attribute} + * @param attributeOperation Mathematical operation type which is used to calculate the final value of the {@link Attribute} */ - public static void addModifier(final LivingEntity entity, final Attribute attribute, final UUID attributeModifierId, final String attributeModifierName, final double amount, + public static void addModifier(final LivingEntity entity, final Holder attribute, final ResourceLocation attributeModifierLocation, final double amount, final AttributeModifier.Operation attributeOperation) { - setModifier(entity, attribute, new AttributeModifier(attributeModifierId, attributeModifierName, amount, attributeOperation)); + setModifier(entity, attribute, new AttributeModifier(attributeModifierLocation, amount, attributeOperation)); } /** * Safe way to remove {@link AttributeModifier} from Entities * - * @param entity Target Entity - * @param attribute Target Attribute - * @param attributeModifierId Unique modifier id + * @param entity Target Entity + * @param attribute Target Attribute + * @param attributeModifierLocation Unique modifier ResourceLocation */ - public static void removeModifier(final LivingEntity entity, final Attribute attribute, final UUID attributeModifierId) { - Optional.ofNullable(entity.getAttribute(attribute)).ifPresent(attributeInstance -> attributeInstance.removeModifier(attributeModifierId)); + public static void removeModifier(final LivingEntity entity, final Holder attribute, final ResourceLocation attributeModifierLocation) { + Optional.ofNullable(entity.getAttribute(attribute)).ifPresent(attributeInstance -> attributeInstance.removeModifier(attributeModifierLocation)); } /** @@ -73,7 +74,7 @@ public static void removeModifier(final LivingEntity entity, final Attribute att * @param attribute Target Attribute * @param modifier Modifier */ - public static void removeModifier(final LivingEntity entity, final Attribute attribute, final AttributeModifier modifier) { - removeModifier(entity, attribute, modifier.getId()); + public static void removeModifier(final LivingEntity entity, final Holder attribute, final AttributeModifier modifier) { + removeModifier(entity, attribute, modifier.id()); } } From f807bce47db25cfbc00718d739f4067e58880345 Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 20:28:44 +0200 Subject: [PATCH 03/12] Change AbstractRegister to conform with new changes. TODO: Find better way to get Holder instance of Attribute. Currently, I can't cast the RangedAttribute Holder to an Attribute Holder, because they're not the same type due to generics. Therefore, I created a Direct Holder. --- .../manascore/api/registry/AbstractRegister.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java index b52f969e..c55a4879 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java @@ -7,6 +7,7 @@ import dev.architectury.registry.registries.DeferredRegister; import dev.architectury.registry.registries.RegistrySupplier; import lombok.NonNull; +import net.minecraft.core.Holder; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.EntityDimensions; @@ -28,6 +29,7 @@ import net.minecraft.world.level.block.entity.BlockEntityType.BlockEntitySupplier; import net.minecraft.world.level.block.state.BlockBehaviour; import org.jetbrains.annotations.Nullable; +import org.w3c.dom.Attr; import java.util.HashMap; import java.util.HashSet; @@ -384,7 +386,7 @@ public RegistrySupplier> end() { RegistrySupplier> supplier = this.register.entityTypes.register(this.id, () -> { EntityType.Builder builder = EntityType.Builder.of(this.entityFactory, this.category) .clientTrackingRange(this.trackingRange) - .sized(this.dimensions.width, this.dimensions.height) + .sized(this.dimensions.width(), this.dimensions.height()) .updateInterval(this.updateInterval); if (!this.summonable) builder.noSummon(); @@ -518,9 +520,12 @@ public RegistrySupplier end() { RegistrySupplier supplier = this.register.attributes.register(this.id, () -> (RangedAttribute) new RangedAttribute(String.format("%s.attribute.%s", this.id.getNamespace(), this.id.getPath().replaceAll("/", ".")), this.defaultValue, this.minimumValue, this.maximumValue).setSyncable(this.syncable)); supplier.listen(attribute -> { + //Shouldn't probably do this, this way. TODO: Find another way to do this, in my search, I didnt find another one. + Holder attributeHolder = Holder.direct((Attribute) attribute); + // TODO something in here is broken on NeoForge and probably on Forge too - if (this.applyToAll) ManasAttributeRegistry.registerToAll(builder -> builder.add(attribute, this.defaultValue)); - this.applicableEntityTypes.forEach((typeSupplier, defaultValue) -> ManasAttributeRegistry.register(typeSupplier, builder -> builder.add(attribute, defaultValue))); + if (this.applyToAll) ManasAttributeRegistry.registerToAll(builder -> builder.add(attributeHolder, this.defaultValue)); + this.applicableEntityTypes.forEach((typeSupplier, defaultValue) -> ManasAttributeRegistry.register(typeSupplier, builder -> builder.add(attributeHolder, defaultValue))); }); return supplier; @@ -590,7 +595,7 @@ protected static abstract class ContentBuilder> private ContentBuilder(final R register, final String name) { this.register = register; - this.id = new ResourceLocation(this.register.modId, name); + this.id = ResourceLocation.fromNamespaceAndPath(register.modId, name); } public R build() { From 8a2e039ee2fd668433eefb2a32b06fb2b444c1ee Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 20:46:43 +0200 Subject: [PATCH 04/12] Change AbstractRegister to conform with new changes. TODO: Find better way to get Holder instance of Attribute. Currently, I can't cast the RangedAttribute Holder to an Attribute Holder, because they're not the same type due to generics. Therefore, I created a Direct Holder. --- .../api/registry/AbstractRegister.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java index c55a4879..ced6452e 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java @@ -203,6 +203,11 @@ public ItemBuilder withStackSize(final int stackSize) { public RegistrySupplier end() { return this.register.items.register(this.id, () -> this.itemFactory.apply(this.properties)); } + + @Override + public Holder endAsHolder() { + return this.end().getRegistrar().getHolder(this.id); + } } /** @@ -250,6 +255,11 @@ public RegistrySupplier end() { if (this.parentBlockRegistryEntry == null) throw new IllegalStateException("Parent block registry entry must not be null!"); return this.register.items.register(this.id, () -> this.itemFactory.apply(this.parentBlockRegistryEntry, this.properties)); } + + @Override + public Holder endAsHolder() { + return this.end().getRegistrar().getHolder(this.id); + } } /** @@ -295,6 +305,11 @@ public RegistrySupplier end() { return blockSupplier; } + @Override + public Holder endAsHolder() { + return this.end().getRegistrar().getHolder(this.id); + } + @FunctionalInterface public interface BlockItemFactory, T extends BlockItem> { BlockItemBuilder modify(BlockItemBuilder builder); @@ -401,6 +416,11 @@ public RegistrySupplier> end() { supplier.listen(type -> ManasAttributeRegistry.registerNew(() -> type, this.attributeBuilder)); return supplier; } + + @Override + public Holder> endAsHolder() { + return this.end().getRegistrar().getHolder(this.id); + } } /** @@ -530,6 +550,10 @@ public RegistrySupplier end() { return supplier; } + + public Holder endAsHolder() { + return this.end().getRegistrar().getHolder(this.id); + } } @@ -569,6 +593,11 @@ public final BlockEntityBuilder withValidBlocks(Supplier> end() { return this.register.blockEntities.register(this.id, () -> BlockEntityType.Builder.of(this.factory, this.validBlocks.stream().map(Supplier::get).toArray(Block[]::new)).build(this.dataFixerType)); } + + @Override + public Holder> endAsHolder() { + return this.end().getRegistrar().getHolder(this.id); + } } public static class SkillBuilder, T extends ManasSkill> extends ContentBuilder { @@ -583,6 +612,11 @@ private SkillBuilder(R register, String name, Supplier skillFactory) { public RegistrySupplier end() { return this.register.skills.register(this.id, this.skillFactory); } + + @Override + public Holder endAsHolder() { + return this.end().getRegistrar().getHolder(this.id); + } } /** @@ -604,5 +638,6 @@ public R build() { } public abstract RegistrySupplier end(); + public abstract Holder endAsHolder(); } } From 1f0d7c339a0b3b7d27c6fb54fd20466ec30c25b2 Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 20:48:07 +0200 Subject: [PATCH 05/12] Fixed references to ResourceLocation and Attribute to use the proper methods. For ResourceLocation, the new ResourceLocation#fromNamespaceAndPath For Attributes, the Holder#direct method --- .../manascore/api/skill/ManasSkill.java | 2 +- .../manasmods/manascore/core/MixinEntity.java | 6 +++++- .../manascore/core/MixinLivingEntity.java | 18 +++++++++++++----- .../manasmods/manascore/core/MixinPlayer.java | 11 ++++++++--- .../manascore/core/client/MixinMinecraft.java | 7 ++++++- .../manascore/network/NetworkManager.java | 2 +- .../manascore/skill/SkillRegistry.java | 4 ++-- .../manascore/skill/SkillStorage.java | 2 +- .../manascore/storage/CombinedStorage.java | 4 ++-- 9 files changed, 39 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java b/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java index 4bac44ac..2f246554 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java @@ -62,7 +62,7 @@ public MutableComponent getName() { public ResourceLocation getSkillIcon() { ResourceLocation id = this.getRegistryName(); if (id == null) return null; - return new ResourceLocation(id.getNamespace(), "icons/skills/" + id.getPath()); + return ResourceLocation.fromNamespaceAndPath(id.getNamespace(), "icons/skills/" + id.getPath()); } /** diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java index c07561f8..779f0119 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java @@ -8,12 +8,14 @@ import com.github.manasmods.manascore.storage.StorageManager; import com.github.manasmods.manascore.storage.StorageManager.StorageKey; import com.github.manasmods.manascore.utils.PlayerLookup; +import net.minecraft.core.Holder; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; 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.level.Level; import org.jetbrains.annotations.NotNull; @@ -102,7 +104,9 @@ protected void maxUpStep(CallbackInfoReturnable cir) { Entity entity = (Entity) (Object) this; if (!(entity instanceof LivingEntity living)) return; - AttributeInstance instance = living.getAttribute(ManasCoreAttributes.STEP_HEIGHT_ADDITION.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.STEP_HEIGHT_ADDITION.get()); + + AttributeInstance instance = living.getAttribute(attributeHolder); if (instance == null) return; cir.setReturnValue((float) (cir.getReturnValue() + instance.getValue())); } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java index f7adf89a..68dad708 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java @@ -1,10 +1,12 @@ package com.github.manasmods.manascore.core; import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import net.minecraft.core.Holder; import net.minecraft.world.entity.LivingEntity; import com.github.manasmods.manascore.attribute.ManasCoreAttributes; import net.minecraft.tags.TagKey; 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.level.material.Fluid; import org.spongepowered.asm.mixin.Mixin; @@ -23,7 +25,9 @@ public abstract class MixinLivingEntity { @Inject(method = "getJumpPower", at = @At("RETURN"), cancellable = true) protected void getJumpPower(CallbackInfoReturnable cir) { LivingEntity entity = (LivingEntity) (Object) this; - AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.JUMP_STRENGTH.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.JUMP_STRENGTH.get()); + + AttributeInstance instance = entity.getAttribute(attributeHolder); if (instance == null) return; double newJump = (cir.getReturnValue() - getJumpBoostPower()) / 0.42F * instance.getValue(); @@ -34,7 +38,8 @@ protected void getJumpPower(CallbackInfoReturnable cir) { target = "Lnet/minecraft/world/entity/LivingEntity;calculateFallDamage(FF)I"), index = 0) public float causeFallDamage(float fallDistance, float multiplier) { LivingEntity entity = (LivingEntity) (Object) this; - AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.JUMP_STRENGTH.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.JUMP_STRENGTH.get()); + AttributeInstance instance = entity.getAttribute(attributeHolder); if (instance == null) return fallDistance; float additionalJumpBlock = (float) ((instance.getValue() - 0.42F) / 0.1F); @@ -44,7 +49,8 @@ public float causeFallDamage(float fallDistance, float multiplier) { @Inject(method = "jumpInLiquid", at = @At("HEAD")) protected void jumpInLiquid(TagKey fluidTag, CallbackInfo ci) { LivingEntity entity = (LivingEntity) (Object) this; - AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); + AttributeInstance instance = entity.getAttribute(attributeHolder); if (instance == null) return; entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, 0.04 * instance.getValue() - 0.04, 0.0)); } @@ -52,7 +58,8 @@ protected void jumpInLiquid(TagKey fluidTag, CallbackInfo ci) { @Inject(method = "goDownInWater", at = @At("HEAD")) protected void goDownInWater(CallbackInfo ci) { LivingEntity entity = (LivingEntity) (Object) this; - AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); + AttributeInstance instance = entity.getAttribute(attributeHolder); if (instance == null) return; entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, 0.04 - 0.04 * instance.getValue(), 0.0)); } @@ -61,7 +68,8 @@ protected void goDownInWater(CallbackInfo ci) { target = "Lnet/minecraft/world/entity/LivingEntity;moveRelative(FLnet/minecraft/world/phys/Vec3;)V", ordinal = 0)) public float travel(float speed) { LivingEntity entity = (LivingEntity) (Object) this; - AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); + AttributeInstance instance = entity.getAttribute(attributeHolder); if (instance == null) return speed; return (float) (speed * instance.getValue()); } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java index 1a028717..bce9174d 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java @@ -2,7 +2,9 @@ import com.github.manasmods.manascore.attribute.ManasCoreAttributes; import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.core.Holder; 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.player.Player; import net.minecraft.world.level.block.state.BlockState; @@ -17,7 +19,8 @@ public abstract class MixinPlayer { private float getCritChanceDamage(float amount, @Local(ordinal = 1) float g, @Local(ordinal = 2) boolean bl3) { Player player = (Player) (Object) this; if (!bl3) { - AttributeInstance instance = player.getAttribute(ManasCoreAttributes.CRIT_CHANCE.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.CRIT_CHANCE.get()); + AttributeInstance instance = player.getAttribute(attributeHolder); if (instance == null || player.getRandom().nextInt(100) > instance.getValue()) return amount; float beforeEnchant = amount - g; @@ -29,7 +32,8 @@ private float getCritChanceDamage(float amount, @Local(ordinal = 1) float g, @Lo @ModifyConstant(method = "attack(Lnet/minecraft/world/entity/Entity;)V", constant = @Constant(floatValue = 1.5F)) private float getCritMultiplier(float multiplier) { Player player = (Player) (Object) this; - AttributeInstance instance = player.getAttribute(ManasCoreAttributes.CRIT_MULTIPLIER.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.CRIT_MULTIPLIER.get()); + AttributeInstance instance = player.getAttribute(attributeHolder); if (instance == null) return multiplier; return (float) instance.getValue(); } @@ -37,7 +41,8 @@ private float getCritMultiplier(float multiplier) { @Inject(method = "getDestroySpeed", at = @At("RETURN"), cancellable = true) protected void getDestroySpeed(BlockState state, CallbackInfoReturnable cir) { LivingEntity entity = (LivingEntity) (Object) this; - AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.MINING_SPEED_MULTIPLIER.get()); + Holder attributeHolder = Holder.direct(ManasCoreAttributes.MINING_SPEED_MULTIPLIER.get()); + AttributeInstance instance = entity.getAttribute(attributeHolder); if (instance == null) return; cir.setReturnValue((float) (cir.getReturnValue() * instance.getValue())); } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java index bb4a3250..946a744a 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java @@ -5,6 +5,8 @@ import com.github.manasmods.manascore.network.toserver.RequestSweepChancePacket; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.Holder; +import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.AttributeInstance; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; @@ -20,7 +22,10 @@ public class MixinMinecraft { target = "Lnet/minecraft/client/player/LocalPlayer;resetAttackStrengthTicker()V", shift = At.Shift.BEFORE)) private void addSweepChance(CallbackInfoReturnable cir) { if (player == null) return; - AttributeInstance instance = player.getAttribute(ManasCoreAttributes.SWEEP_CHANCE.get()); + //For some reason, here it works. Don't know either. + Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWEEP_CHANCE.get()); + + AttributeInstance instance = player.getAttribute(attributeHolder); if (instance == null || player.getRandom().nextInt(100) > instance.getValue()) return; NetworkManager.CHANNEL.sendToServer(new RequestSweepChancePacket()); } diff --git a/common/src/main/java/com/github/manasmods/manascore/network/NetworkManager.java b/common/src/main/java/com/github/manasmods/manascore/network/NetworkManager.java index e274bc37..8f4aa448 100644 --- a/common/src/main/java/com/github/manasmods/manascore/network/NetworkManager.java +++ b/common/src/main/java/com/github/manasmods/manascore/network/NetworkManager.java @@ -12,7 +12,7 @@ import net.minecraft.resources.ResourceLocation; public class NetworkManager { - public static final NetworkChannel CHANNEL = NetworkChannel.create(new ResourceLocation("manascore", "main")); + public static final NetworkChannel CHANNEL = NetworkChannel.create(ResourceLocation.fromNamespaceAndPath("manascore", "main")); public static void init() { CHANNEL.register(SyncEntityStoragePacket.class, SyncEntityStoragePacket::encode, SyncEntityStoragePacket::new, SyncEntityStoragePacket::handle); diff --git a/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java b/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java index 69ccfe60..eb3c996a 100644 --- a/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java +++ b/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java @@ -19,7 +19,7 @@ import net.minecraft.world.phys.EntityHitResult; public class SkillRegistry { - private static final ResourceLocation registryId = new ResourceLocation(ManasCore.MOD_ID, "skills"); + private static final ResourceLocation registryId = ResourceLocation.fromNamespaceAndPath(ManasCore.MOD_ID, "skills"); public static final Registrar SKILLS = RegistrarManager.get(ManasCore.MOD_ID).builder(registryId) .syncToClients() .build(); @@ -98,7 +98,7 @@ public static void init() { return EventResult.pass(); }); - PlayerEvent.PLAYER_RESPAWN.register((newPlayer, conqueredEnd) -> { + PlayerEvent.PLAYER_RESPAWN.register((newPlayer, conqueredEnd, removalReason) -> { for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(newPlayer).getLearnedSkills()) { if (!instance.canInteractSkill(newPlayer)) continue; instance.onRespawn(newPlayer, conqueredEnd); diff --git a/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java b/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java index b3b521eb..6a68b06b 100644 --- a/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java +++ b/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java @@ -46,7 +46,7 @@ public class SkillStorage extends Storage implements Skills { private static final String SKILL_LIST_KEY = "skills"; public static void init() { - StorageEvents.REGISTER_ENTITY_STORAGE.register(registry -> key = registry.register(new ResourceLocation(ManasCore.MOD_ID, "skill_storage"), SkillStorage.class, LivingEntity.class::isInstance, target -> new SkillStorage((LivingEntity) target))); + StorageEvents.REGISTER_ENTITY_STORAGE.register(registry -> key = registry.register(ResourceLocation.fromNamespaceAndPath(ManasCore.MOD_ID, "skill_storage"), SkillStorage.class, LivingEntity.class::isInstance, target -> new SkillStorage((LivingEntity) target))); EntityEvents.LIVING_POST_TICK.register(entity -> { Level level = entity.level(); if (level.isClientSide()) return; diff --git a/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java b/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java index db6b464f..298ed0bf 100644 --- a/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java +++ b/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java @@ -46,7 +46,7 @@ public void load(CompoundTag tag) { // Get serialized storage data CompoundTag entryTag = (CompoundTag) t; // Get storage id - ResourceLocation id = new ResourceLocation(entryTag.getString(STORAGE_ID_KEY)); + ResourceLocation id = ResourceLocation.withDefaultNamespace(entryTag.getString(STORAGE_ID_KEY)); // Construct storage Storage storage = StorageManager.constructStorageFor(this.holder.manasCore$getStorageType(), id, holder); if (storage == null) { @@ -65,7 +65,7 @@ public void handleUpdatePacket(CompoundTag tag) { for (Tag e : entriesTag) { CompoundTag entryTag = (CompoundTag) e; - ResourceLocation id = new ResourceLocation(entryTag.getString(STORAGE_ID_KEY)); + ResourceLocation id = ResourceLocation.withDefaultNamespace(entryTag.getString(STORAGE_ID_KEY)); Storage storage = this.storages.get(id); if (storage == null) { ManasCore.Logger.warn("Failed to find storage for id {}. All information about this storage will be dropped!", id); From a14673282b1338d5b70b1742f1f7c5e7768501c3 Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 20:49:00 +0200 Subject: [PATCH 06/12] Fix StoragePersistentState --- .../manascore/world/StoragePersistentState.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/com/github/manasmods/manascore/world/StoragePersistentState.java b/common/src/main/java/com/github/manasmods/manascore/world/StoragePersistentState.java index a2c71844..76e4afd5 100644 --- a/common/src/main/java/com/github/manasmods/manascore/world/StoragePersistentState.java +++ b/common/src/main/java/com/github/manasmods/manascore/world/StoragePersistentState.java @@ -1,6 +1,7 @@ package com.github.manasmods.manascore.world; import com.github.manasmods.manascore.storage.CombinedStorage; +import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.util.datafix.DataFixTypes; import net.minecraft.world.level.saveddata.SavedData; @@ -11,7 +12,7 @@ public class StoragePersistentState extends SavedData { public static Factory getFactory(CombinedStorage storage) { return new Factory<>( () -> new StoragePersistentState(storage), - tag -> fromNBT(storage, tag), + (tag, holderLookup) -> fromNBT(storage, tag), DataFixTypes.LEVEL ); } @@ -23,13 +24,13 @@ public StoragePersistentState(CombinedStorage storage) { } @Override - public boolean isDirty() { - return true; + public CompoundTag save(CompoundTag tag, HolderLookup.Provider registries) { + return this.storage.toNBT(); } @Override - public CompoundTag save(CompoundTag compoundTag) { - return this.storage.toNBT(); + public boolean isDirty() { + return true; } public static StoragePersistentState fromNBT(CombinedStorage storage, CompoundTag tag) { From d4a018a26bfc5b614b927ed55972014ab490e3f0 Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 20:50:43 +0200 Subject: [PATCH 07/12] Remove getSweepingDamageRatio from calculation for packet. TODO: Find out what happened to EnchanementHelper#getSweepingDamageRatio --- .../manascore/network/toserver/RequestSweepChancePacket.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java index fceffcc3..50f5eba8 100644 --- a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java +++ b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java @@ -35,7 +35,8 @@ public void handle(Supplier ctx) { private void sweepAttack(Player player) { float attack = ManasCoreAttributeUtils.getAttackDamage(player); double radiusAddition = player.isCreative() ? 3 : 1.5F; //TODO Entity Reach Attribute - float sweepAttack = 1.0F + EnchantmentHelper.getSweepingDamageRatio(player) * attack; + //TODO: Find out what happened to getSweepingDamageRatio + float sweepAttack = 1.0F + /*EnchantmentHelper.getSweepingDamageRatio(player)*/ 0.2F * attack; AABB sweepArea = player.getBoundingBox().inflate(1.0 + radiusAddition, 0.25, 1.0 + radiusAddition) .move(ManasCoreAttributeUtils.getLookTowardVec(player, 1 + radiusAddition)); From fcd77b48d7f8aad6ac5bdeb66259d0de3ddd59d9 Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 20:53:08 +0200 Subject: [PATCH 08/12] Fix tests to use proper ResoureceLocation methods --- .../com/github/manasmods/testmod/storage/StorageTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/commonTest/src/main/java/com/github/manasmods/testmod/storage/StorageTest.java b/commonTest/src/main/java/com/github/manasmods/testmod/storage/StorageTest.java index 8da331ce..534340b1 100644 --- a/commonTest/src/main/java/com/github/manasmods/testmod/storage/StorageTest.java +++ b/commonTest/src/main/java/com/github/manasmods/testmod/storage/StorageTest.java @@ -25,15 +25,15 @@ public class StorageTest { public static void init() { // Register storage StorageEvents.REGISTER_ENTITY_STORAGE.register(registry -> { - ENTITY_KEY = registry.register(new ResourceLocation(TestMod.MOD_ID, "test_storage"), TestStorage.class, entity -> entity instanceof Player, TestStorage::new); + ENTITY_KEY = registry.register(ResourceLocation.fromNamespaceAndPath(TestMod.MOD_ID, "test_storage"), TestStorage.class, entity -> entity instanceof Player, TestStorage::new); ManasCore.Logger.info("Registered entity storage key: {}", ENTITY_KEY.id()); }); StorageEvents.REGISTER_CHUNK_STORAGE.register(registry -> { - CHUNK_KEY = registry.register(new ResourceLocation(TestMod.MOD_ID, "test_storage"), TestStorage.class, chunk -> true, TestStorage::new); + CHUNK_KEY = registry.register(ResourceLocation.fromNamespaceAndPath(TestMod.MOD_ID, "test_storage"), TestStorage.class, chunk -> true, TestStorage::new); ManasCore.Logger.info("Registered chunk storage key: {}", CHUNK_KEY.id()); }); StorageEvents.REGISTER_WORLD_STORAGE.register(registry -> { - WORLD_KEY = registry.register(new ResourceLocation(TestMod.MOD_ID, "test_storage"), TestStorage.class, level -> true, TestStorage::new); + WORLD_KEY = registry.register(ResourceLocation.fromNamespaceAndPath(TestMod.MOD_ID, "test_storage"), TestStorage.class, level -> true, TestStorage::new); ManasCore.Logger.info("Registered world storage key: {}", WORLD_KEY.id()); }); // Register event listeners that change the storage From 6135f59577c53c916794cdbbf3af104e889ef86e Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 21:06:59 +0200 Subject: [PATCH 09/12] Fix Mixins --- .../fabric/core/MixinAbstractArrow.java | 13 ++++++------ ...ngProjectile.java => MixinProjectile.java} | 21 +++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) rename fabric/src/main/java/com/github/manasmods/manascore/fabric/core/{MixinAbstractHurtingProjectile.java => MixinProjectile.java} (53%) diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java index 916b8076..6483e462 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java @@ -3,6 +3,7 @@ import com.github.manasmods.manascore.api.world.entity.EntityEvents; import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; @@ -37,20 +38,20 @@ public MixinAbstractArrow(EntityType entityType, Level lev private ProjectileHitResult onHitEventResult = null; private final IntOpenHashSet ignoredEntities = new IntOpenHashSet(); - @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/AbstractArrow;onHit(Lnet/minecraft/world/phys/HitResult;)V")) - void onHit(AbstractArrow instance, HitResult result, Operation original, @Local LocalRef entityHitResult) { + @WrapMethod(method = "onHitEntity") + void onHit(EntityHitResult result, Operation original, @Local LocalRef entityHitResult) { Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, this, resultChangeable); this.onHitEventResult = resultChangeable.get(); switch (this.onHitEventResult) { case DEFAULT -> { - original.call(instance, result); + original.call(result); this.onHitEventResult = null; } case HIT -> { this.setPierceLevel((byte) 0); - original.call(instance, result); + original.call(result); this.onHitEventResult = null; } case HIT_NO_DAMAGE -> { @@ -59,7 +60,7 @@ void onHit(AbstractArrow instance, HitResult result, Operation original, @ } case PASS -> { if (result.getType() != HitResult.Type.ENTITY) { - original.call(instance, result); + original.call(result); this.onHitEventResult = null; } else { this.ignoredEntities.add(entityHitResult.get().getEntity().getId()); diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractHurtingProjectile.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java similarity index 53% rename from fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractHurtingProjectile.java rename to fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java index 35a9ce15..5e957d1e 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractHurtingProjectile.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java @@ -3,21 +3,30 @@ import com.github.manasmods.manascore.api.world.entity.EntityEvents; import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.projectile.AbstractHurtingProjectile; +import net.minecraft.world.entity.projectile.Projectile; +import net.minecraft.world.level.Level; +import net.minecraft.world.phys.EntityHitResult; import net.minecraft.world.phys.HitResult; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -@Mixin(AbstractHurtingProjectile.class) -public abstract class MixinAbstractHurtingProjectile { - @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/AbstractHurtingProjectile;onHit(Lnet/minecraft/world/phys/HitResult;)V")) - void onHit(AbstractHurtingProjectile instance, HitResult result, Operation original) { +@Mixin(Projectile.class) +public abstract class MixinProjectile extends Projectile { + public MixinProjectile(EntityType entityType, Level level) { + super(entityType, level); + } + + @WrapMethod(method = "onHitEntity") + void onHit(EntityHitResult result, Operation original) { Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, this, resultChangeable); if (!resultChangeable.get().equals(ProjectileHitResult.DEFAULT)) return; - original.call(instance, result); + original.call(result); } } From 1f82a263339e1a82821bc9eeea963270d9646c29 Mon Sep 17 00:00:00 2001 From: maltesermailo Date: Wed, 14 Aug 2024 21:21:17 +0200 Subject: [PATCH 10/12] Fix Mixins --- .../fabric/core/MixinAbstractArrow.java | 21 +++++------------ .../fabric/core/MixinFireRocketEntity.java | 5 ++-- .../fabric/core/MixinFishingHook.java | 23 ------------------- .../manascore/fabric/core/MixinLlamaSpit.java | 23 ------------------- .../fabric/core/MixinProjectile.java | 8 ++----- .../fabric/core/MixinShulkerBullet.java | 23 ------------------- .../fabric/core/MixinThrowableProjectile.java | 23 ------------------- .../src/main/resources/manascore.mixins.json | 8 ++----- 8 files changed, 13 insertions(+), 121 deletions(-) delete mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java delete mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java delete mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java delete mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java index 6483e462..18d61146 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java @@ -39,34 +39,25 @@ public MixinAbstractArrow(EntityType entityType, Level lev private final IntOpenHashSet ignoredEntities = new IntOpenHashSet(); @WrapMethod(method = "onHitEntity") - void onHit(EntityHitResult result, Operation original, @Local LocalRef entityHitResult) { + void onHit(EntityHitResult entityHitResult, Operation original) { Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, this, resultChangeable); + EntityEvents.PROJECTILE_HIT.invoker().hit(entityHitResult, this, resultChangeable); this.onHitEventResult = resultChangeable.get(); switch (this.onHitEventResult) { - case DEFAULT -> { - original.call(result); + case DEFAULT, PASS -> { + original.call(entityHitResult); this.onHitEventResult = null; } case HIT -> { this.setPierceLevel((byte) 0); - original.call(result); + original.call(entityHitResult); this.onHitEventResult = null; } case HIT_NO_DAMAGE -> { this.discard(); - entityHitResult.set(null); - } - case PASS -> { - if (result.getType() != HitResult.Type.ENTITY) { - original.call(result); - this.onHitEventResult = null; - } else { - this.ignoredEntities.add(entityHitResult.get().getEntity().getId()); - entityHitResult.set(null); - } } + case null -> {} } } diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java index 068bf6e5..622d54f7 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java @@ -23,7 +23,8 @@ public MixinFireRocketEntity(EntityType entityType, Level @Unique @Nullable private ProjectileHitResult onHitEventResult = null; - @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/FireworkRocketEntity;onHit(Lnet/minecraft/world/phys/HitResult;)V")) + //TODO: This has to be done new for the new changes + /*@WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/FireworkRocketEntity;onHit(Lnet/minecraft/world/phys/HitResult;)V")) void onHit(FireworkRocketEntity instance, HitResult result, Operation original) { Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); @@ -42,7 +43,7 @@ void onHit(FireworkRocketEntity instance, HitResult result, Operation orig } } } - } + }*/ @WrapOperation(method = "tick", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/projectile/FireworkRocketEntity;hasImpulse:Z")) void onImpulseSet(FireworkRocketEntity instance, boolean value, Operation original) { diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java deleted file mode 100644 index 676f1b3d..00000000 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.manasmods.manascore.fabric.core; - -import com.github.manasmods.manascore.api.world.entity.EntityEvents; -import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; -import com.github.manasmods.manascore.utils.Changeable; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import net.minecraft.world.entity.projectile.FishingHook; -import net.minecraft.world.phys.HitResult; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -@Mixin(FishingHook.class) -public abstract class MixinFishingHook { - @WrapOperation(method = "checkCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/FishingHook;onHit(Lnet/minecraft/world/phys/HitResult;)V")) - void onHit(FishingHook instance, HitResult result, Operation original) { - Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); - - if (!resultChangeable.get().equals(ProjectileHitResult.DEFAULT)) return; - original.call(instance, result); - } -} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java deleted file mode 100644 index 21212734..00000000 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.manasmods.manascore.fabric.core; - -import com.github.manasmods.manascore.api.world.entity.EntityEvents; -import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; -import com.github.manasmods.manascore.utils.Changeable; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import net.minecraft.world.entity.projectile.LlamaSpit; -import net.minecraft.world.phys.HitResult; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -@Mixin(LlamaSpit.class) -public abstract class MixinLlamaSpit { - @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/LlamaSpit;onHit(Lnet/minecraft/world/phys/HitResult;)V")) - void onHit(LlamaSpit instance, HitResult result, Operation original) { - Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); - - if (!resultChangeable.get().equals(ProjectileHitResult.DEFAULT)) return; - original.call(instance, result); - } -} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java index 5e957d1e..625df3e3 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java @@ -7,7 +7,6 @@ import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.projectile.AbstractHurtingProjectile; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.level.Level; import net.minecraft.world.phys.EntityHitResult; @@ -16,15 +15,12 @@ import org.spongepowered.asm.mixin.injection.At; @Mixin(Projectile.class) -public abstract class MixinProjectile extends Projectile { - public MixinProjectile(EntityType entityType, Level level) { - super(entityType, level); - } +public abstract class MixinProjectile { @WrapMethod(method = "onHitEntity") void onHit(EntityHitResult result, Operation original) { Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, this, resultChangeable); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, ((Projectile)((Object)this)), resultChangeable); if (!resultChangeable.get().equals(ProjectileHitResult.DEFAULT)) return; original.call(result); diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java deleted file mode 100644 index 09fe6313..00000000 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.manasmods.manascore.fabric.core; - -import com.github.manasmods.manascore.api.world.entity.EntityEvents; -import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; -import com.github.manasmods.manascore.utils.Changeable; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import net.minecraft.world.entity.projectile.ShulkerBullet; -import net.minecraft.world.phys.HitResult; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -@Mixin(ShulkerBullet.class) -public abstract class MixinShulkerBullet { - @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/ShulkerBullet;onHit(Lnet/minecraft/world/phys/HitResult;)V")) - void onHit(ShulkerBullet instance, HitResult result, Operation original) { - Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); - - if (!resultChangeable.get().equals(ProjectileHitResult.DEFAULT)) return; - original.call(instance, result); - } -} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java deleted file mode 100644 index 8f3917c2..00000000 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.manasmods.manascore.fabric.core; - -import com.github.manasmods.manascore.api.world.entity.EntityEvents; -import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; -import com.github.manasmods.manascore.utils.Changeable; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import net.minecraft.world.entity.projectile.ThrowableProjectile; -import net.minecraft.world.phys.HitResult; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -@Mixin(ThrowableProjectile.class) -public abstract class MixinThrowableProjectile{ - @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/ThrowableProjectile;onHit(Lnet/minecraft/world/phys/HitResult;)V")) - void onHit(ThrowableProjectile instance, HitResult result, Operation original) { - Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); - - if (!resultChangeable.get().equals(ProjectileHitResult.DEFAULT)) return; - original.call(instance, result); - } -} diff --git a/fabric/src/main/resources/manascore.mixins.json b/fabric/src/main/resources/manascore.mixins.json index 55161857..26d46cd3 100644 --- a/fabric/src/main/resources/manascore.mixins.json +++ b/fabric/src/main/resources/manascore.mixins.json @@ -5,16 +5,12 @@ "compatibilityLevel": "JAVA_17", "mixins": [ "MixinAbstractArrow", - "MixinAbstractHurtingProjectile", + "MixinProjectile", "MixinFireRocketEntity", - "MixinFishingHook", "MixinLivingEntity", - "MixinLlamaSpit", "MixinMob", "MixinPlayer", - "MixinShulkerBullet", - "MixinStartAttacking", - "MixinThrowableProjectile" + "MixinStartAttacking" ], "injectors": { "defaultRequire": 1 From e30a845e454117a3572eeeaa157c80709f8aa444 Mon Sep 17 00:00:00 2001 From: Minh Date: Fri, 16 Aug 2024 00:03:13 +0700 Subject: [PATCH 11/12] Removed Redundant attributes & Fixed some mixins/events and Attribute registry --- .../api/registry/AbstractRegister.java | 39 +++---- .../api/world/entity/EntityEvents.java | 6 +- .../attribute/ManasCoreAttributeUtils.java | 22 ++++ .../attribute/ManasCoreAttributes.java | 44 ++------ .../manascore/core/MixinChunkSerializer.java | 3 +- .../manasmods/manascore/core/MixinEntity.java | 17 --- .../manascore/core/MixinLivingEntity.java | 79 +++---------- .../manasmods/manascore/core/MixinPlayer.java | 32 +++--- .../manascore/core/client/MixinMinecraft.java | 7 +- .../toserver/RequestSweepChancePacket.java | 11 +- .../manascore/skill/SkillRegistry.java | 106 ++++++++++++------ .../manascore/skill/SkillStorage.java | 2 +- .../manascore/storage/CombinedStorage.java | 4 +- .../testmod/registry/RegisterTest.java | 6 +- .../manasmods/testmod/registry/TestSkill.java | 2 +- .../fabric/core/MixinAbstractArrow.java | 30 +++-- .../core/MixinAbstractHurtingProjectile.java | 27 +++++ .../fabric/core/MixinFireRocketEntity.java | 41 ++----- .../fabric/core/MixinFishingHook.java | 27 +++++ .../fabric/core/MixinLivingEntity.java | 14 --- .../manascore/fabric/core/MixinLlamaSpit.java | 27 +++++ .../manascore/fabric/core/MixinPlayer.java | 15 --- .../fabric/core/MixinProjectile.java | 28 ----- .../fabric/core/MixinShulkerBullet.java | 27 +++++ .../fabric/core/MixinThrowableProjectile.java | 27 +++++ .../src/main/resources/manascore.mixins.json | 8 +- .../neoforge/NeoForgeCommonEventInvoker.java | 29 ++--- .../neoforge/StorageEventListener.java | 2 +- .../neoforge/ManasAttributeRegistryImpl.java | 4 +- 29 files changed, 348 insertions(+), 338 deletions(-) create mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractHurtingProjectile.java create mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java create mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java delete mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java create mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java create mode 100644 fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java diff --git a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java index ced6452e..90e4ebb1 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java @@ -8,14 +8,12 @@ import dev.architectury.registry.registries.RegistrySupplier; import lombok.NonNull; import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.entity.EntityDimensions; -import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.*; import net.minecraft.world.entity.EntityType.EntityFactory; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.Mob; -import net.minecraft.world.entity.MobCategory; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.RangedAttribute; @@ -29,13 +27,8 @@ import net.minecraft.world.level.block.entity.BlockEntityType.BlockEntitySupplier; import net.minecraft.world.level.block.state.BlockBehaviour; import org.jetbrains.annotations.Nullable; -import org.w3c.dom.Attr; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; @@ -424,9 +417,9 @@ public Holder> endAsHolder() { } /** - * Builder class for {@link RangedAttribute}s. + * Builder class for {@link Attribute}s. */ - public static class AttributeBuilder> extends ContentBuilder { + public static class AttributeBuilder> extends ContentBuilder { protected double defaultValue; protected double minimumValue; protected double maximumValue; @@ -536,22 +529,20 @@ public AttributeBuilder applyToAll() { } @Override - public RegistrySupplier end() { - RegistrySupplier supplier = this.register.attributes.register(this.id, () -> (RangedAttribute) new RangedAttribute(String.format("%s.attribute.%s", this.id.getNamespace(), this.id.getPath().replaceAll("/", ".")), this.defaultValue, this.minimumValue, this.maximumValue).setSyncable(this.syncable)); + public RegistrySupplier end() { + Attribute attribute = new RangedAttribute(String.format("%s.attribute.%s", this.id.getNamespace(), + this.id.getPath().replaceAll("/", ".")), + this.defaultValue, this.minimumValue, this.maximumValue).setSyncable(this.syncable); - supplier.listen(attribute -> { - //Shouldn't probably do this, this way. TODO: Find another way to do this, in my search, I didnt find another one. - Holder attributeHolder = Holder.direct((Attribute) attribute); - - // TODO something in here is broken on NeoForge and probably on Forge too - if (this.applyToAll) ManasAttributeRegistry.registerToAll(builder -> builder.add(attributeHolder, this.defaultValue)); - this.applicableEntityTypes.forEach((typeSupplier, defaultValue) -> ManasAttributeRegistry.register(typeSupplier, builder -> builder.add(attributeHolder, defaultValue))); - }); + Holder holder = Registry.registerForHolder(BuiltInRegistries.ATTRIBUTE, this.id, attribute); + RegistrySupplier supplier = this.register.attributes.register(this.id, () -> attribute); + if (this.applyToAll) ManasAttributeRegistry.registerToAll(builder -> builder.add(holder, this.defaultValue)); + this.applicableEntityTypes.forEach((typeSupplier, defaultValue) -> ManasAttributeRegistry.register(typeSupplier, builder -> builder.add(holder, defaultValue))); return supplier; } - public Holder endAsHolder() { + public Holder endAsHolder() { return this.end().getRegistrar().getHolder(this.id); } } diff --git a/common/src/main/java/com/github/manasmods/manascore/api/world/entity/EntityEvents.java b/common/src/main/java/com/github/manasmods/manascore/api/world/entity/EntityEvents.java index 0200266d..4217e1fc 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/world/entity/EntityEvents.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/world/entity/EntityEvents.java @@ -13,9 +13,9 @@ public interface EntityEvents { Event LIVING_PRE_TICK = EventFactory.createLoop(); Event LIVING_POST_TICK = EventFactory.createLoop(); Event LIVING_CHANGE_TARGET = EventFactory.createEventResult(); - Event LIVING_ATTACK = EventFactory.createEventResult(); - Event LIVING_HURT = EventFactory.createEventResult(); - Event LIVING_DAMAGE = EventFactory.createEventResult(); + Event LIVING_ATTACK = EventFactory.createEventResult(); //When attacked, before any check like Immunity, Fire Resistance + Event LIVING_HURT = EventFactory.createEventResult(); //When hurt, before armor and enchantment effect check + Event LIVING_DAMAGE = EventFactory.createEventResult(); //When damaged, after every possible check Event PROJECTILE_HIT = EventFactory.createLoop(); diff --git a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java index 8fe1c52d..ed4fbc14 100644 --- a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java +++ b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java @@ -1,10 +1,19 @@ package com.github.manasmods.manascore.attribute; import lombok.experimental.UtilityClass; +import net.minecraft.server.level.ServerLevel; import net.minecraft.util.Mth; +import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.AttributeModifier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.phys.Vec3; +import org.jetbrains.annotations.Nullable; @UtilityClass public class ManasCoreAttributeUtils { @@ -16,6 +25,19 @@ public static float getAttackDamage(Player player) { return f; } + public static float getWeaponDamage(LivingEntity attacker, @Nullable Entity target, @Nullable DamageSource source) { + AttributeInstance attack = attacker.getAttribute(Attributes.ATTACK_DAMAGE); + if (attack == null) return 0; + + float damage = 1F; + AttributeModifier modifier = attack.getModifier(Item.BASE_ATTACK_DAMAGE_ID); + if (modifier != null) damage += (float) modifier.amount(); + + if (target != null && source != null && attacker.level() instanceof ServerLevel serverLevel) + damage = EnchantmentHelper.modifyDamage(serverLevel, attacker.getWeaponItem(), target, source, damage); + return damage; + } + public static Vec3 getLookTowardVec(Player player, double distance) { float f = player.getXRot(); float g = player.getYRot(); diff --git a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java index d912509c..fa06de07 100644 --- a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java +++ b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java @@ -1,62 +1,34 @@ package com.github.manasmods.manascore.attribute; import com.github.manasmods.manascore.ManasCore; -import dev.architectury.registry.registries.RegistrySupplier; import lombok.experimental.UtilityClass; +import net.minecraft.core.Holder; import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.ai.attributes.RangedAttribute; +import net.minecraft.world.entity.ai.attributes.Attribute; @UtilityClass public class ManasCoreAttributes { - public static final RegistrySupplier CRIT_CHANCE = ManasCore.REGISTER.attribute("crit_chance") + public static final Holder CRIT_CHANCE = ManasCore.REGISTER.attribute("crit_chance") .withDefaultValue(0) .withMinimumValue(0) .withMaximumValue(100) .applyToAll() .syncable() - .end(); - public static final RegistrySupplier CRIT_MULTIPLIER = ManasCore.REGISTER.attribute("crit_multiplier") + .endAsHolder(); + public static final Holder CRIT_MULTIPLIER = ManasCore.REGISTER.attribute("crit_multiplier") .withDefaultValue(1.5) .withMinimumValue(0) .withMaximumValue(1024) .applyToAll() .syncable() - .end(); - public static final RegistrySupplier JUMP_STRENGTH = ManasCore.REGISTER.attribute("jump_strength") - .withDefaultValue(0.42) - .withMinimumValue(0) - .withMaximumValue(1024) - .applyToAll() - .syncable() - .end(); - public static final RegistrySupplier MINING_SPEED_MULTIPLIER = ManasCore.REGISTER.attribute("mining_speed_multiplier") - .withDefaultValue(1.0) - .withMinimumValue(0) - .withMaximumValue(1024) - .applyTo(() -> EntityType.PLAYER) - .syncable() - .end(); - public static final RegistrySupplier STEP_HEIGHT_ADDITION = ManasCore.REGISTER.attribute("step_height") - .withDefaultValue(0) - .withMinimumValue(-1024) - .withMaximumValue(1024) - .applyToAll() - .syncable() - .end(); - public static final RegistrySupplier SWIM_SPEED_ADDITION = ManasCore.REGISTER.attribute("swim_speed") - .withDefaultValue(1) - .withMinimumValue(0) - .withMaximumValue(1024) - .applyToAll() - .syncable() - .end(); - public static final RegistrySupplier SWEEP_CHANCE = ManasCore.REGISTER.attribute("sweep_chance") + .endAsHolder(); + public static final Holder SWEEP_CHANCE = ManasCore.REGISTER.attribute("sweep_chance") .withDefaultValue(0) .withMinimumValue(0) .withMaximumValue(100) .applyTo(() -> EntityType.PLAYER) .syncable() - .end(); + .endAsHolder(); public static void init() { // Just loads the class diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinChunkSerializer.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinChunkSerializer.java index 67ac3096..8addb921 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinChunkSerializer.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinChunkSerializer.java @@ -9,6 +9,7 @@ import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.chunk.ProtoChunk; import net.minecraft.world.level.chunk.storage.ChunkSerializer; +import net.minecraft.world.level.chunk.storage.RegionStorageInfo; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -17,7 +18,7 @@ @Mixin(ChunkSerializer.class) public class MixinChunkSerializer { @Inject(method = "read", at = @At("RETURN")) - private static void onChunkRead(ServerLevel level, PoiManager poiManager, ChunkPos pos, CompoundTag tag, CallbackInfoReturnable cir) { + private static void onChunkRead(ServerLevel level, PoiManager poiManager, RegionStorageInfo regionStorageInfo, ChunkPos pos, CompoundTag tag, CallbackInfoReturnable cir) { if (!(cir.getReturnValue() instanceof ImposterProtoChunk protoChunk)) return; // Apply loaded data to initial storage protoChunk.getWrapped().manasCore$getCombinedStorage().handleUpdatePacket(tag.getCompound("ManasCoreStorage")); diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java index 779f0119..c467c8e6 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinEntity.java @@ -3,20 +3,15 @@ import com.github.manasmods.manascore.api.storage.Storage; import com.github.manasmods.manascore.api.storage.StorageHolder; import com.github.manasmods.manascore.api.storage.StorageType; -import com.github.manasmods.manascore.attribute.ManasCoreAttributes; import com.github.manasmods.manascore.storage.CombinedStorage; import com.github.manasmods.manascore.storage.StorageManager; import com.github.manasmods.manascore.storage.StorageManager.StorageKey; import com.github.manasmods.manascore.utils.PlayerLookup; -import net.minecraft.core.Holder; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; -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.level.Level; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -98,16 +93,4 @@ void onTickSyncCheck(CallbackInfo ci) { if (this.storage.isDirty()) StorageManager.syncTracking((Entity) (Object) this, true); this.level.getProfiler().pop(); } - - @Inject(method = "maxUpStep", at = @At("RETURN"), cancellable = true) - protected void maxUpStep(CallbackInfoReturnable cir) { - Entity entity = (Entity) (Object) this; - if (!(entity instanceof LivingEntity living)) return; - - Holder attributeHolder = Holder.direct(ManasCoreAttributes.STEP_HEIGHT_ADDITION.get()); - - AttributeInstance instance = living.getAttribute(attributeHolder); - if (instance == null) return; - cir.setReturnValue((float) (cir.getReturnValue() + instance.getValue())); - } } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java index 68dad708..d347eef6 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java @@ -1,78 +1,19 @@ package com.github.manasmods.manascore.core; import com.github.manasmods.manascore.api.world.entity.EntityEvents; -import net.minecraft.core.Holder; +import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.LivingEntity; -import com.github.manasmods.manascore.attribute.ManasCoreAttributes; -import net.minecraft.tags.TagKey; -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.level.material.Fluid; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At.Shift; import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.ModifyArg; +import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(LivingEntity.class) public abstract class MixinLivingEntity { - @Shadow public abstract float getJumpBoostPower(); - - @Inject(method = "getJumpPower", at = @At("RETURN"), cancellable = true) - protected void getJumpPower(CallbackInfoReturnable cir) { - LivingEntity entity = (LivingEntity) (Object) this; - Holder attributeHolder = Holder.direct(ManasCoreAttributes.JUMP_STRENGTH.get()); - - AttributeInstance instance = entity.getAttribute(attributeHolder); - if (instance == null) return; - - double newJump = (cir.getReturnValue() - getJumpBoostPower()) / 0.42F * instance.getValue(); - cir.setReturnValue((float) (newJump + getJumpBoostPower())); - } - - @ModifyArg(method = "causeFallDamage", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/entity/LivingEntity;calculateFallDamage(FF)I"), index = 0) - public float causeFallDamage(float fallDistance, float multiplier) { - LivingEntity entity = (LivingEntity) (Object) this; - Holder attributeHolder = Holder.direct(ManasCoreAttributes.JUMP_STRENGTH.get()); - AttributeInstance instance = entity.getAttribute(attributeHolder); - if (instance == null) return fallDistance; - - float additionalJumpBlock = (float) ((instance.getValue() - 0.42F) / 0.1F); - return fallDistance - additionalJumpBlock; - } - - @Inject(method = "jumpInLiquid", at = @At("HEAD")) - protected void jumpInLiquid(TagKey fluidTag, CallbackInfo ci) { - LivingEntity entity = (LivingEntity) (Object) this; - Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); - AttributeInstance instance = entity.getAttribute(attributeHolder); - if (instance == null) return; - entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, 0.04 * instance.getValue() - 0.04, 0.0)); - } - - @Inject(method = "goDownInWater", at = @At("HEAD")) - protected void goDownInWater(CallbackInfo ci) { - LivingEntity entity = (LivingEntity) (Object) this; - Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); - AttributeInstance instance = entity.getAttribute(attributeHolder); - if (instance == null) return; - entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, 0.04 - 0.04 * instance.getValue(), 0.0)); - } - - @ModifyArg(method = "travel", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/entity/LivingEntity;moveRelative(FLnet/minecraft/world/phys/Vec3;)V", ordinal = 0)) - public float travel(float speed) { - LivingEntity entity = (LivingEntity) (Object) this; - Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWIM_SPEED_ADDITION.get()); - AttributeInstance instance = entity.getAttribute(attributeHolder); - if (instance == null) return speed; - return (float) (speed * instance.getValue()); - } @Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;tick()V", shift = Shift.BEFORE)) void onPreTick(CallbackInfo ci) { @@ -83,4 +24,16 @@ void onPreTick(CallbackInfo ci) { void onPostTick(CallbackInfo ci) { EntityEvents.LIVING_POST_TICK.invoker().tick((LivingEntity) (Object) this); } + + @ModifyVariable(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = Shift.BEFORE), argsOnly = true) + float modifyDamage(float amount, @Local DamageSource damageSource) { + Changeable changeable = Changeable.of(amount); + if (EntityEvents.LIVING_HURT.invoker().hurt((LivingEntity) (Object) this, damageSource, changeable).isFalse()) return 0.0F; + return changeable.get(); + } + + @Inject(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = Shift.BEFORE), cancellable = true) + void cancelActuallyHurt(DamageSource damageSource, float damageAmount, CallbackInfo ci) { + if (damageAmount <= 0F) ci.cancel(); + } } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java index bce9174d..5707e7d8 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java @@ -1,16 +1,16 @@ package com.github.manasmods.manascore.core; +import com.github.manasmods.manascore.api.world.entity.EntityEvents; import com.github.manasmods.manascore.attribute.ManasCoreAttributes; +import com.github.manasmods.manascore.utils.Changeable; import com.llamalad7.mixinextras.sugar.Local; -import net.minecraft.core.Holder; +import net.minecraft.world.damagesource.DamageSource; 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.player.Player; -import net.minecraft.world.level.block.state.BlockState; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.*; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(Player.class) public abstract class MixinPlayer { @@ -19,10 +19,8 @@ public abstract class MixinPlayer { private float getCritChanceDamage(float amount, @Local(ordinal = 1) float g, @Local(ordinal = 2) boolean bl3) { Player player = (Player) (Object) this; if (!bl3) { - Holder attributeHolder = Holder.direct(ManasCoreAttributes.CRIT_CHANCE.get()); - AttributeInstance instance = player.getAttribute(attributeHolder); + AttributeInstance instance = player.getAttribute(ManasCoreAttributes.CRIT_CHANCE); if (instance == null || player.getRandom().nextInt(100) > instance.getValue()) return amount; - float beforeEnchant = amount - g; return beforeEnchant * getCritMultiplier(1.5F) + g; } @@ -32,18 +30,20 @@ private float getCritChanceDamage(float amount, @Local(ordinal = 1) float g, @Lo @ModifyConstant(method = "attack(Lnet/minecraft/world/entity/Entity;)V", constant = @Constant(floatValue = 1.5F)) private float getCritMultiplier(float multiplier) { Player player = (Player) (Object) this; - Holder attributeHolder = Holder.direct(ManasCoreAttributes.CRIT_MULTIPLIER.get()); - AttributeInstance instance = player.getAttribute(attributeHolder); + AttributeInstance instance = player.getAttribute(ManasCoreAttributes.CRIT_MULTIPLIER); if (instance == null) return multiplier; return (float) instance.getValue(); } - @Inject(method = "getDestroySpeed", at = @At("RETURN"), cancellable = true) - protected void getDestroySpeed(BlockState state, CallbackInfoReturnable cir) { - LivingEntity entity = (LivingEntity) (Object) this; - Holder attributeHolder = Holder.direct(ManasCoreAttributes.MINING_SPEED_MULTIPLIER.get()); - AttributeInstance instance = entity.getAttribute(attributeHolder); - if (instance == null) return; - cir.setReturnValue((float) (cir.getReturnValue() * instance.getValue())); + @ModifyVariable(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = At.Shift.BEFORE), argsOnly = true) + float modifyDamage(float amount, @Local DamageSource damageSource) { + Changeable changeable = Changeable.of(amount); + if (EntityEvents.LIVING_HURT.invoker().hurt((LivingEntity) (Object) this, damageSource, changeable).isFalse()) return 0.0F; + return changeable.get(); + } + + @Inject(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = At.Shift.BEFORE), cancellable = true) + void cancelActuallyHurt(DamageSource damageSource, float damageAmount, CallbackInfo ci) { + if (damageAmount <= 0F) ci.cancel(); } } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java index 946a744a..583af617 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java @@ -5,8 +5,6 @@ import com.github.manasmods.manascore.network.toserver.RequestSweepChancePacket; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; -import net.minecraft.core.Holder; -import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.AttributeInstance; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; @@ -22,10 +20,7 @@ public class MixinMinecraft { target = "Lnet/minecraft/client/player/LocalPlayer;resetAttackStrengthTicker()V", shift = At.Shift.BEFORE)) private void addSweepChance(CallbackInfoReturnable cir) { if (player == null) return; - //For some reason, here it works. Don't know either. - Holder attributeHolder = Holder.direct(ManasCoreAttributes.SWEEP_CHANCE.get()); - - AttributeInstance instance = player.getAttribute(attributeHolder); + AttributeInstance instance = player.getAttribute(ManasCoreAttributes.SWEEP_CHANCE); if (instance == null || player.getRandom().nextInt(100) > instance.getValue()) return; NetworkManager.CHANNEL.sendToServer(new RequestSweepChancePacket()); } diff --git a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java index 50f5eba8..9087bf98 100644 --- a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java +++ b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSweepChancePacket.java @@ -8,9 +8,9 @@ import net.minecraft.sounds.SoundEvents; import net.minecraft.util.Mth; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.phys.AABB; import java.util.List; @@ -34,12 +34,11 @@ public void handle(Supplier ctx) { private void sweepAttack(Player player) { float attack = ManasCoreAttributeUtils.getAttackDamage(player); - double radiusAddition = player.isCreative() ? 3 : 1.5F; //TODO Entity Reach Attribute - //TODO: Find out what happened to getSweepingDamageRatio - float sweepAttack = 1.0F + /*EnchantmentHelper.getSweepingDamageRatio(player)*/ 0.2F * attack; + double radiusAddition = player.getAttributeValue(Attributes.ENTITY_INTERACTION_RANGE); + float sweepAttack = 1.0F + (float) player.getAttributeValue(Attributes.SWEEPING_DAMAGE_RATIO) * attack; - AABB sweepArea = player.getBoundingBox().inflate(1.0 + radiusAddition, 0.25, 1.0 + radiusAddition) - .move(ManasCoreAttributeUtils.getLookTowardVec(player, 1 + radiusAddition)); + AABB sweepArea = player.getBoundingBox().inflate(radiusAddition, 0.25, radiusAddition) + .move(ManasCoreAttributeUtils.getLookTowardVec(player, radiusAddition)); List list = player.level().getEntitiesOfClass(LivingEntity.class, sweepArea); if (list.isEmpty()) return; diff --git a/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java b/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java index eb3c996a..7d4778f0 100644 --- a/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java +++ b/common/src/main/java/com/github/manasmods/manascore/skill/SkillRegistry.java @@ -1,10 +1,7 @@ package com.github.manasmods.manascore.skill; import com.github.manasmods.manascore.ManasCore; -import com.github.manasmods.manascore.api.skill.ManasSkill; -import com.github.manasmods.manascore.api.skill.ManasSkillInstance; -import com.github.manasmods.manascore.api.skill.SkillAPI; -import com.github.manasmods.manascore.api.skill.SkillEvents; +import com.github.manasmods.manascore.api.skill.*; import com.github.manasmods.manascore.api.world.entity.EntityEvents; import dev.architectury.event.EventResult; import dev.architectury.event.events.common.EntityEvent; @@ -18,6 +15,9 @@ import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.phys.EntityHitResult; +import java.util.List; +import java.util.Optional; + public class SkillRegistry { private static final ResourceLocation registryId = ResourceLocation.fromNamespaceAndPath(ManasCore.MOD_ID, "skills"); public static final Registrar SKILLS = RegistrarManager.get(ManasCore.MOD_ID).builder(registryId) @@ -29,30 +29,44 @@ public class SkillRegistry { public static void init() { InteractionEvent.RIGHT_CLICK_BLOCK.register((player, hand, pos, face) -> { if (player.level().isClientSide()) return EventResult.pass(); - SkillAPI.getSkillsFrom(player).forEachSkill(((storage, skillInstance) -> { - if (!skillInstance.canInteractSkill(player)) return; - skillInstance.onRightClickBlock(player, hand, pos, face); - })); + Skills storage = SkillAPI.getSkillsFrom(player); + + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(player)) continue; + optional.get().onRightClickBlock(player, hand, pos, face); + } return EventResult.pass(); }); EntityEvents.LIVING_CHANGE_TARGET.register((entity, changeableTarget) -> { - if (!changeableTarget.isPresent()) return EventResult.pass(); - LivingEntity owner = changeableTarget.get(); - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(owner).getLearnedSkills()) { - if (!instance.canInteractSkill(owner)) continue; - if (!instance.onBeingTargeted(changeableTarget, entity)) return EventResult.interruptFalse(); + if (owner == null) return EventResult.pass(); + + Skills storage = SkillAPI.getSkillsFrom(owner); + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(owner)) continue; + if (!optional.get().onBeingTargeted(changeableTarget, entity)) return EventResult.interruptFalse(); } return EventResult.pass(); }); EntityEvents.LIVING_ATTACK.register((entity, source, amount) -> { - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(entity).getLearnedSkills()) { - if (!instance.canInteractSkill(entity)) continue; - if (!instance.onBeingDamaged(entity, source, amount)) return EventResult.interruptFalse(); + Skills storage = SkillAPI.getSkillsFrom(entity); + + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(entity)) continue; + if (!optional.get().onBeingDamaged(entity, source, amount)) return EventResult.interruptFalse(); } return EventResult.pass(); @@ -61,9 +75,12 @@ public static void init() { SkillEvents.SKILL_DAMAGE_PRE_CALCULATION.register((storage, target, source, amount) -> { if (!(source.getEntity() instanceof LivingEntity owner)) return EventResult.pass(); - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(owner).getLearnedSkills()) { - if (!instance.canInteractSkill(owner)) continue; - if (!instance.onDamageEntity(owner, target, source, amount)) return EventResult.interruptFalse(); + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(owner)) continue; + if (!optional.get().onDamageEntity(owner, target, source, amount)) return EventResult.interruptFalse(); } return EventResult.pass(); @@ -72,46 +89,67 @@ public static void init() { SkillEvents.SKILL_DAMAGE_POST_CALCULATION.register((storage, target, source, amount) -> { if (!(source.getEntity() instanceof LivingEntity owner)) return EventResult.pass(); - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(owner).getLearnedSkills()) { - if (!instance.canInteractSkill(owner)) continue; - if (!instance.onTouchEntity(owner, target, source, amount)) return EventResult.interruptFalse(); + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(owner)) continue; + if (!optional.get().onTouchEntity(owner, target, source, amount)) return EventResult.interruptFalse(); } return EventResult.pass(); }); EntityEvents.LIVING_DAMAGE.register((entity, source, amount) -> { - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(entity).getLearnedSkills()) { - if (!instance.canInteractSkill(entity)) continue; - if (!instance.onTakenDamage(entity, source, amount)) return EventResult.interruptFalse(); + Skills storage = SkillAPI.getSkillsFrom(entity); + + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(entity)) continue; + if (!optional.get().onTakenDamage(entity, source, amount)) return EventResult.interruptFalse(); } return EventResult.pass(); }); EntityEvent.LIVING_DEATH.register((entity, source) -> { - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(entity).getLearnedSkills()) { - if (!instance.canInteractSkill(entity)) continue; - if (!instance.onDeath(entity, source)) return EventResult.interruptFalse(); + Skills storage = SkillAPI.getSkillsFrom(entity); + + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(entity)) continue; + if (!optional.get().onDeath(entity, source)) return EventResult.interruptFalse(); } return EventResult.pass(); }); PlayerEvent.PLAYER_RESPAWN.register((newPlayer, conqueredEnd, removalReason) -> { - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(newPlayer).getLearnedSkills()) { - if (!instance.canInteractSkill(newPlayer)) continue; - instance.onRespawn(newPlayer, conqueredEnd); + Skills storage = SkillAPI.getSkillsFrom(newPlayer); + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; + + if (!optional.get().canInteractSkill(newPlayer)) continue; + optional.get().onRespawn(newPlayer, conqueredEnd); } }); EntityEvents.PROJECTILE_HIT.register((result, projectile, hitResultChangeable) -> { if (!(result instanceof EntityHitResult hitResult)) return; if (!(hitResult.getEntity() instanceof LivingEntity hitEntity)) return; + Skills storage = SkillAPI.getSkillsFrom(hitEntity); + + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { + Optional optional = storage.getSkill(instance.getSkill()); + if (optional.isEmpty()) continue; - for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(hitEntity).getLearnedSkills()) { - if (!instance.canInteractSkill(hitEntity)) continue; - instance.onProjectileHit(hitEntity, hitResult, projectile, hitResultChangeable); + if (!optional.get().canInteractSkill(hitEntity)) continue; + optional.get().onProjectileHit(hitEntity, hitResult, projectile, hitResultChangeable); } }); } diff --git a/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java b/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java index 6a68b06b..dc314663 100644 --- a/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java +++ b/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java @@ -73,7 +73,7 @@ private static void handleSkillTick(LivingEntity entity, Level level, Skills sto private static void tickSkills(LivingEntity entity, Skills storage) { List tickingSkills = new ArrayList<>(); - for (ManasSkillInstance instance : storage.getLearnedSkills()) { + for (ManasSkillInstance instance : List.copyOf(storage.getLearnedSkills())) { Optional optional = storage.getSkill(instance.getSkill()); if (optional.isEmpty()) continue; diff --git a/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java b/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java index 298ed0bf..d01b3305 100644 --- a/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java +++ b/common/src/main/java/com/github/manasmods/manascore/storage/CombinedStorage.java @@ -46,7 +46,7 @@ public void load(CompoundTag tag) { // Get serialized storage data CompoundTag entryTag = (CompoundTag) t; // Get storage id - ResourceLocation id = ResourceLocation.withDefaultNamespace(entryTag.getString(STORAGE_ID_KEY)); + ResourceLocation id = ResourceLocation.parse(entryTag.getString(STORAGE_ID_KEY)); // Construct storage Storage storage = StorageManager.constructStorageFor(this.holder.manasCore$getStorageType(), id, holder); if (storage == null) { @@ -65,7 +65,7 @@ public void handleUpdatePacket(CompoundTag tag) { for (Tag e : entriesTag) { CompoundTag entryTag = (CompoundTag) e; - ResourceLocation id = ResourceLocation.withDefaultNamespace(entryTag.getString(STORAGE_ID_KEY)); + ResourceLocation id = ResourceLocation.parse(entryTag.getString(STORAGE_ID_KEY)); Storage storage = this.storages.get(id); if (storage == null) { ManasCore.Logger.warn("Failed to find storage for id {}. All information about this storage will be dropped!", id); diff --git a/commonTest/src/main/java/com/github/manasmods/testmod/registry/RegisterTest.java b/commonTest/src/main/java/com/github/manasmods/testmod/registry/RegisterTest.java index a02c59c8..4b215efd 100644 --- a/commonTest/src/main/java/com/github/manasmods/testmod/registry/RegisterTest.java +++ b/commonTest/src/main/java/com/github/manasmods/testmod/registry/RegisterTest.java @@ -11,7 +11,7 @@ import dev.architectury.registry.registries.RegistrySupplier; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.ai.attributes.RangedAttribute; +import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.npc.Villager; import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; @@ -37,12 +37,12 @@ public class RegisterTest { .fireImmune() .withSize(1, 1) .end(); - private static final RegistrySupplier TEST_ATTRIBUTE = REGISTER.attribute("test_attribute") + private static final RegistrySupplier TEST_ATTRIBUTE = REGISTER.attribute("test_attribute") .withDefaultValue(69) .withMaximumValue(420) .applyToAll() .end(); - private static final RegistrySupplier TEST_ENTITY_ATTRIBUTE = REGISTER.attribute("test_player_attribute") + private static final RegistrySupplier TEST_ENTITY_ATTRIBUTE = REGISTER.attribute("test_player_attribute") .withDefaultValue(5) .withMaximumValue(10) .applyTo(() -> EntityType.PLAYER) diff --git a/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java b/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java index 2cf97ad6..77d80b60 100644 --- a/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java +++ b/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java @@ -114,7 +114,7 @@ public void onProjectileHit(ManasSkillInstance instance, LivingEntity living, En if (projectile instanceof ThrownTrident) { ManasCore.Logger.info("Dodged"); result.set(EntityEvents.ProjectileHitResult.PASS); - } + } else result.set(EntityEvents.ProjectileHitResult.HIT_NO_DAMAGE); } public boolean onDeath(ManasSkillInstance instance, LivingEntity owner, DamageSource source) { diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java index 18d61146..2beabd05 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractArrow.java @@ -3,7 +3,6 @@ import com.github.manasmods.manascore.api.world.entity.EntityEvents; import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; import com.github.manasmods.manascore.utils.Changeable; -import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; @@ -13,6 +12,7 @@ import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.projectile.AbstractArrow; import net.minecraft.world.entity.projectile.Projectile; +import net.minecraft.world.entity.projectile.ProjectileDeflection; import net.minecraft.world.level.Level; import net.minecraft.world.phys.EntityHitResult; import net.minecraft.world.phys.HitResult; @@ -38,27 +38,37 @@ public MixinAbstractArrow(EntityType entityType, Level lev private ProjectileHitResult onHitEventResult = null; private final IntOpenHashSet ignoredEntities = new IntOpenHashSet(); - @WrapMethod(method = "onHitEntity") - void onHit(EntityHitResult entityHitResult, Operation original) { + @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/AbstractArrow;hitTargetOrDeflectSelf(Lnet/minecraft/world/phys/HitResult;)Lnet/minecraft/world/entity/projectile/ProjectileDeflection;")) + ProjectileDeflection onHit(AbstractArrow instance, HitResult result, Operation original, @Local LocalRef entityHitResult) { Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(entityHitResult, this, resultChangeable); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); this.onHitEventResult = resultChangeable.get(); - switch (this.onHitEventResult) { - case DEFAULT, PASS -> { - original.call(entityHitResult); + return switch (this.onHitEventResult) { + case DEFAULT -> { this.onHitEventResult = null; + yield original.call(instance, result); } case HIT -> { this.setPierceLevel((byte) 0); - original.call(entityHitResult); this.onHitEventResult = null; + yield original.call(instance, result); } case HIT_NO_DAMAGE -> { this.discard(); + yield null; } - case null -> {} - } + case PASS -> { + if (result.getType() != HitResult.Type.ENTITY) { + this.onHitEventResult = null; + yield original.call(instance, result); + } else { + this.ignoredEntities.add(entityHitResult.get().getEntity().getId()); + yield null; + } + } + case null -> original.call(instance, result); + }; } @WrapOperation(method = "tick", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/projectile/AbstractArrow;hasImpulse:Z")) diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractHurtingProjectile.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractHurtingProjectile.java new file mode 100644 index 00000000..10334b77 --- /dev/null +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinAbstractHurtingProjectile.java @@ -0,0 +1,27 @@ +package com.github.manasmods.manascore.fabric.core; + +import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; +import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.world.entity.projectile.AbstractHurtingProjectile; +import net.minecraft.world.entity.projectile.ProjectileDeflection; +import net.minecraft.world.phys.HitResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.Objects; + +@Mixin(AbstractHurtingProjectile.class) +public abstract class MixinAbstractHurtingProjectile { + + @WrapOperation(method = "tick", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/projectile/AbstractHurtingProjectile;hitTargetOrDeflectSelf(Lnet/minecraft/world/phys/HitResult;)Lnet/minecraft/world/entity/projectile/ProjectileDeflection;")) + ProjectileDeflection onHit(AbstractHurtingProjectile instance, HitResult result, Operation original) { + Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); + if (!Objects.equals(resultChangeable.get(), ProjectileHitResult.DEFAULT)) return null; + return original.call(instance, result); + } +} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java index 622d54f7..47c0aa3c 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFireRocketEntity.java @@ -3,51 +3,26 @@ import com.github.manasmods.manascore.api.world.entity.EntityEvents; import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; import com.github.manasmods.manascore.utils.Changeable; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.projectile.FireworkRocketEntity; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.level.Level; import net.minecraft.world.phys.HitResult; -import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; + +import java.util.Objects; @Mixin(FireworkRocketEntity.class) public abstract class MixinFireRocketEntity extends Projectile { public MixinFireRocketEntity(EntityType entityType, Level level) { super(entityType, level); } - @Unique - @Nullable - private ProjectileHitResult onHitEventResult = null; - //TODO: This has to be done new for the new changes - /*@WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/FireworkRocketEntity;onHit(Lnet/minecraft/world/phys/HitResult;)V")) - void onHit(FireworkRocketEntity instance, HitResult result, Operation original) { - Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); - this.onHitEventResult = resultChangeable.get(); - switch (this.onHitEventResult) { - case DEFAULT, HIT -> { - original.call(instance, result); - this.onHitEventResult = null; - } - case HIT_NO_DAMAGE -> this.discard(); - case PASS -> { - if (result.getType() != HitResult.Type.ENTITY) { - original.call(instance, result); - this.onHitEventResult = null; - } - } - } - }*/ - - @WrapOperation(method = "tick", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/projectile/FireworkRocketEntity;hasImpulse:Z")) - void onImpulseSet(FireworkRocketEntity instance, boolean value, Operation original) { - if (this.onHitEventResult == null) original.call(instance, value); - this.onHitEventResult = null; + @Override + protected void onHit(HitResult result) { + Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, this, resultChangeable); + if (!Objects.equals(resultChangeable.get(), ProjectileHitResult.DEFAULT)) return; + super.onHit(result); } } diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java new file mode 100644 index 00000000..cad9499c --- /dev/null +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinFishingHook.java @@ -0,0 +1,27 @@ +package com.github.manasmods.manascore.fabric.core; + +import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; +import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.world.entity.projectile.FishingHook; +import net.minecraft.world.entity.projectile.ProjectileDeflection; +import net.minecraft.world.phys.HitResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.Objects; + +@Mixin(FishingHook.class) +public abstract class MixinFishingHook { + + @WrapOperation(method = "checkCollision", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/projectile/FishingHook;hitTargetOrDeflectSelf(Lnet/minecraft/world/phys/HitResult;)Lnet/minecraft/world/entity/projectile/ProjectileDeflection;")) + ProjectileDeflection onHit(FishingHook instance, HitResult result, Operation original) { + Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); + if (!Objects.equals(resultChangeable.get(), ProjectileHitResult.DEFAULT)) return null; + return original.call(instance, result); + } +} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLivingEntity.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLivingEntity.java index 54185023..b1f08fa3 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLivingEntity.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLivingEntity.java @@ -7,24 +7,10 @@ import net.minecraft.world.entity.LivingEntity; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.At.Shift; -import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(LivingEntity.class) public class MixinLivingEntity { - @ModifyVariable(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = Shift.BEFORE), argsOnly = true) - float modifyDamage(float amount, @Local DamageSource damageSource) { - Changeable changeable = Changeable.of(amount); - if (EntityEvents.LIVING_HURT.invoker().hurt((LivingEntity) (Object) this, damageSource, changeable).isFalse()) return 0.0F; - return changeable.get(); - } - - @Inject(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = Shift.BEFORE), cancellable = true) - void cancelActuallyHurt(DamageSource damageSource, float damageAmount, CallbackInfo ci) { - if (damageAmount <= 0F) ci.cancel(); - } @ModifyVariable(method = "actuallyHurt", at = @At(value = "LOAD", ordinal = 6), argsOnly = true) float modifyTotalDamage(float amount, @Local DamageSource damageSource) { diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java new file mode 100644 index 00000000..227fa2be --- /dev/null +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinLlamaSpit.java @@ -0,0 +1,27 @@ +package com.github.manasmods.manascore.fabric.core; + +import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; +import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.world.entity.projectile.LlamaSpit; +import net.minecraft.world.entity.projectile.ProjectileDeflection; +import net.minecraft.world.phys.HitResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.Objects; + +@Mixin(LlamaSpit.class) +public abstract class MixinLlamaSpit { + + @WrapOperation(method = "tick", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/projectile/LlamaSpit;hitTargetOrDeflectSelf(Lnet/minecraft/world/phys/HitResult;)Lnet/minecraft/world/entity/projectile/ProjectileDeflection;")) + ProjectileDeflection onHit(LlamaSpit instance, HitResult result, Operation original) { + Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); + if (!Objects.equals(resultChangeable.get(), ProjectileHitResult.DEFAULT)) return null; + return original.call(instance, result); + } +} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinPlayer.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinPlayer.java index d02da0a3..479e162d 100644 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinPlayer.java +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinPlayer.java @@ -8,25 +8,10 @@ import net.minecraft.world.entity.player.Player; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.At.Shift; -import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(Player.class) public class MixinPlayer { - @ModifyVariable(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = Shift.BEFORE), argsOnly = true) - float modifyDamage(float amount, @Local DamageSource damageSource) { - Changeable changeable = Changeable.of(amount); - if (EntityEvents.LIVING_HURT.invoker().hurt((LivingEntity) (Object) this, damageSource, changeable).isFalse()) return 0.0F; - return changeable.get(); - } - - @Inject(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = Shift.BEFORE), cancellable = true) - void cancelActuallyHurt(DamageSource damageSource, float damageAmount, CallbackInfo ci) { - if (damageAmount <= 0F) ci.cancel(); - } - @ModifyVariable(method = "actuallyHurt", at = @At(value = "LOAD", ordinal = 6), argsOnly = true) float modifyTotalDamage(float amount, @Local DamageSource damageSource) { Changeable changeable = Changeable.of(amount); diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java deleted file mode 100644 index 625df3e3..00000000 --- a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinProjectile.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.manasmods.manascore.fabric.core; - -import com.github.manasmods.manascore.api.world.entity.EntityEvents; -import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; -import com.github.manasmods.manascore.utils.Changeable; -import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.projectile.Projectile; -import net.minecraft.world.level.Level; -import net.minecraft.world.phys.EntityHitResult; -import net.minecraft.world.phys.HitResult; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -@Mixin(Projectile.class) -public abstract class MixinProjectile { - - @WrapMethod(method = "onHitEntity") - void onHit(EntityHitResult result, Operation original) { - Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); - EntityEvents.PROJECTILE_HIT.invoker().hit(result, ((Projectile)((Object)this)), resultChangeable); - - if (!resultChangeable.get().equals(ProjectileHitResult.DEFAULT)) return; - original.call(result); - } -} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java new file mode 100644 index 00000000..35b6e0e1 --- /dev/null +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinShulkerBullet.java @@ -0,0 +1,27 @@ +package com.github.manasmods.manascore.fabric.core; + +import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; +import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.world.entity.projectile.ProjectileDeflection; +import net.minecraft.world.entity.projectile.ShulkerBullet; +import net.minecraft.world.phys.HitResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.Objects; + +@Mixin(ShulkerBullet.class) +public abstract class MixinShulkerBullet { + + @WrapOperation(method = "tick", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/projectile/ShulkerBullet;hitTargetOrDeflectSelf(Lnet/minecraft/world/phys/HitResult;)Lnet/minecraft/world/entity/projectile/ProjectileDeflection;")) + ProjectileDeflection onHit(ShulkerBullet instance, HitResult result, Operation original) { + Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); + if (!Objects.equals(resultChangeable.get(), ProjectileHitResult.DEFAULT)) return null; + return original.call(instance, result); + } +} diff --git a/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java new file mode 100644 index 00000000..5372ae51 --- /dev/null +++ b/fabric/src/main/java/com/github/manasmods/manascore/fabric/core/MixinThrowableProjectile.java @@ -0,0 +1,27 @@ +package com.github.manasmods.manascore.fabric.core; + +import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; +import com.github.manasmods.manascore.utils.Changeable; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.world.entity.projectile.ProjectileDeflection; +import net.minecraft.world.entity.projectile.ThrowableProjectile; +import net.minecraft.world.phys.HitResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.Objects; + +@Mixin(ThrowableProjectile.class) +public abstract class MixinThrowableProjectile { + + @WrapOperation(method = "tick", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/projectile/ThrowableProjectile;hitTargetOrDeflectSelf(Lnet/minecraft/world/phys/HitResult;)Lnet/minecraft/world/entity/projectile/ProjectileDeflection;")) + ProjectileDeflection onHit(ThrowableProjectile instance, HitResult result, Operation original) { + Changeable resultChangeable = Changeable.of(ProjectileHitResult.DEFAULT); + EntityEvents.PROJECTILE_HIT.invoker().hit(result, instance, resultChangeable); + if (!Objects.equals(resultChangeable.get(), ProjectileHitResult.DEFAULT)) return null; + return original.call(instance, result); + } +} diff --git a/fabric/src/main/resources/manascore.mixins.json b/fabric/src/main/resources/manascore.mixins.json index 26d46cd3..55161857 100644 --- a/fabric/src/main/resources/manascore.mixins.json +++ b/fabric/src/main/resources/manascore.mixins.json @@ -5,12 +5,16 @@ "compatibilityLevel": "JAVA_17", "mixins": [ "MixinAbstractArrow", - "MixinProjectile", + "MixinAbstractHurtingProjectile", "MixinFireRocketEntity", + "MixinFishingHook", "MixinLivingEntity", + "MixinLlamaSpit", "MixinMob", "MixinPlayer", - "MixinStartAttacking" + "MixinShulkerBullet", + "MixinStartAttacking", + "MixinThrowableProjectile" ], "injectors": { "defaultRequire": 1 diff --git a/neoforge/src/main/java/com/github/manasmods/manascore/neoforge/NeoForgeCommonEventInvoker.java b/neoforge/src/main/java/com/github/manasmods/manascore/neoforge/NeoForgeCommonEventInvoker.java index 906beddf..1c35ab53 100644 --- a/neoforge/src/main/java/com/github/manasmods/manascore/neoforge/NeoForgeCommonEventInvoker.java +++ b/neoforge/src/main/java/com/github/manasmods/manascore/neoforge/NeoForgeCommonEventInvoker.java @@ -4,13 +4,13 @@ import com.github.manasmods.manascore.utils.Changeable; import net.minecraft.world.entity.LivingEntity; import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.common.Mod; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.common.damagesource.DamageContainer; import net.neoforged.neoforge.event.entity.ProjectileImpactEvent; import net.neoforged.neoforge.event.entity.living.LivingChangeTargetEvent; import net.neoforged.neoforge.event.entity.living.LivingDamageEvent; -import net.neoforged.neoforge.event.entity.living.LivingHurtEvent; -@Mod.EventBusSubscriber +@EventBusSubscriber public class NeoForgeCommonEventInvoker { private NeoForgeCommonEventInvoker() { } @@ -26,23 +26,12 @@ static void onLivingChangeTarget(final LivingChangeTargetEvent e) { } @SubscribeEvent - static void onLivingHurt(final LivingHurtEvent e) { - Changeable amount = Changeable.of(e.getAmount()); - if (EntityEvents.LIVING_HURT.invoker().hurt(e.getEntity(), e.getSource(), amount).isFalse()) { - e.setCanceled(true); - } else { - e.setAmount(amount.get()); - } - } - - @SubscribeEvent - static void onLivingDamage(final LivingDamageEvent e) { - Changeable amount = Changeable.of(e.getAmount()); - if (EntityEvents.LIVING_DAMAGE.invoker().damage(e.getEntity(), e.getSource(), amount).isFalse()) { - e.setCanceled(true); - } else { - e.setAmount(amount.get()); - } + static void onLivingDamage(final LivingDamageEvent.Pre e) { + DamageContainer container = e.getContainer(); + Changeable amount = Changeable.of(container.getNewDamage()); + if (EntityEvents.LIVING_DAMAGE.invoker().damage(e.getEntity(), container.getSource(), amount).isFalse()) { + container.setNewDamage(0); + } else container.setNewDamage(amount.get()); } @SubscribeEvent diff --git a/neoforge/src/main/java/com/github/manasmods/manascore/storage/neoforge/StorageEventListener.java b/neoforge/src/main/java/com/github/manasmods/manascore/storage/neoforge/StorageEventListener.java index 46f5a620..f87303e5 100644 --- a/neoforge/src/main/java/com/github/manasmods/manascore/storage/neoforge/StorageEventListener.java +++ b/neoforge/src/main/java/com/github/manasmods/manascore/storage/neoforge/StorageEventListener.java @@ -3,7 +3,7 @@ import com.github.manasmods.manascore.storage.StorageManager; import net.minecraft.server.level.ServerPlayer; import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.common.Mod.EventBusSubscriber; +import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.event.entity.player.PlayerEvent; @EventBusSubscriber diff --git a/neoforge/src/main/java/com/github/manasmods/manascore/world/entity/attribute/neoforge/ManasAttributeRegistryImpl.java b/neoforge/src/main/java/com/github/manasmods/manascore/world/entity/attribute/neoforge/ManasAttributeRegistryImpl.java index 3192f84f..6b4a5fb7 100644 --- a/neoforge/src/main/java/com/github/manasmods/manascore/world/entity/attribute/neoforge/ManasAttributeRegistryImpl.java +++ b/neoforge/src/main/java/com/github/manasmods/manascore/world/entity/attribute/neoforge/ManasAttributeRegistryImpl.java @@ -8,7 +8,7 @@ import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.AttributeSupplier.Builder; import net.neoforged.bus.api.IEventBus; -import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; +import net.neoforged.fml.ModLoadingContext; import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent; import net.neoforged.neoforge.event.entity.EntityAttributeModificationEvent; @@ -64,7 +64,7 @@ static void registerNewAttributes(final EntityAttributeCreationEvent e) { @SuppressWarnings({"removal", "deprecation"}) public static void init() { - IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); + IEventBus modEventBus = ModLoadingContext.get().getActiveContainer().getEventBus(); modEventBus.addListener(ManasAttributeRegistryImpl::registerAttributes); modEventBus.addListener(ManasAttributeRegistryImpl::registerNewAttributes); } From e03577124f3719777f4a7eb1a8933ed89c6a83a8 Mon Sep 17 00:00:00 2001 From: Minh Date: Fri, 16 Aug 2024 02:54:58 +0700 Subject: [PATCH 12/12] ManasSkill's Held Attribute Modifiers + Critical Chance for non-player mobs --- .../api/registry/AbstractRegister.java | 28 +++++---- .../manascore/api/skill/ManasSkill.java | 62 ++++++++++++++++++- .../api/skill/ManasSkillInstance.java | 29 ++++++++- .../attribute/ManasCoreAttributeUtils.java | 9 +++ .../manascore/core/MixinLivingEntity.java | 13 ++++ .../manasmods/manascore/core/MixinPlayer.java | 6 +- .../RequestSkillActivationPacket.java | 1 + .../toserver/RequestSkillReleasePacket.java | 2 +- .../manascore/skill/SkillStorage.java | 20 +++--- .../manascore/skill/TickingSkill.java | 7 +-- .../attribute/ManasAttributeRegistry.java | 3 +- .../manasmods/testmod/registry/TestSkill.java | 3 + 12 files changed, 154 insertions(+), 29 deletions(-) diff --git a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java index 90e4ebb1..33f68508 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/registry/AbstractRegister.java @@ -425,14 +425,14 @@ public static class AttributeBuilder> extends Cont protected double maximumValue; protected boolean syncable; - protected Map>, Double> applicableEntityTypes; + protected Map>, Double> applicableEntityTypes; protected boolean applyToAll = false; private AttributeBuilder(R register, String name) { super(register, name); this.defaultValue = 1; this.minimumValue = 0; - this.maximumValue = 1_000_000; + this.maximumValue = 100_000_000_000D; this.syncable = false; this.applicableEntityTypes = new HashMap<>(); } @@ -472,7 +472,7 @@ public AttributeBuilder syncable() { /** * Applies the attribute to all given entities with the default value. */ - public AttributeBuilder applyTo(double defaultValue, Supplier> entityType) { + public AttributeBuilder applyTo(double defaultValue, Supplier> entityType) { this.applicableEntityTypes.put(entityType, defaultValue); return this; } @@ -480,7 +480,7 @@ public AttributeBuilder applyTo(double defaultValue, Supplier applyTo(Supplier> entityType) { + public AttributeBuilder applyTo(Supplier> entityType) { return applyTo(this.defaultValue, entityType); } @@ -488,8 +488,8 @@ public AttributeBuilder applyTo(Supplier> * Applies the attribute to all given entities with the default value. */ @SafeVarargs - public final AttributeBuilder applyTo(double defaultValue, Supplier>... entityType) { - for (Supplier> typeSupplier : entityType) { + public final AttributeBuilder applyTo(double defaultValue, Supplier>... entityType) { + for (Supplier> typeSupplier : entityType) { this.applicableEntityTypes.put(typeSupplier, defaultValue); } return this; @@ -499,15 +499,15 @@ public final AttributeBuilder applyTo(double defaultValue, Supplier applyTo(Supplier>... entityType) { + public final AttributeBuilder applyTo(Supplier>... entityType) { return applyTo(this.defaultValue, entityType); } /** * Applies the attribute to all given entities with the default value. */ - public AttributeBuilder applyTo(double defaultValue, List>> entityTypes) { - for (Supplier> typeSupplier : entityTypes) { + public AttributeBuilder applyTo(double defaultValue, List>> entityTypes) { + for (Supplier> typeSupplier : entityTypes) { this.applicableEntityTypes.put(typeSupplier, defaultValue); } return this; @@ -516,7 +516,7 @@ public AttributeBuilder applyTo(double defaultValue, List applyTo(List>> entityTypes) { + public AttributeBuilder applyTo(List>> entityTypes) { return applyTo(this.defaultValue, entityTypes); } @@ -537,8 +537,12 @@ public RegistrySupplier end() { Holder holder = Registry.registerForHolder(BuiltInRegistries.ATTRIBUTE, this.id, attribute); RegistrySupplier supplier = this.register.attributes.register(this.id, () -> attribute); - if (this.applyToAll) ManasAttributeRegistry.registerToAll(builder -> builder.add(holder, this.defaultValue)); - this.applicableEntityTypes.forEach((typeSupplier, defaultValue) -> ManasAttributeRegistry.register(typeSupplier, builder -> builder.add(holder, defaultValue))); + if (this.applyToAll) { //registerToAll doesn't work + //ManasAttributeRegistry.registerToAll(builder -> builder.add(holder, this.defaultValue)); + BuiltInRegistries.ENTITY_TYPE.stream().forEach(type -> + ManasAttributeRegistry.register(() -> type, builder -> builder.add(holder, defaultValue))); + } else this.applicableEntityTypes.forEach((typeSupplier, defaultValue) -> + ManasAttributeRegistry.register(typeSupplier, builder -> builder.add(holder, defaultValue))); return supplier; } diff --git a/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java b/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java index 2f246554..9d9f3839 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkill.java @@ -1,10 +1,13 @@ package com.github.manasmods.manascore.api.skill; +import com.github.manasmods.manascore.ManasCore; import com.github.manasmods.manascore.api.world.entity.EntityEvents.ProjectileHitResult; import com.github.manasmods.manascore.utils.Changeable; +import com.google.common.collect.Maps; import lombok.Getter; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.core.Holder; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; @@ -12,17 +15,22 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.damagesource.DamageSource; 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.entity.player.Player; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.phys.EntityHitResult; import org.jetbrains.annotations.Nullable; +import java.util.Map; import java.util.Objects; @SuppressWarnings("unused") public class ManasSkill { @Getter private final SkillRarity rarity; + protected final Map, AttributeModifier> onHeldAttributeModifiers = Maps.newHashMap(); public ManasSkill(final SkillRarity rarity) { this.rarity = rarity; @@ -97,7 +105,7 @@ public boolean canInteractSkill(ManasSkillInstance instance, LivingEntity living * @return the maximum number of ticks that this skill can be held down with the skill activation button. *

*/ - public int getMaxHeldTime() { + public int getMaxHeldTime(ManasSkillInstance instance , LivingEntity entity) { return 72000; } @@ -164,6 +172,58 @@ public void addMasteryPoint(ManasSkillInstance instance, LivingEntity entity) { if (isMastered(instance, entity)) instance.onSkillMastered(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. + *

+ */ + public void addHeldAttributeModifier(Holder pAttribute, String id, double pAmount, AttributeModifier.Operation pOperation) { + ResourceLocation name = ResourceLocation.fromNamespaceAndPath(ManasCore.MOD_ID, id + "." + pAttribute.value().getDescriptionId()); + AttributeModifier attributemodifier = new AttributeModifier(name, pAmount, pOperation); + this.onHeldAttributeModifiers.put(pAttribute, attributemodifier); + } + + /** + * @return the amplifier for each attribute modifier that this skill applies. + *

+ * @param entity Affected {@link LivingEntity} owning this Skill. + * @param instance Affected {@link ManasSkillInstance} + * @param modifier Affected {@link AttributeModifier} that this skill provides. + */ + public double getAttributeModifierAmplifier(ManasSkillInstance instance, LivingEntity entity, AttributeModifier modifier) { + return modifier.amount(); + } + + /** + * 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) { + for (Map.Entry, AttributeModifier> entry : this.onHeldAttributeModifiers.entrySet()) { + AttributeInstance attribute = entity.getAttribute(entry.getKey()); + if (attribute != null) { + AttributeModifier modifier = entry.getValue(); + attribute.removeModifier(modifier.id()); + attribute.addPermanentModifier(new AttributeModifier(modifier.id(), + instance.getAttributeModifierAmplifier(entity, modifier), modifier.operation())); + } + } + } + + /** + * 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, AttributeModifier> entry : this.onHeldAttributeModifiers.entrySet()) { + AttributeInstance attribute = entity.getAttribute(entry.getKey()); + if (attribute != null) attribute.removeModifier(entry.getValue().id()); + } + } + /** * Called when the {@link LivingEntity} owing this Skill toggles it on. * diff --git a/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkillInstance.java b/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkillInstance.java index 34c5e592..39b2deb4 100644 --- a/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkillInstance.java +++ b/common/src/main/java/com/github/manasmods/manascore/api/skill/ManasSkillInstance.java @@ -15,9 +15,11 @@ import net.minecraft.network.chat.Style; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.tags.TagKey; import net.minecraft.world.InteractionHand; 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.player.Player; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.phys.EntityHitResult; @@ -53,6 +55,10 @@ public ResourceLocation getSkillId() { return this.skillRegistryObject.getId(); } + public boolean is(TagKey tag) { + return this.skillRegistryObject.is(tag); + } + /** * Used to create an exact copy of the current instance. */ @@ -164,8 +170,8 @@ public boolean canInteractSkill(LivingEntity living) { * @return the maximum number of ticks that this skill can be held down with the skill activation button. *

*/ - public int getMaxHeldTime() { - return this.getSkill().getMaxHeldTime(); + public int getMaxHeldTime(LivingEntity entity) { + return this.getSkill().getMaxHeldTime(this, entity); } /** @@ -362,6 +368,25 @@ public void setTag(@Nullable CompoundTag tag) { markDirty(); } + /** + * @return the amplifier for each attribute modifier that this instance applies. + *

+ * @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().getAttributeModifierAmplifier(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. * diff --git a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java index ed4fbc14..0bb4a414 100644 --- a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java +++ b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java @@ -1,7 +1,9 @@ package com.github.manasmods.manascore.attribute; import lombok.experimental.UtilityClass; +import net.minecraft.network.protocol.game.ClientboundAnimatePacket; import net.minecraft.server.level.ServerLevel; +import net.minecraft.sounds.SoundEvents; import net.minecraft.util.Mth; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.Entity; @@ -38,6 +40,13 @@ public static float getWeaponDamage(LivingEntity attacker, @Nullable Entity targ return damage; } + public static void triggerCriticalAttackEffect(Entity target, Entity attacker) { + target.level().playSound(null, target.getX(), target.getY(), target.getZ(), + SoundEvents.PLAYER_ATTACK_CRIT, attacker.getSoundSource(), 1.0F, 1.0F); + if (target.level() instanceof ServerLevel level) + level.getChunkSource().broadcastAndSend(target, new ClientboundAnimatePacket(target, 4)); + } + public static Vec3 getLookTowardVec(Player player, double distance) { float f = player.getXRot(); float g = player.getYRot(); diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java index d347eef6..109905d7 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java @@ -1,10 +1,13 @@ package com.github.manasmods.manascore.core; import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; +import com.github.manasmods.manascore.attribute.ManasCoreAttributes; import com.github.manasmods.manascore.utils.Changeable; import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.Mob; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At.Shift; @@ -27,6 +30,16 @@ void onPostTick(CallbackInfo ci) { @ModifyVariable(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F", shift = Shift.BEFORE), argsOnly = true) float modifyDamage(float amount, @Local DamageSource damageSource) { + //Adds Critical Attack system for non-player mobs + if (damageSource.getDirectEntity() instanceof Mob mob) { + double chance = mob.getAttribute(ManasCoreAttributes.CRIT_CHANCE) != null ? mob.getAttributeValue(ManasCoreAttributes.CRIT_CHANCE) : 0; + if (mob.getRandom().nextFloat() <= chance / 100.0) { + double multiplier = mob.getAttribute(ManasCoreAttributes.CRIT_MULTIPLIER) != null ? mob.getAttributeValue(ManasCoreAttributes.CRIT_MULTIPLIER) : 1.5; + amount *= (float) multiplier; + ManasCoreAttributeUtils.triggerCriticalAttackEffect((LivingEntity) (Object) this, mob); + } + } + Changeable changeable = Changeable.of(amount); if (EntityEvents.LIVING_HURT.invoker().hurt((LivingEntity) (Object) this, damageSource, changeable).isFalse()) return 0.0F; return changeable.get(); diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java index 5707e7d8..2382160c 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java @@ -1,10 +1,12 @@ package com.github.manasmods.manascore.core; import com.github.manasmods.manascore.api.world.entity.EntityEvents; +import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; import com.github.manasmods.manascore.attribute.ManasCoreAttributes; import com.github.manasmods.manascore.utils.Changeable; import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.AttributeInstance; import net.minecraft.world.entity.player.Player; @@ -16,12 +18,14 @@ public abstract class MixinPlayer { @ModifyArg(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"), index = 1) - private float getCritChanceDamage(float amount, @Local(ordinal = 1) float g, @Local(ordinal = 2) boolean bl3) { + private float getCritChanceDamage(float amount, @Local(ordinal = 0, argsOnly = true) Entity target, + @Local(ordinal = 1) float g, @Local(ordinal = 2) boolean bl3) { Player player = (Player) (Object) this; if (!bl3) { AttributeInstance instance = player.getAttribute(ManasCoreAttributes.CRIT_CHANCE); if (instance == null || player.getRandom().nextInt(100) > instance.getValue()) return amount; float beforeEnchant = amount - g; + ManasCoreAttributeUtils.triggerCriticalAttackEffect(target, player); return beforeEnchant * getCritMultiplier(1.5F) + g; } return amount; diff --git a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillActivationPacket.java b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillActivationPacket.java index 437d85b5..b98c0331 100644 --- a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillActivationPacket.java +++ b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillActivationPacket.java @@ -46,6 +46,7 @@ public void handle(Supplier contextSupplier) { skill.onPressed(player, keyNumber); storage.markDirty(); + skill.addHeldAttributeModifiers(player); SkillStorage.tickingSkills.put(player.getUUID(), new TickingSkill(skill.getSkill())); }); } diff --git a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillReleasePacket.java b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillReleasePacket.java index 95853511..859aa6dd 100644 --- a/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillReleasePacket.java +++ b/common/src/main/java/com/github/manasmods/manascore/network/toserver/RequestSkillReleasePacket.java @@ -40,7 +40,7 @@ public void handle(Supplier contextSupplier) { context.queue(() -> { Player player = context.getPlayer(); if (player == null) return; - StorageManager.getStorage(player, SkillStorage.getKey()).handleSkillRelease(skillList, heldTick, keyNumber); + StorageManager.getStorage(player, SkillStorage.getKey()).handleSkillRelease(skillList, player, heldTick, keyNumber); }); } } diff --git a/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java b/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java index dc314663..af4864c8 100644 --- a/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java +++ b/common/src/main/java/com/github/manasmods/manascore/skill/SkillStorage.java @@ -110,7 +110,11 @@ private static void checkPlayerOnlyEffects(LivingEntity entity, Skills storage) private static void handleSkillHeldTick(Player player, Skills storage) { if (!tickingSkills.containsKey(player.getUUID())) return; - tickingSkills.get(player.getUUID()).removeIf(skill -> !skill.tick(storage, player)); + tickingSkills.get(player.getUUID()).removeIf(skill -> { + boolean tick = skill.tick(storage, player); + if (!tick) skill.getSkill().removeHeldAttributeModifiers(player); + return !tick; + }); } private final Map skillInstances = new HashMap<>(); @@ -170,18 +174,20 @@ public void forEachSkill(BiConsumer skillInsta markDirty(); } - public void handleSkillRelease(List skillList, int heldTick, int keyNumber) { + public void handleSkillRelease(List skillList, Player player, int heldTick, int keyNumber) { for (final ResourceLocation skillId : skillList) { getSkill(skillId).ifPresent(skill -> { if (!skill.canInteractSkill(getOwner())) return; - if (skill.onCoolDown() && !skill.canIgnoreCoolDown(getOwner())) return; - skill.onRelease(getOwner(), heldTick, keyNumber); - if (skill.isDirty()) markDirty(); + + if (!skill.onCoolDown() || skill.canIgnoreCoolDown(getOwner())) { + skill.onRelease(getOwner(), heldTick, keyNumber); + if (skill.isDirty()) markDirty(); + } UUID ownerID = getOwner().getUUID(); - if (tickingSkills.containsKey(ownerID)) { + if (tickingSkills.containsKey(ownerID)) tickingSkills.get(ownerID).removeIf(tickingSkill -> tickingSkill.getSkill() == skill.getSkill()); - } + skill.getSkill().removeHeldAttributeModifiers(player); }); } } diff --git a/common/src/main/java/com/github/manasmods/manascore/skill/TickingSkill.java b/common/src/main/java/com/github/manasmods/manascore/skill/TickingSkill.java index 0926b51f..bacc558a 100644 --- a/common/src/main/java/com/github/manasmods/manascore/skill/TickingSkill.java +++ b/common/src/main/java/com/github/manasmods/manascore/skill/TickingSkill.java @@ -10,26 +10,25 @@ public class TickingSkill { private int duration = 0; - private final int maxDuration; @Getter private final ManasSkill skill; public TickingSkill(ManasSkill skill) { this.skill = skill; - this.maxDuration = skill.getMaxHeldTime(); } public boolean tick(Skills storage, LivingEntity entity) { Optional optional = storage.getSkill(skill); if (optional.isEmpty()) return false; - if (reachedMaxDuration()) return false; ManasSkillInstance instance = optional.get(); + if (reachedMaxDuration(instance, entity)) return false; if (!instance.canInteractSkill(entity)) return false; return instance.onHeld(entity, this.duration++); } - public boolean reachedMaxDuration() { + public boolean reachedMaxDuration(ManasSkillInstance instance, LivingEntity entity) { + int maxDuration = instance.getMaxHeldTime(entity); if (maxDuration == -1) return false; return duration >= maxDuration; } diff --git a/common/src/main/java/com/github/manasmods/manascore/world/entity/attribute/ManasAttributeRegistry.java b/common/src/main/java/com/github/manasmods/manascore/world/entity/attribute/ManasAttributeRegistry.java index f2536965..675358db 100644 --- a/common/src/main/java/com/github/manasmods/manascore/world/entity/attribute/ManasAttributeRegistry.java +++ b/common/src/main/java/com/github/manasmods/manascore/world/entity/attribute/ManasAttributeRegistry.java @@ -1,6 +1,7 @@ package com.github.manasmods.manascore.world.entity.attribute; import dev.architectury.injectables.annotations.ExpectPlatform; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; @@ -12,7 +13,7 @@ public class ManasAttributeRegistry { @ExpectPlatform - public static void register(Supplier> type, Consumer builder) { + public static void register(Supplier> type, Consumer builder) { throw new AssertionError(); } diff --git a/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java b/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java index 77d80b60..f531f972 100644 --- a/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java +++ b/commonTest/src/main/java/com/github/manasmods/testmod/registry/TestSkill.java @@ -12,6 +12,8 @@ import net.minecraft.world.InteractionHand; 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.animal.IronGolem; import net.minecraft.world.entity.monster.Creeper; import net.minecraft.world.entity.monster.Spider; @@ -25,6 +27,7 @@ public class TestSkill extends ManasSkill { public TestSkill() { super(SkillRarity.Unique); ManasCore.Logger.info("Created skill!"); + this.addHeldAttributeModifier(Attributes.MOVEMENT_SPEED, "test_skill", 2.0F, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL); } public boolean canBeToggled(ManasSkillInstance instance, LivingEntity entity) {