const customShopItems = {
"Grass Block": { apiName: "Grass Block", buyPrice: 1, sellPrice: 0 },
"Dirt": { apiName: "Dirt", buyPrice: 1, sellPrice: 0 },
"Log": { apiName: "Maple Log", buyPrice: 2, sellPrice: 1 },
"Coal": { apiName: "Coal", buyPrice: 20, sellPrice: 7.5 },
"Messy Stone": { apiName: "Messy Stone", buyPrice: 3, sellPrice: 1.5 },
"Stone": { apiName: "Stone", buyPrice: 10, sellPrice: 5 },
"Iron Bar": { apiName: "Iron Bar", buyPrice: 40, sellPrice: 25 },
"Gold Bar": { apiName: "Gold Bar", buyPrice: 70, sellPrice: 25 },
"Diamond": { apiName: "Diamond", buyPrice: 175, sellPrice: 125 },
"Moonstone": { apiName: "Moonstone", buyPrice: 250, sellPrice: 175 },
"Gold Coin": { apiName: "Gold Coin", buyPrice: 750, sellPrice: 350 },
"Aura XP Orb": { apiName: "Aura XP Orb", buyPrice: 450, sellPrice: 20 },
"Raw Porkchop": { apiName: "Raw Porkchop", buyPrice: 5, sellPrice: 4 },
"Rotten Flesh": { apiName: "Rotten Flesh", buyPrice: 4, sellPrice: 2 },
"Fur": { apiName: "Fur", buyPrice: 5500, sellPrice: 4000 },
"Bone": { apiName: "Bone", buyPrice: 3775, sellPrice: 575 },
"Frost Zombie Spawner Block": { apiName: "Frost Zombie Spawner Block", buyPrice: 550, sellPrice: 460 },
"Draugr Knight Spawner": { apiName: "Draugr Knight Spawner Block", buyPrice: 500000, sellPrice: 400000 },
"Knight Heart": { apiName: "Knight Heart", buyPrice: 8500, sellPrice: 5250 },
"Golem Eye": { apiName: "Golem Eye", buyPrice: 1000, sellPrice: 250 },
"Pig Spawner": { apiName: "Pig Spawner Block", buyPrice: 600, sellPrice: 400 },
"Bear Spawner": { apiName: "Bear Spawner Block", buyPrice: 1000000, sellPrice: 750000 },
"Draugr Skeleton Spawner": { apiName: "Draugr Skeleton Spawner Block", buyPrice: 1000, sellPrice: 800 },
"Frost Golem Spawner": { apiName: "Frost Golem Spawner Block", buyPrice: 1500, sellPrice: 1000 },
"Gold Watermelon Stag Spawner": { apiName: "Gold Watermelon Stag Spawner Block", buyPrice: 12000000, sellPrice: 9500000 },
"Gold Antlers": { apiName: "Gold Antlers", buyPrice: 105000, sellPrice: 85000 },
"Gorilla Spawner": { apiName: "Gorilla Spawner Block", buyPrice: 45000, sellPrice: 25000 },
"Net": { apiName: "Net", buyPrice: 200, sellPrice: 50 },
"Obsidian": { apiName: "Obsidian", buyPrice: 950, sellPrice: 175 },
"Moonstone Orb": { apiName: "Moonstone Orb", buyPrice: 10000, sellPrice: 121 },
"Sand": { apiName: "Sand", buyPrice: 10, sellPrice: 5 },
"Cotton": { apiName: "Cotton", buyPrice: 65, sellPrice: 50 },
"Wheat": { apiName: "Wheat", buyPrice: 35, sellPrice: 25 },
"Melon": { apiName: "Melon", buyPrice: 50, sellPrice: 30 },
"Plum": { apiName: "Plum", buyPrice: 7, sellPrice: 3 },
"Coconut": { apiName: "Coconut", buyPrice: 25, sellPrice: 15 },
"Pear": { apiName: "Pear", buyPrice: 45, sellPrice: 30 },
"Cherry": { apiName: "Cherry", buyPrice: 25, sellPrice: 12.5 },
"Watermelon": { apiName: "Watermelon", buyPrice: 20, sellPrice: 10 },
"Iron Watermelon": { apiName: "Iron Watermelon", buyPrice: 45000000, sellPrice: 5000000 },
"Pumpkin": { apiName: "Pumpkin", buyPrice: 80, sellPrice: 40 },
"Corn": { apiName: "Corn", buyPrice: 70, sellPrice: 25 },
"Banana": { apiName: "Banana", buyPrice: 150, sellPrice: 75 },
"Magma": { apiName: "Magma", buyPrice: 3000, sellPrice: 1000 },
"Ice": { apiName: "Ice", buyPrice: 1250, sellPrice: 1000 },
"Mango": { apiName: "Mango", buyPrice: 95, sellPrice: 65 },
"Beetroot": { apiName: "Beetroot", buyPrice: 65, sellPrice: 35 },
"Raw Potato": { apiName: "Raw Potato", buyPrice: 65, sellPrice: 35 },
"Carrot": { apiName: "Carrot", buyPrice: 65, sellPrice: 35 },
"Mushroom": { apiName: "Mushroom", buyPrice: 60, sellPrice: 25 },
"Red Mushroom": { apiName: "Red Mushroom", buyPrice: 50, sellPrice: 25 }
};
let playerPlaytimeUpdateTracker = {};
function safeAPI(func, ...args) {
try { return func && typeof func === "function" ? func(...args) : null; }
catch (e) { console.error("API call error:", e); return null; }
}
function initializePlayerStats(playerId) {
const slot = safeAPI(api.getMoonstoneChestItemSlot, playerId, 0);
if (!slot || !slot.attributes || !slot.attributes.customAttributes) {
safeAPI(api.setMoonstoneChestItemSlot, playerId, 0, "Dirt", 1, { customAttributes: { money: 0, kills: 0, deaths: 0, playtime: 0 } });
}
}
function getPlayerStats(playerId) {
const slot = safeAPI(api.getMoonstoneChestItemSlot, playerId, 0);
const attrs = slot?.attributes?.customAttributes || {};
return {
money: Number(attrs.money) || 0,
kills: Number(attrs.kills) || 0,
deaths: Number(attrs.deaths) || 0,
playtime: Number(attrs.playtime) || 0
};
}
function setPlayerStats(playerId, stats) {
const safeStats = {
money: Number(stats?.money) || 0,
kills: Number(stats?.kills) || 0,
deaths: Number(stats?.deaths) || 0,
playtime: Number(stats?.playtime) || 0
};
safeAPI(api.setMoonstoneChestItemSlot, playerId, 0, "Dirt", 1, { customAttributes: safeStats });
}
function enforceStatsIntegrity(playerId) {
const slot = safeAPI(api.getMoonstoneChestItemSlot, playerId, 0);
if (!slot || slot.itemName !== "Dirt" || slot.amount !== 1 || !slot.attributes?.customAttributes) {
initializePlayerStats(playerId);
}
}
function processPlayerChatCommand(playerId, msg) {
msg = msg.trim();
enforceStatsIntegrity(playerId);
const stats = getPlayerStats(playerId);
if (msg === "!help") return safeAPI(api.sendMessage, playerId, "Commands: !bal !shop !buy <item> !sell <item> !pay <player> <amount> !kills !deaths !kdr !playtime", { color: "#00FFFF" });
if (msg === "!bal") return safeAPI(api.sendMessage, playerId, `Your balance: ${stats.money}`, { color: "#00FF00" });
if (msg === "!playtime") {
const seconds = Math.floor(stats.playtime / 1000);
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
return safeAPI(api.sendMessage, playerId, `Playtime: ${hours}h ${minutes}m`, { color: "#00FFAA" });
}
if (msg === "!kills") return safeAPI(api.sendMessage, playerId, `Kills: ${stats.kills}`, { color: "#FFAA00" });
if (msg === "!deaths") return safeAPI(api.sendMessage, playerId, `Deaths: ${stats.deaths}`, { color: "#FF5555" });
if (msg === "!kdr") {
const ratio = stats.deaths === 0 ? stats.kills.toFixed(2) : (stats.kills / stats.deaths).toFixed(2);
return safeAPI(api.sendMessage, playerId, `KDR: ${ratio}`, { color: "#AAAAFF" });
}
if (msg === "!shop") {
let text = "π The Shop Items π:\n";
for (const itemName in customShopItems) {
const item = customShopItems[itemName];
text += `- ${itemName}: Buy ${item.buyPrice} | Sell ${item.sellPrice}\n`;
}
return safeAPI(api.sendMessage, playerId, text, { color: "#0ADD08" });
}
if (msg.startsWith("!buy") || msg.startsWith("!sell") || msg.startsWith("!pay")) {
handleShopCommands(playerId, stats, msg);
}
}
function handleShopCommands(playerId, stats, msg) {
const parts = msg.split(" ");
const cmd = parts.shift();
if (cmd === "!buy" || cmd === "!sell") {
let quantity = parseInt(parts[0]); if (isNaN(quantity)) quantity = 1; else parts.shift();
const itemName = parts.join(" ");
const item = customShopItems[itemName];
if (!item) return safeAPI(api.sendMessage, playerId, `"${itemName}" not in shop.`, { color: "red" });
if (cmd === "!buy") {
const cost = item.buyPrice * quantity;
if (stats.money < cost) return safeAPI(api.sendMessage, playerId, `Not enough money! Need ${cost}`, { color: "red" });
safeAPI(api.giveItem, playerId, item.apiName, quantity);
stats.money -= cost;
setPlayerStats(playerId, stats);
return safeAPI(api.sendMessage, playerId, `Bought ${quantity}x ${itemName} for ${cost}.`, { color: "#00FF00" });
} else {
const owned = safeAPI(api.getInventoryItemAmount, playerId, item.apiName) || 0;
if (owned < quantity) return safeAPI(api.sendMessage, playerId, `You only have ${owned}x ${itemName}`, { color: "red" });
safeAPI(api.removeItemName, playerId, item.apiName, quantity);
stats.money += item.sellPrice * quantity;
setPlayerStats(playerId, stats);
return safeAPI(api.sendMessage, playerId, `Sold ${quantity}x ${itemName} for ${item.sellPrice * quantity}.`, { color: "#00FF00" });
}
}
if (cmd === "!pay") {
if (parts.length < 2) return safeAPI(api.sendMessage, playerId, "Usage: !pay <player> <amount>", { color: "red" });
const targetId = safeAPI(api.getPlayerId, parts[0]);
const amount = parseInt(parts[1]);
if (!targetId || targetId === playerId || isNaN(amount) || amount <= 0) return safeAPI(api.sendMessage, playerId, "Invalid player or amount", { color: "red" });
const senderStats = getPlayerStats(playerId);
const receiverStats = getPlayerStats(targetId);
if (senderStats.money < amount) return safeAPI(api.sendMessage, playerId, "Not enough money", { color: "red" });
senderStats.money -= amount;
receiverStats.money += amount;
setPlayerStats(playerId, senderStats);
setPlayerStats(targetId, receiverStats);
const senderName = safeAPI(api.getPlayerName, playerId) || "Someone";
const receiverName = safeAPI(api.getPlayerName, targetId) || parts[0] || "Player";
safeAPI(api.sendMessage, playerId, `Paid ${amount} to ${receiverName}`, { color: "#00FF00" });
safeAPI(api.sendMessage, targetId, `Received ${amount} from ${senderName}`, { color: "#00FFAA" });
}
}
function onPlayerJoin(playerId) {
initializePlayerStats(playerId);
registerCustomRecipes(playerId);
}
function onPlayerChat(playerId, msg) {
processPlayerChatCommand(playerId, msg);
}
function onEntityKilled(killerId, victimId) {
if (!killerId || !victimId || killerId === victimId) return;
const killerStats = getPlayerStats(killerId);
const victimStats = getPlayerStats(victimId);
killerStats.kills = (killerStats.kills || 0) + 1;
victimStats.deaths = (victimStats.deaths || 0) + 1;
setPlayerStats(killerId, killerStats);
setPlayerStats(victimId, victimStats);
}
function onPlayerKilled(playerId, killerId) {
onEntityKilled(killerId, playerId);
}
let tickCounter = 0;
function tick() {
tickCounter++;
if (tickCounter < 4) return;
tickCounter = 0;
const now = Date.now();
for (const player of safeAPI(api.getPlayerIds) || []) {
if (!safeAPI(api.playerIsInGame, player)) continue;
enforceStatsIntegrity(player);
const stats = getPlayerStats(player);
if (!playerPlaytimeUpdateTracker[player] || now - playerPlaytimeUpdateTracker[player] >= 60000) {
stats.playtime = (stats.playtime || 0) + 60000;
setPlayerStats(player, stats);
playerPlaytimeUpdateTracker[player] = now;
}
const seconds = Math.floor(stats.playtime / 1000);
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const sidebar = [
{ str: " β¨ π«πππππ πΊπ΄π· π«\n", style: { color: "#FFAA00", fontWeight: "bold", fontSize: "20px" } },
{ str: `πΈ πΌππππ’: ${stats.money}\n`, style: { color: "#00FF00", fontSize: "18px" } },
{ str: `πͺ πΊπππs: ${stats.kills}\n`, style: { color: "#FF5555", fontSize: "18px" } },
{ str: `β οΈ π³ππππs: ${stats.deaths}\n`, style: { color: "#FFAA00", fontSize: "18px" } },
{ str: `β±οΈ πΏπππ’tπππ: ${hours}h ${minutes}m\n`, style: { color: "#AAAAFF", fontSize: "18px" } },
{ str: "Type !help to get started!", style: { color: "#AAAAFF", fontSize: "15px" } },
];
safeAPI(api.setClientOption, player, "RightInfoText", sidebar);
}
}
function registerCustomRecipes(playerId) {
safeAPI(api.editItemCraftingRecipes, playerId, "Gold Coin", [
{ requires: [{ items: ["Moonstone"], amt: 3 }], produces: 1, station: "Workbench" },
]);
safeAPI(api.editItemCraftingRecipes, playerId, "Moonstone", [
{ requires: [{ items: ["Gold Coin"], amt: 1 }], produces: 3, station: "Workbench" },
{ requires: [{ items: ["Block of Moonstone"], amt: 1 }], produces: 9, station: "Workbench" },
]);
safeAPI(api.editItemCraftingRecipes, playerId, "Cornbread", [
{ requires: [{ items: ["Bread"], amt: 5 }, { items: ["Corn"], amt: 5 }], produces: 3, station: "Workbench" },
]);
}
for (const p of safeAPI(api.getPlayerIds) || []) registerCustomRecipes(p);










0 comments