Add gun recoil, and a few new features

This commit is contained in:
ZXMushroom63 2024-12-09 18:08:52 +08:00
parent a519eb7afa
commit 6dd5619897
5 changed files with 48 additions and 16 deletions

View File

@ -27,4 +27,13 @@ function myServerSideModCode() {
ModAPI.dedicatedServer.appendCode(myServerSideModCode); ModAPI.dedicatedServer.appendCode(myServerSideModCode);
``` ```
Once serverside, you can only access server side events, like `serverstart` or `tick`. 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)

View File

@ -3,7 +3,13 @@
ModAPI.meta.description("Adds a crafting recipe to create diamond blocks from dirt."); ModAPI.meta.description("Adds a crafting recipe to create diamond blocks from dirt.");
async function addDiamondRecipe() { 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; var ObjectClass = ModAPI.reflect.getClassById("java.lang.Object").class;
function ToChar(char) { function ToChar(char) {
return ModAPI.reflect.getClassById("java.lang.Character").staticMethods.valueOf.method(char[0].charCodeAt(0)); return ModAPI.reflect.getClassById("java.lang.Character").staticMethods.valueOf.method(char[0].charCodeAt(0));

View File

@ -14,10 +14,10 @@
var movingObjectPosition = event.sender.rayTrace(6, 0).getCorrective(); var movingObjectPosition = event.sender.rayTrace(6, 0).getCorrective();
console.log(movingObjectPosition); console.log(movingObjectPosition);
var hitVec = movingObjectPosition.hitVec; //var hitVec = movingObjectPosition.hitVec;
event.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0]( 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; event.preventDefault = true;

View File

@ -6,6 +6,7 @@
ModAPI.meta.description("Requires AsyncSink."); ModAPI.meta.description("Requires AsyncSink.");
function PistolItem() { function PistolItem() {
var recoilSpeed = 0; //recoil controller
var DamageSourceClass = ModAPI.reflect.getClassByName("DamageSource"); var DamageSourceClass = ModAPI.reflect.getClassByName("DamageSource");
var creativeMiscTab = ModAPI.reflect.getClassById("net.minecraft.creativetab.CreativeTabs").staticVariables.tabMisc; var creativeMiscTab = ModAPI.reflect.getClassById("net.minecraft.creativetab.CreativeTabs").staticVariables.tabMisc;
var itemClass = ModAPI.reflect.getClassById("net.minecraft.item.Item"); 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. itemSuper(this); //Use super function to get block properties on this class.
this.$setCreativeTab(creativeMiscTab); 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 eyePosition = player.getPositionEyes(0.0);
var lookVector = player.getLook(0.0); var targetPosition = player.rayTrace(range, 0).hitVec;
var targetPosition = eyePosition.addVector(lookVector.xCoord * range, lookVector.yCoord * range, lookVector.zCoord * range);
var entities = world.getEntitiesWithinAABBExcludingEntity( var entities = world.getEntitiesWithinAABBExcludingEntity(
player.getRef(), player.getRef(),
player.getEntityBoundingBox().expand(range, range, range).getRef() player.getEntityBoundingBox().expand(range, range, range).getRef()
).getCorrective().array; ).getCorrective().array;
var closestEntity = null; var closestEntity = null;
var isHeadshot = false;
var closestDistance = range; var closestDistance = range;
// Iterate through all entities to find the one the player is looking at // Iterate through all entities to find the one the player is looking at
@ -41,13 +49,14 @@
if (distance < closestDistance) { if (distance < closestDistance) {
closestDistance = distance; closestDistance = distance;
closestEntity = entity; closestEntity = entity;
isHeadshot = entity.getPositionEyes(0.0).distanceTo(intercept.hitVec.getRef()) < HEADSHOT_MAX_DISTANCE_FROM_HEAD;
} }
} }
} }
var rayTraceResult = closestEntity; var rayTraceResult = closestEntity;
if (rayTraceResult != null){ if (rayTraceResult != null){
return rayTraceResult; return {entity: rayTraceResult, headshot: isHeadshot};
} else{ } else{
return null; return null;
} }
@ -57,14 +66,22 @@
DamageSourceClass.staticMethods.$callClinit.method(); DamageSourceClass.staticMethods.$callClinit.method();
//Noticed that the gun only worked after an entity in the world takes damage XD //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 //TeaVM is very optimised. Using $callClinit tells it to hurry up pretty much lol
var cactus = DamageSourceClass.staticVariables.cactus;
var cactus = DamageSourceClassstaticVariables.cactus;
var world = ModAPI.util.wrap($world); var world = ModAPI.util.wrap($world);
var entityplayer = ModAPI.util.wrap($player); var entityplayer = ModAPI.util.wrap($player);
var shotentity = entityRayCast(entityplayer, world, 12.0) var shotentitydata = entityRayCast(entityplayer, world, 16.0);
if (shotentity != null){ if (shotentitydata != null){
shotentity.attackEntityFrom(cactus, 10); if (world.isRemote) {
world.playSoundAtEntity(entityplayer.getRef(), ModAPI.util.str("tile.piston.out"), 1.0, 1.8); 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; return $itemstack;
} }
@ -72,7 +89,7 @@
function internal_reg() { function internal_reg() {
var pistol_item = (new nmi_ItemPistol()).$setUnlocalizedName( var pistol_item = (new nmi_ItemPistol()).$setUnlocalizedName(
ModAPI.util.str("pistol") ModAPI.util.str("pistol")
); ).$setMaxStackSize(1);
itemClass.staticMethods.registerItem.method(ModAPI.keygen.item("pistol"), ModAPI.util.str("pistol"), pistol_item); itemClass.staticMethods.registerItem.method(ModAPI.keygen.item("pistol"), ModAPI.util.str("pistol"), pistol_item);
ModAPI.items["pistol"] = pistol_item; ModAPI.items["pistol"] = pistol_item;

View File

@ -90,7 +90,7 @@ globalThis.modapi_postinit = "(" + (() => {
return console.warn("The dedicated server has already launched, ModAPI.dedicatedServer.appendCode() is useless."); return console.warn("The dedicated server has already launched, ModAPI.dedicatedServer.appendCode() is useless.");
} }
if (typeof code === "function") { if (typeof code === "function") {
ModAPI.dedicatedServer._data.push("(" + code.toString() + ")()"); ModAPI.dedicatedServer._data.push("(" + code.toString() + ")(true)");
} else if (typeof code === "string") { } else if (typeof code === "string") {
ModAPI.dedicatedServer._data.push(code); ModAPI.dedicatedServer._data.push(code);
} }