Skip to content

Commit

Permalink
Explosive Entity Yield Expressions (#2782)
Browse files Browse the repository at this point in the history
* Add expressions

* Creeper Support / Suggestions

* Make Requested Changes

* Cleanup

* Rename element files and update formatting
  • Loading branch information
APickledWalrus authored Jul 20, 2020
1 parent 6df393e commit e0477a5
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 0 deletions.
128 changes: 128 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprExplosionYield.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* 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 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.expressions;

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

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Events;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

@Name("Explosion Yield")
@Description({"The yield of the explosion in an explosion prime event. This is how big the explosion is.",
" When changing the yield, values less than 0 will be ignored.",
" Read <a href='https://minecraft.gamepedia.com/Explosion'>this wiki page</a> for more information"})
@Examples({"on explosion prime:",
"\tset the yield of the explosion to 10"})
@Events("explosion prime")
@Since("INSERT VERSION")
public class ExprExplosionYield extends SimpleExpression<Number> {

static {
Skript.registerExpression(ExprExplosionYield.class, Number.class, ExpressionType.SIMPLE,
"[the] explosion (yield|radius|size)",
"[the] (yield|radius|size) of [the] explosion"
);
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!ScriptLoader.isCurrentEvent(ExplosionPrimeEvent.class)) {
Skript.error("The explosion radius is only usable in explosion prime events", ErrorQuality.SEMANTIC_ERROR);
return false;
}
return true;
}

@Override
@Nullable
protected Number[] get(Event e) {
return new Number[]{((ExplosionPrimeEvent) e).getRadius()};
}

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

@Override
public void change(final Event event, final @Nullable Object[] delta, final ChangeMode mode) {
float f = delta == null ? 0 : ((Number) delta[0]).floatValue();
if (f < 0) // Negative values will throw an error.
return;
ExplosionPrimeEvent e = (ExplosionPrimeEvent) event;
switch (mode) {
case SET:
e.setRadius(f);
break;
case ADD:
float add = e.getRadius() + f;
if (add < 0)
return;
e.setRadius(add);
break;
case REMOVE:
float subtract = e.getRadius() - f;
if (subtract < 0)
return;
e.setRadius(subtract);
break;
case DELETE:
e.setRadius(0);
break;
case REMOVE_ALL:
case RESET:
assert false;
}
}

@Override
public boolean isSingle() {
return true;
}

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

@Override
public String toString(@Nullable Event e, boolean debug) {
return "the yield of the explosion";
}

}
144 changes: 144 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* 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 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.expressions;

import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Explosive;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

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.util.coll.CollectionUtils;

@Name("Explosive Yield")
@Description({"The yield of an explosive (creeper, primed tnt, fireball, etc.). This is how big of an explosion is caused by the entity.",
"Read <a href='https://minecraft.gamepedia.com/Explosion'>this wiki page</a> for more information"})
@Examples({"on spawn of a creeper:",
"\tset the explosive yield of the event-entity to 10"})
@RequiredPlugins("Minecraft 1.12 or newer for creepers")
@Since("INSERT VERSION")
public class ExprExplosiveYield extends SimplePropertyExpression<Entity, Number> {

static {
register(ExprExplosiveYield.class, Number.class, "explosive (yield|radius|size)", "entities");
}

private final static boolean CREEPER_USABLE = Skript.methodExists(Creeper.class, "getExplosionRadius");

@Override
public Number convert(Entity e) {
if (e instanceof Explosive)
return ((Explosive) e).getYield();
if (CREEPER_USABLE && e instanceof Creeper)
return ((Creeper) e).getExplosionRadius();
return 0;
}

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

@Override
public void change(final Event event, final @Nullable Object[] delta, final ChangeMode mode) {
Number change = delta != null ? (Number) delta[0] : 0;
for (Entity entity : getExpr().getArray(event)) {
if (entity instanceof Explosive) {
Explosive e = (Explosive) entity;
float f = change.floatValue();
if (f < 0) // Negative values will throw an error.
return;
switch (mode) {
case SET:
e.setYield(f);
break;
case ADD:
float add = e.getYield() + f;
if (add < 0)
return;
e.setYield(add);
break;
case REMOVE:
float subtract = e.getYield() - f;
if (subtract < 0)
return;
e.setYield(subtract);
break;
case DELETE:
e.setYield(0);
break;
case REMOVE_ALL:
case RESET:
assert false;
}
} else if (CREEPER_USABLE && entity instanceof Creeper) {
Creeper c = (Creeper) entity;
int i = change.intValue();
if (i < 0) // Negative values will throw an error.
return;
switch (mode) {
case SET:
c.setExplosionRadius(i);
break;
case ADD:
int add = c.getExplosionRadius() + i;
if (add < 0)
return;
c.setExplosionRadius(add);
break;
case REMOVE:
int subtract = c.getExplosionRadius() - i;
if (subtract < 0)
return;
c.setExplosionRadius(subtract);
break;
case DELETE:
c.setExplosionRadius(0);
break;
case REMOVE_ALL:
case RESET:
assert false;
}
}
}
}

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

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

}
13 changes: 13 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test "explosive yield":
spawn a primed tnt at spawn of world "world"
assert explosive yield of last spawned tnt is 4 with "explosive yield of tnt is 4 by default"
set explosive yield of last spawned tnt to 10
assert explosive yield of last spawned tnt is 10 with "an explosive's explosive yield should be 10 if it is set to 10"
delete last spawned primed tnt

test "creeper explosive yield" when minecraft version is "1.12.2":
spawn a creeper at spawn of world "world"
assert explosive yield of last spawned creeper is 3 with "explosive yield of a creeper is 3 by default"
set explosive yield of last spawned creeper to 10
assert explosive yield of last spawned creeper is 10 with "a creeper's explosive yield should be 10 if it is set to 10"
delete last spawned creeper

0 comments on commit e0477a5

Please sign in to comment.