mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-12-28 11:39:07 +00:00
Scripting engine
This commit is contained in:
@@ -28,6 +28,7 @@ import com.volmit.iris.engine.object.biome.IrisBiomePaletteLayer;
|
||||
import com.volmit.iris.engine.object.decoration.IrisDecorator;
|
||||
import com.volmit.iris.engine.object.engine.IrisEngineData;
|
||||
import com.volmit.iris.engine.object.objects.IrisObjectPlacement;
|
||||
import com.volmit.iris.engine.scripting.EngineExecutionEnvironment;
|
||||
import com.volmit.iris.util.context.IrisContext;
|
||||
import com.volmit.iris.util.documentation.BlockCoordinates;
|
||||
import com.volmit.iris.util.documentation.ChunkCoordinates;
|
||||
@@ -64,6 +65,9 @@ public class IrisEngine extends BlockPopulator implements Engine {
|
||||
@Getter
|
||||
private final EngineEffects effects;
|
||||
|
||||
@Getter
|
||||
private final EngineExecutionEnvironment execution;
|
||||
|
||||
@Getter
|
||||
private final EngineWorldManager worldManager;
|
||||
|
||||
@@ -116,10 +120,13 @@ public class IrisEngine extends BlockPopulator implements Engine {
|
||||
Iris.callEvent(new IrisEngineHotloadEvent(this));
|
||||
context = new IrisContext(this);
|
||||
context.touch();
|
||||
execution = new IrisExecutionEnvironment(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrisEngineData getEngineData() {
|
||||
World w = null;
|
||||
|
||||
return engineData.aquire(() -> {
|
||||
File f = new File(getWorld().worldFolder(), "iris/engine-data/" + getDimension().getLoadKey() + "-" + getIndex() + ".json");
|
||||
|
||||
|
||||
@@ -18,16 +18,70 @@
|
||||
|
||||
package com.volmit.iris.engine;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
import com.volmit.iris.engine.scripting.EngineExecutionEnvironment;
|
||||
import com.volmit.iris.engine.scripting.IrisScriptingAPI;
|
||||
import lombok.Data;
|
||||
import org.apache.bsf.BSFEngine;
|
||||
import org.apache.bsf.BSFException;
|
||||
import org.apache.bsf.BSFManager;
|
||||
import org.apache.bsf.engines.javascript.JavaScriptEngine;
|
||||
|
||||
@Data
|
||||
public class IrisExecutionEnvironment implements EngineExecutionEnvironment {
|
||||
private final BSFManager manager;
|
||||
private final Engine engine;
|
||||
private final IrisScriptingAPI api;
|
||||
private JavaScriptEngine javaScriptEngine;
|
||||
|
||||
IrisExecutionEnvironment(Engine engine)
|
||||
public IrisExecutionEnvironment(Engine engine)
|
||||
{
|
||||
this.engine = engine;
|
||||
this.api = new IrisScriptingAPI(engine);
|
||||
this.manager = new BSFManager();
|
||||
this.manager.setClassLoader(Iris.class.getClassLoader());
|
||||
try {
|
||||
this.manager.declareBean("Iris", api, api.getClass());
|
||||
this.javaScriptEngine = (JavaScriptEngine) this.manager.loadScriptingEngine("javascript");
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void execute(String script)
|
||||
{
|
||||
try {
|
||||
javaScriptEngine.exec("", 0, 0, getEngine().getData().getScriptLoader().load(script));
|
||||
} catch (BSFException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Object evaluate(String script)
|
||||
{
|
||||
try {
|
||||
return javaScriptEngine.eval("", 0, 0, getEngine().getData().getScriptLoader().load(script));
|
||||
} catch (BSFException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String script, double x, double y, double z) {
|
||||
api.setX(x);
|
||||
api.setY(y);
|
||||
api.setZ(z);
|
||||
execute(script);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(String script, double x, double y, double z) {
|
||||
api.setX(x);
|
||||
api.setY(y);
|
||||
api.setZ(z);
|
||||
return evaluate(script);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import com.volmit.iris.engine.object.loot.IrisLootTable;
|
||||
import com.volmit.iris.engine.object.meta.InventorySlotType;
|
||||
import com.volmit.iris.engine.object.regional.IrisRegion;
|
||||
import com.volmit.iris.engine.parallax.ParallaxAccess;
|
||||
import com.volmit.iris.engine.scripting.EngineExecutionEnvironment;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.context.IrisContext;
|
||||
import com.volmit.iris.util.data.B;
|
||||
@@ -65,6 +66,8 @@ public interface Engine extends DataProvider, Fallible, GeneratorAccess, LootPro
|
||||
|
||||
IrisContext getContext();
|
||||
|
||||
EngineExecutionEnvironment getExecution();
|
||||
|
||||
double getMaxBiomeObjectDensity();
|
||||
|
||||
double getMaxBiomeDecoratorDensity();
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.object.common;
|
||||
|
||||
import com.volmit.iris.core.project.loader.IrisRegistrant;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class IrisScript extends IrisRegistrant {
|
||||
private final String source;
|
||||
|
||||
public IrisScript()
|
||||
{
|
||||
this("");
|
||||
}
|
||||
|
||||
public IrisScript(String source)
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFolderName() {
|
||||
return "scripts";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return "Script";
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -19,11 +19,22 @@
|
||||
package com.volmit.iris.engine.scripting;
|
||||
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
import org.apache.bsf.BSFManager;
|
||||
|
||||
public interface EngineExecutionEnvironment
|
||||
{
|
||||
Engine getEngine();
|
||||
|
||||
BSFManager getManager();
|
||||
|
||||
void execute(String script);
|
||||
|
||||
Object evaluate(String script);
|
||||
|
||||
void execute(String script, double x, double y, double z);
|
||||
|
||||
Object evaluate(String script, double x, double y, double z);
|
||||
|
||||
default void close()
|
||||
{
|
||||
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.scripting;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.project.loader.IrisData;
|
||||
import com.volmit.iris.engine.IrisComplex;
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
import com.volmit.iris.engine.framework.EngineFramework;
|
||||
import com.volmit.iris.engine.object.biome.IrisBiome;
|
||||
import com.volmit.iris.engine.object.dimensional.IrisDimension;
|
||||
import com.volmit.iris.engine.object.noise.IrisExpression;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class IrisScriptingAPI {
|
||||
private final Engine engine;
|
||||
private double x = 0;
|
||||
private double y = 0;
|
||||
private double z = 0;
|
||||
|
||||
public IrisScriptingAPI(Engine engine)
|
||||
{
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public IrisData getData()
|
||||
{
|
||||
return getEngine().getData();
|
||||
}
|
||||
|
||||
public EngineFramework getFramework()
|
||||
{
|
||||
return getEngine().getFramework();
|
||||
}
|
||||
|
||||
public IrisComplex getComplex()
|
||||
{
|
||||
return getFramework().getComplex();
|
||||
}
|
||||
|
||||
public long getSeed()
|
||||
{
|
||||
return getEngine().getTarget().getWorld().seed();
|
||||
}
|
||||
|
||||
public double expression(String expressionName, double x, double y, double z)
|
||||
{
|
||||
IrisExpression expression = getData().getExpressionLoader().load(expressionName);
|
||||
return expression.evaluate(getComplex().getRng(), x, y, z);
|
||||
}
|
||||
|
||||
public double expression(String expressionName, double x, double z)
|
||||
{
|
||||
IrisExpression expression = getData().getExpressionLoader().load(expressionName);
|
||||
return expression.evaluate(getComplex().getRng(), x, z);
|
||||
}
|
||||
|
||||
public IrisBiome getBiomeAt(int x, int z)
|
||||
{
|
||||
return getEngine().getSurfaceBiome(x, z);
|
||||
}
|
||||
|
||||
public IrisDimension getDimension()
|
||||
{
|
||||
return getEngine().getDimension();
|
||||
}
|
||||
|
||||
public void info(String log)
|
||||
{
|
||||
Iris.info(log);
|
||||
}
|
||||
|
||||
public void debug(String log)
|
||||
{
|
||||
Iris.debug(log);
|
||||
}
|
||||
|
||||
public void warn(String log)
|
||||
{
|
||||
Iris.warn(log);
|
||||
}
|
||||
|
||||
public void error(String log)
|
||||
{
|
||||
Iris.error(log);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user