added cheat mode, teleport, insta place, better torch placer

This commit is contained in:
MaxRobinsonTheGreat 2024-06-03 18:23:01 -05:00
parent 440ffdd931
commit af8252cd95
2 changed files with 79 additions and 27 deletions

View file

@ -10,19 +10,25 @@ export function log(bot, message, chat=false) {
bot.chat(message); bot.chat(message);
} }
async function autoLight(bot) { export function shouldPlaceTorch(bot) {
if (bot.modes.isOn('torch_placing') && !bot.interrupt_code) { if (!bot.modes.isOn('torch_placing') || bot.interrupt_code) return false;
const pos = world.getPosition(bot);
// TODO: check light level instead of nearby torches, block.light is broken
let nearest_torch = world.getNearestBlock(bot, 'torch', 6); let nearest_torch = world.getNearestBlock(bot, 'torch', 6);
if (!nearest_torch) { if (!nearest_torch) {
const block = bot.blockAt(pos);
let has_torch = bot.inventory.items().find(item => item.name === 'torch'); let has_torch = bot.inventory.items().find(item => item.name === 'torch');
const curr_block = agent.bot.blockAt(pos); return has_torch && block.name === 'air';
if (has_torch && curr_block.name === 'air') { }
return false;
}
async function autoLight(bot) {
if (shouldPlaceTorch(bot)) {
try { try {
log(bot, `Placing torch at ${bot.entity.position}.`); const pos = world.getPosition(bot);
return await placeBlock(bot, 'torch', bot.entity.position.x, bot.entity.position.y, bot.entity.position.z); return await placeBlock(bot, 'torch', pos.x, pos.y, pos.z, true);
} catch (err) {return true;} } catch (err) {return false;}
}
}
} }
return false; return false;
} }
@ -455,6 +461,13 @@ export async function breakBlockAt(bot, x, y, z) {
if (x == null || y == null || z == null) throw new Error('Invalid position to break block at.'); if (x == null || y == null || z == null) throw new Error('Invalid position to break block at.');
let block = bot.blockAt(Vec3(x, y, z)); let block = bot.blockAt(Vec3(x, y, z));
if (block.name !== 'air' && block.name !== 'water' && block.name !== 'lava') { if (block.name !== 'air' && block.name !== 'water' && block.name !== 'lava') {
if (bot.modes.isOn('cheat')) {
let msg = '/setblock ' + Math.floor(x) + ' ' + Math.floor(y) + ' ' + Math.floor(z) + ' air';
bot.chat(msg);
log(bot, `Used /setblock to break block at ${x}, ${y}, ${z}.`);
return true;
}
if (bot.entity.position.distanceTo(block.position) > 4.5) { if (bot.entity.position.distanceTo(block.position) > 4.5) {
let pos = block.position; let pos = block.position;
let movements = new pf.Movements(bot); let movements = new pf.Movements(bot);
@ -482,7 +495,7 @@ export async function breakBlockAt(bot, x, y, z) {
} }
export async function placeBlock(bot, blockType, x, y, z) { export async function placeBlock(bot, blockType, x, y, z, no_cheat=false) {
/** /**
* Place the given block type at the given position. It will build off from any adjacent blocks. Will fail if there is a block in the way or nothing to build off of. * Place the given block type at the given position. It will build off from any adjacent blocks. Will fail if there is a block in the way or nothing to build off of.
* @param {MinecraftBot} bot, reference to the minecraft bot. * @param {MinecraftBot} bot, reference to the minecraft bot.
@ -495,7 +508,18 @@ export async function placeBlock(bot, blockType, x, y, z) {
* let position = world.getPosition(bot); * let position = world.getPosition(bot);
* await skills.placeBlock(bot, "oak_log", position.x + 1, position.y - 1, position.x); * await skills.placeBlock(bot, "oak_log", position.x + 1, position.y - 1, position.x);
**/ **/
console.log('placing block...') if (!mc.getBlockId(blockType)) {
log(bot, `Invalid block type: ${blockType}.`);
return false;
}
if (bot.modes.isOn('cheat') && !no_cheat) {
let msg = '/setblock ' + Math.floor(x) + ' ' + Math.floor(y) + ' ' + Math.floor(z) + ' ' + blockType;
bot.chat(msg);
log(bot, `Used /setblock to place ${blockType} at ${x}, ${y}, ${z}.`);
return true;
}
let block = bot.inventory.items().find(item => item.name === blockType); let block = bot.inventory.items().find(item => item.name === blockType);
if (!block) { if (!block) {
log(bot, `Don't have any ${blockType} to place.`); log(bot, `Don't have any ${blockType} to place.`);
@ -705,6 +729,13 @@ export async function goToPlayer(bot, username, distance=3) {
* @example * @example
* await skills.goToPlayer(bot, "player"); * await skills.goToPlayer(bot, "player");
**/ **/
if (bot.modes.isOn('cheat')) {
bot.chat('/tp @s ' + username);
log(bot, `Teleported to ${username}.`);
return true;
}
bot.modes.pause('self_defense'); bot.modes.pause('self_defense');
bot.modes.pause('cowardice'); bot.modes.pause('cowardice');
let player = bot.players[username].entity let player = bot.players[username].entity
@ -759,6 +790,20 @@ export async function moveAway(bot, distance) {
let goal = new pf.goals.GoalNear(pos.x, pos.y, pos.z, distance); let goal = new pf.goals.GoalNear(pos.x, pos.y, pos.z, distance);
let inverted_goal = new pf.goals.GoalInvert(goal); let inverted_goal = new pf.goals.GoalInvert(goal);
bot.pathfinder.setMovements(new pf.Movements(bot)); bot.pathfinder.setMovements(new pf.Movements(bot));
if (bot.modes.isOn('cheat')) {
const path = await bot.pathfinder.getPathTo(move, inverted_goal, 10000);
let last_move = path.path[path.path.length-1];
console.log(last_move);
if (last_move) {
let x = Math.floor(last_move.x);
let y = Math.floor(last_move.y);
let z = Math.floor(last_move.z);
bot.chat('/tp @s ' + x + ' ' + y + ' ' + z);
return true;
}
}
await bot.pathfinder.goto(inverted_goal); await bot.pathfinder.goto(inverted_goal);
let new_pos = bot.entity.position; let new_pos = bot.entity.position;
log(bot, `Moved away from nearest entity to ${new_pos}.`); log(bot, `Moved away from nearest entity to ${new_pos}.`);

View file

@ -149,21 +149,16 @@ const modes = [
interrupts: ['followPlayer'], interrupts: ['followPlayer'],
on: true, on: true,
active: false, active: false,
cooldown: 5,
last_place: Date.now(),
update: function (agent) { update: function (agent) {
// TODO: check light level instead of nearby torches, block.light is broken if (skills.shouldPlaceTorch(agent.bot)) {
const near_torch = world.getNearestBlock(agent.bot, 'torch', 6); if (Date.now() - this.last_place < this.cooldown * 1000) return;
if (!near_torch) {
let torches = agent.bot.inventory.items().filter(item => item.name === 'torch');
if (torches.length > 0) {
const torch = torches[0];
const pos = agent.bot.entity.position;
const curr_block = agent.bot.blockAt(pos);
if (curr_block.name === 'air') {
execute(this, agent, async () => { execute(this, agent, async () => {
await skills.placeBlock(agent.bot, torch.name, pos.x, pos.y, pos.z); const pos = agent.bot.entity.position;
await skills.placeBlock(agent.bot, 'torch', pos.x, pos.y, pos.z, true);
}); });
} this.last_place = Date.now();
}
} }
} }
}, },
@ -204,6 +199,14 @@ const modes = [
} }
} }
}, },
{
name: 'cheat',
description: 'Use cheats to instantly place blocks and teleport.',
interrupts: [],
on: false,
active: false,
update: function (agent) { /* do nothing */ }
}
]; ];
async function execute(mode, agent, func, timeout=-1) { async function execute(mode, agent, func, timeout=-1) {
@ -291,4 +294,8 @@ class ModeController {
export function initModes(agent) { export function initModes(agent) {
// the mode controller is added to the bot object so it is accessible from anywhere the bot is used // the mode controller is added to the bot object so it is accessible from anywhere the bot is used
agent.bot.modes = new ModeController(agent); agent.bot.modes = new ModeController(agent);
let modes = agent.prompter.getInitModes();
if (modes) {
agent.bot.modes.loadJson(modes);
}
} }