Skip to content

Commit

Permalink
feat: the. entire mod.
Browse files Browse the repository at this point in the history
  • Loading branch information
vgskye committed Jun 4, 2024
0 parents commit 3fde8e7
Show file tree
Hide file tree
Showing 37 changed files with 1,530 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
build/
*.ipr
run/
*.iws
out/
*.iml
.gradle/
output/
bin/
libs/

.classpath
.project
.idea/
classes/
.metadata
.vscode
.settings
*.launch
55 changes: 55 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.5-SNAPSHOT" apply false
}

architectury {
minecraft = rootProject.minecraft_version
}

subprojects {
apply plugin: "dev.architectury.loom"

loom {
silentMojangMappingsLicense()
}

dependencies {
minecraft "com.mojang:minecraft:${rootProject.minecraft_version}"
// The following line declares the mojmap mappings, you may use other mappings as well
mappings loom.officialMojangMappings()
// The following line declares the yarn mappings you may select this one as well.
// mappings "net.fabricmc:yarn:1.18.2+build.4:v2"
}
}

allprojects {
apply plugin: "java"
apply plugin: "architectury-plugin"
apply plugin: "maven-publish"

base {
archivesName = rootProject.archives_base_name
}

version = rootProject.mod_version
group = rootProject.maven_group

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven { url 'https://jitpack.io' }
}

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.release = 17
}

java {
withSourcesJar()
}
}
30 changes: 30 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
architectury {
common(rootProject.enabled_platforms.split(","))
}

loom {
accessWidenerPath = file("src/main/resources/e4mc_minecraft.accesswidener")
}

dependencies {
// We depend on fabric loader here to use the fabric @Environment annotations and get the mixin dependencies
// Do NOT use other classes from fabric loader
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
implementation("com.github.vgskye.netty-incubator-codec-quic:netty-incubator-codec-classes-quic:${rootProject.quic_version}") {
exclude(group: "io.netty")
}
}

publishing {
publications {
mavenCommon(MavenPublication) {
artifactId = rootProject.archives_base_name
from components.java
}
}

// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
// Add repositories to publish to here.
}
}
10 changes: 10 additions & 0 deletions common/src/main/java/link/e4mc/Agnos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package link.e4mc;

import dev.architectury.injectables.annotations.ExpectPlatform;

public class Agnos {
@ExpectPlatform
public static boolean isClient() {
return false;
}
}
44 changes: 44 additions & 0 deletions common/src/main/java/link/e4mc/E4mcClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package link.e4mc;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;

public class E4mcClient {
public static final String MOD_ID = "e4mc_minecraft";
public static QuiclimeSession session;
public static void registerCommands(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("e4mc")
.requires(src -> {
if (src.getServer().isDedicatedServer()) {
return src.hasPermission(4);
} else {
try {
return src.getServer().isSingleplayerOwner(src.getPlayerOrException().getGameProfile());
} catch (CommandSyntaxException e) {
return false;
}
}
})
.then(Commands.literal("stop").executes(ctx -> {
if ((session != null) && (session.state != QuiclimeSession.State.STOPPED)) {
session.stop();
Mirror.sendSuccessToSource(ctx.getSource(), Mirror.translatable("text.e4mc_minecraft.closeServer"));
} else {
Mirror.sendFailureToSource(ctx.getSource(), Mirror.translatable("text.e4mc_minecraft.serverAlreadyClosed"));
}
return 1;
}))
.then(Commands.literal("restart").executes(ctx -> {
if ((session != null) && (session.state != QuiclimeSession.State.STARTED)) {
session.stop();
session = new QuiclimeSession();
session.startAsync();
}
return 1;
}))
);
}
}
144 changes: 144 additions & 0 deletions common/src/main/java/link/e4mc/Mirror.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package link.e4mc;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

public class Mirror {
private static final String[] LITERAL_CLASS_NAMES = {
"net.minecraft.text.LiteralText", // yarn
"net.minecraft.network.chat.TextComponent",
"net.minecraft.class_2585",
"net.minecraft.src.C_5025_"
};
private static final String[] LITERAL_METHOD_NAMES = {
"literal",
"method_43470",
"m_237113_"
};
private static final String[] TRANSLATABLE_CLASS_NAMES = {
"net.minecraft.text.TranslatableText", // yarn
"net.minecraft.network.chat.TranslatableComponent",
"net.minecraft.class_2588",
"net.minecraft.src.C_5026_"
};
private static final String[] TRANSLATABLE_METHOD_NAMES = {
"translatable",
"method_43469",
"m_237110_"
};
private static final String[] SUCCESS_METHOD_NAMES = {
"sendFeedback", // yarn
"sendSuccess",
"method_9226",
"m_288197_"
};
private static final String[] FAILURE_METHOD_NAMES = {
"sendError", // yarn
"sendFailure",
"method_9213",
"m_81352_"
};
private static final String[] WITH_STYLE_METHOD_NAMES = {
"styled", // yarn
"withStyle",
"method_27694",
"m_130938_"
};
private static final String[] APPEND_METHOD_NAMES = {
"append",
"method_10852",
"m_7220_"
};

public static Component withStyle(Component component, UnaryOperator<Style> operator) {
Class<? extends Component> clazz = component.getClass();
for (String methodName : WITH_STYLE_METHOD_NAMES) {
try {
Method method = clazz.getMethod(methodName, UnaryOperator.class);
return (Component) method.invoke(component, operator);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassCastException ignored) {}
}
throw new RuntimeException("Could not locate any way to style this Component!");
}

public static Component append(Component component, Component other) {
Class<? extends Component> clazz = component.getClass();
for (String methodName : APPEND_METHOD_NAMES) {
try {
Method method = clazz.getMethod(methodName, Component.class);
return (Component) method.invoke(component, other);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassCastException ignored) {}
}
throw new RuntimeException("Could not locate any way to append a Component to this Component!");
}

public static Component literal(String text) {
// Try 1.18-and-older-style TextComponent initialization first
for (String className : LITERAL_CLASS_NAMES) {
try {
Class<?> clazz = Class.forName(className);
Constructor<?> constructor = clazz.getConstructor(String.class);
return (Component) constructor.newInstance(text);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException | ClassCastException ignored) {}
}
Class<Component> clazz = Component.class;
for (String methodName : LITERAL_METHOD_NAMES) {
try {
Method method = clazz.getMethod(methodName, String.class);
return (Component) method.invoke(null, text);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassCastException ignored) {}
}
throw new RuntimeException("Could not locate any way to make a literal Component!");
}


public static Component translatable(String text, Object... args) {
// Try 1.18-and-older-style TranslatableComponent initialization first
for (String className : TRANSLATABLE_CLASS_NAMES) {
try {
Class<?> clazz = Class.forName(className);
Constructor<?> constructor = clazz.getConstructor(String.class, Object[].class);
return (Component) constructor.newInstance(text, args);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException | ClassCastException ignored) {}
}
Class<Component> clazz = Component.class;
for (String methodName : TRANSLATABLE_METHOD_NAMES) {
try {
Method method = clazz.getMethod(methodName, String.class, Object[].class);
return (Component) method.invoke(null, text, args);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassCastException ignored) {}
}
throw new RuntimeException("Could not locate any way to make a literal Component!");
}

public static void sendSuccessToSource(CommandSourceStack source, Component message) {
sendGenericMessageToSource(source, message, SUCCESS_METHOD_NAMES);
}

public static void sendFailureToSource(CommandSourceStack source, Component message) {
sendGenericMessageToSource(source, message, FAILURE_METHOD_NAMES);
}

private static void sendGenericMessageToSource(CommandSourceStack source, Component message, String[] methodNames) {
Class<CommandSourceStack> clazz = CommandSourceStack.class;
for (String methodName : methodNames) {
try {
Method method = clazz.getMethod(methodName, Component.class, boolean.class);
method.invoke(source, message, true);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {}
try {
Method method = clazz.getMethod(methodName, Supplier.class, boolean.class);
method.invoke(source, (Supplier<Component>) () -> message, true);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {}
}
}
}
Loading

0 comments on commit 3fde8e7

Please sign in to comment.