Files
src
build.gradle
settings.gradle
Code
11 475
src/main/java/com/kodari/buildertools/BuilderPowerMod.java
package com.kodari.buildertools;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.mob.WitchEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.gen.structure.Structure;
import net.minecraft.server.world.ServerWorld;
public class BuilderPowerMod implements ModInitializer {
public static final String MOD_ID = "builderpower";
public static final TagKey<Structure> SWAMP_HUT_TAG = TagKey.of(
RegistryKeys.STRUCTURE,
Identifier.of(MOD_ID, "swamp_hut")
);
private static PowerManager powerManager;
@Override
public void onInitialize() {
powerManager = new PowerManager();
PayloadTypeRegistry.playC2S().register(ActivatePowerPayload.ID, ActivatePowerPayload.CODEC);
ServerPlayNetworking.registerGlobalReceiver(ActivatePowerPayload.ID, (payload, context) ->
context.server().execute(() -> powerManager.activate(context.player())));
UseItemCallback.EVENT.register((player, world, hand) -> {
ItemStack stack = player.getStackInHand(hand);
if (!BuilderItems.isAmulet(stack)) {
return TypedActionResult.pass(stack);
}
if (hand != Hand.MAIN_HAND) {
return TypedActionResult.fail(stack);
}
if (!world.isClient && player instanceof ServerPlayerEntity serverPlayer) {
stack.decrement(1);
powerManager.openSelection(serverPlayer);
}
return TypedActionResult.success(stack, world.isClient());
});
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
if (!(entity instanceof WitchEntity witch) || !(witch.getWorld() instanceof ServerWorld serverWorld)) {
return;
}
BlockPos hut = serverWorld.locateStructure(SWAMP_HUT_TAG, witch.getBlockPos(), 32, false);
if (hut == null || hut.getSquaredDistance(witch.getPos()) > 32 * 32) {
return;
}
if (witch.getRandom().nextFloat() < 0.15f) {
witch.dropStack(BuilderItems.createAmulet());
}
});
}
public static PowerManager powerManager() {
return powerManager;
}
}