Files
src
build.gradle
gradle.properties
settings.gradle
Code
25 518
src/main/java/org/kodari/innerlight/InnerLight.java
package org.kodari.innerlight;
import com.mojang.serialization.Codec;
import net.fabricmc.api.ModInitializer;
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.ServerChunkEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.commands.Commands;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.particles.DustParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.Filterable;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.WrittenBookContent;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SignTextSlot;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.phys.Vec3;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
public final class InnerLight implements ModInitializer {
private static final AttachmentType<Integer> KNOWLEDGE = AttachmentRegistry.<Integer>builder()
.persistent(Codec.INT).copyOnDeath().buildAndRegister(id("knowledge"));
private static final AttachmentType<Long> LAST_PRACTICE = AttachmentRegistry.<Long>builder()
.persistent(Codec.LONG).copyOnDeath().buildAndRegister(id("last_practice"));
private static final AttachmentType<Long> LAST_SIDDHI = AttachmentRegistry.<Long>builder()
.persistent(Codec.LONG).copyOnDeath().buildAndRegister(id("last_siddhi"));
private static final AttachmentType<Integer> STUDIED = AttachmentRegistry.<Integer>builder()
.persistent(Codec.INT).copyOnDeath().buildAndRegister(id("studied"));
private static final List<Glyph> GLYPHS = new ArrayList<>();
private record Glyph(ArmorStand stand, long expires) { }
private static Identifier id(String path) {
return Identifier.fromNamespaceAndPath("innerlight", path);
}
@Override
public void onInitialize() {
ModItems.initialize();
ServerChunkEvents.CHUNK_LOAD.register((level, chunk, generated) -> {
if (!generated || !level.dimension().equals(Level.OVERWORLD)) return;
int cx = chunk.getPos().x(), cz = chunk.getPos().z();
Random random = new Random((long) cx * 341873128712L + (long) cz * 132897987541L);
if (random.nextInt(160) != 0) return;
int x = cx * 16 + 8, z = cz * 16 + 8;
int y = level.getHeight(Heightmap.Types.MOTION_BLOCKING, x, z);
if (y < 100) return;
for (int dx = -5; dx <= 5; dx += 5) {
for (int dz = -5; dz <= 5; dz += 5) {
if (Math.abs(level.getHeight(Heightmap.Types.MOTION_BLOCKING, x + dx, z + dz) - y) > 3) return;
}
}
boolean monastery = random.nextBoolean();
for (int dx = -5; dx <= 5; dx++) for (int dz = -5; dz <= 5; dz++) {
BlockPos floor = new BlockPos(x + dx, y - 1, z + dz);
level.setBlock(floor, Blocks.STONE_BRICKS.defaultBlockState(), 3, 512);
for (int h = 0; h < 6; h++) level.setBlock(floor.offset(0, h + 1, 0), Blocks.AIR.defaultBlockState(), 3, 512);
if (monastery && (Math.abs(dx) == 5 || Math.abs(dz) == 5)) {
for (int h = 1; h <= 3; h++) {
if (dz == -5 && dx == 0 && h <= 2) continue;
level.setBlock(floor.offset(0, h, 0), Blocks.POLISHED_ANDESITE.defaultBlockState(), 3, 512);
}
}
if (monastery && Math.abs(dx) <= 5 && Math.abs(dz) <= 5) {
level.setBlock(floor.offset(0, 4, 0), Blocks.DARK_OAK_PLANKS.defaultBlockState(), 3, 512);
}
}
for (int dx = -2; dx <= 2; dx++) for (int dz = -2; dz <= 2; dz++) {
level.setBlock(new BlockPos(x + dx, y, z + dz), Blocks.POLISHED_ANDESITE.defaultBlockState(), 3, 512);
}
level.setBlock(new BlockPos(x, y + 1, z), Blocks.GOLD_BLOCK.defaultBlockState(), 3, 512);
level.setBlock(new BlockPos(x, y + 2, z), Blocks.LANTERN.defaultBlockState(), 3, 512);
int flagY = y + (monastery ? 5 : 4);
for (int h = 0; h < 3; h++) {
level.setBlock(new BlockPos(x - 4, flagY + h, z - 4), Blocks.OAK_FENCE.defaultBlockState(), 3, 512);
level.setBlock(new BlockPos(x + 4, flagY + h, z - 4), Blocks.OAK_FENCE.defaultBlockState(), 3, 512);
}
DyeColor[] colors = {DyeColor.BLUE, DyeColor.WHITE, DyeColor.RED, DyeColor.GREEN, DyeColor.YELLOW};
for (int dx = -3; dx <= 3; dx++) {
level.setBlock(new BlockPos(x + dx, flagY + 2, z - 4), Blocks.IRON_CHAIN.defaultBlockState(), 3, 512);
level.setBlock(new BlockPos(x + dx, flagY + 1, z - 4), Blocks.WOOL.pick(colors[(dx + 3) % colors.length]).defaultBlockState(), 3, 512);
}
BlockPos chestPos = new BlockPos(x + 3, y, z + 2);
level.setBlock(chestPos, Blocks.CHEST.defaultBlockState(), 3, 512);
if (level.getBlockEntity(chestPos) instanceof ChestBlockEntity chest) {
chest.setItem(0, teachingBook(0));
chest.setItem(1, teachingBook(1));
}
BlockPos signPos = new BlockPos(x - 3, y, z + 2);
level.setBlock(signPos, Blocks.OAK_SIGN.defaultBlockState(), 3, 512);
if (level.getBlockEntity(signPos) instanceof SignBlockEntity sign) {
var text = sign.getText(SignTextSlot.FRONT).asMutable();
text.setLine(0, Component.literal("ༀ མ་ཎི"));
text.setLine(1, Component.literal("innerlight:inscription"));
text.setLine(2, Component.literal("Внимание и сострадание"));
sign.setText(text.asImmutable(), SignTextSlot.FRONT);
}
});
UseBlockCallback.EVENT.register((player, level, hand, hit) -> {
if (player instanceof ServerPlayer serverPlayer && level.getBlockEntity(hit.getBlockPos()) instanceof SignBlockEntity sign
&& sign.getText(SignTextSlot.FRONT).getMessages(false).stream().anyMatch(line -> line.getString().equals("innerlight:inscription"))) {
study(serverPlayer, 2, "надпись: внимание и сострадание");
}
return InteractionResult.PASS;
});
UseItemCallback.EVENT.register((player, level, hand) -> {
if (player instanceof ServerPlayer serverPlayer) {
ItemStack held = player.getItemInHand(hand);
if (held.is(ModItems.VAJRA)) {
if (player.getCooldowns().isOnCooldown(held)) return InteractionResult.FAIL;
for (var entity : level.getEntities(player, player.getBoundingBox().inflate(5), e -> e instanceof Monster)) {
entity.hurt(player.damageSources().magic(), 4.0f);
}
player.getCooldowns().addCooldown(held, 80);
held.hurtAndBreak(1, player, hand);
aura(serverPlayer);
player.playSound(SoundEvents.EXPERIENCE_ORB_PICKUP, 0.8f, 0.7f);
return InteractionResult.SUCCESS;
}
var content = player.getItemInHand(hand).get(DataComponents.WRITTEN_BOOK_CONTENT);
if (content != null && content.author().equals("Inner Light")) {
if (content.title().raw().equals("Сутра внимания")) study(serverPlayer, 0, "сутру внимания");
if (content.title().raw().equals("Свиток практики")) study(serverPlayer, 1, "свиток практики");
}
}
return InteractionResult.PASS;
});
ServerTickEvents.END_SERVER_TICK.register(server -> GLYPHS.removeIf(glyph -> {
if (glyph.stand().isRemoved()) return true;
if (glyph.stand().level().getGameTime() >= glyph.expires()) {
glyph.stand().discard();
return true;
}
return false;
}));
CommandRegistrationCallback.EVENT.register((dispatcher, registry, selection) -> dispatcher.register(
Commands.literal("tantra")
.executes(ctx -> status(ctx.getSource().getPlayerOrException()))
.then(Commands.literal("meditate").executes(ctx -> practice(ctx.getSource().getPlayerOrException(), 0)))
.then(Commands.literal("mantra").executes(ctx -> practice(ctx.getSource().getPlayerOrException(), 1)))
.then(Commands.literal("offering").executes(ctx -> practice(ctx.getSource().getPlayerOrException(), 2)))
.then(Commands.literal("tummo").executes(ctx -> siddhi(ctx.getSource().getPlayerOrException(), 0)))
.then(Commands.literal("mist").executes(ctx -> siddhi(ctx.getSource().getPlayerOrException(), 1)))
.then(Commands.literal("wind").executes(ctx -> siddhi(ctx.getSource().getPlayerOrException(), 2)))
.then(Commands.literal("lightness").executes(ctx -> siddhi(ctx.getSource().getPlayerOrException(), 3)))
.then(Commands.literal("illusion").executes(ctx -> siddhi(ctx.getSource().getPlayerOrException(), 4)))
.then(Commands.literal("projection").executes(ctx -> siddhi(ctx.getSource().getPlayerOrException(), 5)))
.then(Commands.literal("sutra").executes(ctx -> writeSutra(ctx.getSource().getPlayerOrException())))
));
}
private static ItemStack teachingBook(int kind) {
ItemStack book = new ItemStack(Items.WRITTEN_BOOK);
String title = kind == 0 ? "Сутра внимания" : "Свиток практики";
String text = kind == 0
? "ༀ མ་ཎི པདྨེ ཧཱུྃ\n\nПрактикуй внимательность и сострадание. Присев на землю, используй /tantra meditate или /tantra mantra."
: "Внутренний огонь и лёгкость тела\n\nПоднеси золотой самородок через /tantra offering. Знание открывает tummo, mist, wind и lightness. Отдыхай между практиками.";
book.set(DataComponents.WRITTEN_BOOK_CONTENT, new WrittenBookContent(Filterable.passThrough(title), "Inner Light", 0,
List.of(Filterable.passThrough(Component.literal(text))), true));
return book;
}
private static void study(ServerPlayer player, int index, String name) {
Integer value = player.getAttached(STUDIED);
int mask = value == null ? 0 : value;
int bit = 1 << index;
if ((mask & bit) != 0) return;
player.setAttached(STUDIED, mask | bit);
int points = gainKnowledge(player, 2);
player.sendSystemMessage(Component.literal("Вы изучили " + name + ". Знание: " + points + "."));
aura(player);
}
private static int knowledge(ServerPlayer player) {