Files
src
build.gradle
gradle.properties
LICENSE
settings.gradle
Code
7 289
src/main/java/com/timebound/Timebound.java
package com.timebound;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.WorldSavePath;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import static com.mojang.brigadier.arguments.LongArgumentType.getLong;
import static com.mojang.brigadier.arguments.LongArgumentType.longArg;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static net.minecraft.command.argument.EntityArgumentType.player;
public class Timebound implements ModInitializer {
public static final String MOD_ID = "timebound";
private static final long HOUR = 3600L;
private static final long START_TIME = 48L * HOUR;
private static final long MAX_TIME = 1000L * HOUR;
private static final Map<UUID, Long> TIMES = new ConcurrentHashMap<>();
private static final Map<UUID, String> NAMES = new ConcurrentHashMap<>();
private static Path dataFile;
private static long ticks;
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(Timebound::load);
ServerLifecycleEvents.SERVER_STOPPING.register(server -> save());
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
ServerPlayerEntity player = handler.player;
TIMES.putIfAbsent(player.getUuid(), START_TIME);
NAMES.put(player.getUuid(), player.getName().getString());
player.sendMessage(Text.literal("Timebound: " + format(get(player.getUuid()))), false);
save();
});
ServerTickEvents.END_SERVER_TICK.register(server -> {
ticks++;
if (ticks % 20 == 0) {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
TIMES.putIfAbsent(player.getUuid(), START_TIME);
NAMES.put(player.getUuid(), player.getName().getString());
if (get(player.getUuid()) > 0) {
change(player.getUuid(), -1);
}
}
}
if (ticks % 200 == 0) {
save();
}
});
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(literal("time").executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
long seconds = get(player.getUuid());
player.sendMessage(Text.literal("TIME " + format(seconds)), false);
player.sendMessage(Text.literal("TIER " + tier(seconds)), false);
return 1;
}));
dispatcher.register(literal("givetime")
.requires(source -> CommandManager.ADMINS_CHECK.allows(source.getPermissions()))
.then(argument("player", player())
.then(argument("hours", longArg(1, 1000))
.executes(context -> {
ServerPlayerEntity target = EntityArgumentType.getPlayer(context, "player");
long hours = getLong(context, "hours");
change(target.getUuid(), hours * HOUR);
save();
target.sendMessage(Text.literal("You received " + hours + " hours."), false);
context.getSource().sendFeedback(() -> Text.literal("Lifetime granted."), true);
return 1;
}))));
dispatcher.register(literal("timeleaderboard").executes(context -> {
var entries = TIMES.entrySet().stream()
.sorted(Map.Entry.<UUID, Long>comparingByValue().reversed())
.limit(10)
.toList();
context.getSource().sendFeedback(() -> Text.literal("=== TIMEBOUND LEADERBOARD ==="), false);
int rank = 1;
for (var entry : entries) {
String line = "#" + rank++ + " "
+ NAMES.getOrDefault(entry.getKey(), "Unknown")
+ " | " + format(entry.getValue())
+ " | " + tier(entry.getValue());
context.getSource().sendFeedback(() -> Text.literal(line), false);
}
return entries.size();
}));
});
}
private static long get(UUID id) {
return TIMES.getOrDefault(id, START_TIME);
}
private static synchronized void change(UUID id, long delta) {
long current = get(id);
long result;
try {
result = Math.addExact(current, delta);
} catch (ArithmeticException exception) {
result = delta > 0 ? MAX_TIME : 0;
}
TIMES.put(id, Math.max(0, Math.min(MAX_TIME, result)));
}
private static String tier(long seconds) {
if (seconds < HOUR) return "FRACTURED";
if (seconds < 12 * HOUR) return "UNSTABLE";
if (seconds < 72 * HOUR) return "STABLE";
if (seconds < 168 * HOUR) return "ENHANCED";
if (seconds < 500 * HOUR) return "ASCENDANT";
return "CHRONOLORD";
}
private static String format(long seconds) {
return String.format("%02dh %02dm %02ds",
seconds / 3600, (seconds % 3600) / 60, seconds % 60);
}
private static synchronized void load(MinecraftServer server) {
dataFile = server.getSavePath(WorldSavePath.ROOT).resolve("timebound.properties");
TIMES.clear();
NAMES.clear();
Properties properties = new Properties();
if (Files.exists(dataFile)) {
try (InputStream input = Files.newInputStream(dataFile)) {
properties.load(input);
} catch (IOException exception) {
throw new IllegalStateException("Cannot load Timebound data", exception);
}
}
for (String key : properties.stringPropertyNames()) {
try {
if (key.startsWith("time.")) {
TIMES.put(UUID.fromString(key.substring(5)),
Math.max(0, Math.min(MAX_TIME, Long.parseLong(properties.getProperty(key)))));
} else if (key.startsWith("name.")) {
NAMES.put(UUID.fromString(key.substring(5)), properties.getProperty(key));
}
} catch (IllegalArgumentException ignored) {
}
}
ticks = 0;
}
private static synchronized void save() {
if (dataFile == null) return;
Properties properties = new Properties();
TIMES.forEach((id, time) -> properties.setProperty("time." + id, time.toString()));
NAMES.forEach((id, name) -> properties.setProperty("name." + id, name));
try {
Files.createDirectories(dataFile.getParent());
try (OutputStream output = Files.newOutputStream(dataFile)) {
properties.store(output, "Timebound player lifetime");
}
} catch (IOException exception) {
throw new IllegalStateException("Cannot save Timebound data", exception);
}
}
}