Skip to content

Commit

Permalink
Initialize 1.18-with-mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
CodexAdrian committed Mar 14, 2022
0 parents commit c32a509
Show file tree
Hide file tree
Showing 33 changed files with 1,245 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
* text eol=lf
*.bat text eol=crlf
*.patch text eol=lf
*.java text eol=lf
*.gradle text eol=crlf
*.png binary
*.gif binary
*.exe binary
*.dll binary
*.jar binary
*.lzma binary
*.zip binary
*.pyd binary
*.cfg text eol=lf
*.jks binary
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# eclipse
bin
*.launch
.settings
.metadata
.classpath
.project

# idea
out
*.ipr
*.iws
*.iml
.idea

# gradle
build
.gradle

# other
eclipse
run
52 changes: 52 additions & 0 deletions Common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
id 'java'
id 'org.spongepowered.gradle.vanilla' version '0.2.1-SNAPSHOT'
id 'maven-publish'
}

archivesBaseName = "${mod_name}-common-${minecraft_version}"

minecraft {
version(minecraft_version)
runs {
if (project.hasProperty('common_runs_enabled') ? project.findProperty('common_runs_enabled').toBoolean() : true) {

server(project.hasProperty('common_server_run_name') ? project.findProperty('common_server_run_name') : 'vanilla_server') {
workingDirectory(this.file("run"))
}
client(project.hasProperty('common_client_run_name') ? project.findProperty('common_client_run_name') : 'vanilla_client') {
workingDirectory(this.file("run"))
}
}
}
}

dependencies {
compileOnly group:'org.spongepowered', name:'mixin', version:'0.8.5'
}

processResources {

def buildProps = project.properties.clone()

filesMatching(['pack.mcmeta']) {

expand buildProps
}
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId project.group
artifactId project.archivesBaseName
version project.version
from components.java
}
}

repositories {
maven {
url "file://" + System.getenv("local_maven")
}
}
}
41 changes: 41 additions & 0 deletions Common/src/main/java/com/example/examplemod/CommonClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.examplemod;

import com.example.examplemod.platform.Services;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;

import java.util.List;

public class CommonClass {

// This method serves as an initialization hook for the mod. The vanilla
// game has no mechanism to load tooltip listeners so this must be
// invoked from a mod loader specific project like Forge or Fabric.
public static void init() {

Constants.LOG.info("Hello from Common init on {}! we are currently in a {} environment!", Services.PLATFORM.getPlatformName(), Services.PLATFORM.isDevelopmentEnvironment() ? "development" : "production");
Constants.LOG.info("Diamond Item >> {}", Registry.ITEM.getKey(Items.DIAMOND));
}

// This method serves as a hook to modify item tooltips. The vanilla game
// has no mechanism to load tooltip listeners so this must be registered
// by a mod loader like Forge or Fabric.
public static void onItemTooltip(ItemStack stack, TooltipFlag context, List<Component> tooltip) {

if (!stack.isEmpty()) {

final FoodProperties food = stack.getItem().getFoodProperties();

if (food != null) {

tooltip.add(new TextComponent("Nutrition: " + food.getNutrition()));
tooltip.add(new TextComponent("Saturation: " + food.getSaturationModifier()));
}
}
}
}
11 changes: 11 additions & 0 deletions Common/src/main/java/com/example/examplemod/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.examplemod;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Constants {

public static final String MOD_ID = "examplemod";
public static final String MOD_NAME = "Example Mod";
public static final Logger LOG = LogManager.getLogger(MOD_NAME);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.examplemod.mixin;

import com.example.examplemod.Constants;
import net.minecraft.SharedConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.TitleScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(TitleScreen.class)
public class ExampleCommonMixin {

@Inject(at = @At("HEAD"), method = "init()V")
private void init(CallbackInfo info) {

Constants.LOG.info("This line is printed by an example mod mixin from Common!");
Constants.LOG.info("MC Version: {}", SharedConstants.getCurrentVersion().getId());
Constants.LOG.info("Classloader: {}", this.getClass().getClassLoader());
}
}
20 changes: 20 additions & 0 deletions Common/src/main/java/com/example/examplemod/platform/Services.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.examplemod.platform;

import com.example.examplemod.Constants;
import com.example.examplemod.platform.services.IPlatformHelper;

import java.util.ServiceLoader;

public class Services {

public static final IPlatformHelper PLATFORM = load(IPlatformHelper.class);

public static <T> T load(Class<T> clazz) {

final T loadedService = ServiceLoader.load(clazz)
.findFirst()
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
Constants.LOG.debug("Loaded {} for service {}", loadedService, clazz);
return loadedService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.examplemod.platform.services;

public interface IPlatformHelper {

/**
* Gets the name of the current platform
*
* @return The name of the current platform.
*/
String getPlatformName();

/**
* Checks if a mod with the given id is loaded.
*
* @param modId The mod to check if it is loaded.
* @return True if the mod is loaded, false otherwise.
*/
boolean isModLoaded(String modId);

/**
* Check if the game is currently in a development environment.
*
* @return True if in a development environment, false otherwise.
*/
boolean isDevelopmentEnvironment();
}
17 changes: 17 additions & 0 deletions Common/src/main/resources/multiloader.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"required": true,
"minVersion": "0.8",
"package": "com.example.examplemod.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
],
"client": [
"ExampleCommonMixin"
],
"server": [
],
"injectors": {
"defaultRequire": 1
},
"refmap": "${refmap_target}refmap.json"
}
6 changes: 6 additions & 0 deletions Common/src/main/resources/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"description": "${mod_name}",
"pack_format": 8
}
}
73 changes: 73 additions & 0 deletions Fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
plugins {
id 'fabric-loom' version '0.10-SNAPSHOT'
id 'maven-publish'
id 'idea'
}

archivesBaseName = "${mod_name}-fabric-${minecraft_version}"

dependencies {
minecraft "com.mojang:minecraft:${minecraft_version}"
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${fabric_loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${fabric_version}"
implementation project(":Common")
}

loom {
runs {
client {
client()
setConfigName("Fabric Client")
ideConfigGenerated(true)
runDir("run")
}
server {
server()
setConfigName("Fabric Server")
ideConfigGenerated(true)
runDir("run")
}
}
}


processResources {
from project(":Common").sourceSets.main.resources
inputs.property "version", project.version

filesMatching("fabric.mod.json") {
expand "version": project.version
}

filesMatching('*.mixins.json') {
expand "refmap_target": "${archivesBaseName}-"
}
}

tasks.withType(JavaCompile) {
source(project(":Common").sourceSets.main.allSource)
}

jar {
from("LICENSE") {
rename { "${it}_${mod_name}" }
}
}

publishing {
publications {
mavenJava(MavenPublication) {
groupId project.group
artifactId project.archivesBaseName
version project.version
from components.java
}
}

repositories {
maven {
url "file://" + System.getenv("local_maven")
}
}
}
23 changes: 23 additions & 0 deletions Fabric/src/main/java/com/example/examplemod/ExampleMod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.examplemod;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;

public class ExampleMod implements ModInitializer {

@Override
public void onInitialize() {

// This method is invoked by the Fabric mod loader when it is ready
// to load your mod. You can access Fabric and Common code in this
// project.

// Use Fabric to bootstrap the Common mod.
Constants.LOG.info("Hello Fabric world!");
CommonClass.init();

// Some code like events require special initialization from the
// loader specific code.
ItemTooltipCallback.EVENT.register(CommonClass::onItemTooltip);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.examplemod.mixin;

import com.example.examplemod.Constants;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.gui.screens.TitleScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.stream.Collectors;

@Mixin(TitleScreen.class)
public class ExampleFabricMixin {

@Inject(at = @At("HEAD"), method = "init()V")
private void init(CallbackInfo info) {

Constants.LOG.info("This line is printed by an example mod mixin from Fabric!");
Constants.LOG.info(FabricLoader.getInstance()
.getAllMods()
.stream()
.map(ModContainer::getMetadata)
.map(ModMetadata::getId)
.collect(Collectors.joining(", ", "Current loaded mods: [", "]")));
}

}
Loading

0 comments on commit c32a509

Please sign in to comment.