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
60 changes: 60 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,60 @@
/**
* 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 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.LivingEntity;

@Name("Can Pick Up Items")
@Description("Whether living entities are able to pick up items off the ground or not.")
@Examples({
"if player can pick items up:",
"\tsend \"You can pick up items!\" to player",
"",
"on drop:",
"\tif player can't pick up items:",
"\t\tsend \"Be careful, you won't be able to pick that up!\" to player"
})
@Since("INSERT VERSION")
public class CondCanPickUpItems extends PropertyCondition<LivingEntity> {

static {
register(CondCanPickUpItems.class, PropertyType.CAN, "pick([ ]up items| items up)", "livingentities");
}

@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

}
79 changes: 79 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,79 @@
/**
* 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.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Toggle Picking Up Items")
@Description("Determines whether living entities are able to pick up items or not")
@Examples({
"forbid player from picking up items",
"send \"You can no longer pick up items!\" to player",
"",
"on drop:",
"\tif player can't pick up items:",
"\t\tallow player to pick up items"
})
@Since("INSERT VERSION")
public class EffToggleCanPickUpItems extends Effect {

static {
Skript.registerEffect(EffToggleCanPickUpItems.class,
"allow %livingentities% to pick([ ]up items| items up)",
"(forbid|disallow) %livingentities% (from|to) pick([ing | ]up items|[ing] items up)");
}

private Expression<LivingEntity> entities;
private boolean allowPickUp;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
entities = (Expression<LivingEntity>) exprs[0];
allowPickUp = matchedPattern == 0;
return true;
}

@Override
protected void execute(Event event) {
for (LivingEntity entity : entities.getArray(event)) {
entity.setCanPickupItems(allowPickUp);
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
if (allowPickUp) {
return "allow " + entities.toString(event, debug) + " to pick up items";
} else {
return "forbid " + entities.toString(event, debug) + " from picking up items";
}
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test "entity can pick up items":
spawn a zombie at spawn of "world":
allow event-entity to pick up items
assert event-entity can pick up items with "failed to allow zombie to pick up items"
forbid event-entity from picking items up
assert event-entity can't pick up items with "failed to disallow zombie to pick up items"
delete event-entity