Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 258
src/client/java/com/kodari/cobwebwater/CobwebWaterClient.java
package com.kodari.cobwebwater;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.block.Blocks;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import java.util.HashSet;
import java.util.Set;
public class CobwebWaterClient implements ClientModInitializer {
private ClientWorld world;
private BlockPos web;
private Hand bucketHand;
private final Set<BlockPos> existingWater = new HashSet<>();
private int remaining;
private int cooldown;
private boolean fireAttempt;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.player == null || client.world == null || client.interactionManager == null) {
world = null;
web = null;
remaining = 0;
cooldown = 0;
fireAttempt = false;
return;
}
if (world != client.world) {
world = client.world;
web = null;
remaining = 0;
cooldown = 0;
fireAttempt = false;
}
if (cooldown > 0) cooldown--;
Hand waterHand = null;
if (client.player.getStackInHand(Hand.MAIN_HAND).isOf(Items.WATER_BUCKET)) waterHand = Hand.MAIN_HAND;
else if (client.player.getStackInHand(Hand.OFF_HAND).isOf(Items.WATER_BUCKET)) waterHand = Hand.OFF_HAND;
Hand lavaHand = null;
if (client.player.getStackInHand(Hand.MAIN_HAND).isOf(Items.LAVA_BUCKET)) lavaHand = Hand.MAIN_HAND;
else if (client.player.getStackInHand(Hand.OFF_HAND).isOf(Items.LAVA_BUCKET)) lavaHand = Hand.OFF_HAND;
if (cooldown == 0 && lavaHand != null) {
Vec3d eye = client.player.getEyePos();
double range = client.player.getBlockInteractionRange();
for (var other : world.getPlayers()) {
if (other == client.player || !other.isAlive() || other.isSpectator()) continue;
Box box = other.getBoundingBox();
for (BlockPos pos : BlockPos.iterate(box)) {
if (!new Box(pos).intersects(box) || !world.getBlockState(pos).isOf(Blocks.COBWEB)) continue;
for (Direction direction : Direction.values()) {
Vec3d target = Vec3d.ofCenter(pos.offset(direction));
double dx = target.x - eye.x;
double dy = target.y - eye.y;
double dz = target.z - eye.z;
if (dx * dx + dy * dy + dz * dz > range * range) continue;
BlockHitResult hit = world.raycast(new RaycastContext(eye, target,
RaycastContext.ShapeType.OUTLINE, RaycastContext.FluidHandling.NONE, client.player));
if (hit.getType() != HitResult.Type.BLOCK
|| !hit.getBlockPos().offset(hit.getSide()).equals(pos)) continue;
float yaw = client.player.getYaw();
float pitch = client.player.getPitch();
client.player.setYaw((float) Math.toDegrees(Math.atan2(-dx, dz)));
client.player.setPitch((float) -Math.toDegrees(Math.atan2(dy, Math.sqrt(dx * dx + dz * dz))));
try {
client.interactionManager.interactItem(client.player, lavaHand);
} finally {
client.player.setYaw(yaw);
client.player.setPitch(pitch);
}
cooldown = 5;
return;
}
}
}
}
if (!fireAttempt && cooldown == 0 && waterHand != null
&& client.player.isOnFire() && client.player.getPitch() >= 60.0f) {
web = client.player.getBlockPos().toImmutable();
bucketHand = waterHand;
remaining = 60;
fireAttempt = true;
existingWater.clear();
for (BlockPos nearby : BlockPos.iterate(web.getX() - 2, web.getY() - 2, web.getZ() - 2,
web.getX() + 2, web.getY() + 2, web.getZ() + 2)) {
if (world.getFluidState(nearby).isOf(Fluids.WATER)) existingWater.add(nearby.toImmutable());
}
client.interactionManager.interactItem(client.player, bucketHand);
return;
}
if (!fireAttempt && web == null && cooldown == 0 && waterHand != null
&& client.player.getPitch() >= 60.0f) {
Box box = client.player.getBoundingBox();
for (BlockPos pos : BlockPos.iterate(box)) {
if (new Box(pos).intersects(box) && world.getBlockState(pos).isOf(Blocks.COBWEB)) {
web = pos.toImmutable();
bucketHand = waterHand;
remaining = 60;
existingWater.clear();
for (BlockPos nearby : BlockPos.iterate(web.getX() - 2, web.getY() - 2, web.getZ() - 2,
web.getX() + 2, web.getY() + 2, web.getZ() + 2)) {
if (world.getFluidState(nearby).isOf(Fluids.WATER)) existingWater.add(nearby.toImmutable());
}
client.interactionManager.interactItem(client.player, bucketHand);
cooldown = 5;
return;
}
}
}
if (web == null || remaining-- <= 0) {
web = null;
fireAttempt = false;
return;
}
if (cooldown > 0 || (fireAttempt && client.player.isOnFire())
|| (!fireAttempt && world.getBlockState(web).isOf(Blocks.COBWEB))
|| !client.player.getStackInHand(bucketHand).isOf(Items.BUCKET)) return;
Vec3d eye = client.player.getEyePos();
BlockPos target = null;
double closest = 20.25;
for (BlockPos pos : BlockPos.iterate(web.getX() - 2, web.getY() - 2, web.getZ() - 2,
web.getX() + 2, web.getY() + 2, web.getZ() + 2)) {
if (!existingWater.contains(pos) && world.getFluidState(pos).isOf(Fluids.WATER)
&& world.getFluidState(pos).isStill()) {
double dx = pos.getX() + 0.5 - eye.x;
double dy = pos.getY() + 0.5 - eye.y;
double dz = pos.getZ() + 0.5 - eye.z;
double distance = dx * dx + dy * dy + dz * dz;
if (distance < closest) {
closest = distance;
target = pos.toImmutable();
}
}
}
if (target == null) return;
double dx = target.getX() + 0.5 - eye.x;
double dy = target.getY() + 0.5 - eye.y;
double dz = target.getZ() + 0.5 - eye.z;
float yaw = client.player.getYaw();
float pitch = client.player.getPitch();
client.player.setYaw((float) Math.toDegrees(Math.atan2(-dx, dz)));
client.player.setPitch((float) -Math.toDegrees(Math.atan2(dy, Math.sqrt(dx * dx + dz * dz))));
try {
client.interactionManager.interactItem(client.player, bucketHand);
} finally {
client.player.setYaw(yaw);
client.player.setPitch(pitch);
}
cooldown = 5;
if (fireAttempt && client.player.getStackInHand(bucketHand).isOf(Items.WATER_BUCKET)) {
fireAttempt = false;
web = null;
}
});
}
}