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

Adds EffToggleCanPickUpItems + CondCanPickUpItems #5357

Merged
merged 13 commits into from
Jun 24, 2023
44 changes: 44 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondCanPickUpItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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;
import org.bukkit.entity.LivingEntity;

public class CondCanPickUpItems extends PropertyCondition<LivingEntity> {

static {
register(CondCanPickUpItems.class, PropertyType.CAN, "pick (up items|items up)", "livingentities");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean check(LivingEntity livingEntity) {
return livingEntity.getCanPickupItems();
}

@Override
protected PropertyType getPropertyType() {
return PropertyType.CAN;
}

@Override
protected String getPropertyName() {
return "pick up items";
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}
63 changes: 63 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffToggleCanPickUpItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

public class EffToggleCanPickUpItems extends Effect {

static {
Skript.registerEffect(EffToggleCanPickUpItems.class,
"allow %livingentities% to pick (up items|items up)",
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
"[negated:(don't|do not)] let %livingentities% pick (up items|items up)",
"(forbid|disallow) %livingentities% (from|to) pick[ing] (up items|items up)");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<LivingEntity> entityExpr;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
private boolean allowPickUp;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
entityExpr = (Expression<LivingEntity>) exprs[0];
allowPickUp = !(matchedPattern == 2 || parseResult.hasTag("negated"));
return true;
}


sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
@Override
protected void execute(Event event) {
for (LivingEntity entity : entityExpr.getArray(event))
entity.setCanPickupItems(allowPickUp);
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String toString(@Nullable Event event, boolean debug) {
if (allowPickUp)
return "allow " + entityExpr.toString(event, debug) + " to pick up items";
else
return "forbid " + entityExpr.toString(event, debug) + " from picking up items";
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test "entity can pick up items":
spawn a zombie at spawn of "world"
set {_zombie} to last spawned zombie
let {_zombie} pick up items
assert {_zombie} can pick up items with "failed to allow zombie to pick up items"
forbid {_zombie} from picking items up
assert {_zombie} can't pick up items with "failed to disallow zombie to pick up items"
delete {_zombie}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved