Skip to content

Commit

Permalink
Update charged syntax (#6860)
Browse files Browse the repository at this point in the history
* Update CondIsCharged

* Adds new test and updates effect
- Also deletes old test

* Fixes test
- Wither skull wasn't spawning as I thought it was, changed to shoot effect

* Apply suggestions from code review

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>

* Suggestions

---------

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
Co-authored-by: Moderocky <admin@moderocky.com>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent de1a529 commit 706c82f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 43 deletions.
32 changes: 17 additions & 15 deletions src/main/java/ch/njol/skript/conditions/CondIsCharged.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,38 @@
*/
package ch.njol.skript.conditions;

import org.bukkit.entity.Creeper;
import org.bukkit.entity.LivingEntity;

import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.WitherSkull;

@Name("Is Charged")
@Description("Checks if a creeper is charged (powered).")
@Description("Checks if a creeper or wither skull is charged (powered).")
@Examples({"if the last spawned creeper is charged:",
"\tbroadcast \"A charged creeper is at %location of last spawned creeper%\""})
@Since("2.5")
public class CondIsCharged extends PropertyCondition<LivingEntity> {
"\tbroadcast \"A charged creeper is at %location of last spawned creeper%\""})
@Since("2.5, INSERT VERSION (wither skulls)")
public class CondIsCharged extends PropertyCondition<Entity> {

static {
register(CondIsCharged.class, "(charged|powered)", "livingentities");
register(CondIsCharged.class, "(charged|powered)", "entities");
}

@Override
public boolean check(final LivingEntity e) {
if (e instanceof Creeper)
return ((Creeper) e).isPowered();
public boolean check(Entity entity) {
if (entity instanceof Creeper)
return ((Creeper) entity).isPowered();
else if (entity instanceof WitherSkull)
return ((WitherSkull) entity).isCharged();
return false;
}

@Override
protected String getPropertyName() {
return "charged";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,54 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

import org.bukkit.entity.Creeper;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Entity;
import org.bukkit.entity.WitherSkull;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

@Name("Charge Creeper")
@Description("Charges or uncharges a creeper. A creeper is charged when it has been struck by lightning.")
@Examples({"on spawn of creeper:",
"\tcharge the event-entity"})
@Name("Charge Entity")
@Description("Charges or uncharges a creeper or wither skull. A creeper is charged when it has been struck by lightning.")
@Examples({
"on spawn of creeper:",
"\tcharge the event-entity"
})
@Since("2.5")
public class EffChargeCreeper extends Effect {
public class EffCharge extends Effect {

static {
Skript.registerEffect(EffChargeCreeper.class,
"make %livingentities% [a[n]] (charged|powered|1¦((un|non[-])charged|(un|non[-])powered)) [creeper[s]]",
"(charge|power|1¦(uncharge|unpower)) %livingentities%");
Skript.registerEffect(EffCharge.class,
"make %entities% [un:(un|not |non[-| ])](charged|powered)",
"[:un](charge|power) %entities%");
}

@SuppressWarnings("null")
private Expression<LivingEntity> entities;
private Expression<Entity> entities;

private boolean charge;

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
entities = (Expression<LivingEntity>) exprs[0];
charge = parseResult.mark != 1;
entities = (Expression<Entity>) exprs[0];
charge = !parseResult.hasTag("un");
return true;
}

@Override
protected void execute(Event e) {
for (LivingEntity le : entities.getArray(e)) {
if (le instanceof Creeper)
((Creeper) le).setPowered(charge);
protected void execute(Event event) {
for (Entity entity : entities.getArray(event)) {
if (entity instanceof Creeper) {
((Creeper) entity).setPowered(charge);
} else if (entity instanceof WitherSkull) {
((WitherSkull) entity).setCharged(charge);
}
}
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "make " + entities.toString(e, debug) + (charge == true ? " charged" : " not charged");
public String toString(@Nullable Event event, boolean debug) {
return "make " + entities.toString(event, debug) + (charge ? " charged" : " not charged");
}

}
31 changes: 31 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondIsCharged.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
test "is charged":
spawn a creeper at (spawn of world "world"):
set {_e} to entity
shoot a wither skull from {_e}
set {_w} to last shot entity

assert {_e} is not charged with "a normally spawned creeper should not be charged"
assert {_w} is not charged with "a normally spawned wither skull should not be charged"

charge {_e}
charge {_w}
assert {_e} is charged with "charging a creeper should do exactly that"
assert {_w} is charged with "charging a wither skull should do exactly that"

uncharge {_e}
uncharge {_w}
assert {_e} is not charged with "uncharging a creeper should do exactly that"
assert {_w} is not charged with "uncharging a wither skull should do exactly that"

spawn an adult zombie at (spawn of world "world"):
set {_z} to entity

assert {_z} is not charged with "a non-creeper/wither skull should never be charged"
charge {_z}
assert {_z} is not charged with "charging a non-creeper/wither skull should do nothing"
uncharge {_z}
assert {_z} is not charged with "uncharging a non-creeper/wither skull should do nothing"

delete entity within {_e}
delete entity within {_w}
delete entity within {_z}
8 changes: 0 additions & 8 deletions src/test/skript/tests/syntaxes/effects/EffChargeCreeper.sk

This file was deleted.

0 comments on commit 706c82f

Please sign in to comment.