Files
src
build.gradle
settings.gradle
Code
8 1,128
src/main/java/com/kodari/wispsmp/WispSMPMod.java
package com.kodari.wispsmp;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.kodari.wispsmp.data.WispData;
import com.kodari.wispsmp.fate.FateBlock;
import com.kodari.wispsmp.item.WispItems;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
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.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.ProjectileImpactCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.projectile.SnowballEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
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.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import net.minecraft.world.World;
import net.minecraft.world.Heightmap;
import net.minecraft.entity.passive.TameableEntity;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.WorldSavePath;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
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 java.util.UUID;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
public final class WispSMPMod implements ModInitializer {
public static WispSMPMod INSTANCE;
public static final Block FATE_BLOCK = Registry.register(Registries.BLOCK, Identifier.of("wispsmp", "fate_block"),
new FateBlock(AbstractBlock.Settings.create().strength(3.0F).requiresTool()));
public static final Item FATE_BLOCK_ITEM = Registry.register(Registries.ITEM, Identifier.of("wispsmp", "fate_block"),
new BlockItem(FATE_BLOCK, new Item.Settings()));
private static final Gson GSON = new Gson();
private static final List<String> ABILITIES = List.of("summon", "magic-tools", "siphon", "randomizer", "gateway", "point-of-origin", "kynara-rupture", "eye-of-revival");
private static final List<String> OUTCOMES = List.of("random_item", "hostile_mobs", "withers", "wardens", "damage", "far_teleport", "nether_roof", "dangerous_effects", "useful_items", "book_ability");
private static final Set<String> BENEFICIAL_EFFECTS = Set.of("speed", "haste", "strength", "instant_health", "regeneration", "resistance", "fire_resistance", "water_breathing", "invisibility", "night_vision", "jump_boost", "absorption", "saturation", "conduit_power", "dolphins_grace", "hero_of_the_village", "slow_falling", "luck", "health_boost");
private final WispData data = new WispData();
private final Map<String, Long> cooldowns = new HashMap<>();
private final Map<String, Long> abilityCooldowns = new HashMap<>();
private final Map<UUID, Long> frozenUntil = new HashMap<>();
private final Map<UUID, Float> previousHealth = new HashMap<>();
private final Map<UUID, Integer> previousFood = new HashMap<>();
private final Map<UUID, Long> lethalWindows = new HashMap<>();
private final Map<String, Long> fateCooldowns = new HashMap<>();
private final Map<String, Boolean> fateEnabledOverrides = new HashMap<>();
private final Map<String, Integer> bookCosts = Map.of("summon", 0, "magic-tools", 3, "siphon", 5, "randomizer", 8, "gateway", 12, "point-of-origin", 0, "kynara-rupture", 20, "eye-of-revival", 25);
private final Map<String, Integer> magicToolExpiry = new HashMap<>();
private JsonObject config = new JsonObject();
private Path dataFile;
@Override
public void onInitialize() {
INSTANCE = this;
loadConfig();
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
dataFile = server.getSavePath(WorldSavePath.ROOT).resolve("wispsmp-data.json");
data.load(dataFile);
});
ServerLifecycleEvents.SERVER_STOPPED.register(server -> data.save());
registerCommands();
registerItems();
registerTicks();
}
private void registerItems() {
UseItemCallback.EVENT.register((player, world, hand) -> {
if (world.isClient || !(player instanceof ServerPlayerEntity serverPlayer)) return ActionResult.PASS;
ItemStack stack = player.getStackInHand(hand);
String id = WispItems.id(stack);
if (id == null) return ActionResult.PASS;
switch (id) {
case "snorlax-snowball" -> {
SnowballEntity snowball = new SnowballEntity(world, player);
snowball.setItem(stack.copyWithCount(1));
snowball.setVelocity(player, player.getPitch(), player.getYaw(), 0.0F, 1.5F, 1.0F);
world.spawnEntity(snowball);
stack.decrement(1);
return ActionResult.SUCCESS;
}
case "division-rod" -> {
if (ready(serverPlayer, id)) divisionSwap(serverPlayer, aimedPlayer(serverPlayer, configInt("items.division-rod.range", 32)));
return ActionResult.SUCCESS;
}
case "restrictor" -> {
if (ready(serverPlayer, id)) restrictNearby(serverPlayer);
return ActionResult.SUCCESS;
}
case "storm-clock" -> {
if (ready(serverPlayer, id)) storm(serverPlayer);
return ActionResult.SUCCESS;
}
case "dragon-egg" -> {
if (ready(serverPlayer, id)) dragonBurst(serverPlayer);
return ActionResult.SUCCESS;
}
case "index" -> {
if (ready(serverPlayer, id)) stealHeldItem(serverPlayer);
return ActionResult.SUCCESS;
}
case "book" -> {
sendBookHelp(serverPlayer);
return ActionResult.SUCCESS;
}
case "eye-of-revival" -> {
if (ready(serverPlayer, id)) reviveAimedPlayer(serverPlayer);
return ActionResult.SUCCESS;
}
default -> { return ActionResult.PASS; }
}
});
ProjectileImpactCallback.EVENT.register((projectile, hitResult) -> {
if (projectile.getWorld().isClient || !(projectile instanceof SnowballEntity snowball)
|| !WispItems.is(snowball.getItem(), "snorlax-snowball") || !(hitResult instanceof EntityHitResult hit)
|| !(hit.getEntity() instanceof ServerPlayerEntity target)) return ActionResult.PASS;
int duration = configInt("items.snorlax-snowball.freeze-seconds", 5);
frozenUntil.put(target.getUuid(), System.currentTimeMillis() + duration * 1000L);
target.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, duration * 20, 255, false, false));
target.sendMessage(Text.literal("You were frozen by a Snorlax Snowball."));
return ActionResult.PASS;
});
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (world.isClient || !(player instanceof ServerPlayerEntity attacker) || !(entity instanceof ServerPlayerEntity target)
|| !WispItems.is(attacker.getMainHandStack(), "mugm-sword") || !ready(attacker, "mugm-sword")) return ActionResult.PASS;
applyPlayerDamage(target, (float) configDouble("items.mugm-sword.bonus-damage", 3.0));
target.addStatusEffect(new StatusEffectInstance(StatusEffects.WITHER, 60, 0));
target.sendMessage(Text.literal("The Mugm Sword's curse burns through you."));
return ActionResult.PASS;
});
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> {
if (!(entity instanceof ServerPlayerEntity player) || player.getHealth() > amount) return true;
WispData.LinkState link = data.link(player.getUuid());
ServerPlayerEntity partner = partner(player);
if (link == null || partner == null || !holdingRole(player) || !holdingRole(partner)) return true;
long now = System.currentTimeMillis();
Long partnerLethal = lethalWindows.get(partner.getUuid());
if (partnerLethal != null && now - partnerLethal <= 5000L) {
unlink(player, partner);
return true;
}
lethalWindows.put(player.getUuid(), now);
player.setHealth(1.0F);
return false;
});
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {