Scripted spawners

This commit is contained in:
Daniel Mills 2021-08-08 00:22:31 -04:00
parent 33ea66ea88
commit 2d43014029
3 changed files with 59 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import com.volmit.iris.Iris;
import com.volmit.iris.engine.framework.Engine; import com.volmit.iris.engine.framework.Engine;
import com.volmit.iris.engine.scripting.EngineExecutionEnvironment; import com.volmit.iris.engine.scripting.EngineExecutionEnvironment;
import com.volmit.iris.engine.scripting.IrisScriptingAPI; import com.volmit.iris.engine.scripting.IrisScriptingAPI;
import com.volmit.iris.util.format.C;
import lombok.Data; import lombok.Data;
import org.apache.bsf.BSFEngine; import org.apache.bsf.BSFEngine;
import org.apache.bsf.BSFException; import org.apache.bsf.BSFException;
@ -56,6 +57,7 @@ public class IrisExecutionEnvironment implements EngineExecutionEnvironment {
public void execute(String script) public void execute(String script)
{ {
Iris.debug("Execute Script (void) " + C.DARK_GREEN + script);
try { try {
javaScriptEngine.exec("", 0, 0, getEngine().getData().getScriptLoader().load(script)); javaScriptEngine.exec("", 0, 0, getEngine().getData().getScriptLoader().load(script));
} catch (BSFException e) { } catch (BSFException e) {
@ -65,6 +67,7 @@ public class IrisExecutionEnvironment implements EngineExecutionEnvironment {
public Object evaluate(String script) public Object evaluate(String script)
{ {
Iris.debug("Execute Script (for result) " + C.DARK_GREEN + script);
try { try {
return javaScriptEngine.eval("", 0, 0, getEngine().getData().getScriptLoader().load(script)); return javaScriptEngine.eval("", 0, 0, getEngine().getData().getScriptLoader().load(script));
} catch (BSFException e) { } catch (BSFException e) {

View File

@ -21,10 +21,8 @@ package com.volmit.iris.engine.object.entity;
import com.volmit.iris.Iris; import com.volmit.iris.Iris;
import com.volmit.iris.core.project.loader.IrisRegistrant; import com.volmit.iris.core.project.loader.IrisRegistrant;
import com.volmit.iris.engine.framework.Engine; import com.volmit.iris.engine.framework.Engine;
import com.volmit.iris.engine.object.annotations.ArrayType; import com.volmit.iris.engine.object.annotations.*;
import com.volmit.iris.engine.object.annotations.Desc; import com.volmit.iris.engine.object.common.IrisScript;
import com.volmit.iris.engine.object.annotations.RegistryListSpecialEntity;
import com.volmit.iris.engine.object.annotations.Required;
import com.volmit.iris.engine.object.loot.IrisLoot; import com.volmit.iris.engine.object.loot.IrisLoot;
import com.volmit.iris.engine.object.loot.IrisLootReference; import com.volmit.iris.engine.object.loot.IrisLootReference;
import com.volmit.iris.engine.object.loot.IrisLootTable; import com.volmit.iris.engine.object.loot.IrisLootTable;
@ -155,17 +153,46 @@ public class IrisEntity extends IrisRegistrant {
@Desc("Create a mob from another plugin, such as Mythic Mobs. Should be in the format of a namespace of PluginName:MobName") @Desc("Create a mob from another plugin, such as Mythic Mobs. Should be in the format of a namespace of PluginName:MobName")
private String specialType = ""; private String specialType = "";
@Desc("Set the entity type to UNKNOWN, then define a script here which ends with the entity variable (the result). You can use Iris.getLocation() to find the target location. You can spawn any entity this way.")
@RegistryListResource(IrisScript.class)
private String spawnerScript = "";
@ArrayType(min = 1, type = String.class)
@Desc("Set the entity type to UNKNOWN, then define a script here. You can use Iris.getLocation() to find the target location. You can spawn any entity this way.")
@RegistryListResource(IrisScript.class)
private KList<String> postSpawnScripts = new KList<>();
public Entity spawn(Engine gen, Location at) { public Entity spawn(Engine gen, Location at) {
return spawn(gen, at, new RNG(at.hashCode())); return spawn(gen, at, new RNG(at.hashCode()));
} }
public Entity spawn(Engine gen, Location at, RNG rng) { public Entity spawn(Engine gen, Location at, RNG rng) {
Entity e = doSpawn(at); Entity ee = doSpawn(at);
if (e == null) { if(!spawnerScript.isEmpty() && ee == null)
{
synchronized (this)
{
gen.getExecution().getAPI().setLocation(at);
try
{
ee = (Entity) gen.getExecution().evaluate(spawnerScript);
}
catch(Throwable ex)
{
Iris.error("You must return an Entity in your scripts to use entity scripts!");
ex.printStackTrace();
}
}
}
if(ee == null)
{
return null; return null;
} }
Entity e = ee;
e.setCustomName(getCustomName() != null ? C.translateAlternateColorCodes('&', getCustomName()) : null); e.setCustomName(getCustomName() != null ? C.translateAlternateColorCodes('&', getCustomName()) : null);
e.setCustomNameVisible(isCustomNameVisible()); e.setCustomNameVisible(isCustomNameVisible());
e.setGlowing(isGlowing()); e.setGlowing(isGlowing());
@ -286,10 +313,29 @@ public class IrisEntity extends IrisRegistrant {
spawnEffect.apply(e); spawnEffect.apply(e);
} }
if(postSpawnScripts.isNotEmpty())
{
synchronized (this)
{
gen.getExecution().getAPI().setLocation(at);
gen.getExecution().getAPI().setEntity(ee);
for(String i : postSpawnScripts)
{
gen.getExecution().execute(i);
}
}
}
return e; return e;
} }
private Entity doSpawn(Location at) { private Entity doSpawn(Location at) {
if(type.equals(EntityType.UNKNOWN))
{
return null;
}
if (!Bukkit.isPrimaryThread()) { if (!Bukkit.isPrimaryThread()) {
// Someone called spawn (worldedit maybe?) on a non server thread // Someone called spawn (worldedit maybe?) on a non server thread
// Due to the structure of iris, we will call it sync and busy wait until it's done. // Due to the structure of iris, we will call it sync and busy wait until it's done.

View File

@ -28,6 +28,8 @@ import com.volmit.iris.engine.object.biome.IrisBiome;
import com.volmit.iris.engine.object.dimensional.IrisDimension; import com.volmit.iris.engine.object.dimensional.IrisDimension;
import com.volmit.iris.engine.object.noise.IrisExpression; import com.volmit.iris.engine.object.noise.IrisExpression;
import lombok.Data; import lombok.Data;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
@Data @Data
public class IrisScriptingAPI { public class IrisScriptingAPI {
@ -36,6 +38,8 @@ public class IrisScriptingAPI {
private double x = 0; private double x = 0;
private double y = 0; private double y = 0;
private double z = 0; private double z = 0;
private Location location;
private Entity entity;
public IrisScriptingAPI(Engine engine) public IrisScriptingAPI(Engine engine)
{ {