Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 303
src/main/java/com/kodari/hybridanimals/HybridAnimals.java
package com.kodari.hybridanimals;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class HybridAnimals implements ModInitializer {
private static final String HYBRID_TAG = "hybridanimals:hybrid";
private static final String GENE_PREFIX = "hybridanimals:gene:";
private static final String SHEARED_UNTIL_PREFIX = "hybridanimals:sheared_until:";
private static final long WOOL_REGROW_TICKS = 12000L;
private static final double APPROACH_RANGE = 12.0;
private static final Map<UUID, UUID> PENDING_PARENT = new HashMap<>();
@Override
public void onInitialize() {
UseEntityCallback.EVENT.register(this::onAnimalInteraction);
ServerTickEvents.END_WORLD_TICK.register(this::attractAnimals);
}
private ActionResult onAnimalInteraction(PlayerEntity player, World world, Hand hand, Entity target,
net.minecraft.util.hit.EntityHitResult hitResult) {
if (!(target instanceof AnimalEntity animal)) {
return ActionResult.PASS;
}
ItemStack held = player.getStackInHand(hand);
if (isHybrid(animal) && held.isOf(Items.BUCKET) && hasGene(animal, "minecraft:cow")) {
if (!world.isClient && player instanceof ServerPlayerEntity serverPlayer) {
milkHybrid(serverPlayer, hand);
}
return ActionResult.SUCCESS;
}
if (isHybrid(animal) && held.isOf(Items.SHEARS) && hasGene(animal, "minecraft:sheep")) {
if (!world.isClient && world instanceof ServerWorld serverWorld) {
shearHybrid(animal, player, serverWorld);
}
return ActionResult.SUCCESS;
}
if (!held.isOf(Items.GOLDEN_APPLE)) {
return ActionResult.PASS;
}
if (world.isClient) {
return ActionResult.SUCCESS;
}
if (!(world instanceof ServerWorld serverWorld) || !(player instanceof ServerPlayerEntity serverPlayer)) {
return ActionResult.PASS;
}
return feedForBreeding(serverPlayer, hand, animal, serverWorld);
}
private ActionResult feedForBreeding(ServerPlayerEntity player, Hand hand, AnimalEntity animal,
ServerWorld world) {
UUID pendingUuid = PENDING_PARENT.get(player.getUuid());
Entity pendingEntity = pendingUuid == null ? null : world.getEntity(pendingUuid);
if (!(pendingEntity instanceof AnimalEntity first) || !first.isAlive()) {
PENDING_PARENT.remove(player.getUuid());
if (consumeOne(player, hand)) {
PENDING_PARENT.put(player.getUuid(), animal.getUuid());
player.sendMessage(Text.literal("Fed! Use another Golden Apple on a different animal to hatch a hybrid."), true);
}
return ActionResult.SUCCESS;
}
if (first.getUuid().equals(animal.getUuid())) {
player.sendMessage(Text.literal("Choose a different animal for the second parent."), true);
return ActionResult.SUCCESS;
}
EntityType<?> bodyType = world.random.nextBoolean() ? first.getType() : animal.getType();
Entity offspring = bodyType.create(world);
if (!(offspring instanceof AnimalEntity hybrid)) {
player.sendMessage(Text.literal("These animals could not produce an offspring."), true);
return ActionResult.SUCCESS;
}
hybrid.refreshPositionAndAngles(animal.getX(), animal.getY(), animal.getZ(), animal.getYaw(), 0.0F);
hybrid.setBreedingAge(-24000);
hybrid.addCommandTag(HYBRID_TAG);
Set<String> genes = collectGenes(first);
genes.addAll(collectGenes(animal));
for (String gene : genes) {
hybrid.addCommandTag(GENE_PREFIX + gene);
}
hybrid.setCustomName(Text.literal("Hybrid: " + first.getName().getString() + " × " + animal.getName().getString()));
hybrid.setCustomNameVisible(true);
if (!world.spawnEntity(hybrid)) {
return ActionResult.FAIL;
}
consumeOne(player, hand);
PENDING_PARENT.remove(player.getUuid());
player.sendMessage(Text.literal("A new hybrid has been born! Its inherited traits persist through future generations."), true);
return ActionResult.SUCCESS;
}
private void attractAnimals(ServerWorld world) {
if (world.getTime() % 10 != 0) {
return;
}
for (ServerPlayerEntity player : world.getPlayers()) {
if (!isHoldingGoldenApple(player)) {
continue;
}
for (AnimalEntity animal : world.getEntitiesByClass(AnimalEntity.class,
player.getBoundingBox().expand(APPROACH_RANGE), AnimalEntity::isAlive)) {
animal.getNavigation().startMovingTo(player, 1.05);
}
}
}
private boolean isHoldingGoldenApple(PlayerEntity player) {
return player.getStackInHand(Hand.MAIN_HAND).isOf(Items.GOLDEN_APPLE)
|| player.getStackInHand(Hand.OFF_HAND).isOf(Items.GOLDEN_APPLE);
}
private void milkHybrid(ServerPlayerEntity player, Hand hand) {
ItemStack bucket = player.getStackInHand(hand);
ItemStack milk = new ItemStack(Items.MILK_BUCKET);
if (player.isCreative()) {
if (!player.getInventory().insertStack(milk)) {
player.dropItem(milk, false);
}
} else if (bucket.getCount() == 1) {
player.setStackInHand(hand, milk);
} else {
bucket.decrement(1);
if (!player.getInventory().insertStack(milk)) {
player.dropItem(milk, false);
}
}
player.sendMessage(Text.literal("You milked the hybrid."), true);
}
private void shearHybrid(AnimalEntity animal, PlayerEntity player, ServerWorld world) {
long regrowAt = getShearedUntil(animal);
if (world.getTime() < regrowAt) {
player.sendMessage(Text.literal("This hybrid's wool is still growing back."), true);
return;
}
removeShearedTags(animal);
animal.addCommandTag(SHEARED_UNTIL_PREFIX + (world.getTime() + WOOL_REGROW_TICKS));
animal.dropStack(new ItemStack(Items.WHITE_WOOL, world.random.nextInt(3) + 1));
player.sendMessage(Text.literal("You sheared the hybrid."), true);
}
private long getShearedUntil(AnimalEntity animal) {
for (String tag : animal.getCommandTags()) {
if (tag.startsWith(SHEARED_UNTIL_PREFIX)) {
try {
return Long.parseLong(tag.substring(SHEARED_UNTIL_PREFIX.length()));
} catch (NumberFormatException ignored) {
return Long.MAX_VALUE;
}
}
}
return Long.MIN_VALUE;
}
private void removeShearedTags(AnimalEntity animal) {
for (String tag : Set.copyOf(animal.getCommandTags())) {
if (tag.startsWith(SHEARED_UNTIL_PREFIX)) {
animal.removeCommandTag(tag);
}
}
}
private boolean isHybrid(AnimalEntity animal) {
return animal.getCommandTags().contains(HYBRID_TAG);
}
private boolean hasGene(AnimalEntity animal, String gene) {
return animal.getCommandTags().contains(GENE_PREFIX + gene);