package online.astradev.weapons.behavior; import java.util.*; import net.minecraft.core.registries.Registries; import net.minecraft.resources.*; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.damagesource.*; import net.minecraft.world.entity.*; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.*; import net.minecraft.world.phys.Vec3; import net.neoforged.bus.api.*; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.entity.player.*; import online.astradev.plus.content.PlusContent; import online.astradev.plus.content.enchantment.CustomEnchantments; import online.astradev.plus.model.ModelCarrier; import online.astradev.weapons.AstraWeapons; import online.astradev.weapons.resource.WeaponPresentation; public final class GauntletCombat { public static final String ID = "astraweapons:gauntlets"; private static final ResourceKey DAMAGE = ResourceKey.create(Registries.DAMAGE_TYPE, Identifier.parse("astraweapons:gauntlet")); private record Chain(ItemStack weapon, long last, int next, int slot, Object dimension) {} private static final Map CHAINS = new HashMap<>(); private static final Map BATCHES = new HashMap<>(); private static final class Batch { final Set accepted = new HashSet<>(); final int step; CustomEnchantments.Attack enchantments; boolean hit; Batch(int step) { this.step = step; } } public static boolean holds(Player p) { return p.getMainHandItem().is(Items.IRON_SWORD) && ID.equals(PlusContent.id(p.getMainHandItem())); } public static boolean striking(Player p) { return BATCHES.containsKey(p.getUUID()); } @SubscribeEvent(priority=EventPriority.LOWEST) public void attack(AttackEntityEvent e) { if (!e.isCanceled() && e.getEntity() instanceof ServerPlayer p && holds(p) && !striking(p)) { e.setCanceled(true); strike(p, e.getTarget()); } } @SubscribeEvent(priority=EventPriority.LOWEST) public void sweep(SweepAttackEvent e) { if (holds(e.getEntity())) { e.setSweeping(false); e.setCanceled(true); } } @SubscribeEvent(priority=EventPriority.HIGHEST) public void useItem(PlayerInteractEvent.RightClickItem e) { if (holds(e.getEntity()) && e.getHand() == InteractionHand.OFF_HAND) e.setCanceled(true); } @SubscribeEvent(priority=EventPriority.HIGHEST) public void useBlock(PlayerInteractEvent.RightClickBlock e) { if (holds(e.getEntity()) && e.getHand() == InteractionHand.OFF_HAND) e.setCanceled(true); } @SubscribeEvent(priority=EventPriority.HIGHEST) public void useEntity(PlayerInteractEvent.EntityInteract e) { if (holds(e.getEntity()) && e.getHand() == InteractionHand.OFF_HAND) e.setCanceled(true); } public static void strike(ServerPlayer p) { strike(p, null); } private static void strike(ServerPlayer p, Entity primary) { if (!holds(p) || striking(p) || p.isSpectator() || !p.isAlive() || !WeaponPresentation.reserveOffhand(p)) return; var config = AstraWeapons.settings(); long now = p.level().getGameTime(); var chain = CHAINS.get(p.getUUID()); if (chain != null && now - chain.last < config.gauntletCooldownTicks) return; if (p.getAttackStrengthScale(0) < .99f) return; var weapon = p.getMainHandItem(); int step = chain != null && chain.weapon == weapon && now - chain.last <= config.comboResetTicks && chain.slot == p.getInventory().getSelectedSlot() && chain.dimension == p.level() ? chain.next : 0; CHAINS.put(p.getUUID(), new Chain(weapon,now,(step+1)%7,p.getInventory().getSelectedSlot(),p.level())); var batch = new Batch(step); double yaw = Math.toRadians(p.getYRot()); Vec3 forward = new Vec3(-Math.sin(yaw),0,Math.cos(yaw)); double reach = Math.min(config.gauntletReach + (step == 6 ? .35 : 0), p.entityInteractionRange()); var targets = p.level().getEntities(p,p.getBoundingBox().inflate(reach), candidate -> { if (ModelCarrier.isHidden(candidate)) return false; Entity owner = ModelCarrier.owner(candidate), target = owner == null ? candidate : owner; return target instanceof LivingEntity && target != p && target.isAlive() && !target.isSpectator() && !(target instanceof net.minecraft.world.entity.decoration.ArmorStand) && !p.isAlliedTo(target) && (!(target instanceof Player other) || p.canHarmPlayer(other)) && candidate.getBoundingBox().distanceToSqr(p.getEyePosition()) <= reach*reach && Combo.within(forward.x,forward.z,candidate.getX()-p.getX(),candidate.getZ()-p.getZ()) && p.hasLineOfSight(candidate); }).stream().map(e -> { Entity owner = ModelCarrier.owner(e); return owner == null ? e : owner; }).distinct().toList(); BATCHES.put(p.getUUID(),batch); boolean sprinting = p.isSprinting(); try { for (var target : targets) { var e = new AttackEntityEvent(p,target); if (target != primary) NeoForge.EVENT_BUS.post(e); if (!e.isCanceled()) batch.accepted.add(target.getId()); } batch.enchantments = CustomEnchantments.beginAttack(p,weapon,false,batch.accepted.size()); for (var target : targets) { if (!batch.accepted.contains(target.getId())) continue; if (weapon.isEmpty() || p.getMainHandItem() != weapon) break; p.setSprinting(sprinting); p.attack(target); } } finally { BATCHES.remove(p.getUUID()); p.onAttack(); if (batch.enchantments != null) batch.enchantments.finish(); } WeaponPresentation.animate(p,Combo.pose(step),Combo.left(step),config.gauntletCooldownTicks); sound(p,step == 6 ? "swing_kick" : step == 5 ? "swing_medium" : "swing_short",.7f); if (batch.hit) sound(p,step == 6 ? "hit_kick" : step == 5 ? "hit_medium" : "hit_light",.8f); // WeaponPresentation sends the accepted hand after the local one-tick input swing. } public static boolean attackAllowed(Player p,Entity t) { return BATCHES.get(p.getUUID()).accepted.contains(t.getId()); } public static boolean hurt(Player p,Entity t,DamageSource source,float damage) { Batch batch = BATCHES.get(p.getUUID()); var ownSource = new DamageSource(p.registryAccess().lookupOrThrow(Registries.DAMAGE_TYPE).getOrThrow(DAMAGE),p); float amount = (float)(damage * Combo.multiplier(batch.step)); if (batch.enchantments != null) amount = batch.enchantments.modifyDamage(t,amount); boolean hit = t.hurtOrSimulate(ownSource,amount); if (hit && t instanceof LivingEntity living) { batch.hit = true; if (batch.enchantments != null) batch.enchantments.hit(living); } return hit; } private static void sound(ServerPlayer p,String name,float volume) { p.level().playSound(null,p.getX(),p.getY(),p.getZ(),net.minecraft.sounds.SoundEvent.createVariableRangeEvent( Identifier.parse("astraweapons:gauntlets."+name)),p.getSoundSource(),volume,1f); } public static void tick(MinecraftServer server) { CHAINS.entrySet().removeIf(e -> { var p = server.getPlayerList().getPlayer(e.getKey()); return p == null || !holds(p) || !p.isAlive() || p.level() != e.getValue().dimension || p.getMainHandItem() != e.getValue().weapon || p.getInventory().getSelectedSlot() != e.getValue().slot || p.level().getGameTime()-e.getValue().last > AstraWeapons.settings().comboResetTicks; }); } public static void close() { CHAINS.clear(); BATCHES.clear(); } }