-
-
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.
+147
−36
Open
Changes from 1 commit
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
104 changes: 70 additions & 34 deletions
104
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,103 @@ | ||
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.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; | ||
|
||
@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.", | ||
"Both use cases will make the entity doing the attacking be the 'attacker' in an " | ||
+ "'on damage' or 'on death' event." | ||
}) | ||
@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 { | ||
|
||
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]]"); | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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; | ||
|
||
@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]; | ||
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; | ||
amount = number.doubleValue() * 2; // hearts | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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 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(); | ||
} | ||
|
||
} |
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.