Files
src
build.gradle
gradle.properties
settings.gradle
Code
10 306
src/main/java/com/kodari/akaza/AkazaMod.java
package com.kodari.akaza;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
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.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class AkazaMod implements ModInitializer {
private static final String MOD_ID = "akaza";
private static final RegistryKey<World> INFINITE_CASTLE_KEY = RegistryKey.of(
RegistryKeys.WORLD,
new Identifier(MOD_ID, "infinite_castle")
);
private static final Set<UUID> AKAZA_PLAYERS = new HashSet<>();
private static final Map<UUID, Integer> STUN_TICKS = new HashMap<>();
private static final Map<UUID, Integer> BARRAGE_TICKS = new HashMap<>();
public static final Item INFINITE_CASTLE_BELL = Registry.register(
Registries.ITEM,
new Identifier(MOD_ID, "infinite_castle_bell"),
new Item(new Item.Settings())
);
@Override
public void onInitialize() {
UseItemCallback.EVENT.register((player, world, hand) -> {
ItemStack stack = player.getStackInHand(hand);
if (!stack.isOf(INFINITE_CASTLE_BELL)) {
return TypedActionResult.pass(stack);
}
if (player instanceof ServerPlayerEntity serverPlayer) {
if (AKAZA_PLAYERS.add(serverPlayer.getUuid())) {
serverPlayer.addStatusEffect(new StatusEffectInstance(
StatusEffects.RESISTANCE, Integer.MAX_VALUE, 2, true, false, false
));
serverPlayer.addStatusEffect(new StatusEffectInstance(
StatusEffects.REGENERATION, Integer.MAX_VALUE, 1, true, false, false
));
serverPlayer.addStatusEffect(new StatusEffectInstance(
StatusEffects.STRENGTH, Integer.MAX_VALUE, 1, true, false, false
));
serverPlayer.addStatusEffect(new StatusEffectInstance(
StatusEffects.SPEED, Integer.MAX_VALUE, 1, true, false, false
));
}
if (player.isSneaking()) {
BARRAGE_TICKS.put(serverPlayer.getUuid(), 12);
return TypedActionResult.success(stack);
}
MinecraftServer server = serverPlayer.getServerWorld().getServer();
if (serverPlayer.getServerWorld().getRegistryKey().equals(INFINITE_CASTLE_KEY)) {
ServerWorld overworld = server.getOverworld();
BlockPos spawn = overworld.getSpawnPos();
serverPlayer.teleport(
overworld,
spawn.getX() + 0.5,
spawn.getY() + 1.0,
spawn.getZ() + 0.5,
serverPlayer.getYaw(),
serverPlayer.getPitch()
);
} else {
ServerWorld castle = server.getWorld(INFINITE_CASTLE_KEY);
if (castle == null) {
return TypedActionResult.fail(stack);
}
serverPlayer.teleport(castle, 0.5, 80.0, 0.5, serverPlayer.getYaw(), serverPlayer.getPitch());
}
}
return TypedActionResult.success(stack);
});
ServerLivingEntityEvents.ALLOW_DEATH.register((entity, damageSource, damageAmount) -> {
if (!(entity instanceof ServerPlayerEntity player) || !AKAZA_PLAYERS.contains(player.getUuid())) {
return true;
}
UUID playerId = player.getUuid();
player.setHealth(1.0F);
if (!STUN_TICKS.containsKey(playerId)) {
player.dropItem(new ItemStack(Items.PLAYER_HEAD), true);
STUN_TICKS.put(playerId, 20 * 20);
}
return false;
});
ServerTickEvents.END_SERVER_TICK.register(this::tickPlayers);
}
private void tickPlayers(MinecraftServer server) {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
UUID playerId = player.getUuid();
Integer remainingStun = STUN_TICKS.get(playerId);
if (remainingStun != null) {
player.setVelocity(0.0, 0.0, 0.0);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 2, 10, true, false, false));
player.addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 2, 4, true, false, false));
if (remainingStun <= 1) {
STUN_TICKS.remove(playerId);
player.setHealth(20.0F);
} else {
STUN_TICKS.put(playerId, remainingStun - 1);
}
continue;
}
Integer remainingBarrage = BARRAGE_TICKS.get(playerId);
if (remainingBarrage != null) {
if (remainingBarrage % 2 == 0) {
for (LivingEntity target : player.getWorld().getEntitiesByClass(
LivingEntity.class,
player.getBoundingBox().expand(3.0),
LivingEntity::isAlive
)) {
if (target != player) {
target.damage(player.getDamageSources().playerAttack(player), 2.0F);
}
}
}
if (remainingBarrage <= 1) {
BARRAGE_TICKS.remove(playerId);
} else {
BARRAGE_TICKS.put(playerId, remainingBarrage - 1);
}
}
if (AKAZA_PLAYERS.contains(playerId)) {
ServerWorld world = player.getServerWorld();
if (world.isDay() && world.isSkyVisible(player.getBlockPos()) && world.getTime() % 20L == 0L) {
player.setOnFireFor(2);
player.damage(player.getDamageSources().magic(), 100.0F);
}
}
}
}
}