-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Duck variants data-driven (#194)
- Loading branch information
Showing
10 changed files
with
157 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package fr.hugman.promenade.entity; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import fr.hugman.promenade.Promenade; | ||
import net.minecraft.entity.passive.WolfVariant; | ||
import net.minecraft.loot.LootTable; | ||
import net.minecraft.registry.RegistryCodecs; | ||
import net.minecraft.registry.RegistryKey; | ||
import net.minecraft.registry.RegistryKeys; | ||
import net.minecraft.registry.entry.RegistryEntryList; | ||
import net.minecraft.registry.tag.TagKey; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.world.biome.Biome; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public final class DuckVariant { | ||
private static final Identifier DEFAULT_DUCKLING_TEXTURE = Promenade.id("entity/duck/duckling"); | ||
|
||
public static final Codec<DuckVariant> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Identifier.CODEC.fieldOf("texture").forGetter(duck -> duck.texture), | ||
Identifier.CODEC.optionalFieldOf("baby_texture", DEFAULT_DUCKLING_TEXTURE).forGetter(duck -> duck.babyTexture), | ||
RegistryCodecs.entryList(RegistryKeys.BIOME).fieldOf("biomes").forGetter(duck -> duck.biomes) | ||
).apply(instance, DuckVariant::new)); | ||
|
||
private final Identifier texture; | ||
private final Identifier babyTexture; | ||
private final RegistryEntryList<Biome> biomes; | ||
|
||
private final Identifier texturePath; | ||
private final Identifier babyTexturePath; | ||
|
||
public DuckVariant(Identifier texture, Identifier babyTexture, RegistryEntryList<Biome> biomes) { | ||
this.texture = texture; | ||
this.babyTexture = babyTexture; | ||
this.biomes = biomes; | ||
|
||
this.texturePath = getTexturePath(texture); | ||
this.babyTexturePath = getTexturePath(babyTexture); | ||
} | ||
|
||
public Identifier texture() { | ||
return texturePath; | ||
} | ||
|
||
public Identifier babyTexture() { | ||
return babyTexturePath; | ||
} | ||
|
||
public RegistryEntryList<Biome> getBiomes() { | ||
return this.biomes; | ||
} | ||
|
||
private static Identifier getTexturePath(Identifier id) { | ||
return id.withPath(oldPath -> "textures/" + oldPath + ".png"); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) return true; | ||
if (obj == null || obj.getClass() != this.getClass()) return false; | ||
var that = (DuckVariant) obj; | ||
return Objects.equals(this.texture, that.texture) && Objects.equals(this.babyTexture, that.babyTexture); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(texture, babyTexture); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/fr/hugman/promenade/entity/DuckVariants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package fr.hugman.promenade.entity; | ||
|
||
import fr.hugman.promenade.Promenade; | ||
import fr.hugman.promenade.registry.PromenadeRegistryKeys; | ||
import net.minecraft.registry.DynamicRegistryManager; | ||
import net.minecraft.registry.Registry; | ||
import net.minecraft.registry.RegistryKey; | ||
import net.minecraft.registry.entry.RegistryEntry; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.world.biome.Biome; | ||
|
||
public class DuckVariants { | ||
public static final RegistryKey<DuckVariant> PEKIN = of("pekin"); | ||
|
||
private static RegistryKey<DuckVariant> of(String path) { | ||
return of(Promenade.id(path)); | ||
} | ||
|
||
public static RegistryKey<DuckVariant> of(Identifier id) { | ||
return RegistryKey.of(PromenadeRegistryKeys.DUCK_VARIANT, id); | ||
} | ||
|
||
public static RegistryEntry<DuckVariant> fromBiome(DynamicRegistryManager dynamicRegistryManager, RegistryEntry<Biome> biome) { | ||
Registry<DuckVariant> registry = dynamicRegistryManager.get(PromenadeRegistryKeys.DUCK_VARIANT); | ||
return registry.streamEntries() | ||
.filter(entry -> entry.value().getBiomes().contains(biome)) | ||
.findFirst() | ||
.orElse(registry.entryOf(PEKIN)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/fr/hugman/promenade/registry/PromenadeRegistries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/resources/data/promenade/promenade/duck_variant/mallard.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"texture": "promenade:entity/duck/mallard", | ||
"biomes": "#promenade:spawns/duck/mallard" | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/resources/data/promenade/promenade/duck_variant/pekin.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"texture": "promenade:entity/duck/pekin", | ||
"biomes": "#promenade:spawns/duck/pekin" | ||
} |
5 changes: 2 additions & 3 deletions
5
src/main/resources/data/promenade/tags/worldgen/biome/spawns/duck/mallard.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"minecraft:plains", | ||
"minecraft:river", | ||
"minecraft:swamp" | ||
"#minecraft:is_ocean", | ||
"#minecraft:is_river" | ||
] | ||
} |