Skip to content

Commit

Permalink
Fix gun bug, add more static variables and add clinit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Dec 9, 2024
1 parent 8c1ae93 commit a519eb7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
7 changes: 6 additions & 1 deletion examplemods/guns.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ModAPI.meta.description("Requires AsyncSink.");

function PistolItem() {
var DamageSourceClass = ModAPI.reflect.getClassByName("DamageSource");
var creativeMiscTab = ModAPI.reflect.getClassById("net.minecraft.creativetab.CreativeTabs").staticVariables.tabMisc;
var itemClass = ModAPI.reflect.getClassById("net.minecraft.item.Item");
var itemSuper = ModAPI.reflect.getSuper(itemClass, (x) => x.length === 1);
Expand Down Expand Up @@ -53,7 +54,11 @@
}
ModAPI.reflect.prototypeStack(itemClass, nmi_ItemPistol);
nmi_ItemPistol.prototype.$onItemRightClick = function ($itemstack, $world, $player) {
var cactus = ModAPI.reflect.getClassByName("DamageSource").staticVariables.cactus;
DamageSourceClass.staticMethods.$callClinit.method();
//Noticed that the gun only worked after an entity in the world takes damage XD
//TeaVM is very optimised. Using $callClinit tells it to hurry up pretty much lol

var cactus = DamageSourceClassstaticVariables.cactus;
var world = ModAPI.util.wrap($world);
var entityplayer = ModAPI.util.wrap($player);
var shotentity = entityRayCast(entityplayer, world, 12.0)
Expand Down
27 changes: 10 additions & 17 deletions injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@ function entriesToStaticVariableProxy(entries, prefix) {
var getComponents = "";
entries.forEach((entry) => {
getComponents += `
case \`${entry.name}\`:
return ${entry.variable};
break;`;
case \`${entry.name}\`: return ${entry.variable};`;
});
getComponents += `
default:
return Reflect.get(a,b,c);`
default: return Reflect.get(a,b,c);`

var setComponents = "";
entries.forEach((entry) => {
setComponents += `
case \`${entry.name}\`:
${entry.variable} = c;
break;`;
case \`${entry.name}\`: ${entry.variable} = c; break;`;
});
setComponents += `
default:
a[b]=c;`
setComponents += ` default: a[b]=c;`
/*/
ModAPI.hooks._rippedStaticIndexer[\`${prefix.replace(
Expand Down Expand Up @@ -196,8 +189,7 @@ var main;(function(){`
(match) => {
if (
match.includes("__init_") ||
match.includes("__clinit_") ||
match.includes("_$callClinit")
match.includes("__clinit_")
) {
return match;
}
Expand All @@ -212,13 +204,14 @@ var main;(function(){`
ModAPI.hooks.methods[\`${fullName}\`]=` +
match.replace(fullName + "(", "(")
);
return match;
}
);
var staticVariables = [
...patchedFile.matchAll(/var \S+?_\S+?_\S+? = null;/gm),
...patchedFile.matchAll(/var \S+?_\S+?_\S+? = /gm),
].flatMap((x) => {
return x[0];
}).filter(x => {
return (!x.includes("$_clinit_$")) && (!x.includes("$lambda$"))
});
patchedFile = patchedFile.replaceAll(
/var \S+?_\S+? = \$rt_classWithoutFields\(\S*?\);/gm,
Expand All @@ -232,7 +225,7 @@ var main;(function(){`
if (entry.startsWith(prefix)) {
var variableName = entry
.replace("var ", "")
.replace(" = null;", "");
.replace(" = ", "");
var segments = variableName.split("_");
segments.splice(0, 2);
var name = segments.join("_");
Expand Down Expand Up @@ -262,7 +255,7 @@ var main;(function(){`
if (entry.startsWith(prefix)) {
var variableName = entry
.replace("var ", "")
.replace(" = null;", "");
.replace(" = ", "");
var segments = variableName.split("_");
segments.splice(0, 2);
var name = segments.join("_");
Expand Down
19 changes: 0 additions & 19 deletions postinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,38 +890,19 @@ globalThis.modapi_postinit = "(" + (() => {
}

ModAPI.events.newEvent("bootstrap", "server");
ModAPI.events.newEvent("prebootstrap", "server");
const bootstrapClass = ModAPI.reflect.getClassById("net.minecraft.init.Bootstrap");
const originalBootstrap = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.init.Bootstrap", "register")];
ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.init.Bootstrap", "register")] = function (...args) {
if (bootstrapClass.staticVariables.alreadyRegistered) {
return;
}
ModAPI.events.callEvent("prebootstrap", {});
var x = originalBootstrap.apply(this, args);
ModAPI.util.bootstrap();
console.log("[ModAPI] Hooked into bootstrap. .blocks, .items, .materials and .enchantments are now accessible.");
ModAPI.events.callEvent("bootstrap", {});
return x;
}


ModAPI.events.newEvent("registeritems", "server");
const originalItemRegister = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.item.Item", "registerItems")];
ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.item.Item", "registerItems")] = function (...args) {
var x = originalItemRegister.apply(this, args);
ModAPI.events.callEvent("registeritems", {});
return x;
}

ModAPI.events.newEvent("registerblocks", "server");
const originalBlockRegister = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.block.Block", "registerBlocks")];
ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.block.Block")] = function (...args) {
var x = originalBlockRegister.apply(this, args);
ModAPI.events.callEvent("registerblocks", {});
return x;
}

const originalOptionsInit = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.client.gui.GuiOptions", "initGui")];
ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.client.gui.GuiOptions", "initGui")] = function (...args) {
var x = originalOptionsInit.apply(this, args);
Expand Down

0 comments on commit a519eb7

Please sign in to comment.