Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const invAPI = require("../inventory-api");
// Subscribe to some events...
invAPI.on("itemDefined", (key, name, description) => {
console.log(`Item defined, key: ${key} | name: ${name} | description: ${description}`);
});
invAPI.on("itemAdded", (player, key, amount, data) => {
console.log(`${player.name} received ${amount}x ${key}.`);
});
invAPI.on("itemUsed", (player, invIdx, key, data) => {
console.log(`${player.name} used ${key}.`);
});
invAPI.on("itemRemoved", (player, invIdx, key, amount, data) => {
console.log(`${player.name} lost ${amount}x ${key}.`);
});
invAPI.on("itemRemovedCompletely", (player, key, data) => {
console.log(`${player.name} no longer has ${key} (${data ? "with data" : "without data"}) in their inventory.`);
});
invAPI.on("inventoryReplaced", (player, oldInventory, newInventory) => {
console.log(`${player.name} had their inventory replaced. (Old item count: ${oldInventory.length}, new: ${newInventory.length})`);
});
// Add some items...
invAPI.addItem("item_bodyarmor", "Body Armor", "Refills your armor when used.", (player, inventoryIndex, itemKey, data) => {
player.armour = 100;
player.outputChatBox("Armor refilled.");
player.removeItem(inventoryIndex);
});
invAPI.addItem("item_firstaid", "First Aid Kit", "Gives 20 health when used.", (player, inventoryIndex, itemKey, data) => {
let amountOfHealth = 20;
if (data && data.hasOwnProperty("effectMultiplier")) amountOfHealth = Math.floor(amountOfHealth * data.effectMultiplier);
if (player.health + amountOfHealth > 100) {
player.outputChatBox("You don't need to use a first aid kit.");
return;
}
player.health += amountOfHealth;
player.outputChatBox(`Used a first aid kit for ${amountOfHealth} health.`);
player.removeItem(inventoryIndex);
});
invAPI.addItem("item_male_hoodie", "Hoodie (Male)", "A hoodie for freemode male model.", (player, inventoryIndex, itemKey, data) => {
if (player.model !== mp.joaat("mp_m_freemode_01")) {
player.outputChatBox("Can't use this item with your current model.");
return;
}
let texture = 0;
if (data && data.hasOwnProperty("texture")) texture = data.texture;
player.setClothes(11, 7, texture, 2);
player.outputChatBox(`Now wearing: Hoodie with texture variation ${texture}.`);
player.removeItem(inventoryIndex);
});
invAPI.addItem("item_copoutfit", "Police Outfit", "To protect and to serve.");
invAPI.addItem("item_repairkit", "Repair Kit", "Repairs your car.");
// Since item_copoutfit and item_repairkit has no onUse defined, you can give them functionality like this
invAPI.on("itemUsed", (player, invIdx, key, data) => {
switch (key) {
case "item_copoutfit":
// imaginary function here that applies the police outfit
player.outputChatBox("Now wearing the police outfit.");
player.removeItem(invIdx);
break;
case "item_repairkit":
const playerVehicle = player.vehicle;
if (playerVehicle) {
playerVehicle.repair();
player.outputChatBox("Vehicle repaired.");
player.removeItem(invIdx);
} else {
player.outputChatBox("You're not in a vehicle.");
}
break;
}
});
// Bunch of inventory commands
const fs = require("fs");
const path = require("path");
// Do /saveinventory to save your inventory to a JSON file. (file path will be printed to console)
mp.events.addCommand("saveinventory", (player) => {
const saveDir = path.join(__dirname, "inventories");
if (!fs.existsSync(saveDir)) fs.mkdirSync(saveDir);
const playerPath = path.join(saveDir, `${player.socialClub}.json`);
fs.writeFileSync(playerPath, JSON.stringify(player.getInventory(), null, 2));
player.outputChatBox("Inventory saved.");
console.log(`Player ${player.name} saved their inventory. (${playerPath})`);
});
// Do /loadinventory to load your inventory from a JSON file.
mp.events.addCommand("loadinventory", (player) => {
const playerPath = path.join(__dirname, "inventories", `${player.socialClub}.json`);
if (fs.existsSync(playerPath)) {
player.setInventory(JSON.parse(fs.readFileSync(playerPath)));
player.outputChatBox("Inventory loaded.");
} else {
player.outputChatBox("Inventory file not found.");
}
});
// Do /inventory to list your items in "slot | name | amount" format.
mp.events.addCommand("inventory", (player) => {
const inventory = player.getInventory();
player.outputChatBox("Your inventory:");
inventory.forEach((item, index) => {
player.outputChatBox(`${index} | ${invAPI.getItemName(item.key)} (${item.key}) | ${item.amount}x`);
});
});
// Do /giveitem [key] [amount] to give yourself an item.
mp.events.addCommand("giveitem", (player, _, itemKey, amount) => {
amount = Number(amount);
if (player.giveItem(itemKey, amount)) {
player.outputChatBox(`Added ${amount}x ${invAPI.getItemName(itemKey)} (${itemKey}) to your inventory.`);
} else {
player.outputChatBox("Failed to give item.");
}
});
// Do /useitem [slot] to use an item from your inventory.
mp.events.addCommand("useitem", (player, _, itemIndex) => {
itemIndex = Number(itemIndex);
if (player.useItem(itemIndex)) {
player.outputChatBox(`Used item at index ${itemIndex}.`);
} else {
player.outputChatBox("Failed to use item.");
}
});
// Do /removeitem [slot] [amount] to remove an item from your inventory.
mp.events.addCommand("removeitem", (player, _, itemIndex, amount) => {
itemIndex = Number(itemIndex);
amount = Number(amount);
if (player.removeItem(itemIndex, amount)) {
player.outputChatBox(`Removed ${amount} of item at index ${itemIndex}.`);
} else {
player.outputChatBox("Failed to remove item.");
}
});
// Custom attribute commands
// Do /betterfirstaid to give yourself a first aid kit that gives +50 health instead of the usual 20.
mp.events.addCommand("betterfirstaid", (player) => {
const giveItemResult = player.giveItem("item_firstaid", 1, {
effectMultiplier: 2.5
});
if (giveItemResult) {
player.outputChatBox("Received +50 HP first aid kit.");
} else {
player.outputChatBox("Failed to give item.");
}
});
// Do /givehoodie [texture] to give yourself a hoodie item with the variation you specified.
mp.events.addCommand("givehoodie", (player, _, texture) => {
const giveItemResult = player.giveItem("item_male_hoodie", 1, {
texture: Number(texture)
});
if (giveItemResult) {
player.outputChatBox(`Received a male hoodie with texture variation ${texture}.`);
} else {
player.outputChatBox("Failed to give item.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.