Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 257
src/main/java/com/kodari/tnthighlight/TntHighlightMod.java
package com.kodari.tnthighlight;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.block.AbstractRailBlock;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
import net.minecraft.entity.vehicle.TntMinecartEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.BowItem;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.world.World;
public class TntHighlightMod implements ModInitializer {
public static final String MOD_ID = "tnthighlight";
private static final List<PendingRailPlacement> PENDING_PLACEMENTS = new ArrayList<>();
@Override
public void onInitialize() {
UseBlockCallback.EVENT.register(TntHighlightMod::onUseBlock);
ServerTickEvents.END_SERVER_TICK.register(server -> processPendingPlacements());
}
private static ActionResult onUseBlock(
net.minecraft.entity.player.PlayerEntity player,
World world,
net.minecraft.util.Hand hand,
BlockHitResult hitResult) {
if (!(world instanceof ServerWorld serverWorld) || player.isSpectator()) {
return ActionResult.PASS;
}
ItemStack heldStack = player.getStackInHand(hand);
if (!(heldStack.getItem() instanceof BlockItem blockItem)
|| !(blockItem.getBlock() instanceof AbstractRailBlock)) {
return ActionResult.PASS;
}
BlockPos clickedPos = hitResult.getBlockPos();
BlockPos adjacentPos = clickedPos.offset(hitResult.getSide());
queuePlacement(serverWorld, clickedPos, player.getUuid());
if (!adjacentPos.equals(clickedPos)) {
queuePlacement(serverWorld, adjacentPos, player.getUuid());
}
return ActionResult.PASS;
}
private static void queuePlacement(ServerWorld world, BlockPos pos, UUID playerId) {
BlockState previousState = world.getBlockState(pos);
if (previousState.getBlock() instanceof AbstractRailBlock) {
return;
}
PENDING_PLACEMENTS.add(new PendingRailPlacement(world, pos.toImmutable(), previousState, playerId));
}
private static void processPendingPlacements() {
for (int index = PENDING_PLACEMENTS.size() - 1; index >= 0; index--) {
PendingRailPlacement placement = PENDING_PLACEMENTS.remove(index);
BlockState currentState = placement.world().getBlockState(placement.pos());
if (!(currentState.getBlock() instanceof AbstractRailBlock)
|| placement.previousState().getBlock() instanceof AbstractRailBlock) {
continue;
}
ServerPlayerEntity player = placement.world().getServer()
.getPlayerManager().getPlayer(placement.playerId());
if (player == null || player.isSpectator() || player.getEntityWorld() != placement.world()) {
continue;
}
placeAndFireTntMinecart(placement.world(), player, placement.pos());
}
}
private static void placeAndFireTntMinecart(ServerWorld world, ServerPlayerEntity player, BlockPos railPos) {
ItemStack minecartStack = findHotbarItem(player, Items.TNT_MINECART);
ItemStack bowStack = findHotbarBow(player);
ItemStack arrowStack = findHotbarItem(player, Items.ARROW);
if (minecartStack == null || bowStack == null || arrowStack == null) {
return;
}
Box minecartBox = new Box(railPos).expand(0.35, 0.25, 0.35);
if (!world.getEntitiesByClass(AbstractMinecartEntity.class, minecartBox, entity -> true).isEmpty()) {
return;
}
TntMinecartEntity minecart = EntityType.TNT_MINECART.create(world, SpawnReason.TRIGGERED);
ArrowEntity arrow = EntityType.ARROW.create(world, SpawnReason.TRIGGERED);
if (minecart == null || arrow == null) {
return;
}
Vec3d minecartPosition = new Vec3d(
railPos.getX() + 0.5,
railPos.getY() + 0.0625,
railPos.getZ() + 0.5);
minecart.refreshPositionAndAngles(minecartPosition.x, minecartPosition.y, minecartPosition.z, 0.0f, 0.0f);
if (!world.spawnEntity(minecart)) {
return;
}
Vec3d direction = minecartPosition.subtract(player.getEyePos()).normalize();
arrow.setOwner(player);
arrow.refreshPositionAndAngles(player.getX(), player.getEyeY(), player.getZ(), player.getYaw(), player.getPitch());
arrow.setVelocity(direction.x, direction.y, direction.z, 3.0f, 0.0f);
arrow.setOnFireFor(100);
world.spawnEntity(arrow);
minecartStack.decrement(1);
arrowStack.decrement(1);
bowStack.damage(1, player, net.minecraft.entity.EquipmentSlot.MAINHAND);
}
private static ItemStack findHotbarItem(ServerPlayerEntity player, net.minecraft.item.Item item) {
for (int slot = 0; slot < 9; slot++) {
ItemStack stack = player.getInventory().getStack(slot);
if (stack.isOf(item)) {
return stack;
}
}
return null;
}
private static ItemStack findHotbarBow(ServerPlayerEntity player) {
for (int slot = 0; slot < 9; slot++) {
ItemStack stack = player.getInventory().getStack(slot);
if (stack.getItem() instanceof BowItem) {
return stack;
}
}
return null;
}
private record PendingRailPlacement(ServerWorld world, BlockPos pos, BlockState previousState, UUID playerId) {
}
}