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

Enhance Shearing Related Elements #5571

Merged
merged 15 commits into from
Sep 18, 2023
71 changes: 71 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsSheared.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* 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.Skript;
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.RequiredPlugins;
import ch.njol.skript.doc.Since;
import io.papermc.paper.entity.Shearable;
import org.bukkit.entity.Cow;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Sheep;
import org.bukkit.entity.Snowman;

@Name("Entity Is Sheared")
@Description("Checks whether an entity is sheared. For below 1.19.4 users, this condition only works on sheeps and snowmen.")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"if targeted entity of player is sheared:",
"\tsend \"This entity has nothing left to shear!\" to player"
})
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
@Since("INSERT VERSION")
@RequiredPlugins("Paper 1.19.4+ (shearable)")
public class CondIsSheared extends PropertyCondition<LivingEntity> {

private static final boolean interfaceMethod = Skript.classExists("io.papermc.paper.entity.Shearable");

static {
register(CondIsSheared.class, "(sheared|shorn)", "livingentities");
}

@Override
public boolean check(LivingEntity entity) {
if (entity instanceof Cow) // As sheared mushroom cow is a Cow which does not implements Shearable
return true;
if (interfaceMethod) {
if (!(entity instanceof Shearable))
return false;
return !((Shearable) entity).readyToBeSheared();
}
if (entity instanceof Sheep)
return ((Sheep) entity).isSheared();
if (entity instanceof Snowman)
return ((Snowman) entity).isDerp();
return false;
}

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

}
73 changes: 49 additions & 24 deletions src/main/java/ch/njol/skript/effects/EffShear.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
package ch.njol.skript.effects;

import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Sheep;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
Expand All @@ -33,45 +28,75 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
import io.papermc.paper.entity.Shearable;

import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Sheep;
import org.bukkit.entity.Snowman;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Shear")
@Description("Shears or 'un-shears' a sheep. Please note that no wool is dropped, this only sets the 'sheared' state of the sheep.")
@Examples({"on rightclick on a sheep holding a sword:",
" shear the clicked sheep"})
@Since("2.0")
@Description({
"Shears or un-shears a shearable entity with drops by shearing and a 'sheared' sound. Using with 'force' will force this effect despite the entity's 'shear state'.",
"\nPlease note that..:",
"\n- If your server is not running with Paper 1.19.4 or higher, this effect will only change its 'shear state', and the 'force' effect is unavailable",
"\n- Force-shearing or un-shearing on a sheared mushroom cow is not possible"
})
@Examples({
"on rightclick on a sheep holding a sword:",
"\tshear the clicked sheep",
"\tchance of 10%",
"\tforce shear the clicked sheep"
})
@Since("2.0, INSERT VERSION (shearable)")
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
public class EffShear extends Effect {

private static final boolean INTERFACE_METHOD = Skript.classExists("io.papermc.paper.entity.Shearable");

static {
Skript.registerEffect(EffShear.class,
"shear %livingentities%",
(INTERFACE_METHOD ? "[:force] " : "") + "shear %livingentities%",
"un[-]shear %livingentities%");
}

@SuppressWarnings("null")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
private Expression<LivingEntity> sheep;
private Expression<LivingEntity> entity;
private boolean force;
private boolean shear;

@SuppressWarnings({"unchecked", "null"})

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
sheep = (Expression<LivingEntity>) exprs[0];
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
entity = (Expression<LivingEntity>) exprs[0];
force = parseResult.hasTag("force");
shear = matchedPattern == 0;
return true;
}

@Override
protected void execute(final Event e) {
for (final LivingEntity en : sheep.getArray(e)) {
if (en instanceof Sheep) {
((Sheep) en).setSheared(shear);
protected void execute(Event event) {
for (LivingEntity entity : entity.getArray(event)) {
if (shear && INTERFACE_METHOD) {
if (!(entity instanceof Shearable))
continue;
Shearable shearable = ((Shearable) entity);
if (!force && !shearable.readyToBeSheared())
continue;
shearable.shear();
continue;
}
if (entity instanceof Sheep) {
((Sheep) entity).setSheared(shear);
} else if (entity instanceof Snowman) {
((Snowman) entity).setDerp(shear);
}
}
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return (shear ? "" : "un") + "shear " + sheep.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return (shear ? "" : "un") + "shear " + entity.toString(event, debug);
}

}