Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 219
src/main/java/dev/kodari/orbitalwindcharge/OrbitalWindchargeMod.java
package dev.kodari.orbitalwindcharge;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ProjectileUtil;
import net.minecraft.entity.projectile.WindChargeEntity;
import net.minecraft.item.FishingRodItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.command.CommandManager;
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.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;
public final class OrbitalWindchargeMod implements ModInitializer {
public static final String MOD_ID = "orbitalwindcharge";
private static final int ROD_MAX_DAMAGE = 64;
private static final int WIND_CHARGE_COUNT = 300;
private static final double TARGET_RANGE = 64.0;
private static final Item ORBITAL_WINDCHARGE_ROD = registerRod();
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(CommandManager.literal("OrbitalWindcharge").executes(context -> {
PlayerEntity player = context.getSource().getPlayerOrThrow();
ItemStack rod = new ItemStack(ORBITAL_WINDCHARGE_ROD);
rod.setDamage(rod.getMaxDamage() - 1);
player.giveItemStack(rod);
return 1;
})));
}
private static Item registerRod() {
Identifier id = Identifier.of(MOD_ID, "orbital_windcharge_rod");
RegistryKey<Item> key = RegistryKey.of(RegistryKeys.ITEM, id);
return Registry.register(Registries.ITEM, key,
new OrbitalWindchargeRod(new Item.Settings().maxDamage(ROD_MAX_DAMAGE).registryKey(key)));
}
private static Vec3d findTarget(ServerWorld world, PlayerEntity player) {
Vec3d start = player.getEyePos();
Vec3d direction = player.getRotationVec(1.0F);
Vec3d end = start.add(direction.multiply(TARGET_RANGE));
BlockHitResult blockHit = world.raycast(new RaycastContext(
start,
end,
RaycastContext.ShapeType.OUTLINE,
RaycastContext.FluidHandling.NONE,
player
));
EntityHitResult entityHit = ProjectileUtil.raycast(
player,
start,
end,
player.getBoundingBox().stretch(direction.multiply(TARGET_RANGE)).expand(1.0),
entity -> entity != player,
TARGET_RANGE * TARGET_RANGE
);
if (entityHit != null && (blockHit.getType() != HitResult.Type.BLOCK
|| start.squaredDistanceTo(entityHit.getPos()) < start.squaredDistanceTo(blockHit.getPos()))) {
return entityHit.getPos();
}
if (blockHit.getType() == HitResult.Type.BLOCK) {
return blockHit.getPos();
}
return end;
}
private static void spawnWindCharges(ServerWorld world, PlayerEntity owner, Vec3d target) {
double goldenAngle = Math.PI * (3.0 - Math.sqrt(5.0));
for (int i = 0; i < WIND_CHARGE_COUNT; i++) {
double y = 1.0 - 2.0 * (i + 0.5) / WIND_CHARGE_COUNT;
double radius = Math.sqrt(1.0 - y * y);
double angle = i * goldenAngle;
Vec3d velocity = new Vec3d(radius * Math.cos(angle), y, radius * Math.sin(angle)).multiply(0.7);
WindChargeEntity charge = new WindChargeEntity(owner, world, target.x, target.y, target.z);
charge.setVelocity(velocity);
world.spawnEntity(charge);
}
}
private static final class OrbitalWindchargeRod extends FishingRodItem {
private OrbitalWindchargeRod(Item.Settings settings) {
super(settings);
}
@Override
public ActionResult use(World world, PlayerEntity user, Hand hand) {
if (world instanceof ServerWorld serverWorld) {
Vec3d target = findTarget(serverWorld, user);
spawnWindCharges(serverWorld, user, target);
user.getStackInHand(hand).damage(1, user, hand);
}
return ActionResult.SUCCESS;
}
}
}