Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ManasCore port to 1.21 #36

Merged
merged 12 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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> 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

Expand All @@ -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> 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> attribute, final ResourceLocation attributeModifierLocation) {
Optional.ofNullable(entity.getAttribute(attribute)).ifPresent(attributeInstance -> attributeInstance.removeModifier(attributeModifierLocation));
}

/**
Expand All @@ -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> attribute, final AttributeModifier modifier) {
removeModifier(entity, attribute, modifier.id());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -201,6 +203,11 @@ public ItemBuilder<R, T> withStackSize(final int stackSize) {
public RegistrySupplier<T> end() {
return this.register.items.register(this.id, () -> this.itemFactory.apply(this.properties));
}

@Override
public Holder<T> endAsHolder() {
return this.end().getRegistrar().getHolder(this.id);
}
}

/**
Expand Down Expand Up @@ -248,6 +255,11 @@ public RegistrySupplier<T> 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<T> endAsHolder() {
return this.end().getRegistrar().getHolder(this.id);
}
}

/**
Expand Down Expand Up @@ -293,6 +305,11 @@ public RegistrySupplier<T> end() {
return blockSupplier;
}

@Override
public Holder<T> endAsHolder() {
return this.end().getRegistrar().getHolder(this.id);
}

@FunctionalInterface
public interface BlockItemFactory<R extends AbstractRegister<R>, T extends BlockItem> {
BlockItemBuilder<R, T> modify(BlockItemBuilder<R, T> builder);
Expand Down Expand Up @@ -384,7 +401,7 @@ public RegistrySupplier<EntityType<T>> end() {
RegistrySupplier<EntityType<T>> supplier = this.register.entityTypes.register(this.id, () -> {
EntityType.Builder<T> 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();
Expand All @@ -399,6 +416,11 @@ public RegistrySupplier<EntityType<T>> end() {
supplier.listen(type -> ManasAttributeRegistry.registerNew(() -> type, this.attributeBuilder));
return supplier;
}

@Override
public Holder<EntityType<T>> endAsHolder() {
return this.end().getRegistrar().getHolder(this.id);
}
}

/**
Expand Down Expand Up @@ -518,13 +540,20 @@ public RegistrySupplier<RangedAttribute> end() {
RegistrySupplier<RangedAttribute> 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<Attribute> 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;
}

public Holder<RangedAttribute> endAsHolder() {
return this.end().getRegistrar().getHolder(this.id);
}
}


Expand Down Expand Up @@ -564,6 +593,11 @@ public final BlockEntityBuilder<R, T> withValidBlocks(Supplier<? extends BaseEnt
public RegistrySupplier<BlockEntityType<T>> 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<BlockEntityType<T>> endAsHolder() {
return this.end().getRegistrar().getHolder(this.id);
}
}

public static class SkillBuilder<R extends AbstractRegister<R>, T extends ManasSkill> extends ContentBuilder<T, R> {
Expand All @@ -578,6 +612,11 @@ private SkillBuilder(R register, String name, Supplier<T> skillFactory) {
public RegistrySupplier<T> end() {
return this.register.skills.register(this.id, this.skillFactory);
}

@Override
public Holder<T> endAsHolder() {
return this.end().getRegistrar().getHolder(this.id);
}
}

/**
Expand All @@ -590,7 +629,7 @@ protected static abstract class ContentBuilder<T, R extends AbstractRegister<R>>

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() {
Expand All @@ -599,5 +638,6 @@ public R build() {
}

public abstract RegistrySupplier<T> end();
public abstract Holder<T> endAsHolder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,7 +104,9 @@ protected void maxUpStep(CallbackInfoReturnable<Float> cir) {
Entity entity = (Entity) (Object) this;
if (!(entity instanceof LivingEntity living)) return;

AttributeInstance instance = living.getAttribute(ManasCoreAttributes.STEP_HEIGHT_ADDITION.get());
Holder<Attribute> attributeHolder = Holder.direct(ManasCoreAttributes.STEP_HEIGHT_ADDITION.get());

AttributeInstance instance = living.getAttribute(attributeHolder);
if (instance == null) return;
cir.setReturnValue((float) (cir.getReturnValue() + instance.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +25,9 @@ public abstract class MixinLivingEntity {
@Inject(method = "getJumpPower", at = @At("RETURN"), cancellable = true)
protected void getJumpPower(CallbackInfoReturnable<Float> cir) {
LivingEntity entity = (LivingEntity) (Object) this;
AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.JUMP_STRENGTH.get());
Holder<Attribute> 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();
Expand All @@ -34,7 +38,8 @@ protected void getJumpPower(CallbackInfoReturnable<Float> 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<Attribute> 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);
Expand All @@ -44,15 +49,17 @@ public float causeFallDamage(float fallDistance, float multiplier) {
@Inject(method = "jumpInLiquid", at = @At("HEAD"))
protected void jumpInLiquid(TagKey<Fluid> fluidTag, CallbackInfo ci) {
LivingEntity entity = (LivingEntity) (Object) this;
AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED_ADDITION.get());
Holder<Attribute> 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;
AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED_ADDITION.get());
Holder<Attribute> 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));
}
Expand All @@ -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<Attribute> attributeHolder = Holder.direct(ManasCoreAttributes.SWIM_SPEED_ADDITION.get());
AttributeInstance instance = entity.getAttribute(attributeHolder);
if (instance == null) return speed;
return (float) (speed * instance.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Attribute> 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;
Expand All @@ -29,15 +32,17 @@ 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<Attribute> attributeHolder = Holder.direct(ManasCoreAttributes.CRIT_MULTIPLIER.get());
AttributeInstance instance = player.getAttribute(attributeHolder);
if (instance == null) return multiplier;
return (float) instance.getValue();
}

@Inject(method = "getDestroySpeed", at = @At("RETURN"), cancellable = true)
protected void getDestroySpeed(BlockState state, CallbackInfoReturnable<Float> cir) {
LivingEntity entity = (LivingEntity) (Object) this;
AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.MINING_SPEED_MULTIPLIER.get());
Holder<Attribute> attributeHolder = Holder.direct(ManasCoreAttributes.MINING_SPEED_MULTIPLIER.get());
AttributeInstance instance = entity.getAttribute(attributeHolder);
if (instance == null) return;
cir.setReturnValue((float) (cir.getReturnValue() * instance.getValue()));
}
Expand Down
Loading
Loading