Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 323
src/main/java/com/kodari/mooncycle/MooncycleMod.java
package com.kodari.mooncycle;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.MinecraftServer;
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.util.TypedActionResult;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class MooncycleMod implements ModInitializer {
public static final String MOD_ID = "mooncycle";
private static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final Type PLAYER_DATA_TYPE = new TypeToken<Map<UUID, CycleData>>() { }.getType();
private static final Map<UUID, CycleData> PLAYER_DATA = new HashMap<>();
private static final long TICKS_PER_DAY = 24_000L;
private static final long CYCLE_LENGTH = 24 * TICKS_PER_DAY;
private static final long PERIOD_LENGTH = 4 * TICKS_PER_DAY;
private static final long GESTATION_LENGTH = 8 * TICKS_PER_DAY;
public static final Item PERIOD_PAD = registerItem("period_pad", new Item(new Item.Settings().maxCount(16)));
public static final Item TAMPON = registerItem("tampon", new Item(new Item.Settings().maxCount(16)));
public static final Item MOONROOT_CHARM = registerItem("moonroot_charm", new Item(new Item.Settings().maxCount(1)));
@Override
public void onInitialize() {
ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS).register(entries -> {
entries.add(PERIOD_PAD);
entries.add(TAMPON);
entries.add(MOONROOT_CHARM);
});
UseItemCallback.EVENT.register((player, world, hand) -> usePeriodProduct(player, world, hand));
UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> useMoonrootCharm(player, world, hand, entity));
ServerTickEvents.END_SERVER_TICK.register(MooncycleMod::tickPlayers);
ServerLifecycleEvents.SERVER_STARTED.register(MooncycleMod::loadData);
ServerLifecycleEvents.SERVER_STOPPING.register(MooncycleMod::saveData);
}
private static Item registerItem(String name, Item item) {
return Registry.register(Registries.ITEM, Identifier.of(MOD_ID, name), item);
}
private static TypedActionResult<ItemStack> usePeriodProduct(net.minecraft.entity.player.PlayerEntity player, World world, Hand hand) {
ItemStack stack = player.getStackInHand(hand);
if (stack.isEmpty() || (stack.getItem() != PERIOD_PAD && stack.getItem() != TAMPON)) {
return TypedActionResult.pass(stack);
}
if (world.isClient) {
return TypedActionResult.pass(stack);
}
CycleData data = getData(player.getUuid());
if (!isMenstruating(data)) {
player.sendMessage(Text.literal("You are not on your period right now."), true);
return TypedActionResult.fail(stack);
}
data.hygieneTicks = TICKS_PER_DAY;
if (!player.getAbilities().creativeMode) {
stack.decrement(1);
}
player.sendMessage(Text.literal("You feel more comfortable. Your period continues as normal."), true);
return TypedActionResult.success(stack);
}
private static ActionResult useMoonrootCharm(net.minecraft.entity.player.PlayerEntity player, World world, Hand hand, Entity target) {
ItemStack stack = player.getStackInHand(hand);
if (stack.getItem() != MOONROOT_CHARM) {
return ActionResult.PASS;
}
if (world.isClient) {
return ActionResult.SUCCESS;
}
if (!(player instanceof ServerPlayerEntity serverPlayer) || !(world instanceof ServerWorld)) {
return ActionResult.PASS;
}
if (!(target instanceof PassiveEntity passive) || passive.isBaby()) {
serverPlayer.sendMessage(Text.literal("The charm can only bond with an adult passive mob."), true);
return ActionResult.FAIL;
}
CycleData data = getData(serverPlayer.getUuid());
if (data.pregnancyTicks > 0) {
serverPlayer.sendMessage(Text.literal("You are already carrying a magical bond."), true);
return ActionResult.FAIL;
}
data.pregnancyMobId = Registries.ENTITY_TYPE.getId(target.getType()).toString();
data.pregnancyTicks = GESTATION_LENGTH;
if (!serverPlayer.getAbilities().creativeMode) {
stack.decrement(1);
}
serverPlayer.sendMessage(Text.literal("The Moonroot Charm begins a magical bond. The baby will arrive in eight Minecraft days."), false);
return ActionResult.SUCCESS;
}
private static void tickPlayers(MinecraftServer server) {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
CycleData data = getData(player.getUuid());
data.cycleTicks = (data.cycleTicks + 1) % CYCLE_LENGTH;
if (data.hygieneTicks > 0) {
data.hygieneTicks--;
}
applyCycleEffects(player, data);
if (data.pregnancyTicks > 0) {
data.pregnancyTicks--;
if (data.pregnancyTicks == 0) {
giveBirth(player, data);
}
}
}
}
private static void applyCycleEffects(ServerPlayerEntity player, CycleData data) {
long tick = data.cycleTicks;
if (tick % 200 != 0) {
return;
}
if (tick < PERIOD_LENGTH) {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 240, 0, true, false, true));
player.addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 240, 0, true, false, true));
if (data.hygieneTicks <= 0) {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.NAUSEA, 240, 0, true, false, true));
}
} else if (tick < 10 * TICKS_PER_DAY) {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.SPEED, 240, 0, true, false, true));
} else if (tick < 12 * TICKS_PER_DAY) {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 240, 0, true, false, true));
player.addStatusEffect(new StatusEffectInstance(StatusEffects.LUCK, 240, 0, true, false, true));
} else {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.HUNGER, 240, 0, true, false, true));
}
}
private static boolean isMenstruating(CycleData data) {
return data.cycleTicks < PERIOD_LENGTH;
}
private static void giveBirth(ServerPlayerEntity player, CycleData data) {
Identifier id = Identifier.tryParse(data.pregnancyMobId == null ? "" : data.pregnancyMobId);
data.pregnancyMobId = null;
if (id == null) {
player.sendMessage(Text.literal("The magical bond faded before it could become a baby."), false);
return;
}
EntityType<?> type = Registries.ENTITY_TYPE.get(id);
Entity baby = type.create(player.getServerWorld(), SpawnReason.BREEDING).orElse(null);
if (!(baby instanceof PassiveEntity passive)) {
player.sendMessage(Text.literal("The magical bond could not create a baby of that species."), false);
return;
}
passive.setBaby(true);
passive.refreshPositionAndAngles(player.getX() + 1.0, player.getY(), player.getZ(), player.getYaw(), 0.0F);
player.getServerWorld().spawnEntity(passive);
player.sendMessage(Text.literal("A baby " + type.getName().getString() + " has arrived from your magical bond."), false);
}
private static CycleData getData(UUID playerId) {
return PLAYER_DATA.computeIfAbsent(playerId, ignored -> new CycleData());
}