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 and Fix ExprAge #4854

Merged
merged 31 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
361a43a
Enhance and fix ExprAge
AyhamAl-Ali Jun 30, 2022
b9c8256
typo
AyhamAl-Ali Jun 30, 2022
c9b19f9
description improvements
AyhamAl-Ali Jun 30, 2022
c4645dd
Fix CCE, Update changers, return Number.class
AyhamAl-Ali Jun 30, 2022
7472f7a
Bring back class check until 2.7 released
AyhamAl-Ali Jun 30, 2022
f24df87
Merge branch 'master' into ench/expr-age
AyhamAl-Ali Jun 30, 2022
4d5a46f
RC
AyhamAl-Ali Jun 30, 2022
fd89f05
Merge remote-tracking branch 'AyhamAl-Ali/ench/expr-age' into ench/ex…
AyhamAl-Ali Jun 30, 2022
1abb814
Move static field inside static block
AyhamAl-Ali Jul 1, 2022
d0837fc
Merge branch 'master' into ench/expr-age
TheLimeGlass Jul 1, 2022
d7d33a5
naming convention
AyhamAl-Ali Jul 1, 2022
a9bdd92
Merge remote-tracking branch 'AyhamAl-Ali/ench/expr-age' into ench/ex…
AyhamAl-Ali Jul 1, 2022
7f0ac37
Update src/main/java/ch/njol/skript/expressions/ExprAge.java
AyhamAl-Ali Jul 2, 2022
decab82
return super.init
AyhamAl-Ali Jul 2, 2022
4b6d3e6
Update src/main/java/ch/njol/skript/expressions/ExprAge.java
AyhamAl-Ali Jul 3, 2022
8660502
Update src/main/java/ch/njol/skript/expressions/ExprAge.java
AyhamAl-Ali Jul 3, 2022
6118dcb
Update src/main/java/ch/njol/skript/expressions/ExprAge.java
AyhamAl-Ali Jul 3, 2022
aed8659
Max -> Maximum
AyhamAl-Ali Jul 3, 2022
7798f15
Merge remote-tracking branch 'AyhamAl-Ali/ench/expr-age' into ench/ex…
AyhamAl-Ali Jul 3, 2022
217334d
Merge branch 'master' into ench/expr-age
AyhamAl-Ali Jul 3, 2022
a592fb9
RC - Improvements & Remove legacy checks
AyhamAl-Ali Jul 4, 2022
5d259fa
Merge remote-tracking branch 'AyhamAl-Ali/ench/expr-age' into ench/ex…
AyhamAl-Ali Jul 4, 2022
7d0fb4e
Remove DELETE case & move out newValue
AyhamAl-Ali Jul 9, 2022
ab5d396
Error on use of max age on entities
AyhamAl-Ali Jul 16, 2022
eed6c48
Return null of isMax on entities
AyhamAl-Ali Jul 17, 2022
33d0a9a
Merge branch 'master' into ench/expr-age
AyhamAl-Ali Jul 17, 2022
96dfab7
Remove unnecessary default value of isMax
AyhamAl-Ali Jul 17, 2022
0e88c09
Merge remote-tracking branch 'AyhamAl-Ali/ench/expr-age' into ench/ex…
AyhamAl-Ali Jul 17, 2022
345f830
Merge branch 'master' into ench/expr-age
TheLimeGlass Jul 20, 2022
a9310d5
Typo
AyhamAl-Ali Aug 5, 2022
a96e530
Merge branch 'master' into ench/expr-age
TheLimeGlass Aug 6, 2022
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
173 changes: 173 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprAge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* 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.classes.Changer.ChangeMode;
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.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Age of Block/Entity")
@Description({
"Returns the age or max age of blocks and age for entities (there in no max age for entities).",
"For blocks, 'Age' represents the different growth stages that a crop-like block can go through. " +
"A value of 0 indicates that the crop was freshly planted, whilst a value equal to 'maximum age' indicates that the crop is ripe and ready to be harvested.",
"For entities, 'Age' represents the time left for them to become adults and it's in minus increasing to be 0 which means they're adults, " +
"e.g. A baby cow needs 20 minutes to become an adult which equals to 24,000 ticks so their age will be -24000 once spawned."
})
@Examples({
"# Set targeted crop to fully grown crop",
"set age of targeted block to max age of targeted block",
" ",
"# Spawn a baby cow that will only need 1 minute to become an adult",
"spawn a baby cow at player",
"set age of last spawned entity to -1200 # in ticks = 60 seconds"
})
@RequiredPlugins("Minecraft 1.13+")
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
@Since("INSERT VERSION")
public class ExprAge extends SimplePropertyExpression<Object, Number> {

AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
private static String FROM_TYPE = "";

static {
if (Skript.classExists("org.bukkit.entity.Ageable"))
FROM_TYPE += "entities";
if (Skript.classExists("org.bukkit.block.data.Ageable"))
FROM_TYPE += "/blocks"; // org.bukkit.entity.Ageable exists before org.bukkit.block.data.Ageable

if (!FROM_TYPE.isEmpty())
register(ExprAge.class, Number.class, "[:max[imum]] age", FROM_TYPE);
}

private boolean isMax = false;

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

@Override
@Nullable
public Number convert(Object obj) {
if (obj instanceof Block) {
BlockData bd = ((Block) obj).getBlockData();
if (!(bd instanceof Ageable))
return null;
Ageable ageable = (Ageable) bd;
return isMax ? ageable.getMaximumAge() : ageable.getAge();
} else if (obj instanceof org.bukkit.entity.Ageable) {
return ((org.bukkit.entity.Ageable) obj).getAge();
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}
return null;
}

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (isMax || mode == ChangeMode.REMOVE_ALL || mode == ChangeMode.DELETE)
return null;
return CollectionUtils.array(Number.class);

}
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
if (mode != ChangeMode.RESET && mode != ChangeMode.DELETE && (delta == null || delta[0] == null))
return;

int newValue;
switch (mode) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
case ADD:
newValue = ((Number) delta[0]).intValue();
for (Object obj : getExpr().getArray(event)) {
Number oldValue = convert(obj);
if (oldValue == null)
continue;
setAge(obj, oldValue.intValue() + newValue);
}
break;
case REMOVE:
newValue = ((Number) delta[0]).intValue();
for (Object obj : getExpr().getArray(event)) {
Number oldValue = convert(obj);
if (oldValue == null)
continue;
setAge(obj, oldValue.intValue() - newValue);
}
break;
case SET:
newValue = ((Number) delta[0]).intValue();
for (Object obj : getExpr().getArray(event)) {
setAge(obj, newValue);
}
break;
case RESET:
for (Object obj : getExpr().getArray(event)) {
int value = 0;
// baby animals takes 20 minutes to grow up - ref: https://minecraft.fandom.com/wiki/Breeding
if (obj instanceof org.bukkit.entity.Ageable)
// it might change later on so removing entity age reset would be better unless
// bukkit adds a method returning the default age
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
value = -24000;
setAge(obj, value);
}
break;
}
}

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

@Override
protected String getPropertyName() {
return (isMax ? "max " : "") + "age";
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

private void setAge(Object obj, int value) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
if (obj instanceof Block) {
Block block = (Block) obj;
BlockData bd = block.getBlockData();
if (bd instanceof Ageable) {
((Ageable) bd).setAge(Math.max(Math.min(value, ((Ageable) bd).getMaximumAge()), 0));
block.setBlockData(bd);
}
} else if (obj instanceof org.bukkit.entity.Ageable) {
// Bukkit accepts higher values than 0, they will keep going down to 0 though (some Animals type might be using that - not sure)
((org.bukkit.entity.Ageable) obj).setAge(value);
}
}

}
106 changes: 0 additions & 106 deletions src/main/java/ch/njol/skript/expressions/ExprBlockAge.java

This file was deleted.