Skip to content

Commit

Permalink
fabric fully working + new class loading + small bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrycat committed May 23, 2023
1 parent f5fb19a commit 5dc67ba
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 78 deletions.
34 changes: 29 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ subprojects {
mavenCentral()
maven { url = "https://jitpack.io" }
maven {
url 'https://maven.fabricmc.net/'
metadataSources {
artifact()
}
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
}
maven {
name = 'ParchmentMC'
url = 'https://maven.parchmentmc.org'
}
}

Expand All @@ -59,7 +61,7 @@ subprojects {
}

if (project.name != "common") {
destinationDirectory = rootProject.buildDir
//destinationDirectory = rootProject.buildDir
doFirst {
archiveClassifier = project.name
}
Expand All @@ -71,9 +73,24 @@ subprojects {
}
from {
"../LICENSE"
"$buildDir/resources/classes.txt"
}
}

compileJava.doLast {
var classFile = new File("$buildDir/resources/classes.txt")
classFile.text = fileTree("$buildDir/classes/java/main")
.filter { File file -> file.text.contains("@") }
.files
.collect {
it.getAbsolutePath().substring(
file("$buildDir/classes/java/main").getAbsolutePath().length() + 1,
it.getAbsolutePath().length() - 6
)
.replaceAll("[/\\\\]", ".")
}.join(";")
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(project.properties.getOrDefault("jdkVersion", 17) as int)
Expand Down Expand Up @@ -105,6 +122,13 @@ subprojects {
tasks.withType(JavaExec) {
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
}

remapJar {
inputFile.set project.jar.archiveFile
destinationDirectory = rootProject.buildDir
dependsOn project.jar
archiveFileName = project.jar.archiveFileName
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public Player buildAndSave() {

lastLanding = landTick ? pos : prev.lastLanding;
lastHit = prev.landTick ? pos : prev.lastHit;
lastJump = jumpTick ? pos : prev.lastJump;
lastJump = jumpTick ? prev.pos : prev.lastJump;

deltaYaw = trueYaw - prev.trueYaw;
deltaPitch = truePitch - prev.truePitch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ public class LabelConfiguration implements Copyable<LabelConfiguration> {
public ArrayList<Component> components;
public boolean isMutable = false;

public LabelConfiguration(ArrayList<Component> components) {
this.components = components;
}
private File saveFile;

public LabelConfiguration() {
this(new ArrayList<>());
}

public LabelConfiguration(ArrayList<Component> components) {
this(null, components);
}

public LabelConfiguration(File saveFile, ArrayList<Component> components) {
this.saveFile = saveFile;
this.components = components;
}

public static void init() {
File savedConfigsFolder = new File(savedConfigsFolderName);
if (!savedConfigsFolder.exists()) savedConfigsFolder.mkdir();
Expand All @@ -50,23 +57,61 @@ public static void init() {
List<File> files = FileUtil.getJSONFiles(savedConfigsFolderName);
if (files != null) {
for (File file : files) {
Component[] components = Serializer.deserialize(file, Component[].class);
if (components == null) continue;
savedConfigs.put(FileUtil.getName(file), new LabelConfiguration(new ArrayList<>(Arrays.asList(components))));
LabelConfiguration c = LabelConfiguration.fromFile(file);
if (c == null) continue;
savedConfigs.put(FileUtil.getName(file), c);
}
}

customConfigurationFile = new File(JSONConfig.configFolderPath + customConfigurationFileName);
Component[] components = Serializer.deserialize(customConfigurationFile, Component[].class);
if (components == null) currentConfig = presets.getOrDefault("default", new LabelConfiguration()).copy();
else currentConfig = new LabelConfiguration(new ArrayList<>(Arrays.asList(components)));
currentConfig = LabelConfiguration.fromFile(customConfigurationFile);
if (currentConfig == null) {
currentConfig = presets.getOrDefault("default", new LabelConfiguration()).copy();
currentConfig.saveFile = customConfigurationFile;
}
currentConfig.isMutable = true;
}

public static LabelConfiguration fromFile(File file) {
ArrayList<Component> components = loadComponentsFromFile(file);
if (components == null) return null;
return new LabelConfiguration(file, components);
}

public LabelConfiguration copy() {
if (components.isEmpty()) return new LabelConfiguration();

String components = Serializer.serializeAsString(this.components);
Component[] copy = Serializer.deserializeString(components, Component[].class);
if (copy == null) return new LabelConfiguration();

return new LabelConfiguration(new ArrayList<>(Arrays.asList(copy)));
}

private static ArrayList<Component> loadComponentsFromFile(File file) {
Component[] components = Serializer.deserialize(file, Component[].class);
if (components == null) return null;
return new ArrayList<>(Arrays.asList(components));
}

public void selectAsCurrent() {
reloadFromFile();
currentConfig = copy();
currentConfig.saveFile = customConfigurationFile;
}

public LabelConfiguration reloadFromFile() {
if (saveFile == null) return this;
ArrayList<Component> components = loadComponentsFromFile(saveFile);
if (components == null) return this;
this.components = components;
return this;
}

public static void delete(String name) {
File file = new File(savedConfigsFolderName + name + ".json");
if(!file.exists()) return;
if(file.delete()) {
if (!file.exists()) return;
if (file.delete()) {
savedConfigs.remove(name);
}
}
Expand All @@ -90,17 +135,7 @@ public void saveInCustom() {
@Override
public String toString() {
return "LabelConfiguration{" +
"components=" + components +
'}';
}

public LabelConfiguration copy() {
if (components.isEmpty()) return new LabelConfiguration();

String components = Serializer.serializeAsString(this.components);
Component[] copy = Serializer.deserializeString(components, Component[].class);
if (copy == null) return new LabelConfiguration();

return new LabelConfiguration(new ArrayList<>(Arrays.asList(copy)));
"components=" + components +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,27 @@ private void initComponents() {
presets.title = "Presets";
addChild(presets, true, true, Anchor.TOP_LEFT, true);

savedConfigs = new ConfigFileList(LabelConfiguration.savedConfigs, new Vector2D(0.2 / 3, 0.07), new Vector2D(4 / 10D, 0.9));
savedConfigs = new ConfigFileList(LabelConfiguration.savedConfigs, new Vector2D(0.2 / 3, 0.07), new Vector2D(4 / 10D, 0.83));
savedConfigs.title = "Saved Configurations";
addChild(savedConfigs, true, true, Anchor.TOP_RIGHT, true);

Button reloadCurrent = new Button(
"Reload from file",
new Vector2D(0.5, 0.5),
new Vector2D(0.8, 20),
b -> {
if(b != Mouse.Button.LEFT) return;
LabelConfiguration.currentConfig.reloadFromFile();
paneHolder.reloadConfig();
}
);
Div reloadDiv = new Div(
new Vector2D(0.2 / 3, 0),
new Vector2D(4 / 10D, 0.1)
);
reloadDiv.addChild(reloadCurrent, true, true, true, false, Anchor.TOP_LEFT);
addChild(reloadDiv, true, true, true, true, Anchor.BOTTOM_RIGHT, true);

TextRectangle r = new TextRectangle(
new Vector2D(0.5, 1/6D),
new Vector2D(1, filename.getDisplayedSize().getY()),
Expand Down Expand Up @@ -108,7 +125,7 @@ public ConfigFileListItem(ScrollableList<ConfigFileListItem> parent, String file
super(parent);
load = new Button("Load", Vector2D.OFFSCREEN, new Vector2D(30, 20), (mouseButton) -> {
if (mouseButton != Mouse.Button.LEFT) return;
LabelConfiguration.currentConfig = configuration.copy();
configuration.selectAsCurrent();
paneHolder.reloadConfig();
close();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,5 @@ private void initComponents() {
);
addChild(pkcFileRadiusLabel, false, false, Anchor.TOP_LEFT);
addChild(pkcFileRadius, false, false, Anchor.TOP_LEFT);

addChild(
new Button("Reload From File",
new Vector2D(20, 80),
new Vector2D(100, 20),
mouseButton -> paneHolder.reloadConfig()
),
false, false, Anchor.TOP_LEFT
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public Option(String name, Boolean value, Boolean defaultValue) {
public static HashMap<String, Option> createOptionMap() {
HashMap<String, Option> optionMap = new HashMap<>();

List<Class<?>> classes = ClassUtil.getClasses(API.packageName);
//List<Class<?>> classes = ClassUtil.getClasses(API.packageName);

if (classes.size() == 0) {
/*if (classes.size() == 0) {
API.LOGGER.warn(API.CONFIG_MARKER, "Error loading package while initializing option map");
return optionMap;
}
}*/

List<Tuple<Field, java.lang.reflect.Field>> annotations = ClassUtil.getFieldAnnotations(classes, Field.class);
List<Tuple<Field, java.lang.reflect.Field>> annotations = ClassUtil.getFieldAnnotations(Field.class);
for (Tuple<Field, java.lang.reflect.Field> t : annotations) {
Field a = t.getFirst();
java.lang.reflect.Field f = t.getSecond();
Expand All @@ -83,7 +83,7 @@ public static HashMap<String, Option> createOptionMap() {
API.LOGGER.debug("Option of type {} added to map: {} with default value: {}", type, name, value);
}

List<Tuple<ChangeListener, Method>> listeners = ClassUtil.getMethodAnnotations(classes, ChangeListener.class);
List<Tuple<ChangeListener, Method>> listeners = ClassUtil.getMethodAnnotations(ChangeListener.class);
for (Tuple<ChangeListener, Method> l : listeners) {
ChangeListener c = l.getFirst();
Method m = l.getSecond();
Expand Down
60 changes: 49 additions & 11 deletions common/src/main/java/io/github/kurrycat/mpkmod/util/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

import io.github.kurrycat.mpkmod.compatibility.API;

import java.io.File;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class ClassUtil {
public static Class<?> ModClass = null;

public static <A extends Annotation> List<Tuple<A, Class<?>>> getClassAnnotations(List<Class<?>> classes, Class<A> annotationClass) {
private static Set<Class<?>> classes = null;

public static <A extends Annotation> List<Tuple<A, Class<?>>> getClassAnnotations(Class<A> annotationClass) {
return getClassAnnotations(classes(), annotationClass);
}

public static <A extends Annotation> List<Tuple<A, Class<?>>> getClassAnnotations(Collection<Class<?>> classes, Class<A> annotationClass) {
List<Tuple<A, Class<?>>> annotations = new ArrayList<>();
for (Class<?> c : classes) {
if (c.isAnnotationPresent(annotationClass)) {
Expand All @@ -26,7 +30,27 @@ public static <A extends Annotation> List<Tuple<A, Class<?>>> getClassAnnotation
return annotations;
}

public static <A extends Annotation> List<Tuple<A, Field>> getFieldAnnotations(List<Class<?>> classes, Class<A> annotationClass) {
private static Set<Class<?>> classes() {
if (classes == null) {
InputStream in = FileUtil.getResource("classes.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
classes = new HashSet<>();
try {
String[] classList = reader.readLine().split(";");
for (String s : classList) {
classes.add(Class.forName(s));
}
} catch (IOException | ClassNotFoundException ignored) {
}
}
return classes;
}

public static <A extends Annotation> List<Tuple<A, Field>> getFieldAnnotations(Class<A> annotationClass) {
return getFieldAnnotations(classes(), annotationClass);
}

public static <A extends Annotation> List<Tuple<A, Field>> getFieldAnnotations(Collection<Class<?>> classes, Class<A> annotationClass) {
List<Tuple<A, Field>> annotations = new ArrayList<>();
for (Class<?> c : classes) {
for (Field f : c.getFields()) {
Expand All @@ -38,7 +62,11 @@ public static <A extends Annotation> List<Tuple<A, Field>> getFieldAnnotations(L
return annotations;
}

public static <A extends Annotation> List<Tuple<A, Method>> getMethodAnnotations(List<Class<?>> classes, Class<A> annotationClass) {
public static <A extends Annotation> List<Tuple<A, Method>> getMethodAnnotations(Class<A> annotationClass) {
return getMethodAnnotations(classes(), annotationClass);
}

public static <A extends Annotation> List<Tuple<A, Method>> getMethodAnnotations(Collection<Class<?>> classes, Class<A> annotationClass) {
List<Tuple<A, Method>> annotations = new ArrayList<>();
for (Class<?> c : classes) {
for (Method m : c.getMethods()) {
Expand All @@ -47,16 +75,26 @@ public static <A extends Annotation> List<Tuple<A, Method>> getMethodAnnotations
}
}
}

return annotations;
}

public static List<Class<?>> getClasses(String packageName) {
public static List<Class<?>> classes(String packageName) {
String path = packageName.replace(".", File.separator);
String path2 = packageName.replace(".", "/");
List<Class<?>> classes = new ArrayList<>();
String[] classPathEntries = System.getProperty("java.class.path").split(
System.getProperty("path.separator")
);

String classPaths = System.getProperty("java.class.path");
if (classPaths == null || !classPaths.contains(path) && !classPaths.contains(path2)) {
classPaths = System.getProperty("legacyClassPath");
}
if (classPaths == null) {
return classes;
}

String[] classPathEntries = classPaths.split(System.getProperty("path.separator"));

API.LOGGER.info(classPaths);

String name;
for (String classpathEntry : classPathEntries) {
Expand Down
Loading

0 comments on commit 5dc67ba

Please sign in to comment.