Skip to content

Commit

Permalink
fix null pointers in temp effect deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeathlyCow committed Apr 12, 2024
1 parent a702415 commit a68049a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import net.minecraft.world.gen.feature.Feature;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

/**
* Represents a configured instance of a {@link TemperatureEffect} type.
* See the <a href="https://github.com/TheDeathlyCow/frostiful/wiki/Temperature-Effects">wiki page</a> for details on
Expand All @@ -37,8 +39,8 @@
public record ConfiguredTemperatureEffect<C>(
TemperatureEffect<C> type,
C config,
@Nullable LootCondition predicate,
@Nullable EntityType<?> entityType,
Optional<LootCondition> predicate,
Optional<EntityType<?>> entityType,
NumberRange.DoubleRange temperatureScaleRange
) {

Expand Down Expand Up @@ -74,8 +76,8 @@ public void applyIfPossible(LivingEntity victim) {
}

private boolean testPredicate(LivingEntity victim, ServerWorld world) {
return this.predicate == null
|| this.predicate.test(
return this.predicate.isEmpty()
|| this.predicate.get().test(
new LootContext.Builder(
new LootContextParameterSet.Builder(world)
.add(LootContextParameters.THIS_ENTITY, victim)
Expand All @@ -84,9 +86,4 @@ private boolean testPredicate(LivingEntity victim, ServerWorld world) {
).build(java.util.Optional.empty())
);
}

@Nullable
public EntityType<?> getEntityType() {
return entityType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SequenceTemperatureEffect(Codec<Config> configCodec) {
@Override
public void apply(LivingEntity victim, ServerWorld serverWorld, Config config) {
for (ConfiguredTemperatureEffect<?> child : config.children()) {
EntityType<?> childType = child.getEntityType();
EntityType<?> childType = child.entityType().orElse(null);
if (childType == null || victim.getType() == childType) {
child.applyIfPossible(victim);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ protected TemperatureEffect(Codec<C> configCodec) {
.fieldOf("config")
.forGetter(ConfiguredTemperatureEffect::config),
LootConditionTypes.CODEC
.fieldOf("entity")
.orElse(null)
.optionalFieldOf("entity")
.forGetter(ConfiguredTemperatureEffect::predicate),
Registries.ENTITY_TYPE.getCodec()
.fieldOf("entity_type")
.orElse(null)
.forGetter(ConfiguredTemperatureEffect::getEntityType),
.optionalFieldOf("entity_type")
.forGetter(ConfiguredTemperatureEffect::entityType),
NumberRange.DoubleRange.CODEC
.fieldOf("temperature_scale_range")
.orElse(NumberRange.DoubleRange.ANY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void loadEffect(
JsonParseException::new
);

EntityType<?> type = effect.getEntityType();
EntityType<?> type = effect.entityType().orElse(null);
if (type != null) {
newTypeEffects.computeIfAbsent(
Registries.ENTITY_TYPE.getId(type),
Expand Down

0 comments on commit a68049a

Please sign in to comment.