Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sponge Implementation #73

Merged
merged 1 commit into from
May 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private MiniPlaceholders() {}
case VELOCITY -> Platform.VELOCITY;
case KRYPTON -> Platform.KRYPTON;
case FABRIC -> Platform.FABRIC;
case SPONGE -> Platform.SPONGE;
};
}

Expand Down Expand Up @@ -200,7 +201,7 @@ public static int getExpansionCount(){
* @see Expansion#builder(String)
* @since 2.1.0
*/
public @Nullable Expansion getExpansionByName(final @NotNull String name) {
public static @Nullable Expansion getExpansionByName(final @NotNull String name) {
for (final Expansion expansion : expansions) {
if (Objects.equals(expansion.name(), name)) {
return expansion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public enum Platform {
/**Krypton Platform */
KRYPTON,
/**Fabric Platform */
FABRIC
FABRIC,
/**Sponge Platform */
SPONGE
}
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ allprojects {
}
}

repositories {
maven("https://repo.jpenilla.xyz/snapshots/")
}

dependencies {
implementation(projects.miniplaceholdersVelocity)
implementation(projects.miniplaceholdersPaper)
implementation(projects.miniplaceholdersSponge)
//implementation(projects.miniplaceholdersKrypton)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ private PlaceholdersCommand(
final Supplier<ArrayList<String>> playersSuggestions,
final Function<String, Audience> toAudience,
final BiPredicate<A, String> hasPermission,
String command,
CommandManager<A> commandManager
final String command,
final CommandManager<A> commandManager
) {
this.playersSuggestions = playersSuggestions;
this.toAudience = toAudience;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public enum InternalPlatform {

PAPER, VELOCITY, KRYPTON, FABRIC;
PAPER, VELOCITY, KRYPTON, FABRIC, SPONGE;

private static InternalPlatform actualPlatform;

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cloud-annotations = { group = "cloud.commandframework", name = "cloud-annotation
cloud-velocity = { group = "cloud.commandframework", name = "cloud-velocity", version.ref = "cloud" }
cloud-paper = { group = "cloud.commandframework", name = "cloud-paper", version.ref = "cloud" }
cloud-fabric = { group = "cloud.commandframework", name = "cloud-fabric", version.ref = "cloud" }
cloud-sponge = { group = "cloud.commandframework", name = "cloud-sponge", version.ref = "cloud" }
cloud-sponge = { group = "cloud.commandframework", name = "cloud-sponge", version = "1.8.0-SNAPSHOT" }
cloud-extras = { group = "cloud.commandframework", name = "cloud-minecraft-extras", version.ref = "cloud" }

brigadier = { group = "com.mojang", name = "brigadier", version.ref = "brigadier" }
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ arrayOf(
"paper",
"velocity",
//"krypton",
"fabric"
"fabric",
"sponge"
).forEach {
include("miniplaceholders-$it")
project(":miniplaceholders-$it").projectDir = file(it)
Expand Down
2 changes: 2 additions & 0 deletions sponge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# run-paper files
/run
59 changes: 59 additions & 0 deletions sponge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import org.spongepowered.gradle.plugin.config.PluginLoaders
import org.spongepowered.plugin.metadata.model.PluginDependency

plugins {
id("org.spongepowered.gradle.plugin") version "2.1.1"
alias(libs.plugins.shadow)
}

repositories {
maven("https://repo.jpenilla.xyz/snapshots/")
}

dependencies {
implementation(projects.miniplaceholdersCommon)
implementation(projects.miniplaceholdersApi)
implementation(projects.miniplaceholdersConnect)
implementation(libs.cloud.sponge)
}

sponge {
apiVersion("8.1.0")
license("GPL-3")
loader {
name(PluginLoaders.JAVA_PLAIN)
version("1.0")
}
plugin("miniplaceholders") {
displayName("MiniPlaceholders")
entrypoint("io.github.miniplaceholders.sponge.SpongePlugin")
description(project.description)
links {
homepage("https://github.com/MiniPlaceholders/MiniPlaceholders")
source("https://github.com/MiniPlaceholders/MiniPlaceholders")
issues("https://github.com/MiniPlaceholders/MiniPlaceholders/issues")
}
contributor("4drian3d") {
description("Lead Developer")
}
dependency("spongeapi") {
loadOrder(PluginDependency.LoadOrder.AFTER)
optional(false)
}
}
}

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))

tasks {
compileJava {
options.encoding = Charsets.UTF_8.name()
options.release.set(17)
}
jar {
manifest {
attributes("Automatic-Module-Name" to "io.github.miniplaceholders.sponge")
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package io.github.miniplaceholders.sponge;

import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.sponge.SpongeCommandManager;
import com.google.inject.Inject;
import io.github.miniplaceholders.api.Expansion;
import io.github.miniplaceholders.api.utils.TagsUtils;
import io.github.miniplaceholders.common.PlaceholdersCommand;
import io.github.miniplaceholders.common.PlaceholdersPlugin;
import io.github.miniplaceholders.connect.InternalPlatform;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.Server;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
import org.spongepowered.api.event.lifecycle.StartingEngineEvent;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.builtin.jvm.Plugin;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Plugin("miniplaceholders")
public class SpongePlugin implements PlaceholdersPlugin {

@Inject
private PluginContainer pluginContainer;
@Inject
private Logger logger;

private Server server;

@Listener
public void onServerStart(final StartingEngineEvent<Server> event) {
this.server = event.engine();
logger.info("Starting MiniPlaceholders Sponge");
InternalPlatform.platform(InternalPlatform.SPONGE);

this.loadDefaultExpansions();
}

private final AtomicInteger registration = new AtomicInteger(0);

@Listener
public void onCommandRegister(final RegisterCommandEvent<Command.Parameterized> event) {
if (registration.getAndIncrement() == 0) {
this.registerPlatformCommand();
}
}

@Override
public void loadDefaultExpansions() {
Expansion.builder("server")
.globalPlaceholder("name", TagsUtils.staticTag("Sponge"))
.globalPlaceholder("online", (queue, ctx) -> TagsUtils.staticTag(Component.text(server.onlinePlayers().size())))
.globalPlaceholder("max_players", (queue, ctx) -> TagsUtils.staticTag(Component.text(server.maxPlayers())))
.globalPlaceholder("unique_joins", (queue, ctx) -> TagsUtils.staticTag(Component.text(server.userManager().streamAll().count())))
.globalPlaceholder("has_whitelist", (queue, ctx) -> TagsUtils.staticTag(Component.text(server.isWhitelistEnabled())))
.globalPlaceholder("total_chunks", (queue, ctx) -> {
int chunkCount = 0;
for (ServerWorld world : server.worldManager().worlds()){
chunkCount += world.entities().size();
}
return TagsUtils.staticTag(Component.text(chunkCount));
})
.globalPlaceholder("total_entities", (queue, ctx) -> {
int entityCount = 0;
for (ServerWorld world : server.worldManager().worlds()){
entityCount += world.entities().size();
}
return TagsUtils.staticTag(Component.text(entityCount));
})
.globalPlaceholder("mspt", (queue, ctx) -> TagsUtils.staticTag(Component.text(server.averageTickTime())))
.build()
.register();

}

@Override
public void registerPlatformCommand() {
final SpongeCommandManager<AudienceHolder> commandManager = new SpongeCommandManager<>(
pluginContainer,
CommandExecutionCoordinator.simpleCoordinator(),
AudienceHolder::cause,
AudienceHolder::new
);

PlaceholdersCommand.<AudienceHolder>builder()
.manager(commandManager)
.command("miniplaceholders")
.playerSuggestions(() -> server.onlinePlayers()
.stream()
.map(ServerPlayer::name)
.collect(Collectors.toCollection(ArrayList::new)))
.toAudience(st -> server.player(st).orElse(null))
.hasPermissionCheck((holder, permission) -> holder.cause.hasPermission(permission))
.build()
.register();
}

record AudienceHolder(@NotNull CommandCause cause) implements ForwardingAudience.Single {

@Override
public @NotNull Audience audience() {
return cause.audience();
}
}
}