duck mod now somewhat works

This commit is contained in:
ZXMushroom63 2025-01-29 11:32:01 +08:00
parent 6b2d19c705
commit 393f6fd2e7
4 changed files with 214 additions and 22 deletions

View File

@ -6,6 +6,7 @@ Properties:
- `classes: ReflectClass[]`
- `ModAPI.reflect.classes` is an array of ReflectClasses, representing (almost) every java class.
- `classMap: Map<String, ReflectClass>` is a map of every class.
Methods:
@ -54,11 +55,8 @@ Each `ReflectClass` has the following properties:
- List of all the static variable names for the class.
- `staticVariables: Map<String, *>`
- key-value dictionary of all the static variables in a class.
- `superclass: Class?`
- The raw teavm class of the superclass.
- `superclassName: String?`
- The class id of the class's superclass. Eg: `net.minecraft.client.entity.AbstractClientPlayer`
- Will be `null` if `hasMeta` is equal to `false`
- `superclass: ReflectClass?`
- The superclass.
Each `ReflectClass` has the following methods:

View File

@ -317,8 +317,25 @@ ModAPI.meta.credits("By ZXMushroom63");
// category: AsyncSink.Audio.Category.*
// SoundEntry = {path: String, pitch: 1, volume: 1, streaming: false}
const SoundPoolEntry = ModAPI.reflect.getClassByName("SoundPoolEntry").constructors.find(x => x.length === 4);
const SoundEventAccessor = ModAPI.reflect.getClassByName("SoundEventAccessor").constructors.find(x => x.length === 2);
const SoundEventAccessorComposite = ModAPI.reflect.getClassByName("SoundEventAccessorComposite").constructors.find(x => x.length === 4);
const SoundEventAccessorClass = ModAPI.reflect.getClassByName("SoundEventAccessor").class;
function makeSoundEventAccessor(soundpoolentry, weight) {
var object = new SoundEventAccessorClass;
var wrapped = ModAPI.util.wrap(object).getCorrective();
wrapped.entry = soundpoolentry;
wrapped.weight = weight;
return object;
}
const SoundEventAccessorCompositeClass = ModAPI.reflect.getClassByName("SoundEventAccessorComposite").class;
function makeSoundEventAccessorComposite(rKey, pitch, volume, category) {
var object = new SoundEventAccessorCompositeClass;
var wrapped = ModAPI.util.wrap(object).getCorrective();
wrapped.soundLocation = rKey;
wrapped.eventPitch = pitch;
wrapped.eventVolume = volume;
wrapped.category = category;
wrapped.soundPool = ModAPI.hooks.methods.cgcc_Lists_newArrayList1();
return object;
}
AsyncSink.Audio.register = function addSfx(key, category, values) {
var snd = ModAPI.mc.mcSoundHandler;
var registry = snd.sndRegistry.soundRegistry;
@ -327,14 +344,14 @@ ModAPI.meta.credits("By ZXMushroom63");
var path = ResourceLocation(ModAPI.util.str(se.path));
return SoundPoolEntry(path, se.pitch, se.volume, 1 * se.streaming);
}).map(spe => {
return SoundEventAccessor(spe, 1); // 1 = weight
return makeSoundEventAccessor(spe, 1); // 1 = weight
});
var compositeSound = SoundEventAccessorComposite(rKey, 1, 1, category);
var compositeSound = makeSoundEventAccessorComposite(rKey, 1, 1, category);
var compositeSoundWrapped = ModAPI.util.wrap(compositeSound);
soundPool.forEach(sound => {
compositeSoundWrapped.addSoundToEventPool(sound);
compositeSoundWrapped.soundPool.add(sound);
});
AsyncSink.Audio.Objects.push([rKey, compositeSound]);
registry.$put(rKey, compositeSound);
registry.put(rKey, compositeSound);
}
})();

166
examplemods/DuckMod.js Normal file

File diff suppressed because one or more lines are too long

View File

@ -353,21 +353,14 @@ globalThis.modapi_postinit = "(" + (() => {
return this.constructors[i];
}
}
}
},
}
}
if (typeof item?.$meta?.superclass === "function" && item?.$meta?.superclass?.$meta) {
ModAPI.hooks._classMap[compiledName].superclass = item.$meta.superclass;
ModAPI.hooks._classMap[compiledName].superclassName = item.$meta.superclass.$meta.name ?? (item.$meta.superclass ? item.$meta.superclass.name.split("_").map((x, i) => {
if (i === 0) {
return x.split("").join(".") + "."
} else {
return x;
}
}).join("_").replace("._", ".") : null);
ModAPI.hooks._classMap[compiledName].superclassRaw = item.$meta.superclass;
} else {
ModAPI.hooks._classMap[compiledName].superclass = null;
ModAPI.hooks._classMap[compiledName].superclassName = null;
ModAPI.hooks._classMap[compiledName].superclassRaw = null;
}
if (item?.["$$constructor$$"]) {
@ -414,7 +407,19 @@ globalThis.modapi_postinit = "(" + (() => {
}));
ModAPI.hooks._classMap[compiledName].staticVariableNames = Object.keys(ModAPI.hooks._classMap[compiledName].staticVariables);
});
//populate superclasses
compiledNames.forEach(compiledName => {
var item = ModAPI.hooks._classMap[compiledName];
if (item.superclassRaw) {
item.superclass = ModAPI.hooks._classMap[item.superclassRaw.name];
} else {
item.superclass = null;
}
});
ModAPI.reflect.classes = Object.values(ModAPI.hooks._classMap);
ModAPI.reflect.classMap = ModAPI.hooks._classMap;
console.log("[ModAPI] Regenerated hook classmap.");
}
ModAPI.hooks.regenerateClassMap();
@ -430,7 +435,13 @@ globalThis.modapi_postinit = "(" + (() => {
//Magical function for making a subclass with a custom constructor that you can easily use super(...) on.
ModAPI.reflect.getSuper = function getSuper(reflectClass, filter) {
filter ||= (x) => x.length === 1;
while (!reflectClass.internalConstructors.find(filter) && reflectClass.superclass) {
reflectClass = reflectClass.superclass;
}
var initialiser = reflectClass.internalConstructors.find(filter);
if (!initialiser) {
throw new Error("[ModAPI] Failed to find matching superclass constructor in tree.");
}
return function superFunction(thisArg, ...extra_args) {
reflectClass.class.call(thisArg);
initialiser(thisArg, ...extra_args);