Files
src
build.gradle
CHANGELOG.md
FEATURE_CHECKLIST.md
gradle.properties
KNOWN_BUGS.md
PROJECT_STATE.md
settings.gradle
TEST_RESULTS.md
Code
10 408
src/main/java/com/kodari/moderncitylife/ModernCityLife.java
package com.kodari.moderncitylife;
import com.mojang.serialization.Codec;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.api.ModInitializer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
public class ModernCityLife implements ModInitializer {
public static final String MOD_ID = "moderncitylife";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final long WORK_COOLDOWN_TICKS = 6_000L;
private static final long NEEDS_STEP_TICKS = 1_200L;
private static final long REST_COOLDOWN_TICKS = 12_000L;
private static final Map<String, Integer> JOB_PAY = Map.of(
"farmer", 8,
"miner", 12,
"builder", 10,
"chef", 8
);
private static final AttachmentType<String> PLAYER_PROFILE = AttachmentRegistry.create(
Identifier.fromNamespaceAndPath(MOD_ID, "player_profile"),
builder -> builder.persistent(Codec.STRING).copyOnDeath()
);
@Override
public void onInitialize() {
ServerTickEvents.END_SERVER_TICK.register(ModernCityLife::tickNeeds);
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> dispatcher.register(
Commands.literal("citylife")
.requires(CommandSourceStack::isPlayer)
.executes(context -> reply(context.getSource(), "Modern City Life — use /citylife jobs, /citylife work, /citylife status, /citylife rest, /citylife relax, /citylife wash, or /citylife shop."))
.then(Commands.literal("jobs").executes(context -> reply(context.getSource(),
"Available careers: farmer ($8/work), miner ($12/work), builder ($10/work), chef ($8/work). Choose one with /citylife job <career>.")))
.then(Commands.literal("job")
.then(Commands.literal("farmer").executes(context -> chooseJob(context.getSource(), "farmer")))
.then(Commands.literal("miner").executes(context -> chooseJob(context.getSource(), "miner")))
.then(Commands.literal("builder").executes(context -> chooseJob(context.getSource(), "builder")))
.then(Commands.literal("chef").executes(context -> chooseJob(context.getSource(), "chef"))))
.then(Commands.literal("work").executes(context -> work(context.getSource())))
.then(Commands.literal("profile").executes(context -> showProfile(context.getSource())))
.then(Commands.literal("status").executes(context -> showProfile(context.getSource())))
.then(Commands.literal("rest").executes(context -> rest(context.getSource())))
.then(Commands.literal("relax").executes(context -> restoreNeed(context.getSource(), 12, "relax", 0, 25, 0)))
.then(Commands.literal("wash").executes(context -> restoreNeed(context.getSource(), 8, "wash", 0, 0, 40)))
.then(Commands.literal("shop")
.executes(context -> reply(context.getSource(),
"City shop: /citylife buy bread (5), torch (3), iron (15), or beef (8). Each purchase is one item.")))
.then(Commands.literal("buy")
.then(Commands.literal("bread").executes(context -> purchase(context.getSource(), new ItemStack(Items.BREAD), 5, "bread")))
.then(Commands.literal("torch").executes(context -> purchase(context.getSource(), new ItemStack(Items.TORCH), 3, "torch")))
.then(Commands.literal("iron").executes(context -> purchase(context.getSource(), new ItemStack(Items.IRON_INGOT), 15, "iron ingot")))
.then(Commands.literal("beef").executes(context -> purchase(context.getSource(), new ItemStack(Items.COOKED_BEEF), 8, "cooked beef"))))
));
LOGGER.info("Modern City Life initialized with careers, needs, services, and shop commands.");
}
private static void tickNeeds(MinecraftServer server) {
long now = server.overworld().getGameTime();
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
if (player.getAttached(PLAYER_PROFILE) == null) {
setProfile(player, CityProfile.fresh(now));
continue;
}
CityProfile profile = getProfile(player);
long elapsed = now - profile.lastNeedsTick();
if (elapsed < NEEDS_STEP_TICKS) {
continue;
}
int moodLoss = now / (NEEDS_STEP_TICKS * 3) > profile.lastNeedsTick() / (NEEDS_STEP_TICKS * 3) ? 1 : 0;
int hygieneLoss = now / (NEEDS_STEP_TICKS * 5) > profile.lastNeedsTick() / (NEEDS_STEP_TICKS * 5) ? 1 : 0;
setProfile(player, new CityProfile(
profile.job(), profile.coins(), profile.lastWorkTick(),
Math.max(0, profile.energy() - 1),
Math.max(0, profile.mood() - moodLoss),
Math.max(0, profile.hygiene() - hygieneLoss),
now, profile.lastRestTick()
));
}
}
private static int chooseJob(CommandSourceStack source, String job) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
CityProfile profile = getProfile(player);
setProfile(player, new CityProfile(job, profile.coins(), profile.lastWorkTick(), profile.energy(), profile.mood(), profile.hygiene(), profile.lastNeedsTick(), profile.lastRestTick()));
return reply(source, "Career selected: " + job + ". Use /citylife work to earn wages; work can be claimed every 5 minutes of game time.");
}
private static int work(CommandSourceStack source) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
CityProfile profile = getProfile(player);
Integer pay = JOB_PAY.get(profile.job());
if (pay == null) {
return reply(source, "Choose a career first with /citylife jobs, then /citylife job <career>.");
}
if (profile.energy() < 12) {
return reply(source, "You are too tired to work. Use /citylife rest to recover energy.");
}
long now = source.getServer().overworld().getGameTime();
long elapsed = now - profile.lastWorkTick();
if (elapsed < WORK_COOLDOWN_TICKS) {
long minutesLeft = (WORK_COOLDOWN_TICKS - elapsed + 1_199L) / 1_200L;
return reply(source, "You are still on shift cooldown. Try again in about " + minutesLeft + " minute(s).");
}
int newBalance = (int) Math.min(Integer.MAX_VALUE, (long) profile.coins() + pay);
int earned = newBalance - profile.coins();
setProfile(player, new CityProfile(profile.job(), newBalance, now, profile.energy() - 12, profile.mood(), profile.hygiene(), profile.lastNeedsTick(), profile.lastRestTick()));
return reply(source, "Shift complete as " + profile.job() + ". Earned " + earned + " city coins and used 12 energy. Balance: " + newBalance + ".");
}
private static int showProfile(CommandSourceStack source) throws CommandSyntaxException {
CityProfile profile = getProfile(source.getPlayerOrException());
String job = JOB_PAY.containsKey(profile.job()) ? profile.job() : "unemployed";
return reply(source, "City status — career: " + job + "; balance: " + profile.coins() + " city coins; energy: " + profile.energy() + "/100; mood: " + profile.mood() + "/100; hygiene: " + profile.hygiene() + "/100. Use /citylife rest, /citylife relax, or /citylife wash to recover.");
}
private static int rest(CommandSourceStack source) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
CityProfile profile = getProfile(player);
long now = source.getServer().overworld().getGameTime();
long elapsed = now - profile.lastRestTick();
if (elapsed < REST_COOLDOWN_TICKS) {
long minutesLeft = (REST_COOLDOWN_TICKS - elapsed + 1_199L) / 1_200L;
return reply(source, "You need more time before resting again. Try in about " + minutesLeft + " minute(s).");
}
if (profile.energy() >= 100) {
return reply(source, "You are already fully rested.");
}
int energy = Math.min(100, profile.energy() + 35);
setProfile(player, new CityProfile(profile.job(), profile.coins(), profile.lastWorkTick(), energy, Math.min(100, profile.mood() + 3), profile.hygiene(), profile.lastNeedsTick(), now));
return reply(source, "You took a break and restored energy to " + energy + "/100. Rest is available again in 10 minutes of game time.");
}
private static int restoreNeed(CommandSourceStack source, int price, String service, int energyGain, int moodGain, int hygieneGain) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
CityProfile profile = getProfile(player);
if (profile.coins() < price) {
return reply(source, "Not enough city coins for " + service + ". Price: " + price + "; balance: " + profile.coins() + ". Earn wages with /citylife work.");
}
int energy = Math.min(100, profile.energy() + energyGain);
int mood = Math.min(100, profile.mood() + moodGain);
int hygiene = Math.min(100, profile.hygiene() + hygieneGain);
if (energy == profile.energy() && mood == profile.mood() && hygiene == profile.hygiene()) {
return reply(source, "You do not need that service right now.");
}
int balance = profile.coins() - price;
setProfile(player, new CityProfile(profile.job(), balance, profile.lastWorkTick(), energy, mood, hygiene, profile.lastNeedsTick(), profile.lastRestTick()));
return reply(source, "City service: " + service + " complete for " + price + " coins. Balance: " + balance + ".");
}
private static int purchase(CommandSourceStack source, ItemStack item, int price, String itemName) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
CityProfile profile = getProfile(player);
if (profile.coins() < price) {
return reply(source, "Not enough city coins. Price: " + price + "; balance: " + profile.coins() + ".");
}
if (!player.getInventory().add(item)) {
return reply(source, "Your inventory is full; no coins were charged.");
}
int balance = profile.coins() - price;
setProfile(player, new CityProfile(profile.job(), balance, profile.lastWorkTick(), profile.energy(), profile.mood(), profile.hygiene(), profile.lastNeedsTick(), profile.lastRestTick()));
return reply(source, "Bought 1 " + itemName + " for " + price + " city coins. Balance: " + balance + ".");
}
private static int reply(CommandSourceStack source, String message) {
source.sendSuccess(() -> Component.literal(message), false);
return 1;
}
private static CityProfile getProfile(ServerPlayer player) {
String encoded = player.getAttached(PLAYER_PROFILE);
if (encoded == null) {
return CityProfile.fresh(0L);
}
String[] fields = encoded.split("\\|", -1);
if ((fields.length != 3 && fields.length != 8) || (!fields[0].equals("unemployed") && !JOB_PAY.containsKey(fields[0]))) {
return CityProfile.fresh(0L);