Skip to content

Commit

Permalink
Merge pull request #886 from pietro-lopes/attribute-fix
Browse files Browse the repository at this point in the history
widened access to variables and fixed lang
  • Loading branch information
LatvianModder authored Aug 18, 2024
2 parents 6d6a63a + 9087fa0 commit ccb7a9b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/main/java/dev/latvian/mods/kubejs/entity/AttributeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.neoforged.neoforge.common.BooleanAttribute;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

Expand All @@ -22,10 +23,10 @@ public class AttributeBuilder extends BuilderBase<Attribute> {
public record Range(double defaultValue, double min, double max) {
}

private final List<Predicate<EntityType<?>>> predicateList = new ArrayList<>();
private Either<Range, Boolean> defaultValue;
private boolean syncable = true;
private Attribute.Sentiment sentiment;
public final List<Predicate<EntityType<?>>> predicateList = new ArrayList<>();
public Either<Range, Boolean> defaultValue;
public boolean syncable = true;
public Attribute.Sentiment sentiment;

public AttributeBuilder(ResourceLocation id) {
super(id);
Expand Down Expand Up @@ -81,7 +82,7 @@ public AttributeBuilder attachToCategory(MobCategory category) {

@HideFromJS
public List<Predicate<EntityType<?>>> getPredicateList() {
return predicateList;
return Collections.unmodifiableList(predicateList);
}

@Override
Expand All @@ -90,11 +91,14 @@ public Attribute createObject() {
throw new IllegalArgumentException("Not possible to create a Boolean or Ranged Attribute. Use bool() or range() methods.");
}

var attribute = Either.unwrap(defaultValue.mapBoth(
l -> new RangedAttribute(this.id.toLanguageKey(), l.defaultValue, l.min, l.max),
r -> new BooleanAttribute(this.id.toLanguageKey(), r))
return Either.unwrap(defaultValue.mapBoth(
l -> new RangedAttribute(this.getBuilderTranslationKey(), l.defaultValue, l.min, l.max),
r -> new BooleanAttribute(this.getBuilderTranslationKey(), r))
);
}

@Override
public Attribute transformObject(Attribute attribute) {
if (syncable) {
attribute.setSyncable(true);
}
Expand All @@ -106,7 +110,6 @@ public Attribute createObject() {
if (predicateList.isEmpty()) {
predicateList.add(Predicates.alwaysTrue());
}

return attribute;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void registerAll(RegisterEvent event) {
@SubscribeEvent(priority = EventPriority.LOW)
public static void registerEntityAttributes(EntityAttributeModificationEvent event) {
var objStorage = RegistryObjectStorage.of(Registries.ATTRIBUTE);
List<Pair<Predicate<EntityType<?>>, Holder<Attribute>>> predicatePair = objStorage.objects.values().stream().map(AttributeBuilder.class::cast).flatMap(b -> b.getPredicateList().stream().map(p -> Pair.of(p, BuiltInRegistries.ATTRIBUTE.wrapAsHolder(b.get())))).toList();
List<Pair<Predicate<EntityType<?>>, Holder<Attribute>>> predicatePair = objStorage.objects.values().stream().filter(AttributeBuilder.class::isInstance).map(AttributeBuilder.class::cast).flatMap(b -> b.getPredicateList().stream().map(p -> Pair.of(p, BuiltInRegistries.ATTRIBUTE.wrapAsHolder(b.get())))).toList();

event.getTypes().forEach(type -> predicatePair.stream().filter(p -> p.first.test(type)).forEach(p -> event.add(type, p.second)));
}
Expand Down

0 comments on commit ccb7a9b

Please sign in to comment.