Skip to content

Commit 541f9b4

Browse files
committed
2.2 beta
1 parent 8743dd9 commit 541f9b4

File tree

8 files changed

+203
-5
lines changed

8 files changed

+203
-5
lines changed

docs/apidoc/events.md

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Can only be used in the context of the dedicated server. More: [DedicatedServerD
4949
- `serverstart`:
5050
- Called when the dedicated server starts.
5151
- Event object is blank.
52+
- `bootstrap`:
53+
- Called when the dedicated server registers blocks, items, materials, enchantments, etc.
54+
- This is when you should register cstom blocks and items.
55+
- Event object is blank.
5256
- `serverstop`:
5357
- Called when the dedicated server stops.
5458
- Event object is blank.

docs/apidoc/reflect.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ Each `ReflectClass` has the following properties:
4444
- List of all the static variable names for the class.
4545
- `staticVariables: Map<String, *>`
4646
- key-value dictionary of all the static variables in a class.
47-
- `superclass: String?`
47+
- `superclass: Class?`
48+
- The raw teavm class of the superclass.
49+
- `superclassName: String?`
4850
- The class id of the class's superclass. Eg: `net.minecraft.client.entity.AbstractClientPlayer`
4951
- Will be `null` if `hasMeta` is equal to `false`
5052

examplemods/AsyncSink.js

+22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ModAPI.meta.credits("By ZXMushroom63");
4040

4141
// @type Map<string, ArrayBuffer>
4242
AsyncSink.FS = new Map();
43+
AsyncSink.L10N = new Map();
4344
AsyncSink.FSOverride = new Set();
4445
AsyncSink.MIDDLEWARE = [];
4546
AsyncSink.setFile = function setFile(path, data) {
@@ -149,7 +150,27 @@ ModAPI.meta.credits("By ZXMushroom63");
149150
return originalFileExists.apply(this, args);
150151
};
151152

153+
const L10NRead = ModAPI.util.getMethodFromPackage("net.minecraft.util.StatCollector", "translateToLocal");
154+
const originalL10NRead = ModAPI.hooks.methods[L10NRead];
155+
ModAPI.hooks.methods[L10NRead] = function (...args) {
156+
var key = ModAPI.util.ustr(args[0]);
157+
if (AsyncSink.L10N.has(key)) {
158+
return ModAPI.util.str(AsyncSink.L10N.get(key));
159+
}
160+
return originalL10NRead.apply(this, args);
161+
};
162+
163+
const L10NCheck = ModAPI.util.getMethodFromPackage("net.minecraft.util.StatCollector", "canTranslate");
164+
const originalL10NCheck = ModAPI.hooks.methods[L10NRead];
165+
ModAPI.hooks.methods[L10NCheck] = function (...args) {
166+
if (AsyncSink.L10N.has(ModAPI.util.ustr(args[0]))) {
167+
return 1;
168+
}
169+
return originalL10NCheck.apply(this, args);
170+
};
171+
152172
globalThis.AsyncSink = AsyncSink;
173+
ModAPI.events.newEvent("lib:asyncsink");
153174
ModAPI.events.callEvent("lib:asyncsink", {});
154175
console.log("[AsyncSink] Loaded!");
155176
}
@@ -225,6 +246,7 @@ ModAPI.meta.credits("By ZXMushroom63");
225246
ModAPI.addEventListener("sendchatmessage", (e) => {
226247
if (e.message.toLowerCase().startsWith(".reload_tex")) {
227248
e.preventDefault = true;
249+
ModAPI.mc.renderItem.itemModelMesher.simpleShapesCache.clear();
228250
ModAPI.promisify(ModAPI.mc.refreshResources)();
229251
}
230252
});

examplemods/block_of_steve.js

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//nice little utility function to fix the block identity map
2+
function fixupBlockIds() {
3+
var blockRegistry = ModAPI.util.wrap(ModAPI.reflect.getClassById("net.minecraft.block.Block").staticVariables.blockRegistry).getCorrective();
4+
var BLOCK_STATE_IDS = ModAPI.util.wrap(ModAPI.reflect.getClassById("net.minecraft.block.Block").staticVariables.BLOCK_STATE_IDS).getCorrective();
5+
blockRegistry.registryObjects.hashTableKToV.forEach(entry => {
6+
if (entry) {
7+
var block = entry.value;
8+
var validStates = block.getBlockState().getValidStates();
9+
var stateArray = validStates.array || [validStates.element];
10+
stateArray.forEach(iblockstate => {
11+
var i = blockRegistry.getIDForObject(block.getRef()) << 4 | block.getMetaFromState(iblockstate.getRef());
12+
BLOCK_STATE_IDS.put(iblockstate.getRef(), i);
13+
});
14+
}
15+
});
16+
}
17+
function makeSteveBlock() {
18+
var blockClass = ModAPI.reflect.getClassById("net.minecraft.block.Block");
19+
var iproperty = ModAPI.reflect.getClassById("net.minecraft.block.properties.IProperty").class;
20+
var makeBlockState = ModAPI.reflect.getClassById("net.minecraft.block.state.BlockState").constructors.find(x => x.length === 2);
21+
var blockSuper = ModAPI.reflect.getSuper(blockClass, (x) => x.length === 2);
22+
var nmb_BlockSteve = function nmb_BlockSteve() {
23+
blockSuper(this, ModAPI.materials.rock.getRef());
24+
}
25+
ModAPI.reflect.prototypeStack(blockClass, nmb_BlockSteve);
26+
nmb_BlockSteve.prototype.$isOpaqueCube = function () {
27+
return 1;
28+
}
29+
nmb_BlockSteve.prototype.$createBlockState = function () {
30+
return makeBlockState(this, ModAPI.array.object(iproperty, 0));
31+
}
32+
globalThis.nmb_BlockSteve = nmb_BlockSteve;
33+
}
34+
function registerSteveClientSide() {
35+
var itemClass = ModAPI.reflect.getClassById("net.minecraft.item.Item");
36+
var blockClass = ModAPI.reflect.getClassById("net.minecraft.block.Block");
37+
var block_of_steve = (new nmb_BlockSteve()).$setHardness(-1.0).$setStepSound(blockClass.staticVariables.soundTypeGravel).$setUnlocalizedName(
38+
ModAPI.util.str("steve")
39+
);
40+
blockClass.staticMethods.registerBlock0.method(
41+
198,
42+
ModAPI.util.str("steve"),
43+
block_of_steve
44+
);
45+
itemClass.staticMethods.registerItemBlock0.method(block_of_steve);
46+
ModAPI.addEventListener("lib:asyncsink", async () => {
47+
AsyncSink.L10N.set("tile.steve.name", "Block Of Steve");
48+
AsyncSink.setFile("resourcepacks/AsyncSinkLib/assets/minecraft/models/block/steve.json", JSON.stringify(
49+
{
50+
"parent": "block/cube_all",
51+
"textures": {
52+
"all": "blocks/steve"
53+
}
54+
}
55+
));
56+
AsyncSink.setFile("resourcepacks/AsyncSinkLib/assets/minecraft/models/item/steve.json", JSON.stringify(
57+
{
58+
"parent": "block/steve",
59+
"display": {
60+
"thirdperson": {
61+
"rotation": [10, -45, 170],
62+
"translation": [0, 1.5, -2.75],
63+
"scale": [0.375, 0.375, 0.375]
64+
}
65+
}
66+
}
67+
));
68+
AsyncSink.setFile("resourcepacks/AsyncSinkLib/assets/minecraft/blockstates/steve.json", JSON.stringify(
69+
{
70+
"variants": {
71+
"normal": [
72+
{ "model": "steve" },
73+
]
74+
}
75+
}
76+
));
77+
AsyncSink.setFile("resourcepacks/AsyncSinkLib/assets/minecraft/textures/blocks/steve.png", await (await fetch(
78+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAE0SURBVDhPpdO9S8NAHMbxy3sVfJmMg6h7FRXXkkUUX0addSjo4OAfIDqLIoiLi3+BRRx0EIQOnV0EcVAIWkR0KIFgrcEktX6vcXD0nuE+5Afhnhw5bWy4qylaidOfVQhT0zFKYozjBHVdzi3TwCZvteaS/0fLD8oGf5OzTeyxNUyE3Ln2HmGctpuxKuS3wd76CgPHsrEj142NeojCkHsFry+4c3aJ6g1OtlZp0Ok4DD4i+Y2GIZ+DMMAhtw+fHu8xi3IDM9t5YfMQF71dLHo+ZjsfXbh4WtnH0vYaqp/BcXGGM3D7BxiYTi+el8uYZWm2gM/VB/Tfaqje4GB5iga2Jv+sUuUa5/ITmOXq7gbnC+MY1r9QvcHG9AgN0lRex1u/ilr7ehqWvBNZvMlRbESfqNhAiG/Pb1bHXpMbFgAAAABJRU5ErkJggg=="
79+
)).arrayBuffer());
80+
});
81+
}
82+
function registerSteveServerSide() {
83+
function fixupBlockIds() {
84+
var blockRegistry = ModAPI.util.wrap(ModAPI.reflect.getClassById("net.minecraft.block.Block").staticVariables.blockRegistry).getCorrective();
85+
var BLOCK_STATE_IDS = ModAPI.util.wrap(ModAPI.reflect.getClassById("net.minecraft.block.Block").staticVariables.BLOCK_STATE_IDS).getCorrective();
86+
blockRegistry.registryObjects.hashTableKToV.forEach(entry => {
87+
if (entry) {
88+
var block = entry.value;
89+
var validStates = block.getBlockState().getValidStates();
90+
var stateArray = validStates.array || [validStates.element];
91+
stateArray.forEach(iblockstate => {
92+
var i = blockRegistry.getIDForObject(block.getRef()) << 4 | block.getMetaFromState(iblockstate.getRef());
93+
BLOCK_STATE_IDS.put(iblockstate.getRef(), i);
94+
});
95+
}
96+
});
97+
}
98+
var blockClass = ModAPI.reflect.getClassById("net.minecraft.block.Block");
99+
var itemClass = ModAPI.reflect.getClassById("net.minecraft.item.Item");
100+
ModAPI.addEventListener("bootstrap", () => {
101+
var block_of_steve = (new nmb_BlockSteve()).$setHardness(-1.0).$setStepSound(blockClass.staticVariables.soundTypeGravel).$setUnlocalizedName(
102+
ModAPI.util.str("steve")
103+
);
104+
blockClass.staticMethods.registerBlock0.method(
105+
198,
106+
ModAPI.util.str("steve"),
107+
block_of_steve
108+
);
109+
itemClass.staticMethods.registerItemBlock0.method(block_of_steve);
110+
fixupBlockIds();
111+
});
112+
}
113+
ModAPI.dedicatedServer.appendCode(makeSteveBlock);
114+
makeSteveBlock();
115+
registerSteveClientSide();
116+
fixupBlockIds();
117+
ModAPI.dedicatedServer.appendCode(registerSteveServerSide);

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ <h6>
167167
ModAPI.hooks._rippedData ||= [];
168168
ModAPI.hooks._teavm ||= {};
169169
ModAPI.hooks._rippedConstructors ||= {};
170+
ModAPI.hooks._rippedInternalConstructors ||= {};
170171
ModAPI.hooks.methods ||= {};
171172
ModAPI.hooks._rippedMethodTypeMap ||= {};
172173
ModAPI.hooks._postInit ||= ()=>{};

injector.js

+14
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ var main;(function(){`
136136
);
137137
}
138138
);
139+
140+
const extractInternalConstructorRegex =
141+
/^\s*function (\S*?)__init_\d*?\(\$this/gm; //same as extract constructor regex, but only allow $this as first argument
142+
patchedFile = patchedFile.replaceAll(
143+
extractInternalConstructorRegex,
144+
(match) => {
145+
var fullName = match.match(extractConstructorFullNameRegex);
146+
fullName = fullName[0].replace("function ", "");
147+
return (
148+
`ModAPI.hooks._rippedInternalConstructors[\`${fullName}\`] = ${fullName};
149+
` + match
150+
);
151+
}
152+
);
139153

140154
if(globalThis.optimizePi){
141155
patchedFile = patchedFile.replaceAll(

patches.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ PatchesRegistry.addPatch(function (input) {
3535
if (!$this.$renderHand)`
3636
);
3737
return output;
38-
})
38+
});

postinit.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ globalThis.modapi_postinit = "(" + (() => {
149149
}
150150
if (xOut && typeof xOut === "object" && !Array.isArray(xOut)) {
151151
if (corrective) {
152-
return new Proxy(outputValue.data, CorrectiveRecursive);
152+
return new Proxy(xOut, CorrectiveRecursive);
153153
}
154154
return new Proxy(xOut, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf);
155155
}
@@ -209,6 +209,7 @@ globalThis.modapi_postinit = "(" + (() => {
209209

210210
ModAPI.hooks.regenerateClassMap = function () {
211211
ModAPI.hooks._rippedConstructorKeys = Object.keys(ModAPI.hooks._rippedConstructors);
212+
ModAPI.hooks._rippedInternalConstructorKeys = Object.keys(ModAPI.hooks._rippedInternalConstructors);
212213
ModAPI.hooks._rippedMethodKeys = Object.keys(ModAPI.hooks._rippedMethodTypeMap);
213214

214215
var compiledNames = new Set();
@@ -255,6 +256,7 @@ globalThis.modapi_postinit = "(" + (() => {
255256
"id": classId,
256257
"binaryName": item?.$meta?.binaryName || null,
257258
"constructors": [],
259+
"internalConstructors": [],
258260
"methods": {},
259261
"staticMethods": {},
260262
"staticVariables": {},
@@ -268,9 +270,11 @@ globalThis.modapi_postinit = "(" + (() => {
268270
}
269271
}
270272
if (typeof item?.$meta?.superclass === "function" && item?.$meta?.superclass?.$meta) {
271-
ModAPI.hooks._classMap[compiledName].superclass = item.$meta.superclass.$meta.name;
273+
ModAPI.hooks._classMap[compiledName].superclassName = item.$meta.superclass.$meta.name;
274+
ModAPI.hooks._classMap[compiledName].superclass = item.$meta.superclass;
272275
} else {
273276
ModAPI.hooks._classMap[compiledName].superclass = null;
277+
ModAPI.hooks._classMap[compiledName].superclassName = null;
274278
}
275279
ModAPI.hooks._classMap[compiledName].staticVariableNames = ModAPI.hooks._rippedStaticIndexer[compiledName];
276280
ModAPI.hooks._classMap[compiledName].staticVariables = ModAPI.hooks._rippedStaticProperties[compiledName];
@@ -286,6 +290,13 @@ globalThis.modapi_postinit = "(" + (() => {
286290
}
287291
});
288292
}
293+
294+
ModAPI.hooks._rippedInternalConstructorKeys.forEach(initialiser => { // Find internal constructors/initialisers. Used for calling super() on custom classes. (They are the different implementations of a classes constructor, that don't automatically create an object. Thus, it is identical to calling super)
295+
if (initialiser.startsWith(compiledName + "__init_") && !initialiser.includes("$lambda$")) {
296+
ModAPI.hooks._classMap[compiledName].internalConstructors.push(ModAPI.hooks._rippedInternalConstructors[initialiser]);
297+
}
298+
});
299+
289300
ModAPI.hooks._rippedMethodKeys.forEach((method) => {
290301
if (method.startsWith(compiledName + "_") && !method.includes("$lambda$")) {
291302
var targetMethodMap = ModAPI.hooks._classMap[compiledName].methods;
@@ -319,6 +330,31 @@ globalThis.modapi_postinit = "(" + (() => {
319330
var key = classKeys.filter(k => { return ModAPI.hooks._classMap[k].name === className })[0];
320331
return key ? ModAPI.hooks._classMap[key] : null;
321332
}
333+
334+
//Magical function for making a subclass with a custom constructor that you can easily use super(...) on.
335+
ModAPI.reflect.getSuper = function getSuper(reflectClass, filter) {
336+
filter ||= ()=>true;
337+
var initialiser = reflectClass.internalConstructors.find(filter);
338+
return function superFunction(thisArg, ...extra_args) {
339+
reflectClass.class.call(thisArg);
340+
initialiser(thisArg, ...extra_args);
341+
}
342+
}
343+
344+
//Iteratively load the superclasses' prototype methods.
345+
ModAPI.reflect.prototypeStack = function prototypeStack(reflectClass, classFn) {
346+
var stack = [reflectClass.class.prototype];
347+
var currentSuperclass = reflectClass.superclass;
348+
while (currentSuperclass) {
349+
stack.push(currentSuperclass.prototype);
350+
currentSuperclass = currentSuperclass?.$meta?.superclass;
351+
}
352+
stack.reverse();
353+
stack.forEach(proto => {
354+
Object.assign(classFn.prototype, proto);
355+
});
356+
}
357+
322358
var reloadDeprecationWarnings = 0;
323359
const TeaVMArray_To_Recursive_BaseData_ProxyConf = {
324360
get(target, prop, receiver) {
@@ -556,7 +592,7 @@ globalThis.modapi_postinit = "(" + (() => {
556592

557593
//Function used for running @Async / @Async-dependent TeaVM methods.
558594
ModAPI.promisify = function promisify(fn) {
559-
return function promisifiedJavaMethpd(...inArguments) {
595+
return function promisifiedJavaMethod(...inArguments) {
560596
return new Promise((res, rej) => {
561597
Promise.resolve().then( //queue microtask
562598
() => {
@@ -850,10 +886,12 @@ globalThis.modapi_postinit = "(" + (() => {
850886
ModAPI.enchantments = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.enchantment.Enchantment")].staticVariables, StaticProps_ProxyConf);
851887
}
852888

889+
ModAPI.events.newEvent("bootstrap", "server");
853890
const originalBootstrap = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.init.Bootstrap", "register")];
854891
ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.init.Bootstrap", "register")] = function (...args) {
855892
var x = originalBootstrap.apply(this, args);
856893
ModAPI.util.bootstrap();
894+
ModAPI.events.callEvent("bootstrap", {});
857895
console.log("[ModAPI] Hooked into bootstrap. .blocks, .items, .materials and .enchantments are now accessible.");
858896
return x;
859897
}

0 commit comments

Comments
 (0)