Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 257
src/main/java/com/kodari/ashentrial/AshenTrialMod.java
package com.kodari.ashentrial;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.WardenEntity;
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.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.Heightmap;
import net.minecraft.world.World;
public class AshenTrialMod implements ModInitializer {
private static final String BOSS_NAME = "Пепельный страж";
private static final int MAX_STAMINA = 100;
private static final int ATTACK_COST = 20;
private static final int DODGE_COST = 35;
private static final Map<UUID, Integer> STAMINA = new HashMap<>();
private static final Map<UUID, Boolean> DODGE_HELD = new HashMap<>();
@Override
public void onInitialize() {
ServerWorldEvents.LOAD.register((server, world) -> {
if (world.getRegistryKey().equals(World.OVERWORLD)) {
createArenaAndBoss(world);
}
});
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (world.isClient()) {
return ActionResult.PASS;
}
UUID playerId = player.getUuid();
int stamina = STAMINA.getOrDefault(playerId, MAX_STAMINA);
if (stamina < ATTACK_COST) {
player.sendMessage(Text.literal("Вы слишком устали, чтобы атаковать."), true);
return ActionResult.FAIL;
}
stamina -= ATTACK_COST;
STAMINA.put(playerId, stamina);
player.sendMessage(Text.literal("Выносливость: " + stamina + "/" + MAX_STAMINA), true);
return ActionResult.PASS;
});
ServerTickEvents.END_WORLD_TICK.register(AshenTrialMod::tickCombat);
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
ServerPlayerEntity player = handler.player;
STAMINA.put(player.getUuid(), MAX_STAMINA);
player.sendMessage(Text.literal("§6[Пепельное испытание] Обучение"), false);
player.sendMessage(Text.literal("§eАрена находится в 48 блоках к востоку от мирового спавна. Внутри вас ждёт Пепельный страж."), false);
player.sendMessage(Text.literal("§eКаждая атака тратит 20 выносливости; она восстанавливается со временем."), false);
player.sendMessage(Text.literal("§eНажмите присесть, чтобы сделать перекат: он стоит 35 выносливости и ненадолго защищает от урона."), false);
});
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
UUID playerId = handler.player.getUuid();
STAMINA.remove(playerId);
DODGE_HELD.remove(playerId);
});
}
private static void tickCombat(ServerWorld world) {
for (ServerPlayerEntity player : world.getPlayers()) {
UUID playerId = player.getUuid();
int stamina = Math.min(MAX_STAMINA, STAMINA.getOrDefault(playerId, MAX_STAMINA) + 1);
STAMINA.put(playerId, stamina);
boolean sneaking = player.isSneaking();
boolean wasSneaking = DODGE_HELD.getOrDefault(playerId, false);
if (sneaking && !wasSneaking) {
if (stamina >= DODGE_COST) {
stamina -= DODGE_COST;
STAMINA.put(playerId, stamina);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.SPEED, 12, 1, false, false, false));
player.addStatusEffect(new StatusEffectInstance(StatusEffects.RESISTANCE, 8, 4, false, false, false));
player.sendMessage(Text.literal("Перекат — выносливость: " + stamina + "/" + MAX_STAMINA), true);
} else {
player.sendMessage(Text.literal("Недостаточно выносливости для переката."), true);
}
}
DODGE_HELD.put(playerId, sneaking);
}
}
private static void createArenaAndBoss(ServerWorld world) {
BlockPos worldSpawn = world.getSpawnPoint().getPos();
int arenaX = worldSpawn.getX() + 48;
int arenaZ = worldSpawn.getZ();
int floorY = world.getTopY(Heightmap.Type.MOTION_BLOCKING, arenaX, arenaZ) - 1;
BlockPos center = new BlockPos(arenaX, floorY + 1, arenaZ);
BlockPos floorCenter = center.down();
boolean arenaExists = world.getBlockState(floorCenter).equals(Blocks.POLISHED_BLACKSTONE_BRICKS.getDefaultState());
if (!arenaExists) {
buildArena(world, center);
}
Box bossArea = new Box(center).expand(128, 16, 128);
boolean bossAlive = !world.getEntitiesByClass(WardenEntity.class, bossArea,
boss -> boss.hasCustomName() && BOSS_NAME.equals(boss.getCustomName().getString())).isEmpty();
if (bossAlive) {
return;
}
WardenEntity boss = EntityType.WARDEN.create(world, SpawnReason.EVENT);
if (boss == null) {
return;
}
var healthAttribute = boss.getAttributeInstance(EntityAttributes.MAX_HEALTH);
if (healthAttribute != null) {
healthAttribute.setBaseValue(320.0);
boss.setHealth(320.0F);
}
boss.setCustomName(Text.literal(BOSS_NAME));
boss.setCustomNameVisible(true);
boss.setPersistent();
boss.refreshPositionAndAngles(center.getX() + 0.5, center.getY(), center.getZ() + 0.5, 180.0F, 0.0F);
world.spawnEntity(boss);
}
private static void buildArena(ServerWorld world, BlockPos center) {
var blackstone = Blocks.POLISHED_BLACKSTONE_BRICKS.getDefaultState();
for (int x = -10; x <= 10; x++) {
for (int z = -10; z <= 10; z++) {
world.setBlockState(center.add(x, -1, z), blackstone);
boolean wall = Math.abs(x) == 10 || Math.abs(z) == 10;
boolean entrance = z == 10 && Math.abs(x) <= 1;
if (wall && !entrance) {
for (int y = 0; y < 4; y++) {
world.setBlockState(center.add(x, y, z), blackstone);
}
}
}
}
for (int x : new int[] {-7, 7}) {
for (int z : new int[] {-7, 7}) {
for (int y = 0; y < 4; y++) {
world.setBlockState(center.add(x, y, z), blackstone);
}
world.setBlockState(center.add(x, 4, z), Blocks.SOUL_LANTERN.getDefaultState());
}
}
}
}