Skip to content
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

Updates EffCharge and CondIsCharged #7013

Merged
merged 7 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions src/main/java/ch/njol/skript/conditions/CondIsCharged.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.conditions;

import ch.njol.skript.conditions.base.PropertyCondition;
Expand All @@ -25,13 +7,14 @@
import ch.njol.skript.doc.Since;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Wither;
import org.bukkit.entity.WitherSkull;

@Name("Is Charged")
@Description("Checks if a creeper or wither skull is charged (powered).")
@Description("Checks if a creeper, wither, 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, INSERT VERSION (wither skulls)")
@Since("2.5, INSERT VERSION (withers, wither skulls)")
public class CondIsCharged extends PropertyCondition<Entity> {

static {
Expand All @@ -40,10 +23,13 @@ public class CondIsCharged extends PropertyCondition<Entity> {

@Override
public boolean check(Entity entity) {
if (entity instanceof Creeper)
return ((Creeper) entity).isPowered();
else if (entity instanceof WitherSkull)
return ((WitherSkull) entity).isCharged();
if (entity instanceof Creeper creeper) {
return creeper.isPowered();
} else if (entity instanceof WitherSkull witherSkull) {
return witherSkull.isCharged();
} else if (entity instanceof Wither wither) {
return wither.isCharged();
}
return false;
}

Expand Down
33 changes: 7 additions & 26 deletions src/main/java/ch/njol/skript/effects/EffCharge.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
Expand All @@ -39,7 +21,7 @@
"on spawn of creeper:",
"\tcharge the event-entity"
})
@Since("2.5")
@Since("2.5, INSERT VERSION (wither skulls)")
public class EffCharge extends Effect {

static {
Expand All @@ -48,9 +30,8 @@ public class EffCharge extends Effect {
"[:un](charge|power) %entities%");
}

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

private boolean charge;

@SuppressWarnings({"unchecked", "null"})
Expand All @@ -64,11 +45,11 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
@Override
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);
}
if (entity instanceof Creeper creeper) {
creeper.setPowered(charge);
} else if (entity instanceof WitherSkull witherSkull) {
witherSkull.setCharged(charge);
}
}
}

Expand Down
38 changes: 21 additions & 17 deletions src/test/skript/tests/syntaxes/conditions/CondIsCharged.sk
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
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
set {_c} to entity
spawn a wither at (spawn of world "world"):
set {_w} to entity
shoot a wither skull from {_w}
set {_s} 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"
assert {_c} is not charged with "a normally spawned creeper should not be charged"
assert {_w} is not charged with "a normally spawned wither should not be charged"
assert {_s} 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"
charge {_c}
charge {_s}
assert {_c} is charged with "charging a creeper should do exactly that"
assert {_s} 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"
uncharge {_c}
uncharge {_s}
assert {_c} is not charged with "uncharging a creeper should do exactly that"
assert {_s} 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"
assert {_z} is not charged with "a non-supported entity should never be charged"
charge {_z}
assert {_z} is not charged with "charging a non-creeper/wither skull should do nothing"
assert {_z} is not charged with "charging a non-supported entity should do nothing"
uncharge {_z}
assert {_z} is not charged with "uncharging a non-creeper/wither skull should do nothing"
assert {_z} is not charged with "uncharging a non-supported entity should do nothing"

delete entity within {_e}
delete entity within {_c}
delete entity within {_w}
delete entity within {_s}
delete entity within {_z}