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

Active Item API #5365

Merged
merged 48 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
8cb1dd1
initial
sovdeeth Jan 18, 2023
35ad92c
MaxUseTime and pivot to ActiveItem
sovdeeth Jan 18, 2023
583a137
cancel item use
sovdeeth Jan 18, 2023
a4cda4b
Update ExprActiveItem.java
sovdeeth Jan 19, 2023
b6c2cc3
Clean up + Docs
sovdeeth Jan 19, 2023
34fb5e7
i'm a fool, forgot two checks
sovdeeth Jan 19, 2023
f23bf85
Apply suggestions from code review
sovdeeth Jan 19, 2023
6cbbf8f
More requested changes
sovdeeth Jan 19, 2023
f5ab8d8
Apply suggestions from code review
sovdeeth Jan 23, 2023
9be5a00
fix doc brackets, import, and required version tag
sovdeeth Jan 23, 2023
7feeb6f
Apply suggestions from code review
sovdeeth Jan 27, 2023
3c59b96
Rest of suggested changes
sovdeeth Jan 27, 2023
6d68af7
Requested Changes
sovdeeth Feb 13, 2023
4b70d90
PlayerStopUsingItemEvent
sovdeeth Feb 17, 2023
5c5ea6b
Merge branch 'master' into ActiveItemAPI
sovdeeth Feb 18, 2023
7d0e147
Add PlayerReadyArrowEvent
sovdeeth Feb 19, 2023
5b59892
Create ExprConsumedItem.java
sovdeeth Feb 19, 2023
9809ff7
Merge branch 'master' into ActiveItemAPI
sovdeeth Feb 19, 2023
53c5fea
Delete ExprConsumedItem.java
sovdeeth Feb 19, 2023
bbb053a
Merge branch 'ActiveItemAPI' of https://github.com/sovdeeth/Skript in…
sovdeeth Feb 19, 2023
e1bafb9
Merge branch 'master' into ActiveItemAPI
sovdeeth Mar 3, 2023
1ad580f
Merge branch 'master' into ActiveItemAPI
sovdeeth Mar 8, 2023
5de833c
Merge branch 'master' into ActiveItemAPI
sovdeeth Mar 17, 2023
2bb2c47
Requested Changes
sovdeeth Mar 27, 2023
5cbcbf9
Update ExprEntityItemUseTime.java
sovdeeth Mar 27, 2023
76ae2e7
Update BukkitEventValues.java
sovdeeth Mar 27, 2023
7f8c105
Merge branch 'master' into ActiveItemAPI
sovdeeth Apr 13, 2023
b7d6bbf
Apply suggestions from code review
sovdeeth Apr 15, 2023
dcf55cd
Update src/main/java/ch/njol/skript/conditions/CondIsHandRaised.java
sovdeeth May 6, 2023
7414481
Apply suggestions from code review
sovdeeth Jun 1, 2023
a7ce05a
Requested Changes
sovdeeth Aug 2, 2023
a566ddc
Merge branch 'master' into ActiveItemAPI
sovdeeth Aug 14, 2023
f6d30d3
Update src/main/java/ch/njol/skript/conditions/CondIsHandRaised.java
sovdeeth Aug 24, 2023
042d837
Apply suggestions from code review
sovdeeth Aug 25, 2023
95a5573
Suggestions and Fix CondIsHandRaised
sovdeeth Aug 25, 2023
61610ca
Suggested ready syntax
sovdeeth Aug 25, 2023
6d09588
Merge branch 'master' into ActiveItemAPI
sovdeeth Aug 25, 2023
489cbfa
Merge branch 'dev/feature' into ActiveItemAPI
sovdeeth Sep 18, 2023
275a695
Merge branch 'dev/feature' into ActiveItemAPI
sovdeeth Oct 27, 2023
4542c21
Merge branch 'dev/feature' into ActiveItemAPI
sovdeeth Nov 12, 2023
62e669f
Merge branch 'dev/feature' into ActiveItemAPI
sovdeeth Nov 24, 2023
10a85e0
Merge branch 'dev/feature' into ActiveItemAPI
sovdeeth Dec 5, 2023
8d085a5
Merge branch 'dev/feature' into ActiveItemAPI
sovdeeth Dec 17, 2023
00e9e6a
Apply suggestions from code review
sovdeeth Dec 17, 2023
4b752d7
requested changes
sovdeeth Dec 17, 2023
89adf73
Update EffCancelItemUse.java
sovdeeth Dec 17, 2023
7cd3a7a
Update src/main/java/ch/njol/skript/effects/EffCancelItemUse.java
sovdeeth Dec 19, 2023
8aad1de
Merge branch 'dev/feature' into ActiveItemAPI
sovdeeth Dec 19, 2023
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
102 changes: 102 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsHandRaised.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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 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.bukkit.inventory.EquipmentSlot;
import org.eclipse.jdt.annotation.Nullable;

@Name("Is Hand Raised")
@Description({
"Checks whether an entity has one or both of their hands raised. ",
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
"Hands are raised when an entity is using an item (eg: blocking, drawing a bow, eating)."
})
@Examples({
"on damage of player:",
"\tif victim's main hand is raised:",
"\t\tdrop player's tool at player",
"\t\tset player's tool to air"
})
@Since("INSERT VERSION")
@RequiredPlugins("Paper")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
public class CondIsHandRaised extends PropertyCondition<LivingEntity> {

static {
Skript.registerCondition(CondIsHandRaised.class,
"%livingentities%'[s] [:main] hand[s] (is|are) raised",
"%livingentities%'[s] [:main] hand[s] (isn't|is not|aren't|are not) raised",
"%livingentities%'[s] off[( |-)]hand[s] (is|are) raised",
"%livingentities%'[s] off[( |-)]hand[s] (isn't|is not|aren't|are not) raised");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<LivingEntity> entities;

// 0 for either hand, 1 for main hand, 2 for off-hand.
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
private static final int EITHER_HAND = 0, MAIN_HAND = 1, OFF_HAND = 2;
private int hand;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

@Override
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<LivingEntity>) exprs[0]);
setNegated(matchedPattern == 1 || matchedPattern == 3);
if (matchedPattern >= 2) {
hand = OFF_HAND;
} else if (parseResult.hasTag("main")) {
hand = MAIN_HAND;
} else {
hand = EITHER_HAND;
}
return true;
}

@Override
public boolean check(LivingEntity livingEntity) {
if (hand == EITHER_HAND) {
return livingEntity.isHandRaised();
} else if (hand == MAIN_HAND) {
return livingEntity.isHandRaised() && livingEntity.getHandRaised().equals(EquipmentSlot.HAND);
} else if (hand == OFF_HAND) {
return livingEntity.isHandRaised() && livingEntity.getHandRaised().equals(EquipmentSlot.OFF_HAND);
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
return false;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

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

@Override
public String toString(@Nullable Event event, boolean debug) {
return entities.toString(event, debug) + "'s " + (hand == 0 ? "" : (hand == 1 ? "main " : "off ")) + "hand " +
(entities.isSingle() ? "is" : "s are") + " raised ";
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

}
77 changes: 77 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffCancelItemUse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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.RequiredPlugins;
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("Cancel Active Item Use")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
@Description({
"Interrupts the action an entity is trying to complete. ",
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
"For example, interrupting eating, or drawing back a bow."
})
@Examples({
"on damage of player:",
"\tif victim's active tool is a bow:",
"\t\tinterrupt player's active item use"
})
@Since("INSERT VERSION")
@RequiredPlugins("Paper 1.16.1+")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
public class EffCancelItemUse extends Effect {

static {
if (Skript.methodExists(LivingEntity.class, "clearActiveItem")) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
Skript.registerEffect(EffCancelItemUse.class,
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
"(cancel|interrupt) %livingentities%'[s] [active|current] item us(e|age)",
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
"(cancel|interrupt) [active|current] item us(e|age) [of %livingentities%]");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<LivingEntity> entityExpression;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
entityExpression = (Expression<LivingEntity>) exprs[0];
return true;
}

@Override
protected void execute(Event event) {
for (LivingEntity entity : entityExpression.getArray(event)) {
entity.clearActiveItem();
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "cancel item usage of " + entityExpression.toString(event, debug);
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

}
79 changes: 79 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprActiveItem.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.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
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 ch.njol.skript.expressions.base.PropertyExpression;
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("Active Item")
@Description(
"Returns the item the entity is currently using (ie: the food they're eating, " +
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
"the bow they're drawing back, etc.). This cannot be changed. " +
"If an entity is not using any item, this will return 0 air."
)
@Examples({
"on damage of player:",
"\tif victim's active tool is a bow:",
"\t\tinterrupt player's active item use"
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
@RequiredPlugins("Paper")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
public class ExprActiveItem extends PropertyExpression<LivingEntity, ItemType> {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

static {
if (Skript.methodExists(LivingEntity.class, "getActiveItem"))
register(ExprActiveItem.class, ItemType.class, "(raised|active) (tool|item|weapon)", "livingentities");
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<LivingEntity>) exprs[0]);
return true;
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

@Override
protected ItemType[] get(Event event, LivingEntity[] source) {
return get(source, (livingEntity -> {
return new ItemType(livingEntity.getActiveItem());
}));
}

@Override
public Class<? extends ItemType> getReturnType() {
return ItemType.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "active item of " + getExpr().toString(event, debug);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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.expressions;

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.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean;
import org.bukkit.entity.LivingEntity;
import org.eclipse.jdt.annotation.Nullable;

@Name("Active Item Use Time")
@Description(
"Returns the time that an the entity either has spent using an item, " +
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
"or the time left for them to finish using an item. " +
"If an entity is not using any item, this will return 0 seconds."
)
@Examples({
"on right click:",
"\tbroadcast player's remaining item use time",
"\twait 1 second",
"\tbroadcast player's item use time"
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
@RequiredPlugins("Paper")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
public class ExprEntityItemUseTime extends SimplePropertyExpression<LivingEntity, Timespan> {

static {
if (Skript.methodExists(LivingEntity.class, "getItemUseRemainingTime"))
register(ExprEntityItemUseTime.class, Timespan.class, "[elapsed|:remaining] (item|tool) use time", "livingentities");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean remaining;

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

@Override
public Timespan convert(LivingEntity livingEntity) {
if (remaining)
return Timespan.fromTicks(livingEntity.getItemUseRemainingTime());
return Timespan.fromTicks(livingEntity.getHandRaisedTime());
}

@Override
public Class<? extends Timespan> getReturnType() {
return Timespan.class;
}

@Override
protected String getPropertyName() {
return (remaining ? "remaining" : "elapsed") + " item use time";
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading