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

NeoForge Support #390

Merged
merged 2 commits into from
Apr 7, 2024
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
9 changes: 9 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ pluginManagement {
name = 'Forge'
url = 'https://maven.minecraftforge.net/'
}
maven {
name = 'NeoForge'
url = 'https://maven.neoforged.net/releases'
}
gradlePluginPortal()
}
}

plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0'
}

rootProject.name = 'spark'
include (
'spark-api',
Expand All @@ -23,6 +31,7 @@ include (
'spark-sponge7',
'spark-sponge8',
'spark-forge',
'spark-neoforge',
'spark-fabric',
'spark-nukkit',
'spark-waterdog',
Expand Down
64 changes: 64 additions & 0 deletions spark-neoforge/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
plugins {
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'net.neoforged.gradle.userdev' version '7.0.97'
}

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

tasks.withType(JavaCompile) {
// override, compile targeting J17
options.release = 17
}

minecraft {
accessTransformers {
file('src/main/resources/META-INF/accesstransformer.cfg')
}
}

configurations {
shade
implementation.extendsFrom shade
}

dependencies {
implementation "net.neoforged:neoforge:20.4.223"
shade project(':spark-common')
}

processResources {
from(sourceSets.main.resources.srcDirs) {
include 'META-INF/mods.toml'
expand (
'pluginVersion': project.pluginVersion,
'pluginDescription': project.pluginDescription
)
}

from(sourceSets.main.resources.srcDirs) {
exclude 'META-INF/mods.toml'
}
}

shadowJar {
archiveFileName = "spark-${project.pluginVersion}-neoforge.jar"
configurations = [project.configurations.shade]

relocate 'net.kyori.adventure', 'me.lucko.spark.lib.adventure'
relocate 'net.kyori.examination', 'me.lucko.spark.lib.adventure.examination'
relocate 'net.bytebuddy', 'me.lucko.spark.lib.bytebuddy'
relocate 'com.google.protobuf', 'me.lucko.spark.lib.protobuf'
relocate 'org.objectweb.asm', 'me.lucko.spark.lib.asm'
relocate 'one.profiler', 'me.lucko.spark.lib.asyncprofiler'
relocate 'me.lucko.bytesocks.client', 'me.lucko.spark.lib.bytesocks'
relocate 'org.java_websocket', 'me.lucko.spark.lib.bytesocks.ws'

exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
}

artifacts {
archives shadowJar
shadow shadowJar
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.neoforge;

import cpw.mods.modlauncher.TransformingClassLoader;
import me.lucko.spark.common.sampler.source.ClassSourceLookup;

public class NeoForgeClassSourceLookup implements ClassSourceLookup {

@Override
public String identify(Class<?> clazz) {
if (clazz.getClassLoader() instanceof TransformingClassLoader) {
String name = clazz.getModule().getName();
return name.equals("forge") || name.equals("minecraft") ? null : name;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.neoforge;

import me.lucko.spark.common.command.sender.AbstractCommandSender;
import me.lucko.spark.neoforge.plugin.NeoForgeSparkPlugin;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.commands.CommandSource;
import net.minecraft.network.chat.Component.Serializer;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.rcon.RconConsoleSource;
import net.minecraft.world.entity.player.Player;

import java.util.Objects;
import java.util.UUID;

public class NeoForgeCommandSender extends AbstractCommandSender<CommandSource> {
private final NeoForgeSparkPlugin plugin;

public NeoForgeCommandSender(CommandSource source, NeoForgeSparkPlugin plugin) {
super(source);
this.plugin = plugin;
}

@Override
public String getName() {
if (super.delegate instanceof Player) {
return ((Player) super.delegate).getGameProfile().getName();
} else if (super.delegate instanceof MinecraftServer) {
return "Console";
} else if (super.delegate instanceof RconConsoleSource) {
return "RCON Console";
} else {
return "unknown:" + super.delegate.getClass().getSimpleName();
}
}

@Override
public UUID getUniqueId() {
if (super.delegate instanceof Player) {
return ((Player) super.delegate).getUUID();
}
return null;
}

@Override
public void sendMessage(Component message) {
MutableComponent component = Serializer.fromJson(GsonComponentSerializer.gson().serializeToTree(message));
Objects.requireNonNull(component, "component");
super.delegate.sendSystemMessage(component);
}

@Override
public boolean hasPermission(String permission) {
return this.plugin.hasPermission(super.delegate, permission);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.neoforge;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import me.lucko.spark.common.platform.MetadataProvider;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.server.packs.repository.PackSource;

import java.util.LinkedHashMap;
import java.util.Map;

public class NeoForgeExtraMetadataProvider implements MetadataProvider {

private final PackRepository resourcePackManager;

public NeoForgeExtraMetadataProvider(PackRepository resourcePackManager) {
this.resourcePackManager = resourcePackManager;
}

@Override
public Map<String, JsonElement> get() {
Map<String, JsonElement> metadata = new LinkedHashMap<>();
metadata.put("datapacks", datapackMetadata());
return metadata;
}

private JsonElement datapackMetadata() {
JsonObject datapacks = new JsonObject();
for (Pack profile : this.resourcePackManager.getSelectedPacks()) {
JsonObject obj = new JsonObject();
obj.addProperty("name", profile.getTitle().getString());
obj.addProperty("description", profile.getDescription().getString());
obj.addProperty("source", resourcePackSource(profile.getPackSource()));
datapacks.add(profile.getId(), obj);
}
return datapacks;
}

private static String resourcePackSource(PackSource source) {
if (source == PackSource.DEFAULT) {
return "none";
} else if (source == PackSource.BUILT_IN) {
return "builtin";
} else if (source == PackSource.WORLD) {
return "world";
} else if (source == PackSource.SERVER) {
return "server";
} else {
return "unknown";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.neoforge;

import me.lucko.spark.common.platform.PlatformInfo;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.internal.versions.neoform.NeoFormVersion;

public class NeoForgePlatformInfo implements PlatformInfo {
private final Type type;

public NeoForgePlatformInfo(Type type) {
this.type = type;
}

@Override
public Type getType() {
return this.type;
}

@Override
public String getName() {
return "NeoForge";
}

@Override
public String getVersion() {
return NeoForgeVersion.getVersion();
}

@Override
public String getMinecraftVersion() {
return NeoFormVersion.getMCVersion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.neoforge;

import com.google.common.collect.ImmutableMap;
import me.lucko.spark.common.monitor.ping.PlayerPingProvider;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;

import java.util.Map;

public class NeoForgePlayerPingProvider implements PlayerPingProvider {
private final MinecraftServer server;

public NeoForgePlayerPingProvider(MinecraftServer server) {
this.server = server;
}

@Override
public Map<String, Integer> poll() {
ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
for (ServerPlayer player : this.server.getPlayerList().getPlayers()) {
builder.put(player.getGameProfile().getName(), player.connection.latency());
}
return builder.build();
}
}
Loading
Loading