Files
src
build.gradle
gradle.properties
settings.gradle
Code
22 909
src/main/java/dev/kodari/abyssalblade/AbyssalBladeMod.java
package dev.kodari.abyssalblade;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.Monster;
import net.minecraft.entity.passive.WolfEntity;
import net.minecraft.entity.player.PlayerAbilities;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.FireballEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.Heightmap;
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public final class AbyssalBladeMod implements ModInitializer {
public static final String MOD_ID = "abyssalblade";
public static final Item VIRA_BLADE = Registry.register(Registries.ITEM,
new Identifier(MOD_ID, "vira_blade"), new Item(new Item.Settings().maxDamage(2000)));
public static final Item VIRA_BANDS = Registry.register(Registries.ITEM,
new Identifier(MOD_ID, "vira_bands"), new ViraBandsItem(new Item.Settings().maxCount(1)));
public static final Block GOO_BLOCK = Registry.register(Registries.BLOCK,
new Identifier(MOD_ID, "digital_goo"),
new GooBlock(FabricBlockSettings.of(Material.ORGANIC_PRODUCT).strength(0.5f)));
public static final Identifier ACTION_PACKET = new Identifier(MOD_ID, "ability_action");
public static final Identifier STATE_PACKET = new Identifier(MOD_ID, "ability_state");
private static final Map<UUID, Integer> INFECTION = new ConcurrentHashMap<>();
private static final Map<UUID, Long> LAST_USE = new ConcurrentHashMap<>();
private static final Map<UUID, Boolean> BLADE_ACTIVE = new ConcurrentHashMap<>();
private static final List<CrossEvent> CROSSES = new ArrayList<>();
private static long gameTick;
@Override
public void onInitialize() {
Registry.register(Registries.ITEM, new Identifier(MOD_ID, "digital_goo"),
new BlockItem(GOO_BLOCK, new Item.Settings()));
ServerPlayNetworking.registerGlobalReceiver(ACTION_PACKET, (server, player, handler, buf, responseSender) -> {
byte action = buf.readByte();
server.execute(() -> activate(player, action));
});
AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> {
if (world.isClient || !(player instanceof ServerPlayerEntity serverPlayer)
|| !isBladeActive(serverPlayer) || !wearsBands(serverPlayer)) {
return ActionResult.PASS;
}
world.breakBlock(pos, false, player, 512);
addInfection(serverPlayer, 2);
syncState(serverPlayer);
return ActionResult.SUCCESS;
});
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (world.isClient || !(player instanceof ServerPlayerEntity serverPlayer)
|| !isBladeActive(serverPlayer) || !wearsBands(serverPlayer)
|| !(entity instanceof LivingEntity target) || entity instanceof PlayerEntity) {
return ActionResult.PASS;
}
if (world instanceof ServerWorld serverWorld) {
serverWorld.spawnParticles(ParticleTypes.SCULK_SOUL, target.getX(), target.getY() + 0.7,
target.getZ(), 90, 0.7, 0.8, 0.7, 0.08);
serverWorld.spawnParticles(DustParticleEffect.DEFAULT, target.getX(), target.getY() + 0.7,
target.getZ(), 50, 0.6, 0.7, 0.6, 0.02);
serverWorld.spawnParticles(ParticleTypes.CRIT, target.getX(), target.getY() + 0.7,
target.getZ(), 60, 0.65, 0.7, 0.65, 0.1);
}
target.discard();
addInfection(serverPlayer, 8);
syncState(serverPlayer);
return ActionResult.SUCCESS;
});
ServerTickEvents.END_SERVER_TICK.register(AbyssalBladeMod::tickServer);
}
public static boolean isBladeActive(PlayerEntity player) {
return BLADE_ACTIVE.getOrDefault(player.getUuid(), false);
}
public static int getInfection(PlayerEntity player) {
return INFECTION.getOrDefault(player.getUuid(), 0);
}
private static boolean wearsBands(PlayerEntity player) {
return player.getEquippedStack(EquipmentSlot.CHEST).isOf(VIRA_BANDS);
}
private static void activate(ServerPlayerEntity player, byte action) {
switch (action) {
case 1 -> {
if (!wearsBands(player)) return;
boolean active = !isBladeActive(player);
BLADE_ACTIVE.put(player.getUuid(), active);
if (active) addInfection(player, 3);
}
case 2 -> fireball(player);
case 3 -> laser(player);
case 4 -> summonBots(player);
case 5 -> redCross(player);
case 6 -> portal(player);
default -> { return; }
}
syncState(player);
}
private static void fireball(ServerPlayerEntity player) {
ServerWorld world = player.getWorld();
Vec3d direction = player.getRotationVec(1.0f);
Vec3d eye = player.getEyePos();
FireballEntity fireball = new FireballEntity(world, player,
direction.x * 1.5, direction.y * 1.5, direction.z * 1.5, 2);
fireball.teleport(eye.x, eye.y, eye.z);
world.spawnEntity(fireball);
}
private static void laser(ServerPlayerEntity player) {
World world = player.getWorld();
Vec3d start = player.getEyePos();
Vec3d direction = player.getRotationVec(1.0f);
Vec3d end = start.add(direction.multiply(50.0));
var blockHit = world.raycast(new RaycastContext(start, end, RaycastContext.ShapeType.COLLIDER,
RaycastContext.FluidHandling.NONE, player));
double range = Math.min(50.0, start.distanceTo(blockHit.getPos()));
for (double distance = 0.5; distance <= range; distance += 0.5) {
double x = start.x + direction.x * distance;
double y = start.y + direction.y * distance;
double z = start.z + direction.z * distance;
if (world instanceof ServerWorld serverWorld) {
serverWorld.spawnParticles(DustParticleEffect.DEFAULT, x, y, z, 2, 0.03, 0.03, 0.03, 0.0);
serverWorld.spawnParticles(ParticleTypes.CRIT, x, y, z, 1, 0.02, 0.02, 0.02, 0.0);
}
List<Entity> hits = world.getOtherEntities(player, new Box(x - 0.35, y - 0.35, z - 0.35,
x + 0.35, y + 0.35, z + 0.35), entity -> entity instanceof LivingEntity && entity.isAlive());
if (!hits.isEmpty() && hits.get(0) instanceof LivingEntity target) {
target.damage(player.getDamageSources().playerAttack(player), 10.0f);
target.setOnFireFor(5);
break;
}
}
}
private static void summonBots(ServerPlayerEntity player) {
if (!wearsBands(player)) return;
ServerWorld world = player.getWorld();
List<Entity> hostiles = world.getOtherEntities(player,
new Box(player.getX() - 24, player.getY() - 12, player.getZ() - 24,
player.getX() + 24, player.getY() + 12, player.getZ() + 24),
entity -> entity instanceof Monster && entity.isAlive());
LivingEntity target = hostiles.isEmpty() ? null : (LivingEntity) hostiles.get(0);
for (int index = 0; index < 3; index++) {
WolfEntity bot = new WolfEntity(EntityType.WOLF, world);
bot.setOwner(player);
bot.setCollarColor(DyeColor.RED);
bot.teleport(player.getX() + (index - 1) * 1.5, player.getY(), player.getZ() + 1.5);
if (target != null) bot.setTarget(target);
world.spawnEntity(bot);
}
addInfection(player, 20);
}
private static void redCross(ServerPlayerEntity player) {