Files
src
build.gradle
gradle.properties
settings.gradle
Code
15 403
src/main/java/com/kodari/saiyanhero/SaiyanHero.java
package com.kodari.saiyanhero;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
public class SaiyanHero implements ModInitializer {
private static final Map<UUID, FallingBlockEntity> HELD = new HashMap<>();
private static final Map<UUID, Long> COOLDOWNS = new HashMap<>();
@Override
public void onInitialize() {
PayloadTypeRegistry.serverboundPlay().register(ActionPayload.TYPE, ActionPayload.CODEC);
ServerPlayNetworking.registerGlobalReceiver(ActionPayload.TYPE, (payload, context) -> act(context.player(), payload.action()));
ServerTickEvents.END_SERVER_TICK.register(server -> {
Iterator<Map.Entry<UUID, FallingBlockEntity>> it = HELD.entrySet().iterator();
while (it.hasNext()) {
var entry = it.next();
ServerPlayer player = server.getPlayerList().getPlayers().stream().filter(p -> p.getUUID().equals(entry.getKey())).findFirst().orElse(null);
FallingBlockEntity block = entry.getValue();
if (player == null || !player.isAlive() || !block.isAlive() || player.level() != block.level()) {
block.setNoGravity(false);
it.remove();
continue;
}
block.setPos(player.getEyePosition().add(player.getLookAngle().scale(2.0)).add(0, 0.6, 0));
block.setDeltaMovement(Vec3.ZERO);
}
});
}
private static void act(ServerPlayer player, int action) {
if (!(player.level() instanceof ServerLevel level) || !player.isAlive()) return;
if (action == 0) {
boolean flying = !player.getAbilities().flying;
player.getAbilities().mayfly = true;
player.getAbilities().flying = flying;
player.onUpdateAbilities();
return;
}
if (action == 3) {
FallingBlockEntity held = HELD.remove(player.getUUID());
if (held != null && held.isAlive()) {
held.setNoGravity(false);
held.setDeltaMovement(player.getLookAngle().scale(2.0));
return;
}
HitResult hit = player.pick(6.0, 1.0f, false);
if (hit.getType() != HitResult.Type.BLOCK) return;
BlockPos pos = ((BlockHitResult) hit).getBlockPos();
BlockState state = level.getBlockState(pos);
if (state.isAir() || state.getDestroySpeed(level, pos) < 0 || level.getBlockEntity(pos) != null) return;
FallingBlockEntity block = FallingBlockEntity.fall(level, pos, state);
block.setNoGravity(true);
HELD.put(player.getUUID(), block);
return;
}
if (action == 4) {
Vec3 pos = player.position();
level.playSound(null, pos.x, pos.y, pos.z, SoundEvents.BEACON_ACTIVATE, SoundSource.PLAYERS, 2.0f, 0.7f);
level.playSound(null, pos.x, pos.y, pos.z, SoundEvents.ENDER_DRAGON_SHOOT, SoundSource.PLAYERS, 1.5f, 0.6f);
level.sendParticles(ParticleTypes.FLAME, pos.x, pos.y + 1, pos.z, 120, 0.8, 1.0, 0.8, 0.04);
level.sendParticles(ParticleTypes.ELECTRIC_SPARK, pos.x, pos.y + 1, pos.z, 100, 1.0, 1.0, 1.0, 0.15);
if (player.mayBuild()) {
BlockPos center = player.blockPosition();
for (BlockPos blockPos : BlockPos.betweenClosed(center.offset(-4, -4, -4), center.offset(4, 4, 4))) {
if (blockPos.distToCenterSqr(center.getX() + 0.5, center.getY() + 0.5, center.getZ() + 0.5) > 16) continue;
BlockState state = level.getBlockState(blockPos);
if (state.isAir() || state.getDestroySpeed(level, blockPos) < 0 || level.getBlockEntity(blockPos) != null) continue;
level.destroyBlock(blockPos, true, player, 512);
}
}
return;
}
if (action != 1 && action != 2) return;
long now = System.currentTimeMillis();
if (COOLDOWNS.getOrDefault(player.getUUID(), 0L) > now) return;
COOLDOWNS.put(player.getUUID(), now + (action == 1 ? 2500 : 350));
if (action == 1) {
fireBeam(level, player);
return;
}
double range = 32;
Vec3 start = player.getEyePosition();
Vec3 end = player.pick(range, 1.0f, false).getLocation();
Vec3 direction = player.getLookAngle();
double distance = Math.sqrt((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y) + (end.z - start.z) * (end.z - start.z));
level.playSound(null, start.x, start.y, start.z, SoundEvents.ENDER_DRAGON_SHOOT, SoundSource.PLAYERS, 1.5f, 1.5f);
level.playSound(null, start.x, start.y, start.z, SoundEvents.CONDUIT_ATTACK_TARGET, SoundSource.PLAYERS, 1.0f, 1.4f);
for (double d = 1; d < distance; d += 0.5) {
Vec3 point = start.add(direction.scale(d));
level.sendParticles(ParticleTypes.ELECTRIC_SPARK, point.x, point.y, point.z, 3, 0.15, 0.15, 0.15, 0.01);
level.sendParticles(ParticleTypes.END_ROD, point.x, point.y, point.z, 2, 0.08, 0.08, 0.08, 0.005);
}
level.sendParticles(ParticleTypes.SOUL_FIRE_FLAME, end.x, end.y, end.z, 35, 0.5, 0.5, 0.5, 0.05);
level.explode(player, end.x, end.y, end.z, 2.0f, Level.ExplosionInteraction.TNT);
}
private static void fireBeam(ServerLevel level, ServerPlayer player) {
Vec3 start = player.getEyePosition();
Vec3 direction = player.getLookAngle();
Vec3 side = new Vec3(-direction.z, 0, direction.x);
if (side.x * side.x + side.z * side.z < 0.0001) side = new Vec3(1, 0, 0);
side = side.normalize();
Vec3 up = new Vec3(
direction.y * side.z - direction.z * side.y,
direction.z * side.x - direction.x * side.z,
direction.x * side.y - direction.y * side.x
).normalize();
Set<UUID> damaged = new HashSet<>();
level.playSound(null, start.x, start.y, start.z, SoundEvents.BEACON_POWER_SELECT, SoundSource.PLAYERS, 3.0f, 0.7f);
level.playSound(null, start.x, start.y, start.z, SoundEvents.WARDEN_SONIC_BOOM, SoundSource.PLAYERS, 3.0f, 0.7f);
level.sendParticles(ParticleTypes.SOUL_FIRE_FLAME, start.x, start.y, start.z, 80, 0.5, 0.5, 0.5, 0.06);
level.sendParticles(ParticleTypes.ELECTRIC_SPARK, start.x, start.y, start.z, 80, 0.7, 0.7, 0.7, 0.15);
for (int distance = 2; distance <= 64; distance++) {
Vec3 point = start.add(direction.scale(distance));
BlockPos center = BlockPos.containing(point.x, point.y, point.z);
if (!level.isLoaded(center)) break;
level.sendParticles(ParticleTypes.END_ROD, point.x, point.y, point.z, 5, 0.35, 0.35, 0.35, 0.01);
level.sendParticles(ParticleTypes.SOUL_FIRE_FLAME, point.x, point.y, point.z, 8, 0.55, 0.55, 0.55, 0.01);
if (distance % 2 == 0) {
for (int i = 0; i < 8; i++) {
double angle = i * Math.PI / 4 + distance * 0.3;
Vec3 ring = point.add(side.scale(Math.cos(angle) * 1.5)).add(up.scale(Math.sin(angle) * 1.5));
level.sendParticles(ParticleTypes.ELECTRIC_SPARK, ring.x, ring.y, ring.z, 1, 0, 0, 0, 0);
}
}
if (distance % 8 == 0) {
level.sendParticles(ParticleTypes.SONIC_BOOM, point.x, point.y, point.z, 1, 0, 0, 0, 0);
}
AABB area = new AABB(point.add(-2.5, -2.5, -2.5), point.add(2.5, 2.5, 2.5));
for (var entity : level.getEntities(player, area, entity -> entity.isAlive())) {
if (damaged.add(entity.getUUID())) entity.hurt(level.damageSources().playerAttack(player), 40.0f);
}
if (player.mayBuild()) {
for (BlockPos pos : BlockPos.betweenClosed(center.offset(-3, -3, -3), center.offset(3, 3, 3))) {
if (pos.distToCenterSqr(point.x, point.y, point.z) > 9 || !level.isLoaded(pos)) continue;
BlockState state = level.getBlockState(pos);
if (!state.isAir() && state.getDestroySpeed(level, pos) >= 0) {
level.destroyBlock(pos, false, player, 512);
}
}
}
}
}
}