Files
src
build.gradle
gradle.properties
settings.gradle
Code
14 786
src/main/java/com/kodari/nanogauntlet/NanoGauntletMod.java
package com.kodari.nanogauntlet;
import dev.emi.trinkets.api.TrinketComponent;
import dev.emi.trinkets.api.TrinketsApi;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.FireBlock;
import net.minecraft.block.TrapdoorBlock;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.condition.RandomChanceLootCondition;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.loot.provider.number.ConstantLootNumberProvider;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
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.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class NanoGauntletMod implements ModInitializer {
public static final String MOD_ID = "nanogauntlet";
public static final NanoGauntletItem NANO_GAUNTLET = Registry.register(
Registries.ITEM,
new Identifier(MOD_ID, "nano_gauntlet"),
new NanoGauntletItem(new net.minecraft.item.Item.Settings().maxCount(1))
);
public static final ItemGroup ITEM_GROUP = Registry.register(
Registries.ITEM_GROUP,
new Identifier(MOD_ID, "main"),
FabricItemGroup.builder()
.displayName(Text.translatable("itemGroup.nanogauntlet.main"))
.icon(() -> new ItemStack(NANO_GAUNTLET))
.entries((context, entries) -> entries.add(NANO_GAUNTLET))
.build()
);
private static final Identifier ACTION_PACKET = new Identifier(MOD_ID, "action");
private static final UUID SPIKES_SPEED_MODIFIER = UUID.fromString("a51c9b73-b6af-4d12-87dc-032e6b8fa092");
private static final Map<UUID, AbilityState> ABILITIES = new HashMap<>();
@Override
public void onInitialize() {
ServerPlayNetworking.registerGlobalReceiver(ACTION_PACKET, (server, player, handler, buf, responseSender) -> {
int action = buf.readVarInt();
server.execute(() -> handleAction(player, action));
});
AttackEntityCallback.EVENT.register((player, world, hand, target, hitResult) -> {
if (world.isClient || hand != Hand.MAIN_HAND) {
return ActionResult.PASS;
}
ItemStack gauntlet = getEquippedStack(player);
if (gauntlet.isEmpty() || !NanoGauntletItem.isActive(gauntlet) || !(target instanceof LivingEntity livingTarget)) {
return ActionResult.PASS;
}
float bonus = NanoGauntletItem.isSpikesMode(gauntlet) ? 5.0F : 3.0F;
livingTarget.damage(player.getDamageSources().playerAttack(player), bonus);
return ActionResult.PASS;
});
AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> {
if (world.isClient || hand != Hand.MAIN_HAND || player.isCreative()) {
return ActionResult.PASS;
}
ItemStack gauntlet = getEquippedStack(player);
if (gauntlet.isEmpty() || !NanoGauntletItem.isActive(gauntlet) || NanoGauntletItem.isSpikesMode(gauntlet)) {
return ActionResult.PASS;
}
BlockState state = world.getBlockState(pos);
if (state.isAir() || state.getHardness(world, pos) < 0.0F) {
return ActionResult.PASS;
}
BlockEntity blockEntity = world.getBlockEntity(pos);
ItemStack silkTouchTool = new ItemStack(Items.DIAMOND_PICKAXE);
silkTouchTool.addEnchantment(Enchantments.SILK_TOUCH, 1);
Block.dropStacks(state, world, pos, blockEntity, player, silkTouchTool);
world.breakBlock(pos, false, player, 512);
return ActionResult.SUCCESS;
});
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
ItemStack gauntlet = getEquippedStack(player);
if (gauntlet.isEmpty() || !NanoGauntletItem.isActive(gauntlet)) {
return ActionResult.PASS;
}
BlockPos pos = hitResult.getBlockPos();
BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof DoorBlock door) {
door.setOpen(player, world, state, pos, !state.get(Properties.OPEN));
return ActionResult.SUCCESS;
}
if (state.getBlock() instanceof TrapdoorBlock) {
world.setBlockState(pos, state.with(Properties.OPEN, !state.get(Properties.OPEN)), 3);
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
});
LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
if (id.equals(new Identifier("minecraft", "chests/end_city_treasure"))) {
tableBuilder.pool(LootPool.builder()
.rolls(ConstantLootNumberProvider.create(1.0F))
.with(ItemEntry.builder(NANO_GAUNTLET))
.conditionally(RandomChanceLootCondition.builder(0.12F)));
}
});
ServerTickEvents.END_SERVER_TICK.register(NanoGauntletMod::tickServer);
}
public static ItemStack getEquippedStack(LivingEntity entity) {
ItemStack[] result = {ItemStack.EMPTY};
TrinketsApi.getTrinketComponent(entity).ifPresent(component -> component.forEach((slot, stack) -> {
if (result[0].isEmpty() && stack.getItem() == NANO_GAUNTLET) {
result[0] = stack;
}
}));
return result[0];
}
public static void sendAction(net.minecraft.client.network.ClientPlayerEntity player, int action) {
var buf = PacketByteBufs.create();
buf.writeVarInt(action);
net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking.send(ACTION_PACKET, buf);
}
private static void handleAction(ServerPlayerEntity player, int action) {
ItemStack stack = getEquippedStack(player);
if (stack.isEmpty()) {
return;
}
AbilityState state = ABILITIES.computeIfAbsent(player.getUuid(), uuid -> new AbilityState());
long now = System.currentTimeMillis();
switch (action) {
case 0 -> {
boolean active = !NanoGauntletItem.isActive(stack);
NanoGauntletItem.setActive(stack, active);
if (!active) {
state.laserStarted = 0L;
state.bStarted = 0L;
state.stuckTarget = null;
state.shockTarget = null;
}
syncStack(player, stack);
}
case 1 -> {
if (NanoGauntletItem.isActive(stack)) {
NanoGauntletItem.setSpikesMode(stack, !NanoGauntletItem.isSpikesMode(stack));
syncStack(player, stack);
}
}
case 2 -> useV(player, stack, state, now);
case 3 -> startB(player, stack, state, now);
case 4 -> releaseB(player, stack, state, now);
default -> {
}
}
}