0% found this document useful (0 votes)
7 views3 pages

Message

The document outlines a flexible riding system for a game, allowing players to ride mobs with customizable options such as speed, jump height, and health. It includes functions for starting rides, handling player actions, and managing the state of the riding player and mob. Additionally, it addresses player dismounting and cleanup processes when a mob despawns or the player leaves the game.

Uploaded by

Miha Skichko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Message

The document outlines a flexible riding system for a game, allowing players to ride mobs with customizable options such as speed, jump height, and health. It includes functions for starting rides, handling player actions, and managing the state of the riding player and mob. Additionally, it addresses player dismounting and cleanup processes when a mob despawns or the player leaves the game.

Uploaded by

Miha Skichko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

====================================================================
F L E X I B L E R I D I N G S Y S T E M (v5.7 - Bug Fix)
====================================================================
*/

// --- Global State ---


var ridingPlayerId = null;
var riddenMobId = null;

// --- RIDE ACTIVATOR FUNCTION ---


function startRideWithOptions(playerId, options) {
ridingPlayerId = playerId;
riddenMobId = null;

// --- APPLY RIDING STATS ---


api.setClientOption(playerId, "speedMultiplier", options.speed);
api.setClientOption(playerId, "jumpAmount", options.jump);
api.setClientOption(playerId, "dealingDamageMultiplier", options.damage);
api.setClientOption(playerId, "maxHealth", options.health);

// --- CORRECTED LINE ---


// Changed 'null' to 'undefined' for the 3rd argument.
api.setHealth(playerId, options.health, undefined, true);

// --- Apply cosmetic/mob options ---


if (options.setOpacity) {
api.setPlayerOpacity(playerId, 0);
}

if (options.useMob) {
const spawnPos = api.getPosition(playerId);
if (spawnPos) {
const mobId = api.attemptSpawnMob(options.mobType, spawnPos[0],
spawnPos[1], spawnPos[2]);
if (mobId) {
riddenMobId = mobId;
if (!options.isHostile) {
api.setMobSetting(mobId, "attackDamage", 0);
api.setMobSetting(mobId, "attackRadius", 0);
api.setMobSetting(mobId, "hostilityRadius", 0);
}
api.sendMessage(playerId, "You are now controlling the " +
options.mobType + "! Crouch to dismount.", { color: "lime" });
} else {
api.sendMessage(playerId, "Failed to spawn mob.", { color:
"red" });
forceDismount();
}
}
} else {
api.sendMessage(playerId, "You are now in driving mode! Crouch to exit.", {
color: "cyan" });
}
}

// --- Core Riding Logic ---


tick = (ms) => {
if (ridingPlayerId) {
if (!api.playerIsInGame(ridingPlayerId)) {
forceDismount();
return;
}
if (api.isPlayerCrouching(ridingPlayerId)) {
forceDismount();
return;
}
if (riddenMobId) {
if (!api.isAlive(riddenMobId)) {
cleanupAfterDespawn();
return;
}
const playerPos = api.getPosition(ridingPlayerId);
const facingInfo = api.getPlayerFacingInfo(ridingPlayerId);
if (!playerPos || !facingInfo) return;
api.setEntityHeading(riddenMobId, facingInfo.moveHeading);
api.setPosition(riddenMobId, playerPos[0], playerPos[1], playerPos[2]);
}
}
};

// --- Event Handlers ---


onPlayerLeave = (playerId, serverIsShuttingDown) => {
if (playerId === ridingPlayerId) {
forceDismount();
}
};
onMobDespawned = (mobId) => {
if (mobId === riddenMobId) {
cleanupAfterDespawn();
}
};
onPlayerDamagingMob = (playerId, mobId, damageDealt, withItem) => {
if (ridingPlayerId && playerId === ridingPlayerId && mobId === riddenMobId) {
return "preventDamage";
}
};

// --- Helper Functions ---


function resetPlayerStats(playerId) {
if (playerId && api.playerIsInGame(playerId)) {
api.setClientOptionToDefault(playerId, "speedMultiplier");
api.setClientOptionToDefault(playerId, "jumpAmount");
api.setClientOptionToDefault(playerId, "dealingDamageMultiplier");
api.setClientOptionToDefault(playerId, "maxHealth");
api.setHealth(playerId, 100);
}
}

function cleanupAfterDespawn() {
resetPlayerStats(ridingPlayerId);
if (ridingPlayerId && api.playerIsInGame(ridingPlayerId)) {
api.setPlayerOpacity(ridingPlayerId, 1);
api.sendMessage(ridingPlayerId, "Dismounted.", { color: "yellow" });
}
ridingPlayerId = null;
riddenMobId = null;
}

function forceDismount() {
resetPlayerStats(ridingPlayerId);
if (ridingPlayerId && api.playerIsInGame(ridingPlayerId)) {
api.setPlayerOpacity(ridingPlayerId, 1);
api.sendMessage(ridingPlayerId, "Dismounted.", { color: "yellow" });
}
if (riddenMobId && api.isAlive(riddenMobId)) {
api.despawnMob(riddenMobId);
}
ridingPlayerId = null;
riddenMobId = null;
}

You might also like