Skip to content
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
28 changes: 24 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'java'
// id 'skript-test' version('1.0.1')
id 'com.gradleup.shadow' version('9.2.2')
}

group = 'com.sovdee'
Expand All @@ -12,6 +14,7 @@ test {

repositories {
mavenCentral()
mavenLocal()
maven {
url 'https://repo.papermc.io/repository/maven-public/'
}
Expand All @@ -21,14 +24,31 @@ repositories {
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

compileOnly 'org.spigotmc:spigot-api:1.21.4-R0.1-SNAPSHOT'
compileOnly "com.github.SkriptLang:Skript:2.11.0"
compileOnly "com.github.SkriptLang:Skript:2.12.2"
compileOnly 'org.jetbrains:annotations:26.0.1'
implementation 'net.bytebuddy:byte-buddy:1.14.18' // for runtime code generation
}

processResources {
filter ReplaceTokens, tokens: ["version": project.property("version")]
}

shadowJar {
relocate 'net.bytebuddy', 'com.sovdee.oopsk.bytebuddy'
minimize()
archiveClassifier.set('')
}

build {
dependsOn shadowJar
}


//skriptTest {
// dependsOn build
// testScriptDirectory = file("src/test/scripts")
// extraPluginsDirectory = new File("./build/libs")
// skriptRepoRef = "dev/patch"
// runVanillaTests = false
//}
9 changes: 9 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
}

rootProject.name = 'oopsk'


6 changes: 6 additions & 0 deletions src/main/java/com/sovdee/oopsk/Oopsk.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.njol.skript.bstats.bukkit.Metrics;
import com.sovdee.oopsk.core.StructManager;
import com.sovdee.oopsk.core.TemplateManager;
import com.sovdee.oopsk.core.generation.TemporaryClassManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
Expand All @@ -16,6 +17,7 @@ public final class Oopsk extends JavaPlugin {
private static SkriptAddon addon;
private static StructManager structManager;
private static TemplateManager templateManager;
private static TemporaryClassManager classManager = new TemporaryClassManager();
private static Logger logger;

public static Oopsk getInstance() {
Expand All @@ -34,6 +36,10 @@ public static TemplateManager getTemplateManager() {
return templateManager;
}

public static TemporaryClassManager getClassManager() {
return classManager;
}

public static void info(String message) {
logger.info(message);
}
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/com/sovdee/oopsk/core/Struct.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,41 @@ public class Struct {
private StructTemplate template;
private final Map<Field<?>, Object[]> fieldValues;

public static Struct newInstance(@NotNull StructTemplate template, @Nullable Event event) {
Class<? extends Struct> structClass = template.getCustomClass();
try {
return structClass.getDeclaredConstructor(StructTemplate.class, Event.class).newInstance(template, event);
} catch (Exception e) {
throw new RuntimeException("Failed to create new instance of struct class " + structClass.getName(), e);
}
}

public static Struct newInstance(Struct source) {
Class<? extends Struct> structClass = source.getTemplate().getCustomClass();
try {
return structClass.getDeclaredConstructor(Struct.class).newInstance(source);
} catch (Exception e) {
throw new RuntimeException("Failed to create new instance of struct class " + structClass.getName(), e);
}
}

public static Struct newInstance(@NotNull StructTemplate template, @Nullable Event event, @Nullable Map<String, Expression<?>> initialValues) {
Class<? extends Struct> structClass = template.getCustomClass();
try {
return structClass.getDeclaredConstructor(StructTemplate.class, Event.class, Map.class).newInstance(template, event, initialValues);
} catch (Exception e) {
throw new RuntimeException("Failed to create new instance of struct class " + structClass.getName(), e);
}
}

/**
* Creates a new struct with the given template and event.
*
* @param template The template to create the struct from.
* @param event The event to evaluate the default values in.
* @see StructManager#createStruct(StructTemplate, Event)
*/
public Struct(@NotNull StructTemplate template, @Nullable Event event) {
protected Struct(@NotNull StructTemplate template, @Nullable Event event) {
this.template = template;
fieldValues = new HashMap<>();
for (Field<?> field : template.getFields()) {
Expand All @@ -44,7 +71,7 @@ public Struct(@NotNull StructTemplate template, @Nullable Event event) {
* @param source The struct to copy from.
* @see StructManager#createStruct(StructTemplate, Event)
*/
public Struct(Struct source) {
protected Struct(Struct source) {
this.template = source.template;
fieldValues = new HashMap<>();
for (Map.Entry<Field<?>, Object[]> entry : source.fieldValues.entrySet()) {
Expand All @@ -64,7 +91,7 @@ public Struct(Struct source) {
* @param initialValues The initial values to set in the struct. This is a map of field names to expressions.
* @see StructManager#createStruct(StructTemplate, Event)
*/
Struct(@NotNull StructTemplate template, @Nullable Event event, @Nullable Map<String, Expression<?>> initialValues) {
protected Struct(@NotNull StructTemplate template, @Nullable Event event, @Nullable Map<String, Expression<?>> initialValues) {
this.template = template;
fieldValues = new HashMap<>();
for (Field<?> field : template.getFields()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/sovdee/oopsk/core/StructManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Struct createStruct(StructTemplate template, @Nullable Event event) {
* @return The created struct.
*/
public Struct createStruct(StructTemplate template, @Nullable Event event, @Nullable Map<String, Expression<?>> initialValues) {
Struct struct = new Struct(template, event, initialValues);
Struct struct = Struct.newInstance(template, event, initialValues);
activeStructs.computeIfAbsent(template, k -> Collections.newSetFromMap(new WeakHashMap<>())).add(struct);
return struct;
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/sovdee/oopsk/core/StructTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
public class StructTemplate {
private final String name;
private final Map<String, Field<?>> fields;
private final Class<? extends Struct> customClass;

/**
* Creates a new struct template with the given name and fields.
*
* @param name The name of the template.
* @param fields The fields of the template.
*/
public StructTemplate(String name, @NotNull List<Field<?>> fields) {
public StructTemplate(String name, @NotNull List<Field<?>> fields, Class<? extends Struct> customClass) {
this.name = name;
this.customClass = customClass;
this.fields = new HashMap<>();
for (Field<?> field : fields) {
this.fields.put(field.name(), field);
Expand All @@ -36,6 +38,13 @@ public String getName() {
return name;
}

/**
* @return The custom class for this struct, or null if none was specified.
*/
public Class<? extends Struct> getCustomClass() {
return customClass;
}

/**
* Parses all the default value expressions for this struct. Prints errors.
* @return true if no errors were encountered. False otherwise.
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/sovdee/oopsk/core/TemplateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class TemplateManager {

private final Map<String, StructTemplate> templates = new HashMap<>();
private final Map<Class<? extends Struct>, StructTemplate> templatesByClass = new HashMap<>();

/**
* Adds a new template to the manager. Attempts to reparent all orphaned structs that match this template's name.
Expand All @@ -29,6 +30,7 @@ public boolean addTemplate(@NotNull StructTemplate template) {
if (templates.containsKey(template.getName()))
return false; // Template with the same name already exists
templates.put(template.getName(), template);
templatesByClass.put(template.getCustomClass(), template);
// reparent all orphaned structs of this template
Oopsk.getStructManager().reparentStructs(template);
return true; // Template added successfully
Expand All @@ -44,6 +46,16 @@ public StructTemplate getTemplate(String name) {
return templates.get(name);
}

/**
* Retrieves a template by class.
*
* @param structClass The name of the template to retrieve.
* @return The template, or null if it does not exist.
*/
public StructTemplate getTemplate(Class<?extends Struct> structClass) {
return templatesByClass.get(structClass);
}

/**
* Removes a template from the manager. This will orphan all structs of this template.
*
Expand All @@ -54,6 +66,7 @@ public void removeTemplate(@NotNull StructTemplate template) {
if (!templates.containsKey(name))
return; // Template with the given name does not exist
templates.remove(name);
templatesByClass.remove(template.getCustomClass());
// mark all structs of this template as orphaned
Oopsk.getStructManager().orphanStructs(template);
}
Expand Down
Loading