frame event, scaledresolution, crafting postprocessing for LCI

This commit is contained in:
ZXMushroom63 2024-09-18 15:45:13 +08:00
parent c1234e997b
commit 8d4a8152be
5 changed files with 47 additions and 9 deletions

View File

@ -39,7 +39,9 @@ Events broadcast data for use in mods.
- String representing the type of event being fired.
- `data: Object`
- Object representing the original arguments to be passed to the callback.
- `frame`:
- Called just when a frame is rendered on the client.
- Event object is blank.
### Server Side Events
Can only be used in the context of the dedicated server. More: [DedicatedServerDocumentation](dedicatedserver.md)
- `serverstart`:

View File

@ -59,6 +59,10 @@ The global object has the following properties:
- `ModAPI.array`
- This object is used to interact and create arrays easily.
- More: [ArrayDocumentation](array.md)
- `ModAPI.resolution`
- This object is used to query information about GUI dimensions, to make drawing to the screen easier, generated when the `frame` event is fired.
- Deprecated alias (please do not use): `ModAPI.ScaledResolution`
- More: [ArrayDocumentation](array.md)
- `ModAPI.version: String`
- The version of ModAPI.
- `ModAPI.flavour: String`

View File

@ -6,11 +6,12 @@
ModAPI.meta.description("Library to make adding basic custom items easier.");
ModAPI.events.newEvent("lib:libcustomitems:loaded");
function libServerside() {
var packetblockchange = ModAPI.reflect.getClassByName("S23PacketBlockChange").constructors.find(x => {return x.length === 2});
var packetblockchange = ModAPI.reflect.getClassByName("S23PacketBlockChange").constructors.find(x => { return x.length === 2 });
var sendPacket = ModAPI.reflect.getClassByName("NetHandlerPlayServer").methods.sendPacket.method;
globalThis.LCI_REGISTRY ||= [];
globalThis.LCI_RMBEVENTS ||= {};
globalThis.LCI_LMBEVENTS ||= {};
globalThis.LCI_RECIPEEVENTS ||= {};
var useName = ModAPI.util.getMethodFromPackage("net.minecraft.network.NetHandlerPlayServer", "processPlayerBlockPlacement");
var oldUse = ModAPI.hooks.methods[useName];
ModAPI.hooks.methods[useName] = function ($this, packet) {
@ -61,15 +62,15 @@
var statusTag = Object.keys(packet.$status).find(x => { return x.startsWith("$name") });
var positionTag = Object.keys(packet).filter(x => { return x.startsWith("$position") })[0];
var stat = ModAPI.util.unstr(packet.$status[statusTag]);
if (stat === "START_DESTROY_BLOCK") {
if (stat === "START_DESTROY_BLOCK") {
sendPacket($this, packetblockchange($this.$serverController.$worldServerForDimension($this.$playerEntity.$dimension), packet[positionTag]));
}
if (stat !== "START_DESTROY_BLOCK") {
if (stat === "STOP_DESTROY_BLOCK") {
sendPacket($this, packetblockchange($this.$serverController.$worldServerForDimension($this.$playerEntity.$dimension), packet[positionTag]));
}
if (stat !== "START_DESTROY_BLOCK") {
if (stat === "STOP_DESTROY_BLOCK") {
sendPacket($this, packetblockchange($this.$serverController.$worldServerForDimension($this.$playerEntity.$dimension), packet[positionTag]));
}
return 0;
}
return 0;
}
var r = globalThis.LCI_LMBEVENTS[cid].call(globalThis,
new Proxy($this.$playerEntity, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf),
@ -91,6 +92,7 @@
globalThis.LCI_REGISTRY ||= [];
globalThis.LCI_RMBEVENTS ||= {};
globalThis.LCI_LMBEVENTS ||= {};
globalThis.LCI_RECIPEEVENTS ||= {};
globalThis.LCI_REGISTRY.push(data.tag);
if (data.onRightClickGround) {
globalThis.LCI_RMBEVENTS[data.tag] = new Function("user", "world", "itemstack", "blockpos", data.onRightClickGround);
@ -98,6 +100,9 @@
if (data.onLeftClickGround) {
globalThis.LCI_LMBEVENTS[data.tag] = new Function("user", "world", "itemstack", "blockpos", data.onLeftClickGround);
}
if (data.craftingExtra) {
globalThis.LCI_RECIPEEVENTS[data.tag] = new Function("itemstack", data.craftingExtra);
}
var ObjectClass = ModAPI.reflect.getClassById("java.lang.Object").class;
function ToChar(char) {
return ModAPI.reflect.getClassById("java.lang.Character").staticMethods.valueOf.method(char[0].charCodeAt(0));
@ -132,6 +137,9 @@
var lore = ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagList").constructors[0]();
lore.$appendTag(ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagString").constructors.filter(x => { return x.length === 1 })[0](ModAPI.util.str(data.tag)));
displayTag.$setTag(ModAPI.util.str("Lore"), lore);
if (globalThis.LCI_RECIPEEVENTS[data.tag]) {
globalThis.LCI_RECIPEEVENTS[data.tag](new Proxy(testItem, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf));
}
var craftingManager = ModAPI.reflect.getClassById("net.minecraft.item.crafting.CraftingManager").staticMethods.getInstance.method();
ModAPI.hooks.methods.nmic_CraftingManager_addRecipe(craftingManager, testItem, recipe);

View File

@ -648,6 +648,18 @@ globalThis.modapi_postinit = `(() => {
return sendChatMessage.apply(this, [$this, $message]);
}
const ScaledResolutionConstructor = ModAPI.reflect.getClassByName("ScaledResolution").constructors[0];
ModAPI.events.newEvent("frame", "client");
const frameMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.client.Minecraft", "runTick");
const frameMethod = ModAPI.hooks.methods[frameMethodName];
ModAPI.hooks.methods[frameMethodName] = function (...args) {
ModAPI.events.callEvent("frame", {});
if (ModAPI.required.has("resolution") && ModAPI.mcinstance) {
ModAPI.ScaledResolution = ModAPI.resolution = new Proxy(ScaledResolutionConstructor(ModAPI.mcinstance), TeaVM_to_Recursive_BaseData_ProxyConf);
}
return frameMethod.apply(this, args);
}
ModAPI.events.newEvent("tick", "server");
const serverTickMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.server.MinecraftServer", "tick");
const serverTickMethod = ModAPI.hooks.methods[serverTickMethodName];

View File

@ -648,6 +648,18 @@
return sendChatMessage.apply(this, [$this, $message]);
}
const ScaledResolutionConstructor = ModAPI.reflect.getClassByName("ScaledResolution").constructors[0];
ModAPI.events.newEvent("frame", "client");
const frameMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.client.Minecraft", "runTick");
const frameMethod = ModAPI.hooks.methods[frameMethodName];
ModAPI.hooks.methods[frameMethodName] = function (...args) {
ModAPI.events.callEvent("frame", {});
if (ModAPI.required.has("resolution") && ModAPI.mcinstance) {
ModAPI.ScaledResolution = ModAPI.resolution = new Proxy(ScaledResolutionConstructor(ModAPI.mcinstance), TeaVM_to_Recursive_BaseData_ProxyConf);
}
return frameMethod.apply(this, args);
}
ModAPI.events.newEvent("tick", "server");
const serverTickMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.server.MinecraftServer", "tick");
const serverTickMethod = ModAPI.hooks.methods[serverTickMethodName];