diff --git a/docs/apidoc/dedicatedserver.md b/docs/apidoc/dedicatedserver.md index 3127e94..04fda60 100644 --- a/docs/apidoc/dedicatedserver.md +++ b/docs/apidoc/dedicatedserver.md @@ -27,4 +27,13 @@ function myServerSideModCode() { ModAPI.dedicatedServer.appendCode(myServerSideModCode); ``` -Once serverside, you can only access server side events, like `serverstart` or `tick`. \ No newline at end of file +You can check if a mod if running serverside using this: +```javascript +function myServerSideModCode(isServerSide) { + console.log(isServerSide); +} +myServerSideModCode(); //logs undefined +ModAPI.dedicatedServer.appendCode(myServerSideModCode); //when server starts, will log `true` +``` + +Once serverside, you can only access server side events, like `serverstart` or `tick`. [See serverside events](events.md#server-side-events) \ No newline at end of file diff --git a/examplemods/diamondBlockCustomCraft.js b/examplemods/diamondBlockCustomCraft.js index 6c3412f..d6a9520 100644 --- a/examplemods/diamondBlockCustomCraft.js +++ b/examplemods/diamondBlockCustomCraft.js @@ -3,7 +3,13 @@ ModAPI.meta.description("Adds a crafting recipe to create diamond blocks from dirt."); async function addDiamondRecipe() { - await new Promise((res,rej)=>{var x = setInterval(()=>{if(ModAPI.blocks){clearInterval(x);res();}}, 100);}) + await new Promise((res, rej) => { + if (ModAPI.blocks) { + res() + } else { + ModAPI.addEventListener("bootstrap", res); + } + }); 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)); diff --git a/examplemods/entity_trace_test.js b/examplemods/entity_trace_test.js index d6207a3..b5031b4 100644 --- a/examplemods/entity_trace_test.js +++ b/examplemods/entity_trace_test.js @@ -14,10 +14,10 @@ var movingObjectPosition = event.sender.rayTrace(6, 0).getCorrective(); console.log(movingObjectPosition); - var hitVec = movingObjectPosition.hitVec; + //var hitVec = movingObjectPosition.hitVec; event.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0]( - movingObjectPosition.toString() //This is a java string, but that's ok since it goes into a java method. + movingObjectPosition.toString().getRef() //This is a java string, but that's ok since it goes into a java method. )); } event.preventDefault = true; diff --git a/examplemods/guns.js b/examplemods/guns.js index de22609..083708b 100644 --- a/examplemods/guns.js +++ b/examplemods/guns.js @@ -6,6 +6,7 @@ ModAPI.meta.description("Requires AsyncSink."); function PistolItem() { + var recoilSpeed = 0; //recoil controller 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"); @@ -14,15 +15,22 @@ itemSuper(this); //Use super function to get block properties on this class. this.$setCreativeTab(creativeMiscTab); } - function entityRayCast(player, world, range){ + + ModAPI.addEventListener("update", ()=>{ //recoil update loop (client) + ModAPI.player.rotationPitch -= recoilSpeed; + recoilSpeed *= 0.7; + }); + + function entityRayCast(player, world, range) { + const HEADSHOT_MAX_DISTANCE_FROM_HEAD = 0.72; var eyePosition = player.getPositionEyes(0.0); - var lookVector = player.getLook(0.0); - var targetPosition = eyePosition.addVector(lookVector.xCoord * range, lookVector.yCoord * range, lookVector.zCoord * range); + var targetPosition = player.rayTrace(range, 0).hitVec; var entities = world.getEntitiesWithinAABBExcludingEntity( player.getRef(), player.getEntityBoundingBox().expand(range, range, range).getRef() ).getCorrective().array; var closestEntity = null; + var isHeadshot = false; var closestDistance = range; // Iterate through all entities to find the one the player is looking at @@ -41,13 +49,14 @@ if (distance < closestDistance) { closestDistance = distance; closestEntity = entity; + isHeadshot = entity.getPositionEyes(0.0).distanceTo(intercept.hitVec.getRef()) < HEADSHOT_MAX_DISTANCE_FROM_HEAD; } } } var rayTraceResult = closestEntity; if (rayTraceResult != null){ - return rayTraceResult; + return {entity: rayTraceResult, headshot: isHeadshot}; } else{ return null; } @@ -57,14 +66,22 @@ 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 cactus = DamageSourceClass.staticVariables.cactus; var world = ModAPI.util.wrap($world); var entityplayer = ModAPI.util.wrap($player); - var shotentity = entityRayCast(entityplayer, world, 12.0) - if (shotentity != null){ - shotentity.attackEntityFrom(cactus, 10); - world.playSoundAtEntity(entityplayer.getRef(), ModAPI.util.str("tile.piston.out"), 1.0, 1.8); + var shotentitydata = entityRayCast(entityplayer, world, 16.0); + if (shotentitydata != null){ + if (world.isRemote) { + recoilSpeed += 4; + } else { + shotentitydata.entity.attackEntityFrom(cactus, 10 + (16 * shotentitydata.headshot)); + if (shotentitydata.headshot) { + console.log("H E A D S H O T"); + } + world.playSoundAtEntity(entityplayer.getRef(), ModAPI.util.str("tile.piston.out"), 1.0, 1.8); + } + } else if (!world.isRemote) { + world.playSoundAtEntity(entityplayer.getRef(), ModAPI.util.str("random.click"), 1.0, 1.8); } return $itemstack; } @@ -72,7 +89,7 @@ function internal_reg() { var pistol_item = (new nmi_ItemPistol()).$setUnlocalizedName( ModAPI.util.str("pistol") - ); + ).$setMaxStackSize(1); itemClass.staticMethods.registerItem.method(ModAPI.keygen.item("pistol"), ModAPI.util.str("pistol"), pistol_item); ModAPI.items["pistol"] = pistol_item; diff --git a/postinit.js b/postinit.js index 857026d..f7b290c 100644 --- a/postinit.js +++ b/postinit.js @@ -90,7 +90,7 @@ globalThis.modapi_postinit = "(" + (() => { return console.warn("The dedicated server has already launched, ModAPI.dedicatedServer.appendCode() is useless."); } if (typeof code === "function") { - ModAPI.dedicatedServer._data.push("(" + code.toString() + ")()"); + ModAPI.dedicatedServer._data.push("(" + code.toString() + ")(true)"); } else if (typeof code === "string") { ModAPI.dedicatedServer._data.push(code); }