Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
9 668
src/main/java/com/kodari/smpbounty/SmpBountyMod.java
package com.kodari.smpbounty;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.core.registries.BuiltInRegistries;
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.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
public final class SmpBountyMod implements ModInitializer {
public static final String MOD_ID = "smpbounty";
private static final long TICKS_PER_MINUTE = 1_200L;
private static final long TICKS_PER_HOUR = 72_000L;
@Override
public void onInitialize() {
BountyConfig.load();
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> BountyCommands.register(dispatcher));
ServerTickEvents.END_SERVER_TICK.register(SmpBountyMod::onServerTick);
ServerPlayerEvents.JOIN.register(SmpBountyMod::restoreRewards);
ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> restoreRewards(newPlayer));
ServerLivingEntityEvents.AFTER_DEATH.register(SmpBountyMod::onDeath);
}
private static void onServerTick(MinecraftServer server) {
BountyData data = BountyData.get(server);
long now = server.overworld().getGameTime();
if (data.lastLeakGameTime() < 0) {
data.setLastLeakGameTime(now);
return;
}
long interval = BountyConfig.leakIntervalMinutes() * TICKS_PER_MINUTE;
if (now - data.lastLeakGameTime() < interval) {
return;
}
data.setLastLeakGameTime(now);
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
BountyData.PlayerStats stats = data.getOrCreate(player.getUUID(), player.getName().getString());
if (!stats.bountyActive()) {
continue;
}
String dimension = dimensionName(player.level());
String message = "[BOUNTY LEAK] " + player.getName().getString() + " was spotted at X: "
+ (int) Math.floor(player.getX()) + ", Y: " + (int) Math.floor(player.getY()) + ", Z: "
+ (int) Math.floor(player.getZ()) + " in the " + dimension + "! Hunt them down!";
broadcast(server, message);
}
}
private static void onDeath(LivingEntity entity, DamageSource damageSource) {
if (!(entity instanceof ServerPlayer victim)) {
return;
}
MinecraftServer server = victim.level().getServer();
if (server == null) {
return;
}
BountyData data = BountyData.get(server);
BountyData.PlayerStats victimStats = data.getOrCreate(victim.getUUID(), victim.getName().getString());
boolean wasBountyTarget = victimStats.bountyActive();
if (wasBountyTarget) {
victimStats = copyStats(victimStats, victimStats.kills(), victimStats.lastBountyAtKills(), false,
victimStats.rewards(), victimStats.managedEffects(), victimStats.lastVictimKills());
data.update(victim.getUUID(), victimStats);
}
ServerPlayer killer = damageSource.getEntity() instanceof ServerPlayer player ? player : null;
if (killer != null && killer.getUUID().equals(victim.getUUID())) {
killer = null;
}
boolean eligibleKill = false;
BountyData.PlayerStats killerStats = null;
if (killer != null) {
killerStats = data.getOrCreate(killer.getUUID(), killer.getName().getString());
Map<String, Long> recentVictims = new HashMap<>(killerStats.lastVictimKills());
String victimId = victim.getUUID().toString();
Long previousKill = recentVictims.get(victimId);
long cooldown = BountyConfig.sameVictimCooldownHours() * TICKS_PER_HOUR;
long now = server.overworld().getGameTime();
eligibleKill = cooldown == 0 || previousKill == null || now - previousKill >= cooldown;
recentVictims.put(victimId, now);
int kills = killerStats.kills();
int lastBountyAtKills = killerStats.lastBountyAtKills();
boolean bountyActive = killerStats.bountyActive();
if (eligibleKill) {
kills++;
int progressSinceMilestone = kills - lastBountyAtKills;
int requiredKills = BountyConfig.killsPerBounty();
if (progressSinceMilestone >= requiredKills) {
lastBountyAtKills += (progressSinceMilestone / requiredKills) * requiredKills;
if (!bountyActive) {
bountyActive = true;
broadcast(server, "[BOUNTY] " + killer.getName().getString()
+ " now has a bounty! Hunt them down! (" + kills + " kills)");
}
}
}
killerStats = copyStats(killerStats, kills, lastBountyAtKills, bountyActive,
killerStats.rewards(), killerStats.managedEffects(), recentVictims);
data.update(killer.getUUID(), killerStats);
}
if (wasBountyTarget && killer != null && eligibleKill && killerStats != null) {
killerStats = grantReward(killerStats);
data.update(killer.getUUID(), killerStats);
reconcileRewards(killer, killerStats);
broadcast(server, "[BOUNTY] " + killer.getName().getString() + " claimed "
+ victim.getName().getString() + "'s bounty and earned a permanent reward!");
} else if (wasBountyTarget && killer != null && !eligibleKill) {
killer.sendSystemMessage(Component.literal("That bounty kill did not count because you recently killed this player."));
}
if (BountyConfig.deathPenalty() && !victimStats.rewards().isEmpty()) {
List<BountyData.Reward> rewards = new ArrayList<>(victimStats.rewards());
BountyData.Reward lostReward = rewards.remove(rewards.size() - 1);
victimStats = copyStats(victimStats, victimStats.kills(), victimStats.lastBountyAtKills(),
victimStats.bountyActive(), rewards, victimStats.managedEffects(), victimStats.lastVictimKills());
data.update(victim.getUUID(), victimStats);
reconcileRewards(victim, victimStats);
victim.sendSystemMessage(Component.literal("You lost one permanent bounty reward: "
+ lostReward.effectId() + " level " + lostReward.level() + "."));
}
}
private static BountyData.PlayerStats grantReward(BountyData.PlayerStats stats) {
List<BountyConfig.EffectOption> options = validRewardOptions();
BountyConfig.EffectOption selected = options.get(stats.rewards().size() % options.size());
int existingRewardsForEffect = (int) stats.rewards().stream()
.filter(reward -> reward.effectId().equals(selected.effectId())).count();
BountyData.Reward reward = new BountyData.Reward(selected.effectId(), selected.level() + existingRewardsForEffect);
List<BountyData.Reward> rewards = new ArrayList<>(stats.rewards());
rewards.add(reward);
List<String> managedEffects = new ArrayList<>(stats.managedEffects());
if (!managedEffects.contains(reward.effectId())) {
managedEffects.add(reward.effectId());
}
return copyStats(stats, stats.kills(), stats.lastBountyAtKills(), stats.bountyActive(), rewards,
managedEffects, stats.lastVictimKills());
}
private static List<BountyConfig.EffectOption> validRewardOptions() {
List<BountyConfig.EffectOption> valid = BountyConfig.effects().stream()
.filter(option -> effectHolderExists(option.effectId())).toList();
return valid.isEmpty() ? List.of(new BountyConfig.EffectOption("minecraft:speed", 1)) : valid;
}
private static boolean effectHolderExists(String effectId) {
Identifier identifier = Identifier.tryParse(effectId);
return identifier != null && BuiltInRegistries.MOB_EFFECT.get(identifier).isPresent();
}
private static void restoreRewards(ServerPlayer player) {
BountyData data = BountyData.get(player.level().getServer());
BountyData.PlayerStats stats = data.getOrCreate(player.getUUID(), player.getName().getString());
reconcileRewards(player, stats);
}
private static void reconcileRewards(ServerPlayer player, BountyData.PlayerStats stats) {
Set<String> managedEffects = new HashSet<>(stats.managedEffects());
for (BountyData.Reward reward : stats.rewards()) {
managedEffects.add(reward.effectId());
}
for (String effectId : managedEffects) {
Identifier identifier = Identifier.tryParse(effectId);
if (identifier != null) {
BuiltInRegistries.MOB_EFFECT.get(identifier).ifPresent(player::removeEffect);
}
}
Map<String, Integer> highestLevels = new HashMap<>();
for (BountyData.Reward reward : stats.rewards()) {
highestLevels.merge(reward.effectId(), reward.level(), Math::max);
}
for (Map.Entry<String, Integer> entry : highestLevels.entrySet()) {
Identifier identifier = Identifier.tryParse(entry.getKey());
if (identifier == null) {
continue;