import * as mc from "@minecraft/server"
import * as gt from "@minecraft/server-gametest"
import { PLAYER_EYE_HEIGHT, getLookAtLocation, getLookAtRotation, makeVector3, stringifyLocation, subtractVectors } from "utils"
import { ephemeral, print, start } from "./data"
// Define the list of logs and their corresponding planks
const logToPlankMapping = {
"oak": "minecraft:oak_planks",
"spruce": "minecraft:spruce_planks",
"birch": "minecraft:birch_planks",
"jungle": "minecraft:jungle_planks",
"acacia": "minecraft:acacia_planks",
"dark_oak": "minecraft:dark_oak_planks",
"mangrove": "minecraft:mangrove_planks",
"cherry": "minecraft:cherry_planks", // If you're using Minecraft 1.20 and have cherry wood
"crimson": "minecraft:crimson_planks",
"warped": "minecraft:warped_planks"
};
// Define all crafting recipes, including planks for each type of log
const craftingRecipes = {
"wooden_plank": {
ingredients: [
{ item: "minecraft:oak_log", quantity: 1 },
{ item: "minecraft:spruce_log", quantity: 1 },
{ item: "minecraft:birch_log", quantity: 1 },
{ item: "minecraft:jungle_log", quantity: 1 },
{ item: "minecraft:acacia_log", quantity: 1 },
{ item: "minecraft:dark_oak_log", quantity: 1 },
{ item: "minecraft:mangrove_log", quantity: 1 },
{ item: "minecraft:cherry_log", quantity: 1 },
{ item: "minecraft:crimson_log", quantity: 1 },
{ item: "minecraft:warped_log", quantity: 1 }
],
result: { item: "minecraft:planks", quantity: 4 } // Will craft planks based on the log type
},
"stick": {
ingredients: [
{ item: "minecraft:oak_planks", quantity: 2 },
{ item: "minecraft:spruce_planks", quantity: 2 },
{ item: "minecraft:birch_planks", quantity: 2 },
{ item: "minecraft:jungle_planks", quantity: 2 },
{ item: "minecraft:acacia_planks", quantity: 2 },
{ item: "minecraft:dark_oak_planks", quantity: 2 },
{ item: "minecraft:mangrove_planks", quantity: 2 },
{ item: "minecraft:cherry_planks", quantity: 2 },
{ item: "minecraft:crimson_planks", quantity: 2 },
{ item: "minecraft:warped_planks", quantity: 2 }
],
result: { item: "minecraft:stick", quantity: 4 }
},
"crafting_table": {
ingredients: [
{ item: "minecraft:oak_planks", quantity: 4 },
{ item: "minecraft:spruce_planks", quantity: 4 },
{ item: "minecraft:birch_planks", quantity: 4 },
{ item: "minecraft:jungle_planks", quantity: 4 },
{ item: "minecraft:acacia_planks", quantity: 4 },
{ item: "minecraft:dark_oak_planks", quantity: 4 },
{ item: "minecraft:mangrove_planks", quantity: 4 },
{ item: "minecraft:cherry_planks", quantity: 4 },
{ item: "minecraft:crimson_planks", quantity: 4 },
{ item: "minecraft:warped_planks", quantity: 4 }
],
result: { item: "minecraft:crafting_table", quantity: 1 }
},
// You can add more recipes as needed
};
class SimulatedPlayer {
/**
* @type {gt.Test}
*/
static test;
#simulatedPlayer
#intervals
#validRepeatActions = ["attack", "jump", "shift", "breakBlock", "interact", "useItem", "build"]
/**
* @param {string} username
* @param {mc.GameMode} gameMode
* @param {mc.Player} sender
*/
constructor(username, gameMode, sender) {
const simulatedPlayer = SimulatedPlayer.test.spawnSimulatedPlayer({ x: 0, y: 1, z: 0 }, username, gameMode)
simulatedPlayer.teleport(sender.location, {
dimension: sender.dimension,
facingLocation: sender.getViewDirection()
})
this.#simulatedPlayer = simulatedPlayer
this.#intervals = {}
return this
}
// Stop Functions
stopBuild() { return this.#simulatedPlayer.stopBuild() }
stopBreakingBlock() { return this.#simulatedPlayer.stopBreakingBlock() }
stopFlying() { return this.#simulatedPlayer.stopFlying() }
stopGliding() { return this.#simulatedPlayer.stopGliding() }
stopInteracting() { return this.#simulatedPlayer.stopInteracting() }
stopMoving() { return this.#simulatedPlayer.stopMoving() }
stopSwimming() { return this.#simulatedPlayer.stopSwimming() }
stopUsingItem() { return this.#simulatedPlayer.stopUsingItem() }
// Variables
get location() { return this.#simulatedPlayer.location }
get headRotation() { return this.#simulatedPlayer.headRotation }
get camera() { return this.#simulatedPlayer.camera }
get dimension() { return this.#simulatedPlayer.dimension }
get id() { return this.#simulatedPlayer.id }
get typeId() { return this.#simulatedPlayer.typeId }
get name() { return this.#simulatedPlayer.name }
get nameTag() { return this.#simulatedPlayer.nameTag }
// "is" Variables
get isClimbing() { return this.#simulatedPlayer.isClimbing }
get isEmoting() { return this.#simulatedPlayer.isEmoting }
get isFalling() { return this.#simulatedPlayer.isFalling }
get isFlying() { return this.#simulatedPlayer.isFlying }
get isGliding() { return this.#simulatedPlayer.isGliding }
get isInWater() { return this.#simulatedPlayer.isInWater }
get isJumping() { return this.#simulatedPlayer.isJumping }
get isOnGround() { return this.#simulatedPlayer.isOnGround }
get isSleeping() { return this.#simulatedPlayer.isSleeping }
get isSneaking() { return this.#simulatedPlayer.isSneaking }
get isSprinting() { return this.#simulatedPlayer.isSprinting }
get isSwimming() { return this.#simulatedPlayer.isSwimming }
// Extra Variables
validRepeatActions = this.#validRepeatActions
intervals = this.#intervals
/**
* The inventory of the Simulated Player
* @type {mc.Container}
*/
inventory = this.#simulatedPlayer.getComponent(mc.EntityInventoryComponent.componentId).container
// Booleans
attack() {
this.#simulatedPlayer.attack()
}
/**
* @param {mc.Entity} entity
*/
attackEntity(entity) {
this.#simulatedPlayer.attackEntity(entity)
}
breakBlock() {
this.getBlockFromViewDirection((b) => {
const lookingAtLocation = b.location
//if (lookingAtLocation === undefined) return;
this.#simulatedPlayer.breakBlock(this.getRelativeCoords(lookingAtLocation))
})
// const lookingAtLocation = this.#simulatedPlayer.getBlockFromViewDirection({ maxDistance: 6 })?.block?.location
// if (lookingAtLocation === undefined) return;
// this.#simulatedPlayer.breakBlock(this.getRelativeCoords(lookingAtLocation))
}
dropItem(slot = 0) {
if (slot < 0 || slot > 8) return;
this.selectedSlot = slot
this.#simulatedPlayer.dropSelectedItem()
}
glide(trueOrFalse = false) {
if (trueOrFalse) { this.#simulatedPlayer.glide() } else { this.#simulatedPlayer.stopGliding() }
}
/**
* @param {mc.Entity} entity
*/
interact(mode = "default", entity) {
if (mode == "default") { this.#simulatedPlayer.interact() }
else if (mode == "block") { this.getBlockFromViewDirection((b) => { const loc = this.getRelativeCoords(b.location); this.#simulatedPlayer.interactWithBlock(loc) }) }
else if (mode == "entity") {
this.#simulatedPlayer.interactWithEntity(entity)
}
}
kill() {
this.#simulatedPlayer.kill()
}
disconnect() {
this.#simulatedPlayer.disconnect()
}
/**
* Respawns the Simulated Player
* @param {mc.Player} sender
*/
respawn(sender) {
this.#simulatedPlayer.respawn()
this.teleport(sender)
}
/**
* If the Simulated Player is not sneaking/crouching then it will crouch but if it is then it will unsneak/uncrouch
*/
shift() {
if (!this.isSneaking) { this.isSneaking = true } else { this.isSneaking = false }
}
/**
* if the slot is selected 0-8 (and not 1-9, eg...) it will show the item in the hand, it's similar to SimulatedPlayer.useItemInSlot()
*/
hotbar(slot = 0) {
if (slot < 0 || slot > 8) return;
this.#simulatedPlayer.selectedSlotIndex = slot
}
useItem(mode = "default") {
const slot = this.#simulatedPlayer.selectedSlotIndex;
if (mode === "block") {
this.getBlockFromViewDirection((b) => {
const loc = this.getRelativeCoords(b.location)
this.#simulatedPlayer.useItemInSlotOnBlock(slot, loc)
})
} else {
this.#simulatedPlayer.useItemInSlot(slot)
}
}
jump() {
this.#simulatedPlayer.jump()
}
/**
-
@param {mc.Player} sender
*/
teleport(sender) {
this.#simulatedPlayer.teleport(sender.location, {
dimension: sender.dimension,
facingLocation: sender.getViewDirection()
})
}
/**
- @param {mc.Player} sender
*/
look(sender) {
this.#getBlockFromViewDirection(sender, (b) => {
const loc = this.getRelativeCoords(b.block.location)
this.#simulatedPlayer.lookAtBlock(loc)
})
// const b = sender.getBlockFromViewDirection()
// if (!b) return;
// const loc = this.getRelativeCoords(b.block.location)
// this.#simulatedPlayer.lookAtBlock(loc)
}
build(slot = 0) {
this.getBlockFromViewDirection((b) => {
const loc = this.getRelativeCoords(b.location)
this.#simulatedPlayer.startBuild(slot)
this.stopBuild()
})
}
/**
-
@param {string} action
-
@param {any[]} params1
-
@param {any[]} params2
*/
repeat(action, params1, params2) {
if (params1 === undefined || params1 === null) { params1 = [] }
if (params2 === undefined || params2 === null) { params2 = [] }
if (!this.#validRepeatActions.includes(action)) return
else if (this.#intervals[action]) return;
this.#intervals[action] = mc.system.runInterval(() => {
this[action](...params1, ...params2)
}, 4)
}
dismount() {
this.#simulatedPlayer.teleport(this.#simulatedPlayer.location)
}
/**
- Sends a chat message to the Hugging Face AI model and receives a response.
- @param {string} message The message to send to the AI.
- @param {string} apiKey Your Hugging Face API key.
*/
chatWithAI(message, apiKey, model = "openai-community/gpt2") {
const url = https://api-inference.huggingface.co/models/${model};
const data = {
"inputs": message, // The input message to send to the model
};
try {
const response = await this.#query(data, apiKey, url); // Send the data to the API
if (response && response.length > 0) {
const aiResponse = response[0].generated_text.trim(); // Extracting the response text
this.sendMessage(`${aiResponse}`); // Send the AI's reply to the simulated player
} else {
this.sendMessage("AI returned no response.");
}
} catch (error) {
this.sendMessage(`Error interacting with AI: ${error.message}`);
}
}
/**
* Crafts an item based on the defined recipes.
* @param {string} recipeName The name of the recipe to craft (e.g., "wooden_plank").
* @returns {boolean} Returns true if crafting was successful, otherwise false.
*/
craft(recipeName) {
const recipe = craftingRecipes[recipeName];
if (!recipe) {
this.sendMessage(`Recipe ${recipeName} not found.`);
return false;
}
// Check if the player has the required ingredients in their inventory
const missingIngredients = this.checkIngredients(recipe.ingredients);
if (missingIngredients.length > 0) {
this.sendMessage(`Missing ingredients: ${missingIngredients.join(', ')}`);
return false;
}
// Remove the ingredients from the player's inventory
this.removeIngredients(recipe.ingredients);
// Add the crafted item to the player's inventory
this.addCraftedItem(recipe.result);
// Send success message
this.sendMessage(`Crafted ${recipe.result.quantity} ${recipe.result.item}(s).`);
return true;
export default SimulatedPlayer