Skip to content

Commit

Permalink
是时候展示真正的绝活了:@mod(dist = Dist.CLIENT)
Browse files Browse the repository at this point in the history
No @mod will be loaded on dedicated server!
  • Loading branch information
3TUSK committed Aug 17, 2024
1 parent fdaa007 commit bff1084
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 166 deletions.
185 changes: 139 additions & 46 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,41 +1,115 @@
plugins {
id 'java-library'
id 'eclipse'
id 'idea'
id 'maven-publish'
id 'net.minecraftforge.gradle' version '[6.0,6.2)'
id 'org.parchmentmc.librarian.forgegradle' version '1.+'
id 'net.neoforged.moddev' version '1.0.15'
}

abstract class TeaConDumpPathToGitHub extends DefaultTask {
@Input
abstract Property<String> getPublishName()
@InputFile
abstract RegularFileProperty getTargetFile()
@TaskAction
void dump() {
if (System.env.GITHUB_ACTIONS) {
File theFile = targetFile.getAsFile().get()

def outputFile = new File(System.env.GITHUB_OUTPUT)
// Use the env-specific line separator for maximally possible compatibility
def newLine = System.getProperty('line.separator')

// Write out new env variable for later usage
outputFile << newLine << "artifact_name=${theFile.getName()}"
outputFile << newLine << "artifact_publish_name=${publishName.get()}"
outputFile << newLine << "artifact_path=${theFile.absolutePath}"
}
}
}

tasks.named('wrapper', Wrapper).configure {
distributionType = Wrapper.DistributionType.BIN
}

group = 'org.teacon'
version = '3.0.2'
version = mod_version

java {
archivesBaseName = 'Xibao-Forge-1.20'
toolchain.languageVersion = JavaLanguageVersion.of(17)
archivesBaseName = 'Xibao-NeoForge-1.21'
toolchain.languageVersion = JavaLanguageVersion.of(21)
}

minecraft {
mappings channel: 'official', version: '1.20'
neoForge {
// Specify the version of NeoForge to use.
version = project.neo_version

parchment {
mappingsVersion = project.parchment_mappings_version
minecraftVersion = project.parchment_minecraft_version
}

// This line is optional. Access Transformers are automatically detected
// accessTransformers = project.files('src/main/resources/META-INF/accesstransformer.cfg')

// Default run configurations.
// These can be tweaked, removed, or duplicated as needed.
runs {
client {
workingDirectory project.file('run')
property 'forge.logging.markers', 'REGISTRIES'
property 'forge.logging.console.level', 'info'
mods {
xibao { source sourceSets.main }
}
client()

// Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
}

server {
workingDirectory project.file('run')
property 'forge.logging.markers', 'REGISTRIES'
property 'forge.logging.console.level', 'info'
mods {
xibao { source sourceSets.main }
}
server()
programArgument '--nogui'
systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
}

// This run config launches GameTestServer and runs all registered gametests, then exits.
// By default, the server will crash when no gametests are provided.
// The gametest system is also enabled by default for other run configs under the /test command.
gameTestServer {
type = "gameTestServer"
systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
}

data {
data()

// example of overriding the workingDirectory set in configureEach above, uncomment if you want to use it
// gameDirectory = project.file('run-data')

// Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources.
programArguments.addAll '--mod', project.mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath()
}

// Yes this mod does not have any data to generate.
// applies to all the run configs above
configureEach {
// Recommended logging data for a userdev environment
// The markers can be added/remove as needed separated by commas.
// "SCAN": For mods scan.
// "REGISTRIES": For firing of registry events.
// "REGISTRYDUMP": For getting the contents of all registries.
systemProperty 'forge.logging.markers', 'REGISTRIES'

// Recommended logging level for the console
// You can set various levels here.
// Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
logLevel = org.slf4j.event.Level.DEBUG
}
}

mods {
// define mod <-> source bindings
// these are used to tell the game which sources are for which mod
// mostly optional in a single mod project
// but multi mod projects should define one per mod
"${mod_id}" {
sourceSet(sourceSets.main)
}
}
}

Expand All @@ -45,38 +119,55 @@ sourceSets.main.resources { srcDir 'src/generated/resources' }
repositories { /* no repository here*/ }

dependencies {
minecraft 'net.minecraftforge:forge:1.20-46.0.1'

}

jar {
manifest {
attributes([
"Specification-Title": "Xibao",
"Specification-Vendor": "IzzelAliz",
"Specification-Version": "1",
"Implementation-Title": project.name,
"Implementation-Version": "${project.version}",
"Implementation-Vendor": "TeaConMC",
"Implementation-Timestamp": "${Instant.now().truncatedTo(java.time.temporal.ChronoUnit.SECONDS)}"
])
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
}

tasks.withType(ProcessResources).configureEach {
var replaceProperties = [
minecraft_version : minecraft_version,
minecraft_version_range: minecraft_version_range,
neo_version : neo_version,
neo_version_range : neo_version_range,
loader_version_range : loader_version_range,
mod_id : mod_id,
mod_name : mod_name,
mod_license : mod_license,
mod_version : mod_version,
mod_authors : mod_authors,
mod_description : mod_description
]
inputs.properties replaceProperties

filesMatching(['META-INF/neoforge.mods.toml']) {
expand replaceProperties
}
}

jar.finalizedBy('reobfJar')
// IDEA no longer automatically downloads sources/javadoc jars for dependencies, so we need to explicitly enable the behavior.
idea {
module {
downloadSources = true
downloadJavadoc = true
}
}

publishing {
publications {
release(MavenPublication) {
groupId = "org.teacon"
artifactId = "Xibao-Forge-1.20"
artifactId = "Xibao-NeoForge-1.21"

artifact jar
pom {
name = 'Xibao for Minecraft 1.20'
name = 'Xibao for Minecraft 1.21'
description = 'Ask IzzelAliz what is this'
licenses {
license {
name = 'BSD-3-Clause'
name = 'The Unlicense'
}
}
}
Expand All @@ -94,17 +185,19 @@ publishing {
}
}

tasks.withType(PublishToMavenRepository) {
onlyIf { System.env.ARCHIVE_ACCESS_KEY && System.env.ARCHIVE_SECRET_KEY }
tasks.withType(PublishToMavenRepository).configureEach {
if (repository && repository.name == "teacon") {
it.onlyIf {
System.env.ARCHIVE_ACCESS_KEY && System.env.ARCHIVE_SECRET_KEY
}
}
}

/**
* A simple task to pass down the artifact name and path to other GitHub actions.
*/
task("githubActionOutput") {
onlyIf { System.env.GITHUB_ACTIONS }
doLast {
println "::set-output name=artifact_path::${jar.archiveFile.get().asFile.absolutePath}"
println "::set-output name=artifact_name::${jar.archiveFileName.get()}"
tasks.register("githubActionOutput", TeaConDumpPathToGitHub) { task ->
task.onlyIf {
System.env.GITHUB_ACTIONS
}
task.getPublishName().set("${jar.archiveBaseName.get()}-${version}.jar")
task.getTargetFile().set(jar.archiveFile)
}

46 changes: 44 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
org.gradle.jvmargs=-Xmx1G
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=true

#read more on this at https://github.com/neoforged/ModDevGradle?tab=readme-ov-file#better-minecraft-parameter-names--javadoc-parchment
# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started
parchment_minecraft_version=1.21
parchment_mappings_version=2024.07.07
# Environment Properties
# You can find the latest versions here: https://projects.neoforged.net/neoforged/neoforge
# The Minecraft version must agree with the Neo version to get a valid artifact
minecraft_version=1.21.1
# The Minecraft version range can use any release version of Minecraft as bounds.
# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.21,1.21.1)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=21.1.4
# The Neo version range can use any version of Neo as bounds
neo_version_range=[21.0.0-beta,)
# The loader version range can only use the major version of FML as bounds
loader_version_range=[4,)

## Mod Properties

# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63}
# Must match the String constant located in the main mod class annotated with @Mod.
mod_id=xibao
# The human-readable display name for the mod.
mod_name=Xibao
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=UNLICENSE
# The mod version. See https://semver.org/
mod_version=4.0.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
mod_group_id=org.teacon
# The authors of the mod. This is a simple text string that is used for display purposes in the mod list.
mod_authors=IzzelAliz
mod_description=Xibao????: Good News
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
70 changes: 13 additions & 57 deletions src/main/java/org/teacon/xibao/Xibao.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,27 @@
package org.teacon.xibao;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.DisconnectedScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ScreenEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.IExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.loading.FMLPaths;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.loading.FMLPaths;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Mod("xibao")
@Mod(value = "xibao", dist = Dist.CLIENT)
public class Xibao {

@Mod.EventBusSubscriber(modid = "xibao", value = Dist.CLIENT)
public static final class XibaoImpl {
private static final ResourceLocation LOCATION = new ResourceLocation("xibao", "textures/xibao.png");

@SubscribeEvent
public static void on(ScreenEvent.Init.Post event) {
var showXibao = !Files.exists(getXibaoStopFile());
if (showXibao && event.getScreen() instanceof DisconnectedScreen s) {
var translatable = Component.translatable("xibao.do_not_show_again");
event.addListener(Button
.builder(translatable, XibaoImpl::onPress)
.pos(s.width / 2 - 75, s.height - 30).size(150, 20).build());
}
}

@SubscribeEvent
public static void on(ScreenEvent.BackgroundRendered event) {
var showXibao = !Files.exists(getXibaoStopFile());
if (showXibao && event.getScreen() instanceof DisconnectedScreen s) {
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder bufferbuilder = tesselator.getBuilder();
RenderSystem.setShaderTexture(0, LOCATION);
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
bufferbuilder.vertex(0.0D, s.height, 0.0D).uv(0F, 1F).color(255, 255, 255, 255).endVertex();
bufferbuilder.vertex(s.width, s.height, 0.0D).uv(1F, 1F).color(255, 255, 255, 255).endVertex();
bufferbuilder.vertex(s.width, 0.0D, 0.0D).uv(1F, 0F).color(255, 255, 255, 255).endVertex();
bufferbuilder.vertex(0.0D, 0.0D, 0.0D).uv(0F, 0F).color(255, 255, 255, 255).endVertex();
tesselator.end();
}
}

private static void onPress(Button btn) {
try (var out = Files.newBufferedWriter(getXibaoStopFile())) {
out.write("Remove this file to show Xibao again\n");
} catch (IOException e) {
return;
}
btn.active = false;
}
public static Path getXibaoStopFile() {
return FMLPaths.GAMEDIR.get().resolve(".xibao_stop");
}

private static Path getXibaoStopFile() {
return FMLPaths.GAMEDIR.get().resolve(".xibao_stop");
public static void onPress(Button btn) {
try (var out = Files.newBufferedWriter(getXibaoStopFile())) {
out.write("Remove this file to show Xibao again\n");
} catch (IOException e) {
return;
}
btn.active = false;
}
}
Loading

0 comments on commit bff1084

Please sign in to comment.