Skip to content

Commit

Permalink
fix converter null default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed May 14, 2023
1 parent 9fd088a commit f62396c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public class ConvertedCowEntity extends ConvertedCreatureEntity<Cow> {
public ConvertedCowEntity(EntityType<? extends ConvertedCreatureEntity> type, Level world) {
super(type, world);
Expand Down Expand Up @@ -59,10 +61,11 @@ public ConvertedCreatureEntity<Cow> createFrom(@NotNull Cow entity) {
public static class CowConverter extends DefaultConverter {

public static final Codec<CowConverter> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper", ConvertiblesReloadListener.EntityEntry.Attributes.DEFAULT).forGetter(i -> i.helper)
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper").forGetter(i -> Optional.ofNullable(i.helper))
).apply(instance, CowConverter::new));

public CowConverter(ConvertiblesReloadListener.EntityEntry.Attributes helper) {
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public CowConverter(Optional<ConvertiblesReloadListener.EntityEntry.Attributes> helper) {
super(helper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

/**
* {@link IConvertedCreature} for sheep
Expand Down Expand Up @@ -130,10 +131,11 @@ public ConvertedCreatureEntity<Sheep> createFrom(@NotNull Sheep entity) {
public static class SheepConverter extends DefaultConverter {

public static final Codec<SheepConverter> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper", ConvertiblesReloadListener.EntityEntry.Attributes.DEFAULT).forGetter(i -> i.helper)
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper").forGetter(i -> Optional.ofNullable(i.helper))
).apply(instance, SheepConverter::new));

public SheepConverter(ConvertiblesReloadListener.EntityEntry.Attributes helper) {
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public SheepConverter(Optional<ConvertiblesReloadListener.EntityEntry.Attributes> helper) {
super(helper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
import de.teamlapen.vampirism.entity.converted.DefaultConvertingHandler;
import de.teamlapen.vampirism.entity.converted.VampirismEntityRegistry;

import java.util.Optional;

public class DefaultConverter implements Converter {

public static final Codec<DefaultConverter> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper", ConvertiblesReloadListener.EntityEntry.Attributes.DEFAULT).forGetter(i -> i.helper)
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper").forGetter(i -> Optional.ofNullable(i.helper))
).apply(instance, DefaultConverter::new));

protected final ConvertiblesReloadListener.EntityEntry.Attributes helper;

public DefaultConverter(ConvertiblesReloadListener.EntityEntry.Attributes helper) {
this.helper = helper;
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public DefaultConverter(Optional<ConvertiblesReloadListener.EntityEntry.Attributes> helper) {
this.helper = helper.orElse(ConvertiblesReloadListener.EntityEntry.Attributes.DEFAULT);
}

public DefaultConverter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@
import net.minecraft.world.entity.PathfinderMob;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.Optional;
import java.util.function.Supplier;

public class SpecialConverter<T extends PathfinderMob, Z extends PathfinderMob & IConvertedCreature<T>> implements Converter {

public static final Codec<SpecialConverter<?, ?>> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ForgeRegistries.ENTITY_TYPES.getCodec().fieldOf("converted_type").forGetter(i -> i.convertedType),
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper", ConvertiblesReloadListener.EntityEntry.Attributes.DEFAULT).forGetter(i -> i.helper)
ConvertiblesReloadListener.EntityEntry.Attributes.CODEC.optionalFieldOf("attribute_helper").forGetter(i -> Optional.ofNullable(i.helper))
).apply(instance, SpecialConverter::new));

private final EntityType<Z> convertedType;
private final ConvertiblesReloadListener.EntityEntry.Attributes helper;

private SpecialConverter(EntityType<?> convertedType, ConvertiblesReloadListener.EntityEntry.Attributes helper) {
@SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "unchecked"})
private SpecialConverter(EntityType<?> convertedType, Optional<ConvertiblesReloadListener.EntityEntry.Attributes> helper) {
this.convertedType = (EntityType<Z>) convertedType;
this.helper = helper;
this.helper = helper.orElse(ConvertiblesReloadListener.EntityEntry.Attributes.DEFAULT);
}

public SpecialConverter(Supplier<EntityType<Z>> convertedType, ConvertiblesReloadListener.EntityEntry.Attributes helper) {
public SpecialConverter(Supplier<? extends EntityType<Z>> convertedType, ConvertiblesReloadListener.EntityEntry.Attributes helper) {
this.convertedType = convertedType.get();
this.helper = helper;
}
Expand Down

0 comments on commit f62396c

Please sign in to comment.