generated from AllayMC/JavaPluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
rootProject.name = "JavaPluginTemplate" | ||
rootProject.name = "AllayVanillaWorldGen" | ||
|
28 changes: 28 additions & 0 deletions
28
src/main/java/org/allaymc/allayvanillaworldgen/AllayVanillaWorldGen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/org/allaymc/allayvanillaworldgen/JeGeneratorLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
src/main/java/org/allaymc/javaplugintemplate/JavaPluginTemplate.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |