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

Add support for Trial Spawners in Spawner Type expression. #7008

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
121 changes: 71 additions & 50 deletions src/main/java/ch/njol/skript/expressions/ExprSpawnerType.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
/**
* 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.bukkitutil.EntityUtils;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.TrialSpawner;
import org.bukkit.entity.EntityType;
import org.bukkit.event.Event;
import org.bukkit.spawner.TrialSpawnerConfiguration;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
Expand All @@ -39,55 +22,93 @@

@Name("Spawner Type")
@Description("Retrieves, sets, or resets the spawner's entity type")
@Examples({"on right click:",
" if event-block is spawner:",
" send \"Spawner's type is %target block's entity type%\""})
@Since("2.4")
@Examples({
"on right click:",
"\tif event-block is spawner:",
"\t\tsend \"Spawner's type is %target block's entity type%\""
})
@Since("2.4, INSERT VERSION (trial spawner)")
public class ExprSpawnerType extends SimplePropertyExpression<Block, EntityData> {


private static final boolean HAS_TRIAL_SPAWNER = Skript.classExists("org.bukkit.block.TrialSpawner");

static {
register(ExprSpawnerType.class, EntityData.class, "(spawner|entity|creature) type[s]", "blocks");
}

@Override

@Nullable
public EntityData convert(Block block) {
if (!(block.getState() instanceof CreatureSpawner))
return null;
EntityType type = ((CreatureSpawner) block.getState()).getSpawnedType();
if (type == null)
return null;
return EntityUtils.toSkriptEntityData(type);
if (block.getState() instanceof CreatureSpawner) {
EntityType type = ((CreatureSpawner) block.getState()).getSpawnedType();
if (type == null)
return null;
return EntityUtils.toSkriptEntityData(type);
}
if (HAS_TRIAL_SPAWNER && block.getState() instanceof TrialSpawner) {
TrialSpawner trialSpawner = (TrialSpawner) block.getState();
EntityType type;
if (trialSpawner.isOminous()) {
type = trialSpawner.getOminousConfiguration().getSpawnedType();
} else {
type = trialSpawner.getNormalConfiguration().getSpawnedType();
}
if (type == null)
return null;
return EntityUtils.toSkriptEntityData(type);
}
return null;
}

@Nullable
@Override
public Class<?>[] acceptChange(Changer.ChangeMode mode) {
if (mode == ChangeMode.SET || mode == ChangeMode.RESET)
return CollectionUtils.array(EntityData.class);
return null;
switch (mode) {
case SET:
case RESET:
return CollectionUtils.array(EntityData.class);
default:
return null;
}
}

@SuppressWarnings("null")
@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
for (Block b : getExpr().getArray(event)) {
if (!(b.getState() instanceof CreatureSpawner))
continue;
CreatureSpawner s = (CreatureSpawner) b.getState();
switch (mode) {
case SET:
assert delta != null;
s.setSpawnedType(EntityUtils.toBukkitEntityType((EntityData) delta[0]));
break;
case RESET:
s.setSpawnedType(org.bukkit.entity.EntityType.PIG);
break;
for (Block block : getExpr().getArray(event)) {
if (block.getState() instanceof CreatureSpawner) {
CreatureSpawner spawner = (CreatureSpawner) block.getState();
switch (mode) {
case SET:
assert delta != null;
spawner.setSpawnedType(EntityUtils.toBukkitEntityType((EntityData) delta[0]));
break;
case RESET:
spawner.setSpawnedType(EntityType.PIG);
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
break;
}
spawner.update(); // Actually trigger the spawner's update
} else if (HAS_TRIAL_SPAWNER && block.getState() instanceof TrialSpawner) {
TrialSpawner trialSpawner = (TrialSpawner) block.getState();
TrialSpawnerConfiguration config;
if (trialSpawner.isOminous()) {
config = trialSpawner.getOminousConfiguration();
} else {
config = trialSpawner.getNormalConfiguration();
}
switch (mode) {
case SET:
assert delta != null;
config.setSpawnedType((EntityUtils.toBukkitEntityType((EntityData) delta[0])));
break;
case RESET:
config.setSpawnedType(EntityType.PIG);
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
break;
}
trialSpawner.update();
}
s.update(); // Actually trigger the spawner's update
}
}

@Override
public Class<EntityData> getReturnType() {
return EntityData.class;
Expand Down
23 changes: 23 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprSpawnerType.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test "spawner type":
set block at event-location to spawner
set {_block} to block at event-location

set spawner type of {_block} to zombie
assert spawner type of {_block} is zombie with "Spawner type was set to a Zombie"

reset spawner type of {_block}
assert spawner type of {_block} is pig with "Spawner type was not reset correctly"

set block at {_block} to air

test "trial spawner type" when running minecraft "1.21":
set block at event-location to trial spawner
set {_block} to block at event-location

set spawner type of {_block} to zombie
assert spawner type of {_block} is zombie with "Spawner type was set to a Zombie"

reset spawner type of {_block}
assert spawner type of {_block} is pig with "Spawner type was not reset correctly"

set block at {_block} to air