Skip to content

Commit

Permalink
allow swep event to bypass item ability
Browse files Browse the repository at this point in the history
  • Loading branch information
lcy0x1 committed Sep 16, 2024
1 parent 1dc33f4 commit 86ed748
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
20 changes: 10 additions & 10 deletions patches/net/minecraft/world/entity/player/Player.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
if (p_36347_.getType().is(EntityTypeTags.REDIRECTABLE_PROJECTILE)
&& p_36347_ instanceof Projectile projectile
&& projectile.deflect(ProjectileDeflection.AIM_DEFLECT, this, this, true)) {
@@ -1170,18 +_,26 @@
@@ -1170,18 +_,25 @@
&& !this.isPassenger()
&& p_36347_ instanceof LivingEntity
&& !this.isSprinting();
Expand All @@ -271,18 +271,18 @@
boolean flag2 = false;
double d0 = (double)(this.walkDist - this.walkDistO);
- if (flag4 && !flag1 && !flag && this.onGround() && d0 < (double)this.getSpeed()) {
+ boolean allowSweepPreconditions = flag4 && !flag && this.onGround() && d0 < (double)this.getSpeed();
+ // Neo: the boolean local above (allowSweepPreconditions) is the vanilla sweep attack condition, excluding the condition of not doing critical hit
+ // Fire the sweep attack event based on current information.
+ // The sweep attack event default result will be allowSweep && !critEvent.disableSweep()
+ // The event cannot bypass item ability check
+ var sweepEvent = net.neoforged.neoforge.common.CommonHooks.fireSweepAttack(this, p_36347_, allowSweepPreconditions, critEvent.disableSweep());
+ if (sweepEvent.allowSweep()) {
ItemStack itemstack1 = this.getItemInHand(InteractionHand.MAIN_HAND);
- ItemStack itemstack1 = this.getItemInHand(InteractionHand.MAIN_HAND);
- if (itemstack1.getItem() instanceof SwordItem) {
- flag2 = true;
- }
+ flag2 = itemstack1.canPerformAction(net.neoforged.neoforge.common.ItemAbilities.SWORD_SWEEP);
+ boolean allowSweepPreconditions = flag4 && !flag && this.onGround() && d0 < (double)this.getSpeed();
+ boolean itemSupportSweep = this.getItemInHand(InteractionHand.MAIN_HAND).canPerformAction(net.neoforged.neoforge.common.ItemAbilities.SWORD_SWEEP);
+ // Neo: the boolean local allowSweepPreconditions is the vanilla sweep attack condition, excluding the condition of not doing critical hit
+ // Fire the sweep attack event based on current information.
+ // The sweep attack event default result will be allowSweepPreconditions && itemSupportSweep && !critEvent.disableSweep()
+ var sweepEvent = net.neoforged.neoforge.common.CommonHooks.fireSweepAttack(this, p_36347_, allowSweepPreconditions, itemSupportSweep, critEvent.disableSweep());
+ if (sweepEvent.allowSweep()) {
+ flag2 = true;
}

float f6 = 0.0F;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/neoforged/neoforge/common/CommonHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,11 @@ public static CriticalHitEvent fireCriticalHit(Player player, Entity target, boo
* @param player The attacking player
* @param target The attack target
* @param allowSweepPreconditions If the attack would have been a sweep attack by vanilla's rules in {@link Player#attack(Entity)}, other than the condition of not critical hit.
* @param itemSupportSweep If the weapon support sweep attack
* @param disabledByCrit If the vanilla sweep attack would have been disabled by critical hit
*/
public static SweepAttackEvent fireSweepAttack(Player player, Entity target, boolean allowSweepPreconditions, boolean disabledByCrit) {
return NeoForge.EVENT_BUS.post(new SweepAttackEvent(player, target, allowSweepPreconditions, disabledByCrit));
public static SweepAttackEvent fireSweepAttack(Player player, Entity target, boolean allowSweepPreconditions, boolean itemSupportSweep, boolean disabledByCrit) {
return NeoForge.EVENT_BUS.post(new SweepAttackEvent(player, target, allowSweepPreconditions, itemSupportSweep, disabledByCrit));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public boolean isCriticalHit() {
}

/**
* Changes the critical hit state. If set to true, {@code disableSweep} will also be set to true.
* Changes the critical hit state. If set to true, {@code disableSweep} will also be set to true.<br>
* Don't forget to check {@link Player#getAttackStrengthScale(float)}
*
* @param isCriticalHit true if the attack should critically hit
*/
Expand All @@ -85,7 +86,8 @@ public void setCriticalHit(boolean isCriticalHit) {
}

/**
* Changes the attack to critical hit without disabling sweep
* Changes the attack to critical hit without disabling sweep<br>
* Don't forget to check {@link Player#getAttackStrengthScale(float)}
*/
public void setCriticalHitRetainSweep() {
this.isCriticalHit = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@

public class SweepAttackEvent extends PlayerEvent {
private final Entity target;
private final boolean vanillaAllowSweepPreconditions, disabledByCrit;
private final boolean vanillaAllowSweepPreconditions, itemSupportSweep, disabledByCrit;

private boolean allowSweep;

public SweepAttackEvent(Player player, Entity target, boolean vanillaAllowSweepPreconditions, boolean disabledByCrit) {
public SweepAttackEvent(Player player, Entity target, boolean vanillaAllowSweepPreconditions, boolean itemSupportSweep, boolean disabledByCrit) {
super(player);
this.target = target;
this.vanillaAllowSweepPreconditions = vanillaAllowSweepPreconditions;
this.itemSupportSweep = itemSupportSweep;
this.disabledByCrit = disabledByCrit;
this.allowSweep = vanillaAllowSweepPreconditions && !disabledByCrit;
this.allowSweep = vanillaAllowSweepPreconditions && itemSupportSweep && !disabledByCrit;
}

/**
Expand All @@ -36,6 +37,13 @@ public boolean vanillaAllowSweepPreconditions() {
return vanillaAllowSweepPreconditions;
}

/**
* @return true if the attack weapon support sweep attack.
*/
public boolean itemSupportSweep() {
return itemSupportSweep;
}

/**
* @return true if the sweep attack would be disabled by critical hit
*/
Expand All @@ -47,7 +55,7 @@ public boolean sweepDisabledByCrit() {
* @return true if the attack would have been a sweep attack by vanilla's rules in {@link Player#attack(Entity)}.
*/
public boolean vanillaAllowSweep() {
return vanillaAllowSweepPreconditions && !disabledByCrit;
return vanillaAllowSweepPreconditions && itemSupportSweep && !disabledByCrit;
}

/**
Expand Down

0 comments on commit 86ed748

Please sign in to comment.