-
-
Notifications
You must be signed in to change notification settings - Fork 394
EffForceAttack Damage Entity By Amount #7878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Absolutionism
wants to merge
11
commits into
SkriptLang:dev/feature
Choose a base branch
from
Absolutionism:dev/EntityDamageEntityAmount
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
63f7053
Update EffForceAttack.java
Absolutionism 0986882
Update EffForceAttack.java
Absolutionism ddb43b4
Create EffForceAttack.sk
Absolutionism 2cd9760
Update EffForceAttack.java
Absolutionism 2d1c1a2
Edge Cases
Absolutionism ff8be03
Merge branch 'dev/feature' into dev/EntityDamageEntityAmount
Absolutionism e904736
Merge branch 'dev/feature' into dev/EntityDamageEntityAmount
Absolutionism 76f7f0a
Merge branch 'dev/feature' into dev/EntityDamageEntityAmount
Absolutionism 6ce1471
Merge branch 'dev/feature' into dev/EntityDamageEntityAmount
Absolutionism 5f68de4
Merge branch 'dev/feature' into dev/EntityDamageEntityAmount
Efnilite 46fcb8d
Merge branch 'dev/feature' into dev/EntityDamageEntityAmount
Absolutionism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 86 additions & 36 deletions
122
src/main/java/ch/njol/skript/effects/EffForceAttack.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,117 @@ | ||
package ch.njol.skript.effects; | ||
|
||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.RequiredPlugins; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.config.Node; | ||
import ch.njol.skript.doc.*; | ||
import ch.njol.skript.lang.Effect; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.SyntaxStringBuilder; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.entity.Damageable; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; | ||
|
||
@Name("Force Attack") | ||
@Description("Makes a living entity attack an entity with a melee attack.") | ||
@Description({ | ||
"Makes a living entity attack an entity with a melee attack.", | ||
"Using 'attack' will make the attacker use the item in their main hand " | ||
+ "and will apply extra data from the item, including enchantments and attributes.", | ||
"Using 'damage' with a number of hearts will not account for the item in the main hand " | ||
+ "and will always be the number provided." | ||
}) | ||
@Example(""" | ||
spawn a wolf at location(0, 0, 0) | ||
make last spawned wolf attack all players | ||
""") | ||
@Example(""" | ||
spawn a zombie at location(0, 0, 0) | ||
make player damage last spawned zombie by 2 | ||
""") | ||
@Examples({"spawn a wolf at player's location", | ||
"make last spawned wolf attack player"}) | ||
@Since("2.5.1") | ||
@Since("2.5.1, INSERT VERSION (multiple, amount)") | ||
@RequiredPlugins("Minecraft 1.15.2+") | ||
public class EffForceAttack extends Effect { | ||
public class EffForceAttack extends Effect implements SyntaxRuntimeErrorProducer { | ||
|
||
static { | ||
Skript.registerEffect(EffForceAttack.class, | ||
"make %livingentities% attack %entity%", | ||
"force %livingentities% to attack %entity%"); | ||
"make %livingentities% attack %entities%", | ||
"force %livingentities% to attack %entities%", | ||
"make %livingentities% damage %entities% by %number% [heart[s]]", | ||
"force %livingentities% to damage %entities% by %number% [heart[s]]"); | ||
} | ||
|
||
private static final boolean ATTACK_IS_SUPPORTED = Skript.methodExists(LivingEntity.class, "attack", Entity.class); | ||
|
||
@SuppressWarnings("null") | ||
private Expression<LivingEntity> entities; | ||
@SuppressWarnings("null") | ||
private Expression<Entity> target; | ||
|
||
private Expression<LivingEntity> attackers; | ||
private Expression<Entity> victims; | ||
private @Nullable Expression<Number> amount; | ||
private Node node; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
if (!ATTACK_IS_SUPPORTED) { | ||
Skript.error("The force attack effect requires Minecraft 1.15.2 or newer"); | ||
return false; | ||
} | ||
entities = (Expression<LivingEntity>) exprs[0]; | ||
target = (Expression<Entity>) exprs[1]; | ||
attackers = (Expression<LivingEntity>) exprs[0]; | ||
victims = (Expression<Entity>) exprs[1]; | ||
if (matchedPattern >= 2) | ||
amount = (Expression<Number>) exprs[2]; | ||
node = getParser().getNode(); | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void execute(Event e) { | ||
Entity target = this.target.getSingle(e); | ||
if (target != null) { | ||
for (LivingEntity entity : entities.getArray(e)) { | ||
entity.attack(target); | ||
protected void execute(Event event) { | ||
Double amount = null; | ||
if (this.amount != null) { | ||
Number number = this.amount.getSingle(event); | ||
if (number == null) | ||
return; | ||
Double preAmount = number.doubleValue(); | ||
if (preAmount <= 0) { | ||
error("Cannot damage an entity by 0 or less. Consider healing instead."); | ||
return; | ||
} else if (!Double.isFinite(preAmount)) { | ||
return; | ||
} | ||
amount = preAmount * 2; // hearts | ||
} | ||
|
||
LivingEntity[] attackers = this.attackers.getArray(event); | ||
Entity[] victims = this.victims.getArray(event); | ||
if (amount == null) { | ||
for (Entity victim : victims) { | ||
for (LivingEntity attacker : attackers) { | ||
attacker.attack(victim); | ||
} | ||
} | ||
} else { | ||
for (Entity victim : victims) { | ||
if (!(victim instanceof Damageable damageable)) | ||
continue; | ||
for (LivingEntity attacker : attackers) { | ||
damageable.damage(amount, attacker); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event e, boolean debug) { | ||
return "make " + entities.toString(e, debug) + " attack " + target.toString(e, debug); | ||
public Node getNode() { | ||
return node; | ||
} | ||
|
||
@Override | ||
public String toString(Event event, boolean debug) { | ||
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); | ||
builder.append("make", attackers); | ||
if (amount == null) { | ||
builder.append("attack", victims); | ||
} else { | ||
builder.append("damage", victims, "by", amount); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
test "force attack": | ||
spawn a zombie at test-location: | ||
set {_attacker} to entity | ||
spawn a pig at test-location: | ||
set {_victim} to entity | ||
|
||
set {_max} to the max health of {_victim} | ||
make {_attacker} attack {_victim} | ||
assert the health of {_victim} < {_max} with "Victim should have taken damage" | ||
assert the last damage of {_victim} is 1.5 with "Damage taken should be 1.5" | ||
|
||
clear entity within {_attacker} | ||
clear entity within {_victim} | ||
|
||
test "entity damage entity by amount": | ||
spawn a zombie at test-location: | ||
set {_attacker} to entity | ||
spawn a pig at test-location: | ||
set {_victim} to entity | ||
|
||
set {_max} to the max health of {_victim} | ||
make {_attacker} damage {_victim} by 5 | ||
assert the health of {_victim} < {_max} with "Victim should have taken damage" | ||
assert the last damage of {_victim} is 5 with "Damage taken should be 5" | ||
|
||
clear entity within {_attacker} | ||
clear entity within {_victim} | ||
|
||
test "entity damage entity by negative": | ||
spawn a zombie at test-location: | ||
set {_attacker} to entity | ||
spawn a pig at test-location: | ||
set {_victim} to entity | ||
|
||
set {_max} to the max health of {_victim} | ||
# TODO: Catch Runtime Error | ||
make {_attacker} damage {_victim} by -2 | ||
assert the health of {_victim} is {_max} with "Victim's health should be max - negative value" | ||
assert the last damage of {_victim} is 0 with "Victim should not have taken damage - negative value" | ||
|
||
clear entity within {_attacker} | ||
clear entity within {_victim} | ||
|
||
test "entity damage entity by infinity/NaN": | ||
spawn a zombie at test-location: | ||
set {_attacker} to entity | ||
spawn a pig at test-location: | ||
set {_victim} to entity | ||
|
||
set {_max} to the max health of {_victim} | ||
make {_attacker} damage {_victim} by infinity value | ||
assert the health of {_victim} is {_max} with "Victim's health should be max - infinity value" | ||
assert the last damage of {_victim} is 0 with "Victim should not have taken damage - infinity value" | ||
|
||
make {_attacker} damage {_victim} by NaN value | ||
assert the health of {_victim} is {_max} with "Victim's health should be max - NaN value" | ||
assert the last damage of {_victim} is 0 with "Victim should not have taken damage - NaN value" | ||
|
||
clear entity within {_attacker} | ||
clear entity within {_victim} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.