Files
src
build.gradle
gradle.properties
settings.gradle
Code
10 209
src/main/java/com/kodari/orbitalfishing/OrbitalFishing.java
package com.kodari.orbitalfishing;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FishingRodItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.Heightmap;
public class OrbitalFishing implements ModInitializer {
private static final RegistryKey<Item> ROD_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("orbitalfishing", "orbital_rod"));
private static final Item ROD = Registry.register(Registries.ITEM, ROD_KEY, new OrbitalRod(new Item.Settings().registryKey(ROD_KEY).maxDamage(1)));
private static final List<Barrage> BARRAGES = new ArrayList<>();
@Override
public void onInitialize() {
RegistryKey<ItemGroup> tools = RegistryKey.of(RegistryKeys.ITEM_GROUP, Identifier.of("minecraft", "tools_and_utilities"));
ItemGroupEvents.modifyEntriesEvent(tools).register(entries -> entries.add(ROD));
ServerTickEvents.END_WORLD_TICK.register(OrbitalFishing::tick);
}
private static void tick(ServerWorld world) {
Iterator<Barrage> iterator = BARRAGES.iterator();
while (iterator.hasNext()) {
Barrage barrage = iterator.next();
if (barrage.world != world) continue;
for (int i = 0; i < 4; i++) {
double angle = world.getRandom().nextDouble() * Math.PI * 2;
double radius = Math.sqrt(world.getRandom().nextDouble()) * 16;
double x = barrage.center.x + Math.cos(angle) * radius;
double z = barrage.center.z + Math.sin(angle) * radius;
double y = world.getTopY(Heightmap.Type.WORLD_SURFACE, (int) Math.floor(x), (int) Math.floor(z));
for (int height = 0; height <= 10; height += 2) {
world.spawnParticles(ParticleTypes.SONIC_BOOM, x, y + height, z, 1, 0, 0, 0, 0);
}
world.playSound(null, x, y, z, SoundEvents.ENTITY_WARDEN_SONIC_BOOM, SoundCategory.PLAYERS, 0.5f, 1.0f);
for (var entity : world.getOtherEntities(null, new Box(x - 1.5, y - 1, z - 1.5, x + 1.5, y + 3, z + 1.5), e -> e.isAlive())) {
entity.damage(world, barrage.attacker.getDamageSources().sonicBoom(barrage.attacker), 14.0f);
}
}
if (--barrage.ticks == 0) iterator.remove();
}
}
private static class Barrage {
final ServerWorld world;
final PlayerEntity attacker;
final Vec3d center;
int ticks = 20;
Barrage(ServerWorld world, PlayerEntity attacker, Vec3d center) {
this.world = world;
this.attacker = attacker;
this.center = center;
}
}
private static class OrbitalRod extends FishingRodItem {
OrbitalRod(Item.Settings settings) {
super(settings);
}
@Override
public ActionResult use(World world, PlayerEntity user, Hand hand) {
HitResult target = user.raycast(128, 1.0f, false);
if (target.getType() != HitResult.Type.BLOCK) return ActionResult.PASS;
if (world instanceof ServerWorld serverWorld) {
BARRAGES.add(new Barrage(serverWorld, user, target.getPos()));
ItemStack stack = user.getStackInHand(hand);
stack.damage(1, user, hand);
}
return ActionResult.SUCCESS;
}
}
}