diff --git a/docs/apidoc/events.md b/docs/apidoc/events.md index ee6df14..a976db0 100644 --- a/docs/apidoc/events.md +++ b/docs/apidoc/events.md @@ -65,6 +65,8 @@ Can only be used in the context of the dedicated server. More: [DedicatedServerD - Passes an object with properties: - `command: String` - String representing the command. + - `sender: ICommandSender` + - Object that sent the command - `preventDefault: Boolean` - Boolean representing whether or not to cancel processing the command. Default is `false`. diff --git a/docs/quirks.md b/docs/quirks.md index 411a7d4..1b894f1 100644 --- a/docs/quirks.md +++ b/docs/quirks.md @@ -5,4 +5,7 @@ When TeaVM compiles code, it sometimes does strange things. TeaVM will add suffixes to some variables, seemingly randomly. An example is the property `inGround` of any entity. When accessing this on the `ModAPI.player.fishEntity` object, TeaVM has renamed it to `inGround2`. #### Collapsing Methods -When I was trying to hook into the server-side processing of chat messages, I found that chat packets were handled by the method `processChatMessage` in `NetHandlerPlayServer`. However, in the compiled JavaScript, this method no longer exists. This is because it is only used once, in the `processPacket` method of `C01PacketChatMessage`. TeaVM automatically saw this, and collapsed one method into the other. \ No newline at end of file +When I was trying to hook into the server-side processing of chat messages, I found that chat packets were handled by the method `processChatMessage` in `NetHandlerPlayServer`. However, in the compiled JavaScript, this method no longer exists. This is because it is only used once, in the `processPacket` method of `C01PacketChatMessage`. TeaVM automatically saw this, and collapsed one method into the other. + +#### Dedicated Server Not Responding / Client Not Responding +Incorrectly patching methods with ModAPI.hooks (such as returning `false` instead of `0`, or a javascript string without using `ModAPI.str()`) will effectively cause the memory stack to implode, along with all crash handlers. I came across this when I was using the `processcommand` event with preventDefault set to true. I didn't return any value when patching methods for the event being `preventDefault`ed, when the output expected was a java boolean (`0`/`1`). This would cause the dedicated server to freeze/lock up, without triggering any form of crash. \ No newline at end of file diff --git a/examplemods/blocklook.js b/examplemods/blocklook.js index b0dd689..4e8b81e 100644 --- a/examplemods/blocklook.js +++ b/examplemods/blocklook.js @@ -1,9 +1,9 @@ //WIP Blocklook plugin //If someone can fix the crash, thank you and also i'll add you to credits. -ModAPI.meta.title("BlockLook"); -ModAPI.meta.credits("Made with ❤️ by ZXMushroom63"); -ModAPI.meta.icon("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAPlJREFUOE+Fk7EVwjAMRM8TQJEmS0DBBtAwBEtQwBBkCBqGoIEhsgFNGrKDeOdEjuw4Rk1eLN33WbIdAEEmROJl51yuDFyVtFgrVVTKZwEqfAnQAjiPm+dcRQAVfkchnRCg33sCYn0ABLsd0NeTiACFfAC8DSQLoFS6AUDQFQCFDBX7GhHMAPIE3HFqNkGHOhZWAvSuAFC/jlvbkIv/q9AUADdz4Ad8g3xwHHvtBBPNwhEUMHYuAuwArJgoAU5mZm3iIAAAuO2CAwLM4GcOyKeLHIC5cBc2A2gGWA8reiOjMdqGLz2cv1c5GdzkKHmZWhccpEJr0xbn6n64M6oBwREDxAAAAABJRU5ErkJggg=="); -ModAPI.meta.description("EaglerForge port of the bukkit BlockLook plugin by GeorgeNotFound. Use /blocklook in a single-player world to toggle the plugin."); +// ModAPI.meta.title("BlockLook"); +// ModAPI.meta.credits("Made with ❤️ by ZXMushroom63"); +// ModAPI.meta.icon("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAPlJREFUOE+Fk7EVwjAMRM8TQJEmS0DBBtAwBEtQwBBkCBqGoIEhsgFNGrKDeOdEjuw4Rk1eLN33WbIdAEEmROJl51yuDFyVtFgrVVTKZwEqfAnQAjiPm+dcRQAVfkchnRCg33sCYn0ABLsd0NeTiACFfAC8DSQLoFS6AUDQFQCFDBX7GhHMAPIE3HFqNkGHOhZWAvSuAFC/jlvbkIv/q9AUADdz4Ad8g3xwHHvtBBPNwhEUMHYuAuwArJgoAU5mZm3iIAAAuO2CAwLM4GcOyKeLHIC5cBc2A2gGWA8reiOjMdqGLz2cv1c5GdzkKHmZWhccpEJr0xbn6n64M6oBwREDxAAAAABJRU5ErkJggg=="); +// ModAPI.meta.description("EaglerForge port of the bukkit BlockLook plugin by GeorgeNotFound. Use /blocklook in a single-player world to toggle the plugin."); ModAPI.dedicatedServer.appendCode(function () { var worldMethodMap = ModAPI.reflect.getClassById("net.minecraft.world.World").methods; var rayTraceMethod = worldMethodMap[Object.keys(worldMethodMap).filter(key => { @@ -62,10 +62,16 @@ ModAPI.dedicatedServer.appendCode(function () { console.log("trace complete."); if (hitResult) { console.log("Attempting to set world state."); - var blockPos = blockPosConstructor(hitResult.$hitVec.$xCoord, hitResult.$hitVec.$yCoord, hitResult.$hitVec.$zCoord); + var blockPos = blockPosConstructor(Math.round(hitResult.$hitVec.$xCoord), Math.round(hitResult.$hitVec.$yCoord), Math.round(hitResult.$hitVec.$zCoord)); var blockType = blockTypesList[Math.floor(Math.random() * blockTypesList.length)]; - blockType = ModAPI.blocks[blockType]; - pair.world.setBlockState(blockPos, blockStateConstructor(blockType.getRef(), ModAPI.util.makeArray(iproperty, [])), 0); + blockType = ModAPI.blocks["dirt"]; //blockType + var block = blockStateConstructor(blockType.getRef(), ModAPI.util.makeArray(iproperty, [])); + console.log(blockPos); + console.log(block); + ModAPI.freezeCallstack(); + pair.world.setBlockState(blockPos, block, 0); + ModAPI.unfreezeCallstack(); + console.log("Set world state."); } console.log("sub complete"); }); diff --git a/examplemods/setblocktest.js b/examplemods/setblocktest.js new file mode 100644 index 0000000..89e602c --- /dev/null +++ b/examplemods/setblocktest.js @@ -0,0 +1,15 @@ +//Test to make sure I can set a block +ModAPI.dedicatedServer.appendCode(function () { + var blockPosConstructor = ModAPI.reflect.getClassById("net.minecraft.util.BlockPos").constructors.find((x) => { return x.length === 3 }); + var blockStateConstructor = ModAPI.reflect.getClassByName("BlockState").constructors[0]; + var iproperty = ModAPI.reflect.getClassById("net.minecraft.block.property.IProperty").class; + ModAPI.addEventListener("processcommand", (event) => { + if (event.command.toLowerCase().startsWith("/testcmd")) { + var blockPos = blockPosConstructor(0, 0, 0); + var blockType = ModAPI.blocks["dirt"]; //blockType + var block = blockStateConstructor(blockType.getRef(), ModAPI.util.makeArray(iproperty, [])); + event.sender.getServerForPlayer().setBlockState(blockPos, block, 0); + event.preventDefault = true; + } + }); +}); \ No newline at end of file diff --git a/examplemods/talkback.js b/examplemods/talkback.js index c2db73e..2947a36 100644 --- a/examplemods/talkback.js +++ b/examplemods/talkback.js @@ -3,13 +3,13 @@ PluginAPI.addEventListener("processcommand", (event) => { if (event.command.toLowerCase().startsWith("/talkback")) { var message = event.command.substring("/talkback ".length); - /*if ( - ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender) - ) {*/ + if ( + ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender.getRef()) + ) { event.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str(message.toUpperCase()))); - //} + } event.preventDefault = true; } }); }); -})(); +})(); \ No newline at end of file diff --git a/index.html b/index.html index f55b3dd..da70d57 100644 --- a/index.html +++ b/index.html @@ -249,7 +249,7 @@ var main;(function(){` `return $rt_currentNativeThread;`, (match) => { return ( - `if(ModAPI.hooks.freezeCallstack){return {push: (a)=>{}, pop: ()=>{console.warn("Frozen stack was popped, context is now unstable.")}}};` + + `if(ModAPI.hooks.freezeCallstack){return {isResuming: ()=>{false}, isSuspending: ()=>{false}, push: (a)=>{}, pop: ()=>{console.warn("Frozen stack was popped, context is now unstable.")}}};` + match ); }