Files
src
build.gradle
gradle.properties
settings.gradle
Code
10 244
src/main/java/com/pearlcatcher/PearlCatcher.java
package com.pearlcatcher;
import com.pearlcatcher.network.ThrowPearlPayload;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.projectile.thrown.EnderPearlEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class PearlCatcher implements ModInitializer {
public static final String MOD_ID = "pearlcatcher";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
/**
* Players whose next pearl teleport should trigger a wind charge.
* Populated when the keybind packet is received; consumed in EnderPearlMixin.
*/
public static final Set<UUID> PENDING_WIND_CHARGE =
Collections.newSetFromMap(new ConcurrentHashMap<>());
@Override
public void onInitialize() {
PayloadTypeRegistry.playC2S().register(ThrowPearlPayload.ID, ThrowPearlPayload.CODEC);
ServerPlayNetworking.registerGlobalReceiver(ThrowPearlPayload.ID, (payload, context) -> {
ServerPlayerEntity player = context.player();
ServerWorld world = player.getServerWorld();
if (player.isSpectator() || player.isCreative()) return;
// Consume one pearl from inventory (survival check)
boolean foundPearl = false;
for (int i = 0; i < player.getInventory().size(); i++) {
ItemStack stack = player.getInventory().getStack(i);
if (stack.isOf(Items.ENDER_PEARL)) {
stack.decrement(1);
foundPearl = true;
break;
}
}
if (!foundPearl) return;
// Mark this player for wind-charge follow-up when the pearl lands
PENDING_WIND_CHARGE.add(player.getUuid());
// Throw the pearl
EnderPearlEntity pearl = new EnderPearlEntity(world, player, new ItemStack(Items.ENDER_PEARL));
pearl.setVelocity(player, player.getPitch(), player.getYaw(), 0.0f, 2.5f, 1.0f);
world.spawnEntity(pearl);
});
}
}