Skip to content

Commit

Permalink
Merge pull request #43 from eaglerforge/main
Browse files Browse the repository at this point in the history
Add gun recoil, and a few new features
  • Loading branch information
ZXMushroom63 authored Dec 9, 2024
2 parents bc0bba9 + 6dd5619 commit 5689fc8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
11 changes: 10 additions & 1 deletion docs/apidoc/dedicatedserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ function 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)
8 changes: 7 additions & 1 deletion examplemods/diamondBlockCustomCraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions examplemods/entity_trace_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 28 additions & 11 deletions examplemods/guns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -57,22 +66,30 @@
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;
}

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;

Expand Down
2 changes: 1 addition & 1 deletion postinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5689fc8

Please sign in to comment.