Skip to content

Commit

Permalink
Convert to paper plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Sep 26, 2024
1 parent 032fce4 commit 275a0b3
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 80 deletions.
31 changes: 22 additions & 9 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import net.minecrell.pluginyml.paper.PaperPluginDescription

plugins {
kotlin("jvm")
kotlin("plugin.serialization")

id("com.gradleup.shadow") version "8.3.2"
id("net.minecrell.plugin-yml.bukkit") version "0.6.0"
id("net.minecrell.plugin-yml.paper") version "0.6.0"
id("xyz.jpenilla.run-paper") version "2.3.0"

id("io.github.seggan.uom")
Expand All @@ -19,7 +21,7 @@ repositories {

dependencies {
fun DependencyHandlerScope.libraryAndTest(dependency: Any) {
library(dependency)
paperLibrary(dependency)
testImplementation(dependency)
}

Expand All @@ -28,7 +30,7 @@ dependencies {
testImplementation(dependency)
}

library(kotlin("stdlib"))
paperLibrary(kotlin("stdlib"))
libraryAndTest("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0") // For some reason libraryloader doesn't like this
libraryAndTest(kotlin("reflect"))
Expand Down Expand Up @@ -74,8 +76,6 @@ tasks.test {
}

tasks.shadowJar {
dependsOn(tasks.test)

manifest {
attributes["paperweight-mappings-namespace"] = "mojang"
}
Expand Down Expand Up @@ -107,15 +107,28 @@ tasks.shadowJar {
archiveBaseName = "galactifun2"
}

bukkit {
paper {
name = rootProject.name
main = "io.github.addoncommunity.galactifun.Galactifun2"
loader = "io.github.addoncommunity.galactifun.Galactifun2Loader"
bootstrapper = "io.github.addoncommunity.galactifun.Galactifun2Bootstrapper"
version = project.version.toString()
author = "Seggan"
apiVersion = "1.20"
softDepend = listOf("ClayTech")
loadBefore = listOf("Multiverse-Core")
depend = listOf("Slimefun")
generateLibrariesJson = true

serverDependencies {
register("Multiverse-Core") {
required = false
load = PaperPluginDescription.RelativeLoadOrder.AFTER
}

register("Slimefun") {
required = true
load = PaperPluginDescription.RelativeLoadOrder.BEFORE
joinClasspath = true
}
}
}

tasks.runServer {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.addoncommunity.galactifun;

import com.google.gson.Gson;
import io.papermc.paper.plugin.loader.PluginClasspathBuilder;
import io.papermc.paper.plugin.loader.PluginLoader;
import io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.repository.RemoteRepository;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@SuppressWarnings("UnstableApiUsage")
public class Galactifun2Loader implements PluginLoader {

@Override
public void classloader(@NotNull PluginClasspathBuilder classpathBuilder) {
MavenLibraryResolver resolver = new MavenLibraryResolver();
PluginLibraries pluginLibraries = load();
pluginLibraries.asDependencies().forEach(resolver::addDependency);
pluginLibraries.asRepositories().forEach(resolver::addRepository);
classpathBuilder.addLibrary(resolver);
}

private PluginLibraries load() {
try (var in = getClass().getResourceAsStream("/paper-libraries.json")) {
assert in != null;
return new Gson().fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), PluginLibraries.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private record PluginLibraries(Map<String, String> repositories, List<String> dependencies) {
public Stream<Dependency> asDependencies() {
return dependencies.stream()
.map(d -> new Dependency(new DefaultArtifact(d), null));
}

public Stream<RemoteRepository> asRepositories() {
return repositories.entrySet().stream()
.map(e -> new RemoteRepository.Builder(e.getKey(), "default", e.getValue()).build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import io.github.seggan.sf4k.AbstractAddon
import io.github.seggan.sf4k.serial.serializers.BukkitSerializerRegistry
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun
import io.github.thebusybiscuit.slimefun4.libraries.paperlib.PaperLib
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.withContext
Expand All @@ -48,38 +47,21 @@ import kotlin.script.experimental.host.toScriptSource
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

open class Galactifun2 : AbstractAddon() {
object Galactifun2 : AbstractAddon() {

private lateinit var manager: PaperCommandManager
lateinit var launchMessages: List<String> private set
lateinit var structuresFolder: Path private set

var isTest = classLoader.javaClass.packageName.startsWith("be.seeseemelk.mockbukkit")

override suspend fun onLoadAsync() {
if (!isTest) {
Bukkit.spigot().config["world-settings.default.verbose"] = false
}
BukkitSerializerRegistry.addSerializer(BlockVectorSerializer)
}

override suspend fun onEnableAsync() {
instance = this

var shouldDisable = false
if (!PaperLib.isPaper() && !isTest) {
logger.log(Level.SEVERE, "Galactifun2 only supports Paper and its forks (e.x. Airplane or Purpur)")
logger.log(Level.SEVERE, "Please use Paper or a fork of Paper")
shouldDisable = true
}
if (Slimefun.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_19)) {
logger.log(Level.SEVERE, "Galactifun2 only supports Minecraft 1.19 and above")
logger.log(Level.SEVERE, "Please use Minecraft 1.19 or above")
shouldDisable = true
}
if (Bukkit.getPluginManager().isPluginEnabled("ClayTech")) {
logger.log(Level.SEVERE, "Galactifun2 will not work properly with ClayTech")
logger.log(Level.SEVERE, "Please disable ClayTech")
if (Slimefun.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_20_5)) {
logger.log(Level.SEVERE, "Galactifun2 only supports Minecraft 1.20.5 and above")
logger.log(Level.SEVERE, "Please use Minecraft 1.20.5 or above")
shouldDisable = true
}
if (Bukkit.getPluginManager().isPluginEnabled("Galactifun")) {
Expand All @@ -93,9 +75,7 @@ open class Galactifun2 : AbstractAddon() {
return
}

if (!isTest) {
Metrics(this, 23447)
}
Metrics(this, 23447)

manager = PaperCommandManager(this)
manager.enableUnstableAPI("help")
Expand Down Expand Up @@ -156,6 +136,8 @@ open class Galactifun2 : AbstractAddon() {

GalactifunItems // Trigger static init

doTestingStuff()

launch {
Bukkit.getConsoleSender().sendMessage(
NamedTextColor.GREEN + """################# Galactifun2 $pluginVersion #################
Expand All @@ -166,8 +148,6 @@ open class Galactifun2 : AbstractAddon() {
###################################################""".trimIndent()
)
}

doTestingStuff()
}

override fun getJavaPlugin(): JavaPlugin = this
Expand Down Expand Up @@ -237,14 +217,9 @@ open class Galactifun2 : AbstractAddon() {
}
}

private var instance: Galactifun2? = null

val pluginInstance: Galactifun2
get() = checkNotNull(instance) { "Plugin is not enabled" }

fun JavaPlugin.launchAsync(
context: CoroutineContext = asyncDispatcher,
block: suspend CoroutineScope.() -> Unit
): Job = pluginInstance.launch {
): Job = Galactifun2.launch {
withContext(context, block)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.addoncommunity.galactifun

import io.papermc.paper.plugin.bootstrap.BootstrapContext
import io.papermc.paper.plugin.bootstrap.PluginBootstrap
import io.papermc.paper.plugin.bootstrap.PluginProviderContext
import org.bukkit.plugin.java.JavaPlugin

@Suppress("UnstableApiUsage")
class Galactifun2Bootstrapper : PluginBootstrap {

override fun bootstrap(context: BootstrapContext) {
// Nothing to do here
}

override fun createPlugin(context: PluginProviderContext): JavaPlugin {
return Galactifun2
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.addoncommunity.galactifun.api.objects.planet

import io.github.addoncommunity.galactifun.Galactifun2
import io.github.addoncommunity.galactifun.api.objects.planet.gen.WorldGenerator
import io.github.addoncommunity.galactifun.pluginInstance
import io.github.addoncommunity.galactifun.util.general.log
import org.bukkit.GameRule
import org.bukkit.Material
Expand All @@ -19,7 +19,7 @@ abstract class AlienWorld(name: String, baseItem: ItemStack) : PlanetaryWorld(na
private val blockMappings = EnumMap<Material, ItemStack>(Material::class.java)

override fun loadWorld(): World {
pluginInstance.logger.log("Loading world $name")
Galactifun2.logger.log("Loading world $name")

val world = WorldCreator("world_galactifun_$id")
.generator(generator)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.addoncommunity.galactifun.api.objects.properties

import io.github.addoncommunity.galactifun.EARTH_GRAVITY
import io.github.addoncommunity.galactifun.Galactifun2
import io.github.addoncommunity.galactifun.api.objects.CelestialObject
import io.github.addoncommunity.galactifun.pluginInstance
import io.github.addoncommunity.galactifun.units.*
import io.github.addoncommunity.galactifun.units.Angle.Companion.radians
import io.github.addoncommunity.galactifun.units.Distance.Companion.meters
Expand Down Expand Up @@ -143,7 +143,7 @@ data class Orbit(
} while (iterations++ != MAX_ITERATIONS && minDiff.radians > 1e-3)

if (iterations == MAX_ITERATIONS + 1) {
pluginInstance.logger.warning("Failed to find a transfer orbit after $iterations iterations")
Galactifun2.logger.warning("Failed to find a transfer orbit after $iterations iterations")
}

return when (val transfer = transfers.minBy { it.key }.value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.addoncommunity.galactifun.impl

import io.github.addoncommunity.galactifun.Galactifun2
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas
import io.github.addoncommunity.galactifun.impl.items.*
import io.github.addoncommunity.galactifun.pluginInstance
import io.github.addoncommunity.galactifun.units.Force.Companion.kilonewtons
import io.github.addoncommunity.galactifun.units.Volume.Companion.liters
import io.github.addoncommunity.galactifun.util.items.MaterialType
Expand Down Expand Up @@ -94,7 +94,7 @@ object GalactifunItems {
init {
for (gas in Gas.entries) {
if (gas.item == null) continue
Gas.Item(GalactifunCategories.GASES, gas, RecipeType.NULL, emptyArray()).register(pluginInstance)
Gas.Item(GalactifunCategories.GASES, gas, RecipeType.NULL, emptyArray()).register(Galactifun2)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package io.github.addoncommunity.galactifun.impl.items

import com.destroystokyo.paper.ParticleBuilder
import com.github.shynixn.mccoroutine.bukkit.launch
import io.github.addoncommunity.galactifun.Galactifun2
import io.github.addoncommunity.galactifun.api.betteritem.BetterSlimefunItem
import io.github.addoncommunity.galactifun.api.betteritem.ItemHandler
import io.github.addoncommunity.galactifun.api.betteritem.Ticker
import io.github.addoncommunity.galactifun.api.rockets.RocketInfo
import io.github.addoncommunity.galactifun.impl.items.abstract.Seat
import io.github.addoncommunity.galactifun.impl.managers.PlanetManager
import io.github.addoncommunity.galactifun.impl.managers.RocketManager
import io.github.addoncommunity.galactifun.pluginInstance
import io.github.addoncommunity.galactifun.units.abs
import io.github.addoncommunity.galactifun.util.*
import io.github.addoncommunity.galactifun.util.bukkit.*
Expand Down Expand Up @@ -66,7 +66,7 @@ class CommandComputer(
private val frozenEntities = mutableSetOf<Entity>()

init {
Bukkit.getPluginManager().registerEvents(this, pluginInstance)
Bukkit.getPluginManager().registerEvents(this, Galactifun2)
}

@EventHandler
Expand Down Expand Up @@ -136,7 +136,7 @@ class CommandComputer(
&& BlockStorage.check(seat) is CaptainsChair
&& seat.getBlockStorage<BlockPosition>("rocket") == pos
) {
RocketManager.launches += pluginInstance.launch { launchRocket(p, pos, info, seat) }
RocketManager.launches += Galactifun2.launch { launchRocket(p, pos, info, seat) }
} else {
e.player.sendMessage(NamedTextColor.GOLD + info.info)
}
Expand Down Expand Up @@ -207,7 +207,7 @@ class CommandComputer(
var launched = false

// Engine smoke
pluginInstance.launch {
Galactifun2.launch {
while (!launched) {
for ((_, engine) in firstStage.engines) {
ParticleBuilder(Particle.CAMPFIRE_SIGNAL_SMOKE)
Expand All @@ -221,7 +221,7 @@ class CommandComputer(
}

// Countdown
val launchMessages = ArrayDeque(pluginInstance.launchMessages)
val launchMessages = ArrayDeque(Galactifun2.launchMessages)
launchMessages.shuffle()
repeat(10) {
val message = NamedTextColor.GOLD + "${launchMessages.removeFirst()}..."
Expand Down Expand Up @@ -305,7 +305,7 @@ class CommandComputer(
}
for (entity in entities) {
pluginInstance.launch {
Galactifun2.launch {
if (!entity.galactifunTeleport(
dest + offsets[entity]!!.copy(world = PlanetManager.spaceWorld)
).await()) {
Expand Down
Loading

0 comments on commit 275a0b3

Please sign in to comment.