Skip to content

Commit

Permalink
feat: upload code
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Sep 5, 2024
1 parent 77b23a1 commit b118cae
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 28 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plugins {
id("com.github.johnrengelman.shadow") version "8.1.1"
}

group = "org.allaymc.myplugin"
description = "My plugin"
group = "org.allaymc.allayvanillaworldgen"
description = "AllayVanillaWorldGen"
version = "1.0.0"

java {
Expand All @@ -23,7 +23,9 @@ repositories {

dependencies {
compileOnly(group = "com.github.AllayMC.Allay", name = "Allay-API", version = "master-SNAPSHOT")
compileOnly(group = "com.github.AllayMC.Allay", name = "Allay-Server", version = "master-SNAPSHOT")
compileOnly(group = "org.projectlombok", name = "lombok", version = "1.18.34")
implementation(group = "org.allaymc", name = "JEGeneratorBinary", version = "1.20.6-R0.2")

annotationProcessor(group = "org.projectlombok", name = "lombok", version = "1.18.34")
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name = "JavaPluginTemplate"
rootProject.name = "AllayVanillaWorldGen"

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.allaymc.allayvanillaworldgen;

import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.plugin.Plugin;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.world.DimensionInfo;

@Slf4j
public class AllayVanillaWorldGen extends Plugin {
@Override
public void onLoad() {
log.info("AllayVanillaWorldGen loaded!");
}

@Override
public void onEnable() {
log.info("AllayVanillaWorldGen enabled!");
JeGeneratorLoader.setup();
Registries.WORLD_GENERATOR_FACTORIES.register("JEGEN_OVERWORLD", $1 -> JeGeneratorLoader.getJeGenerator(DimensionInfo.OVERWORLD));
Registries.WORLD_GENERATOR_FACTORIES.register("JEGEN_NETHER", $1 -> JeGeneratorLoader.getJeGenerator(DimensionInfo.NETHER));
Registries.WORLD_GENERATOR_FACTORIES.register("JEGEN_END", $1 -> JeGeneratorLoader.getJeGenerator(DimensionInfo.THE_END));
}

@Override
public void onDisable() {
log.info("AllayVanillaWorldGen disabled!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.allaymc.allayvanillaworldgen;

import io.papermc.paperclip.Paperclip;
import org.allaymc.api.world.DimensionInfo;
import org.allaymc.api.world.generator.WorldGenerator;
import org.allaymc.server.Allay;
import org.allaymc.server.world.generator.AllayWorldGenerator;

import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

public final class JeGeneratorLoader {
public static final String WORK_PATH = "jegenerator";
private static final AtomicBoolean LOADED = new AtomicBoolean(false);
private static MethodHandle OVERWORLD;
private static MethodHandle NETHER;
private static MethodHandle THE_END;

public static void setup() {
if (LOADED.compareAndSet(false, true)) {
setup0();
waitStart();
}
}

public static WorldGenerator getJeGenerator(DimensionInfo info) {
if (LOADED.compareAndSet(false, true)) {
JeGeneratorLoader.setup();
JeGeneratorLoader.waitStart();
}
try {
if (info == DimensionInfo.NETHER) {
return (AllayWorldGenerator) NETHER.invokeExact();
} else if (info == DimensionInfo.THE_END) {
return (AllayWorldGenerator) THE_END.invokeExact();
} else {
return (AllayWorldGenerator) OVERWORLD.invokeExact();
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

private static void setup0() {
File file = Path.of(WORK_PATH).toFile();
if (!file.exists()) {
if (!file.mkdirs()) {
throw new RuntimeException("Failed to create directory: " + WORK_PATH);
}
}
Paperclip.setup(Allay.EXTRA_RESOURCE_CLASS_LOADER, new String[]{WORK_PATH, "allay", "--noconsole", "--nogui", "--universe=jegenerator"});
try {
final Class<?> main = Class.forName("org.allaymc.jegenerator.AllayVanillaGeneratorExtension", true, Allay.EXTRA_RESOURCE_CLASS_LOADER);
final MethodType methodType = MethodType.methodType(AllayWorldGenerator.class);
OVERWORLD = MethodHandles.lookup()
.findStatic(main, "overworld", methodType)
.asFixedArity();
NETHER = MethodHandles.lookup()
.findStatic(main, "nether", methodType)
.asFixedArity();
THE_END = MethodHandles.lookup()
.findStatic(main, "end", methodType)
.asFixedArity();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

private static void waitStart() {
spinUntil(() -> !System.getProperties().getOrDefault("complete_start", false).equals("true"), Duration.of(20, ChronoUnit.MILLIS));
}

private static void spinUntil(Supplier<Boolean> end, Duration interval) {
while (end.get()) {
try {
long times = MILLISECONDS.convert(interval);
//noinspection BusyWait
Thread.sleep(times);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/resources/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"entrance": "org.allaymc.javaplugintemplate.JavaPluginTemplate",
"name": "JavaPluginTemplate",
"authors": ["mdx"],
"entrance": "org.allaymc.allayvanillaworldgen.AllayVanillaWorldGen",
"name": "AllayVanillaWorldGen",
"authors": ["CoolLoong", "daoge_cmd"],
"version": "1.0.0"
}

0 comments on commit b118cae

Please sign in to comment.