mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-04-22 06:02:07 +02:00
239 lines
No EOL
5.6 KiB
JavaScript
239 lines
No EOL
5.6 KiB
JavaScript
import minecraftData from 'minecraft-data';
|
|
import settings from '../settings.js';
|
|
import { createBot } from 'mineflayer';
|
|
import { pathfinder } from 'mineflayer-pathfinder';
|
|
import { plugin as pvp } from 'mineflayer-pvp';
|
|
import { plugin as collectblock } from 'mineflayer-collectblock';
|
|
import { plugin as autoEat } from 'mineflayer-auto-eat';
|
|
import plugin from 'mineflayer-armor-manager';
|
|
const armorManager = plugin;
|
|
|
|
const mc_version = settings.minecraft_version;
|
|
const mcdata = minecraftData(mc_version);
|
|
|
|
|
|
export const WOOD_TYPES = ['oak', 'spruce', 'birch', 'jungle', 'acacia', 'dark_oak'];
|
|
export const MATCHING_WOOD_BLOCKS = [
|
|
'log',
|
|
'planks',
|
|
'sign',
|
|
'boat',
|
|
'fence_gate',
|
|
'door',
|
|
'fence',
|
|
'slab',
|
|
'stairs',
|
|
'button',
|
|
'pressure_plate',
|
|
'trapdoor'
|
|
]
|
|
export const WOOL_COLORS = [
|
|
'white',
|
|
'orange',
|
|
'magenta',
|
|
'light_blue',
|
|
'yellow',
|
|
'lime',
|
|
'pink',
|
|
'gray',
|
|
'light_gray',
|
|
'cyan',
|
|
'purple',
|
|
'blue',
|
|
'brown',
|
|
'green',
|
|
'red',
|
|
'black'
|
|
]
|
|
|
|
|
|
export function initBot(username) {
|
|
let bot = createBot({
|
|
username: username,
|
|
|
|
host: settings.host,
|
|
port: settings.port,
|
|
auth: settings.auth,
|
|
|
|
version: mc_version,
|
|
});
|
|
bot.loadPlugin(pathfinder);
|
|
bot.loadPlugin(pvp);
|
|
bot.loadPlugin(collectblock);
|
|
bot.loadPlugin(autoEat);
|
|
bot.loadPlugin(armorManager); // auto equip armor
|
|
|
|
return bot;
|
|
}
|
|
|
|
export function isHuntable(mob) {
|
|
if (!mob || !mob.name) return false;
|
|
const animals = ['chicken', 'cow', 'llama', 'mooshroom', 'pig', 'rabbit', 'sheep'];
|
|
return animals.includes(mob.name.toLowerCase()) && !mob.metadata[16]; // metadata 16 is not baby
|
|
}
|
|
|
|
export function isHostile(mob) {
|
|
if (!mob || !mob.name) return false;
|
|
return (mob.type === 'mob' || mob.type === 'hostile') && mob.name !== 'iron_golem' && mob.name !== 'snow_golem';
|
|
}
|
|
|
|
export function getItemId(itemName) {
|
|
let item = mcdata.itemsByName[itemName];
|
|
if (item) {
|
|
return item.id;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function getItemName(itemId) {
|
|
let item = mcdata.items[itemId]
|
|
if (item) {
|
|
return item.name;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function getBlockId(blockName) {
|
|
let block = mcdata.blocksByName[blockName];
|
|
if (block) {
|
|
return block.id;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function getBlockName(blockId) {
|
|
let block = mcdata.blocks[blockId]
|
|
if (block) {
|
|
return block.name;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function getAllItems(ignore) {
|
|
if (!ignore) {
|
|
ignore = [];
|
|
}
|
|
let items = []
|
|
for (const itemId in mcdata.items) {
|
|
const item = mcdata.items[itemId];
|
|
if (!ignore.includes(item.name)) {
|
|
items.push(item);
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
export function getAllItemIds(ignore) {
|
|
const items = getAllItems(ignore);
|
|
let itemIds = [];
|
|
for (const item of items) {
|
|
itemIds.push(item.id);
|
|
}
|
|
return itemIds;
|
|
}
|
|
|
|
export function getAllBlocks(ignore) {
|
|
if (!ignore) {
|
|
ignore = [];
|
|
}
|
|
let blocks = []
|
|
for (const blockId in mcdata.blocks) {
|
|
const block = mcdata.blocks[blockId];
|
|
if (!ignore.includes(block.name)) {
|
|
blocks.push(block);
|
|
}
|
|
}
|
|
return blocks;
|
|
}
|
|
|
|
export function getAllBlockIds(ignore) {
|
|
const blocks = getAllBlocks(ignore);
|
|
let blockIds = [];
|
|
for (const block of blocks) {
|
|
blockIds.push(block.id);
|
|
}
|
|
return blockIds;
|
|
}
|
|
|
|
export function getAllBiomes() {
|
|
return mcdata.biomes;
|
|
}
|
|
|
|
export function getItemCraftingRecipes(itemName) {
|
|
let itemId = getItemId(itemName);
|
|
if (!mcdata.recipes[itemId]) {
|
|
return null;
|
|
}
|
|
|
|
let recipes = [];
|
|
for (let r of mcdata.recipes[itemId]) {
|
|
let recipe = {};
|
|
let ingredients = [];
|
|
if (r.ingredients) {
|
|
ingredients = r.ingredients;
|
|
} else if (r.inShape) {
|
|
ingredients = r.inShape.flat();
|
|
}
|
|
for (let ingredient of ingredients) {
|
|
let ingredientName = getItemName(ingredient);
|
|
if (ingredientName === null) continue;
|
|
if (!recipe[ingredientName])
|
|
recipe[ingredientName] = 0;
|
|
recipe[ingredientName]++;
|
|
}
|
|
recipes.push(recipe);
|
|
}
|
|
|
|
return recipes;
|
|
}
|
|
|
|
export function getItemSmeltingIngredient(itemName) {
|
|
return {
|
|
baked_potato: 'potato',
|
|
steak: 'raw_beef',
|
|
cooked_chicken: 'raw_chicken',
|
|
cooked_cod: 'raw_cod',
|
|
cooked_mutton: 'raw_mutton',
|
|
cooked_porkchop: 'raw_porkchop',
|
|
cooked_rabbit: 'raw_rabbit',
|
|
cooked_salmon: 'raw_salmon',
|
|
dried_kelp: 'kelp',
|
|
iron_ingot: 'raw_iron',
|
|
gold_ingot: 'raw_gold',
|
|
copper_ingot: 'raw_copper',
|
|
glass: 'sand'
|
|
}[itemName];
|
|
}
|
|
|
|
export function getItemBlockSources(itemName) {
|
|
let itemId = getItemId(itemName);
|
|
let sources = [];
|
|
for (let block of getAllBlocks()) {
|
|
if (block.drops.includes(itemId)) {
|
|
sources.push(block.name);
|
|
}
|
|
}
|
|
return sources;
|
|
}
|
|
|
|
export function getItemAnimalSource(itemName) {
|
|
return {
|
|
raw_beef: 'cow',
|
|
raw_chicken: 'chicken',
|
|
raw_cod: 'cod',
|
|
raw_mutton: 'sheep',
|
|
raw_porkchop: 'pig',
|
|
raw_rabbit: 'rabbit',
|
|
raw_salmon: 'salmon',
|
|
leather: 'cow',
|
|
wool: 'sheep'
|
|
}[itemName];
|
|
}
|
|
|
|
export function getBlockTool(blockName) {
|
|
let block = mcdata.blocksByName[blockName];
|
|
if (!block || !block.harvestTools) {
|
|
return null;
|
|
}
|
|
return getItemName(Object.keys(block.harvestTools)[0]); // Double check first tool is always simplest
|
|
} |