Skip to content

Commit

Permalink
Updates, fix talkback again
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Sep 12, 2024
1 parent 07c0254 commit 886f85a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/apidoc/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
5 changes: 4 additions & 1 deletion docs/quirks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
20 changes: 13 additions & 7 deletions examplemods/blocklook.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down Expand Up @@ -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");
});
Expand Down
15 changes: 15 additions & 0 deletions examplemods/setblocktest.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
10 changes: 5 additions & 5 deletions examplemods/talkback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
});
})();
})();
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ <h6>
`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
);
}
Expand Down

0 comments on commit 886f85a

Please sign in to comment.