Files
src
build.gradle
gradle.properties
settings.gradle
Code
16 428
src/main/java/com/arboris/tricolourwoods/ArborisTricolourWoods.java
package com.arboris.tricolourwoods;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.block.Block;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.WardenEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.Heightmap;
import net.minecraft.world.World;
public final class ArborisTricolourWoods implements ModInitializer {
public static final String MOD_ID = "arboris";
public static final RegistryKey<World> ARBORIS_WORLD = RegistryKey.of(
RegistryKeys.WORLD,
Identifier.of(MOD_ID, "arboris")
);
@Override
public void onInitialize() {
ArborisBlocks.initialize();
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
if (world.isClient || !player.getStackInHand(hand).isOf(Items.FLINT_AND_STEEL)) {
return ActionResult.PASS;
}
BlockPos clicked = hitResult.getBlockPos();
if (!world.getBlockState(clicked).isOf(ArborisBlocks.ARBORIS_FRAME)) {
return ActionResult.PASS;
}
return activatePortal((ServerWorld) world, clicked) ? ActionResult.SUCCESS : ActionResult.PASS;
});
ServerTickEvents.END_WORLD_TICK.register(ArborisTricolourWoods::tickArboris);
}
private static boolean activatePortal(ServerWorld world, BlockPos clicked) {
for (boolean alongX : new boolean[]{true, false}) {
int axisPosition = alongX ? clicked.getX() : clicked.getZ();
int fixedPosition = alongX ? clicked.getZ() : clicked.getX();
for (int left = axisPosition - 2; left <= axisPosition + 1; left++) {
for (int bottom = clicked.getY() - 4; bottom <= clicked.getY() + 1; bottom++) {
if (!isPortalFrame(world, alongX, fixedPosition, left, bottom)) {
continue;
}
for (int width = 1; width <= 2; width++) {
for (int height = 1; height <= 3; height++) {
BlockPos portalPos = alongX
? new BlockPos(left + width, bottom + height, fixedPosition)
: new BlockPos(fixedPosition, bottom + height, left + width);
world.setBlockState(portalPos, ArborisBlocks.ARBORIS_PORTAL.getDefaultState(), Block.NOTIFY_ALL);
}
}
return true;
}
}
}
return false;
}
private static boolean isPortalFrame(ServerWorld world, boolean alongX, int fixedPosition, int left, int bottom) {
for (int width = 0; width < 4; width++) {
for (int height = 0; height < 5; height++) {
BlockPos pos = alongX
? new BlockPos(left + width, bottom + height, fixedPosition)
: new BlockPos(fixedPosition, bottom + height, left + width);
boolean border = width == 0 || width == 3 || height == 0 || height == 4;
if (border) {
if (!world.getBlockState(pos).isOf(ArborisBlocks.ARBORIS_FRAME)) {
return false;
}
} else if (!world.getBlockState(pos).isAir()) {
return false;
}
}
}
return true;
}
private static void tickArboris(ServerWorld world) {
if (!world.getRegistryKey().equals(ARBORIS_WORLD)) {
return;
}
long time = world.getTimeOfDay() % 24000L;
boolean night = time >= 13000L && time < 23000L;
if (world.getTime() % 20L == 0L) {
for (ServerPlayerEntity player : world.getPlayers()) {
if (player.isTouchingWater()) {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.FIRE_RESISTANCE, 6000, 0));
player.addStatusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 6000, 0));
player.addStatusEffect(new StatusEffectInstance(StatusEffects.HEALTH_BOOST, 6000, 0));
}
}
}
if (night && world.getTime() % 8L == 0L) {
for (ServerPlayerEntity player : world.getPlayers()) {
double x = player.getX() + world.getRandom().nextDouble() * 28.0D - 14.0D;
double y = player.getY() + 8.0D + world.getRandom().nextDouble() * 12.0D;
double z = player.getZ() + world.getRandom().nextDouble() * 28.0D - 14.0D;
switch (world.getRandom().nextInt(3)) {
case 0 -> world.spawnParticles(ParticleTypes.FLAME, x, y, z, 1, 0.08D, 0.08D, 0.08D, 0.0D);
case 1 -> world.spawnParticles(ParticleTypes.END_ROD, x, y, z, 1, 0.08D, 0.08D, 0.08D, 0.0D);
default -> world.spawnParticles(ParticleTypes.HAPPY_VILLAGER, x, y, z, 1, 0.08D, 0.08D, 0.08D, 0.0D);
}
}
}
if (!night || world.getTime() % 1200L != 0L || world.getPlayers().isEmpty() || world.getRandom().nextInt(3) != 0) {
return;
}
ServerPlayerEntity target = world.getPlayers().get(world.getRandom().nextInt(world.getPlayers().size()));
Box nearby = target.getBoundingBox().expand(128.0D);
boolean bossNearby = !world.getEntitiesByType(EntityType.WARDEN, nearby, ArborisTricolourWoods::isTimberWarden).isEmpty();
if (bossNearby) {
return;
}
int x = target.getBlockX() + world.getRandom().nextBetween(-24, 24);
int z = target.getBlockZ() + world.getRandom().nextBetween(-24, 24);
int y = world.getTopY(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, x, z);
WardenEntity warden = EntityType.WARDEN.create(world, SpawnReason.EVENT);
if (warden != null) {
warden.refreshPositionAndAngles(x + 0.5D, y, z + 0.5D, world.getRandom().nextFloat() * 360.0F, 0.0F);
warden.setCustomName(net.minecraft.text.Text.literal("Timber Warden"));
warden.setCustomNameVisible(true);
world.spawnEntity(warden);
}
}
private static boolean isTimberWarden(WardenEntity entity) {
return entity.getCustomName() != null && entity.getCustomName().getString().equals("Timber Warden");
}
}