Skip to content

Commit

Permalink
WIP: Mod API
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuperGamer20578 committed Jan 22, 2024
1 parent e69f22d commit 7dd0715
Show file tree
Hide file tree
Showing 33 changed files with 331 additions and 845 deletions.
File renamed without changes
172 changes: 90 additions & 82 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,108 +5,116 @@ plugins {
id 'com.modrinth.minotaur' version '2.+'
}

allprojects {
apply plugin: 'fabric-loom'

def git = versionDetails()
def prefix = 'v'
def tag
if (git.lastTag.startsWith(prefix)) {
tag = git.lastTag.substring(prefix.length())
} else {
tag = git.lastTag
}
if (hasProperty('ci')) {
switch (property('ci')) {
case 'release':
if (git.commitDistance != 0) {
throw new InvalidUserDataException('Not on a tag')
}
if (!git.lastTag.startsWith('v')) {
throw new InvalidUserDataException('Last tag was not a version tag')
repositories {
maven {
url 'https://maven.terraformersmc.com/'
}
maven {
url 'https://maven.shedaniel.me/'
}
maven {
url 'https://maven.pkg.github.com/thesupergamer20578/chroma/'
credentials {
username = project.findProperty('gpr.user') ?: System.getenv('GITHUB_ACTOR')
password = project.findProperty('gpr.key') ?: System.getenv('GITHUB_TOKEN')
}
version = "${tag}"
break
case 'snapshot':
version = "${tag}${tag.contains('-') ? '.' : '-'}SNAPSHOT.${git.commitDistance}.${git.branchName}.${git.gitHash}"
break
default:
throw new InvalidUserDataException("Invalid ci argument: ${property("ci")}")
}
}
} else {
version = "${tag}${tag.contains('-') ? '.' : '-'}SNAPSHOT.${git.commitDistance}.${git.branchName}.${git.gitHash}+local"
}
group = 'io.github.thesupergamer20578'
java.sourceCompatibility = java.targetCompatibility = JavaVersion.VERSION_17


repositories {
maven {
url 'https://maven.terraformersmc.com/'
dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}"
implementation "io.github.thesupergamer20578:chroma:${project.chroma_version}"
api it
}
maven {
url 'https://maven.shedaniel.me/'

def git = versionDetails()
def prefix = 'v'
def tag
if (git.lastTag.startsWith(prefix)) {
tag = git.lastTag.substring(prefix.length())
} else {
tag = git.lastTag
}
maven {
url 'https://maven.pkg.github.com/thesupergamer20578/chroma/'
credentials {
username = project.findProperty('gpr.user') ?: System.getenv('GITHUB_ACTOR')
password = project.findProperty('gpr.key') ?: System.getenv('GITHUB_TOKEN')
if (hasProperty('ci')) {
switch (property('ci')) {
case 'release':
if (git.commitDistance != 0) {
throw new InvalidUserDataException('Not on a tag')
}
if (!git.lastTag.startsWith('v')) {
throw new InvalidUserDataException('Last tag was not a version tag')
}
version = "${tag}"
break
case 'snapshot':
version = "${tag}${tag.contains('-') ? '.' : '-'}SNAPSHOT.${git.commitDistance}.${git.branchName}.${git.gitHash}"
break
default:
throw new InvalidUserDataException("Invalid ci argument: ${property("ci")}")
}
} else {
version = "${tag}${tag.contains('-') ? '.' : '-'}SNAPSHOT.${git.commitDistance}.${git.branchName}.${git.gitHash}+local"
}
}
group = 'io.github.thesupergamer20578'
java.sourceCompatibility = java.targetCompatibility = JavaVersion.VERSION_17

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}"
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}"
modApi("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}") {
exclude(group: 'net.fabricmc.fabric-api')
}
implementation "io.github.thesupergamer20578:chroma:${project.chroma_version}"
shadow "io.github.thesupergamer20578:chroma:${project.chroma_version}"
}
processResources {
inputs.property 'version', project.version

processResources {
inputs.property 'version', project.version
filesMatching('fabric.mod.json') {
expand 'version': project.version
}
}

filesMatching('fabric.mod.json') {
expand 'version': project.version
tasks.withType(JavaCompile).configureEach {
it.options.encoding 'UTF-8'
}
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding 'UTF-8'
}
java {
withSourcesJar()
}

java {
withSourcesJar()
}
jar {
from('LICENSE') {
rename { "${it}_${project.archivesBaseName}" }
}
}

jar {
from('LICENSE') {
rename { "${it}_${project.archivesBaseName}" }
shadowJar {
archiveClassifier = 'unmapped'
configurations = [project.configurations.shadow]
minimize() {
exclude(dependency('com.github.hypfvieh:dbus-java-transport-.*:.*'))
}
enableRelocation true
relocationPrefix = "${project.group}.${project.name}.shadow"
relocate("${project.group}.${project.name}", "${project.group}.${project.name}")
relocate('org.freedesktop.dbus', 'org.freedesktop.dbus')
relocate('jnr', 'jnr')
relocate('com.kenai', 'com.kenai')
}
}

shadowJar {
archiveClassifier = 'unmapped'
configurations = [project.configurations.shadow]
minimize() {
exclude(dependency('com.github.hypfvieh:dbus-java-transport-.*:.*'))
remapJar {
dependsOn shadowJar
mustRunAfter shadowJar
inputFile = file(shadowJar.archiveFile)
}
enableRelocation true
relocationPrefix = "${project.group}.${project.name}.shadow"
relocate("${project.group}.${project.name}", "${project.group}.${project.name}")
relocate('org.freedesktop.dbus', 'org.freedesktop.dbus')
relocate('jnr', 'jnr')
relocate('com.kenai', 'com.kenai')
}

remapJar {
dependsOn shadowJar
mustRunAfter shadowJar
inputFile = file(shadowJar.archiveFile)
dependencies {
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}"
modImplementation("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}") {
exclude(group: 'net.fabricmc.fabric-api')
}
shadow "io.github.thesupergamer20578:chroma:${project.chroma_version}"
// include project(':chromatic-api')
// include project(':chromatic-core')
}

modrinth {
Expand Down
Empty file added chromatic-api/build.gradle
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.thesupergamer20578.chromatic.api;

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

import java.util.*;

public final class Chromatic {
private static Chromatic INSTANCE;
static final Logger logger = LogManager.getLogger("Chromatic");
static final Queue<Effect> effectQueue = new LinkedList<>();
static final Map<Byte, List<Overlay>> overlays = new TreeMap<>();
static final List<Module> modules = new ArrayList<>();

private Chromatic() {}

static Chromatic getInstance() {
if (INSTANCE == null) {
INSTANCE = new Chromatic();
}
return INSTANCE;
}

public static void addModule(Module module) {
modules.add(module);
module.init(getInstance());
}

public void queueEffect(Effect effect) {
effectQueue.add(effect);
}

public void applyOverlay(Overlay overlay, byte priority) {
overlays.computeIfAbsent(priority, k -> new ArrayList<>()).add(overlay);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.thesupergamer20578.chromatic.api;

import io.github.thesupergamer20578.chroma.Chroma;
import io.github.thesupergamer20578.chroma.Colour;
import io.github.thesupergamer20578.chroma.drivers.Driver;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.network.ClientPlayerEntity;
import org.freedesktop.dbus.exceptions.DBusException;
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.List;
import java.util.concurrent.atomic.AtomicBoolean;

@Mixin(MinecraftClient.class)
public class ClientMixin {
@Inject(at = @At("HEAD"), method = "joinWorld")
private void joinWorld(CallbackInfo ci) {
Chromatic.effectQueue.clear();
for (Module module : Chromatic.modules) {
module.reset(Chromatic.getInstance());
}
}

@Inject(at = @At("HEAD"), method = "tick")
private void tick(CallbackInfo ci) {
MinecraftClient client = MinecraftClient.getInstance();
ClientPlayerEntity player = client.player;
Screen screen = client.currentScreen;
Driver driver;
try {
driver = Chroma.getDriver();
assert driver != null;
} catch (DBusException e) {
throw new RuntimeException(e);
}

for (Module module : Chromatic.modules) {
module.tick(Chromatic.getInstance(), client, player, screen);
}

final AtomicBoolean end = new AtomicBoolean(false);
Chromatic.effectQueue.removeIf(effect -> {
if (end.get()) {
return false;
}
EffectResult result = effect.next(driver, client, player, screen);
if (result == EffectResult.CONTINUE) {
end.set(true);
}
return result == EffectResult.END;
});

Colour[][] matrix = new Colour[6][22];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = new Colour(0x000000);
}
}
end.set(false);
for (List<Overlay> overlays : Chromatic.overlays.values()) {
overlays.removeIf(overlay -> {
if (end.get()) {
return false;
}
EffectResult result = overlay.apply(matrix, client, player, screen);
if (result == EffectResult.CONTINUE) {
end.set(true);
}
return result == EffectResult.END;
});
if (end.get()) {
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.thesupergamer20578.chromatic.api;

import io.github.thesupergamer20578.chroma.drivers.Driver;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.network.ClientPlayerEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface Effect {
@NotNull EffectResult next(
@NotNull Driver driver,
@NotNull MinecraftClient client,
@Nullable ClientPlayerEntity player,
@Nullable Screen currentScreen
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.thesupergamer20578.chromatic.api;

public enum EffectResult {
CONTINUE,
END,
TRANSPARENT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.thesupergamer20578.chromatic.api;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.network.ClientPlayerEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface Module {
@NotNull String name();
default boolean enabledByDefault() { return true; }
default void init(Chromatic chromatic) {}
default void reset(Chromatic chromatic) {}
default void tick(@NotNull Chromatic chromatic, @NotNull MinecraftClient client, @Nullable ClientPlayerEntity player, @Nullable Screen currentScreen) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.thesupergamer20578.chromatic.api;

import io.github.thesupergamer20578.chroma.Colour;
import io.github.thesupergamer20578.chroma.drivers.Driver;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.network.ClientPlayerEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface Overlay {
@NotNull EffectResult apply(
@NotNull Colour @NotNull [] @NotNull [] matrix,
@NotNull MinecraftClient client,
@Nullable ClientPlayerEntity player,
@Nullable Screen currentScreen
);
}
1 change: 1 addition & 0 deletions chromatic-api/src/main/resources/assets/chromatic
7 changes: 7 additions & 0 deletions chromatic-api/src/main/resources/chromatic-api.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"required": true,
"package": "io.github.thesupergamer20578.chromatic.api",
"client": [
"ClientMixin"
]
}
Loading

0 comments on commit 7dd0715

Please sign in to comment.