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
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,14 +7,13 @@
import dev.architectury.registry.registries.DeferredRegister;
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;
Expand All @@ -29,11 +28,7 @@
import net.minecraft.world.level.block.state.BlockBehaviour;
import org.jetbrains.annotations.Nullable;

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;
Expand Down Expand Up @@ -201,6 +196,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 +248,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 +298,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 +394,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,25 +409,30 @@ 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);
}
}

/**
* Builder class for {@link RangedAttribute}s.
* Builder class for {@link Attribute}s.
*/
public static class AttributeBuilder<R extends AbstractRegister<R>> extends ContentBuilder<RangedAttribute, R> {
public static class AttributeBuilder<R extends AbstractRegister<R>> extends ContentBuilder<Attribute, R> {
protected double defaultValue;
protected double minimumValue;
protected double maximumValue;

protected boolean syncable;
protected Map<Supplier<EntityType<? extends LivingEntity>>, Double> applicableEntityTypes;
protected Map<Supplier<EntityType<? extends Entity>>, 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<>();
}
Expand Down Expand Up @@ -457,24 +472,24 @@ public AttributeBuilder<R> syncable() {
/**
* Applies the attribute to all given entities with the default value.
*/
public AttributeBuilder<R> applyTo(double defaultValue, Supplier<EntityType<? extends LivingEntity>> entityType) {
public AttributeBuilder<R> applyTo(double defaultValue, Supplier<EntityType<? extends Entity>> entityType) {
this.applicableEntityTypes.put(entityType, defaultValue);
return this;
}

/**
* Applies the attribute to all given entities with the default value.
*/
public AttributeBuilder<R> applyTo(Supplier<EntityType<? extends LivingEntity>> entityType) {
public AttributeBuilder<R> applyTo(Supplier<EntityType<? extends Entity>> entityType) {
return applyTo(this.defaultValue, entityType);
}

/**
* Applies the attribute to all given entities with the default value.
*/
@SafeVarargs
public final AttributeBuilder<R> applyTo(double defaultValue, Supplier<EntityType<? extends LivingEntity>>... entityType) {
for (Supplier<EntityType<? extends LivingEntity>> typeSupplier : entityType) {
public final AttributeBuilder<R> applyTo(double defaultValue, Supplier<EntityType<? extends Entity>>... entityType) {
for (Supplier<EntityType<? extends Entity>> typeSupplier : entityType) {
this.applicableEntityTypes.put(typeSupplier, defaultValue);
}
return this;
Expand All @@ -484,15 +499,15 @@ public final AttributeBuilder<R> applyTo(double defaultValue, Supplier<EntityTyp
* Applies the attribute to all given entities with the default value.
*/
@SafeVarargs
public final AttributeBuilder<R> applyTo(Supplier<EntityType<? extends LivingEntity>>... entityType) {
public final AttributeBuilder<R> applyTo(Supplier<EntityType<? extends Entity>>... entityType) {
return applyTo(this.defaultValue, entityType);
}

/**
* Applies the attribute to all given entities with the default value.
*/
public AttributeBuilder<R> applyTo(double defaultValue, List<Supplier<EntityType<? extends LivingEntity>>> entityTypes) {
for (Supplier<EntityType<? extends LivingEntity>> typeSupplier : entityTypes) {
public AttributeBuilder<R> applyTo(double defaultValue, List<Supplier<EntityType<? extends Entity>>> entityTypes) {
for (Supplier<EntityType<? extends Entity>> typeSupplier : entityTypes) {
this.applicableEntityTypes.put(typeSupplier, defaultValue);
}
return this;
Expand All @@ -501,7 +516,7 @@ public AttributeBuilder<R> applyTo(double defaultValue, List<Supplier<EntityType
/**
* Applies the attribute to all given entities with the default value.
*/
public AttributeBuilder<R> applyTo(List<Supplier<EntityType<? extends LivingEntity>>> entityTypes) {
public AttributeBuilder<R> applyTo(List<Supplier<EntityType<? extends Entity>>> entityTypes) {
return applyTo(this.defaultValue, entityTypes);
}

Expand All @@ -514,17 +529,26 @@ public AttributeBuilder<R> applyToAll() {
}

@Override
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 -> {
// 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)));
});

public RegistrySupplier<Attribute> 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);

Holder<Attribute> holder = Registry.registerForHolder(BuiltInRegistries.ATTRIBUTE, this.id, attribute);
RegistrySupplier<Attribute> supplier = this.register.attributes.register(this.id, () -> attribute);

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;
}

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


Expand Down Expand Up @@ -564,6 +588,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 +607,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 +624,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 +633,6 @@ public R build() {
}

public abstract RegistrySupplier<T> end();
public abstract Holder<T> endAsHolder();
}
}
Loading
Loading