Commit 2ff9eb39 authored by Ticxo's avatar Ticxo
Browse files

lol done

parent 9d3c8a5d
......@@ -6,16 +6,50 @@
<groupId>com.ticxo.megmolang</groupId>
<artifactId>Meg-Molang</artifactId>
<version>1.0-SNAPSHOT</version>
<version>R1.0.0</version>
<properties>
<project.version>R1.0.0</project.version>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<build>
<finalName>Model-Engine-${project.version}</finalName>
<finalName>MEG-MoLang-Extension-${version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.1-SNAPSHOT</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
......
package com.ticxo.megml;
import com.bedrockk.molang.MoLang;
import com.ticxo.modelengine.api.ModelEngineAPI;
import com.ticxo.modelengine.api.animation.keyframe.IKeyframeData;
import org.bukkit.plugin.java.JavaPlugin;
public class MegML extends JavaPlugin {
private static MegML instance;
private RuntimeManager manager;
@Override
public void onEnable() {
instance = this;
manager = new RuntimeManager();
manager.runTaskTimerAsynchronously(this, 0, 200);
ModelEngineAPI.getKeyframeReaderRegistry().register("molang", MegML::parse);
}
@Override
......@@ -14,4 +26,8 @@ public class MegML extends JavaPlugin {
}
public static IKeyframeData parse(String data) {
return new QueryData(instance.manager, MoLang.parse(data));
}
}
package com.ticxo.megml.query;
package com.ticxo.megml;
import com.bedrockk.molang.runtime.MoParams;
import com.ticxo.modelengine.api.animation.AnimationProperty;
......@@ -21,21 +21,26 @@ public class ModelQuery {
public static final Map<String, BiFunction<MoParams, AnimationProperty, Object>> MODEL_QUERY = new HashMap<>() {
{
put("anim_time", (params, property) -> property.getTime());
put("body_y_rotation", (params, property) -> getBase(property).getYBodyRot());
put("count", (params, property) -> params.getParams().size());
put("head_x_rotation", (params, property) -> getBase(property).getXHeadRot());
put("head_y_rotation", (params, property) -> getBase(property).getYHeadRot());
put("is_on_ground", (params, property) -> livingOrZero(property, living -> living.isOnGround() ? 1 : 0));
put("is_in_water", (params, property) -> livingOrZero(property, living -> living.isInWater() ? 1 : 0));
put("health", (params, property) -> livingOrZero(property, Damageable::getHealth));
put("max_health", (params, property) -> livingOrZero(property, living -> living.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
put("is_on_fire", (params, property) -> livingOrZero(property, living -> Math.min(1, living.getFireTicks())));
put("on_fire_time", (params, property) -> livingOrZero(property, Entity::getMaxFireTicks));
put("ground_speed", (params, property) -> getModel(property).getMoveController().getVelocity().distance(ZERO) * 20);
put("anim_time", (params, property) -> property.getTime());
put("body_y_rotation", (params, property) -> getBase(property).getYBodyRot());
put("count", (params, property) -> params.getParams().size());
put("head_x_rotation", (params, property) -> getBase(property).getXHeadRot());
put("head_y_rotation", (params, property) -> getBase(property).getYHeadRot());
put("is_on_ground", (params, property) -> livingOrZero(property, living -> living.isOnGround() ? 1 : 0));
put("is_in_water", (params, property) -> livingOrZero(property, living -> living.isInWater() ? 1 : 0));
put("health", (params, property) -> livingOrZero(property, Damageable::getHealth));
put("max_health", (params, property) -> livingOrZero(property, ModelQuery::getMaxHealth));
put("is_on_fire", (params, property) -> livingOrZero(property, living -> Math.min(1, living.getFireTicks())));
put("on_fire_time", (params, property) -> livingOrZero(property, Entity::getMaxFireTicks));
put("ground_speed", (params, property) -> getModel(property).getMoveController().getVelocity().distance(ZERO) * 20);
}
};
private static double getMaxHealth(LivingEntity living) {
var attr = living.getAttribute(Attribute.GENERIC_MAX_HEALTH);
return attr == null ? 0 : attr.getValue();
}
private static Object livingOrZero(AnimationProperty property, Function<LivingEntity, Object> function) {
if(getBase(property).getOriginal() instanceof LivingEntity living) {
return function.apply(living);
......@@ -48,7 +53,7 @@ public class ModelQuery {
}
private static ModeledEntity getModel(AnimationProperty property) {
return property.getModel();
return property.getModel().getModeledEntity();
}
}
package com.ticxo.megml;
import com.bedrockk.molang.Expression;
import com.ticxo.modelengine.api.animation.AnimationProperty;
import com.ticxo.modelengine.api.animation.keyframe.IKeyframeData;
import lombok.RequiredArgsConstructor;
import java.util.List;
@RequiredArgsConstructor
public class QueryData implements IKeyframeData {
private final RuntimeManager manager;
private final List<Expression> script;
@Override
public double getValue(AnimationProperty property) {
var runtime = manager.getOrCreateRuntime(property);
try {
var value = runtime.execute(script);
if(value != null)
return value.asDouble();
}catch (Throwable e) {
e.printStackTrace();
}
return 0;
}
}
package com.ticxo.megml;
import com.bedrockk.molang.MoLang;
import com.bedrockk.molang.runtime.MoLangRuntime;
import com.bedrockk.molang.runtime.struct.QueryStruct;
import com.ticxo.modelengine.api.animation.AnimationProperty;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class RuntimeManager extends BukkitRunnable {
private final Map<AnimationProperty, MoLangRuntime> runtimes = new ConcurrentHashMap<>();
@Override
public void run() {
for(var property : runtimes.keySet()) {
if(property.isFinished())
runtimes.remove(property);
}
}
public MoLangRuntime getOrCreateRuntime(AnimationProperty property) {
return runtimes.computeIfAbsent(property, p -> {
var runtime = MoLang.createRuntime();
runtime.getEnvironment().getStructs().put("query", new QueryStruct<>(p, ModelQuery.MODEL_QUERY));
return runtime;
});
}
}
name: ModelEngine-MoLang
author: Ticxo
main: com.ticxo.megml.MegML
version: ${project.version}
version: ${version}
api-version: 1.16
depend:
- ModelEngine
\ No newline at end of file
name: ModelEngine-MoLang
author: Ticxo
main: com.ticxo.megml.MegML
version: R1.0.0
api-version: 1.16
depend:
- ModelEngine
\ No newline at end of file
#Generated by Maven
#Wed Dec 07 01:24:15 PST 2022
groupId=com.ticxo.megmolang
artifactId=Meg-Molang
version=R1.0.0
com\ticxo\megml\MegML.class
com\ticxo\megml\ModelQuery.class
com\ticxo\megml\ModelQuery$1.class
com\ticxo\megml\QueryData.class
com\ticxo\megml\RuntimeManager.class
C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\RuntimeManager.java
C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\QueryData.java
C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\ModelQuery.java
C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\MegML.java
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment