Untitled
unknown
plain_text
a year ago
9.9 kB
10
Indexable
import { world, Player, ItemStack } from "@minecraft/server";
import { ModalFormData, ActionFormData } from "@minecraft/server-ui";
const itemsForSale = [];
const playerIncomes = {};
export async function showMarketplaceInterface(player) {
const form = new ActionFormData()
.title("Marketplace")
.button("Sell Items")
.button("Public Market")
.button("Inventory");
form.show(player).then(response => {
if (response.canceled) return;
if (response.selection === 0) {
showSellItemsForm(player);
} else if (response.selection === 1) {
showPublicMarketForm(player);
} else if (response.selection === 2) {
showInventoryForm(player);
}
}).catch(error => console.error("Error showing marketplace interface:", error));
}
const knownItemIds = [
"minecraft:stone",
"minecraft:grass_block",
"minecraft:dirt",
"minecraft:cobblestone",
"minecraft:wooden_sword",
"minecraft:wooden_pickaxe",
"minecraft:iron_ingot",
"minecraft:gold_ingot",
"minecraft:diamond",
// Add more known item IDs as needed
];
function showSellItemsForm(player) {
const inventory = player.getComponent("minecraft:inventory").container;
const form = new ActionFormData().title("Select Item to Sell").body("Select an item from your inventory:");
let hasItems = false;
for (let i = 0; i < inventory.size; i++) {
const item = inventory.getItem(i);
if (item && item.id) {
const itemId = item.id.toLowerCase();
if (knownItemIds.includes(itemId)) {
const itemName = itemId.replace("minecraft:", "").replace(/_/g, " ");
form.button(`${item.amount}x ${itemName}`, i); // Add button for the item
hasItems = true;
}
}
}
if (!hasItems) {
// Inform the player that there are no items
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"No items available in your inventory."}]}`);
return; // Exit the function early
}
// Show the form only if there are items
form.show(player).then(response => {
if (response.canceled) return;
handleSellItems(player, response.selection); // Handle the selected item
}).catch(error => {
console.error("Error showing sell items form:", error);
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"An error occurred while showing the sell items form."}]}`);
});
}
function handleSellItems(player, selectedIndex) {
const inventory = player.getComponent("minecraft:inventory").container;
const item = inventory.getItem(selectedIndex);
if (item && item.id) {
// Create ItemStack and ensure item data is handled correctly
const clonedItem = new ItemStack(item.id, item.amount);
clonedItem.data = item.data !== undefined ? item.data : 0; // Default to 0 if undefined
const form = new ModalFormData()
.title("Sell Item")
.textField("Type value:", "Value")
.textField("Add description:", "Description");
form.show(player).then(response => {
if (response.canceled) return;
const value = parseInt(response.formValues[0]);
const description = response.formValues[1];
if (isNaN(value)) {
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Invalid value."}]}`);
return;
}
// Debug: Log item details
console.log(`Item ID: ${item.id}`);
console.log(`Item Amount: ${item.amount}`);
console.log(`Item Data: ${item.data}`);
itemsForSale.push({ item: clonedItem, value, description, seller: player.name });
inventory.setItem(selectedIndex, null);
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Item listed for ${value} money."}]}`);
}).catch(error => {
console.error("Error showing modal form:", error);
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"An error occurred while showing the sell item form."}]}`);
});
} else {
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"No item selected."}]}`);
}
}
function showPublicMarketForm(player) {
const form = new ActionFormData().title("Public Market").body("Available items:");
if (itemsForSale.length === 0) {
form.button("No items available");
} else {
itemsForSale.forEach((entry, index) => {
if (entry && entry.item && entry.item.id) {
const itemName = entry.item.id.replace("minecraft:", "").replace(/_/g, " ");
const itemDetails = `${entry.item.amount}x ${itemName} - ${entry.value} money\n${entry.description}`;
form.button(itemDetails);
} else {
console.warn(`Invalid item entry at index ${index}:`, JSON.stringify(entry));
}
});
}
form.show(player).then(response => {
if (response.canceled) return;
if (itemsForSale.length > 0 && response.selection >= 0 && response.selection < itemsForSale.length) {
handlePublicMarket(player, response.selection);
} else {
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Invalid selection or no items available."}]}`);
}
}).catch(error => {
console.error("Error showing public market form:", error);
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"An error occurred while showing the public market."}]}`);
});
}
function handlePublicMarket(player, selectedIndex) {
const entry = itemsForSale[selectedIndex];
if (entry) {
player.runCommand(`scoreboard players remove ${player.name} money ${entry.value}`);
player.runCommand(`give ${player.name} ${entry.item.id} ${entry.item.amount}`);
addIncome(entry.seller, entry.value);
itemsForSale.splice(selectedIndex, 1);
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Item purchased for ${entry.value} money."}]}`);
}
}
function showInventoryForm(player) {
const form = new ActionFormData()
.title("Inventory")
.button("See Income")
.button("Edit Items")
.button("Back");
form.show(player).then(response => {
if (response.canceled) return;
if (response.selection === 0) {
showIncomeForm(player);
} else if (response.selection === 1) {
showEditItemsForm(player);
}
}).catch(error => console.error("Error showing inventory form:", error));
}
function showIncomeForm(player) {
const income = playerIncomes[player.name] || 0;
const form = new ActionFormData()
.title("Income")
.body(`You have ${income} money to claim. Do you want to claim your income?`)
.button("Yes")
.button("No");
form.show(player).then(response => {
if (response.canceled) return;
if (response.selection === 0) {
player.runCommand(`scoreboard players add ${player.name} money ${income}`);
playerIncomes[player.name] = 0;
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Income claimed."}]}`);
}
}).catch(error => console.error("Error showing income form:", error));
}
function showEditItemsForm(player) {
const playerItems = itemsForSale.filter(entry => entry.seller === player.name);
const form = new ActionFormData().title("Edit Items");
playerItems.forEach((entry, index) => {
if (entry.item && entry.item.id) {
const itemName = entry.item.id.replace("minecraft:", "").replace(/_/g, " ");
form.button(`${entry.item.amount}x ${itemName} - ${entry.value} money\n${entry.description}`);
}
});
if (playerItems.length === 0) {
form.button("No items available to edit");
}
form.show(player).then(response => {
if (response.canceled) return;
const selectedIndex = response.selection;
if (selectedIndex < playerItems.length) {
const item = playerItems[selectedIndex];
showEditItemForm(player, item);
} else {
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Invalid selection."}]}`);
}
}).catch(error => console.error("Error showing edit items form:", error));
}
function showEditItemForm(player, item) {
const form = new ModalFormData()
.title("Edit Item")
.textField("Update value:", "Value", item.value.toString())
.textField("Update description:", "Description", item.description);
form.show(player).then(response => {
if (response.canceled) return;
const newValue = parseInt(response.formValues[0]);
const newDescription = response.formValues[1];
if (isNaN(newValue)) {
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Invalid value."}]}`);
return;
}
item.value = newValue;
item.description = newDescription;
player.runCommand(`tellraw ${player.name} {"rawtext":[{"text":"Item updated."}]}`);
}).catch(error => console.error("Error showing edit item form:", error));
}
function addIncome(playerName, amount) {
if (!playerIncomes[playerName]) {
playerIncomes[playerName] = 0;
}
playerIncomes[playerName] += amount;
}
Editor is loading...
Leave a Comment