Files
behavior_pack
resource_pack
INSTALLATION.md
pack_as_mcaddon.bat
pack_as_mcaddon.sh
README.md
TECHNICAL.md
Code
19 1,568
behavior_pack/scripts/main.js
import { world, system, BlockPermutation, BlockTypes } from '@minecraft/server';
// List of blocks that should NOT be destroyed by water
const WATER_RESISTANT_BLOCKS = [
'minecraft:wheat',
'minecraft:carrots',
'minecraft:potatoes',
'minecraft:beetroot',
'minecraft:melon_stem',
'minecraft:pumpkin_stem',
'minecraft:torchflower',
'minecraft:pitcher_plant',
'minecraft:red_flower',
'minecraft:yellow_flower',
'minecraft:tallgrass',
'minecraft:double_plant',
'minecraft:sapling',
'minecraft:red_mushroom',
'minecraft:brown_mushroom',
'minecraft:crimson_fungus',
'minecraft:warped_fungus',
'minecraft:crimson_roots',
'minecraft:warped_roots',
'minecraft:nether_sprouts',
'minecraft:sweet_berry_bush',
'minecraft:cave_vines',
'minecraft:sugar_cane',
'minecraft:cactus',
'minecraft:bamboo',
'minecraft:kelp',
'minecraft:seagrass',
'minecraft:sea_pickle',
'minecraft:torch',
'minecraft:redstone_wire',
'minecraft:lever',
'minecraft:wooden_button',
'minecraft:stone_button',
'minecraft:tripwire',
'minecraft:rail',
'minecraft:powered_rail',
'minecraft:detector_rail',
'minecraft:activator_rail'
];
// Cache for checking if blocks should be protected
const protectedBlockCache = new Set(WATER_RESISTANT_BLOCKS);
/**
* Initialize the submerged world system
*/
function initialize() {
console.warn('[Submerged World] Initializing underwater world mechanics...');
// Subscribe to world events
world.afterEvents.playerSpawn.subscribe(onPlayerSpawn);
world.beforeEvents.worldInitialize.subscribe(onWorldInitialize);
// Start the main update loop
system.runInterval(mainUpdate, 20); // Run every second (20 ticks)
console.warn('[Submerged World] Initialization complete!');
}
/**
* Handle world initialization
*/
function onWorldInitialize(event) {
console.warn('[Submerged World] World initialized - underwater mechanics active');
}
/**
* Handle player spawn - give underwater breathing by default
*/
function onPlayerSpawn(event) {
const player = event.player;
// Welcome message
player.sendMessage('§b§l=== SUBMERGED WORLD ===§r');
player.sendMessage('§3The entire world is underwater!§r');
player.sendMessage('§7Plants and crops grow normally beneath the waves.§r');
// Optional: Give water breathing effect on spawn
system.runTimeout(() => {
try {
player.addEffect('water_breathing', 999999, { amplifier: 0, showParticles: false });
} catch (error) {
// Effect might not be available or player left
}
}, 20);
}
/**
* Main update loop - protect blocks from water destruction
*/
function mainUpdate() {
try {
// Iterate through all players to check their nearby blocks
for (const player of world.getAllPlayers()) {
protectNearbyBlocks(player);
}
} catch (error) {
console.warn('[Submerged World] Update error:', error);
}
}
/**
* Protect blocks near player from water destruction
*/
function protectNearbyBlocks(player) {
try {
const dimension = player.dimension;
const playerPos = player.location;
const checkRadius = 8; // Check 8 blocks around player
// Scan nearby blocks
for (let x = -checkRadius; x <= checkRadius; x++) {
for (let y = -checkRadius; y <= checkRadius; y++) {
for (let z = -checkRadius; z <= checkRadius; z++) {
const blockPos = {
x: Math.floor(playerPos.x) + x,
y: Math.floor(playerPos.y) + y,
z: Math.floor(playerPos.z) + z
};
try {
const block = dimension.getBlock(blockPos);
if (!block) continue;
const blockType = block.typeId;
// Check if this is a protected block
if (isProtectedBlock(blockType)) {
// Check if water is adjacent (could destroy the block)
if (hasAdjacentWater(dimension, blockPos)) {
// The block is protected - water won't destroy it naturally
// We rely on the block component overrides for this
}
}
} catch (blockError) {
// Block might be unloaded or in ungenerated chunk
}
}
}
}
} catch (error) {
// Player might have left or dimension changed
}
}
/**
* Check if a block should be protected from water
*/
function isProtectedBlock(blockTypeId) {
return protectedBlockCache.has(blockTypeId);
}
/**
* Check if water is adjacent to a position
*/
function hasAdjacentWater(dimension, pos) {
const offsets = [
{ x: 1, y: 0, z: 0 },
{ x: -1, y: 0, z: 0 },
{ x: 0, y: 1, z: 0 },
{ x: 0, y: -1, z: 0 },
{ x: 0, y: 0, z: 1 },
{ x: 0, y: 0, z: -1 }
];
for (const offset of offsets) {
try {
const checkPos = {
x: pos.x + offset.x,
y: pos.y + offset.y,
z: pos.z + offset.z
};
const block = dimension.getBlock(checkPos);
if (block && (block.typeId === 'minecraft:water' || block.typeId === 'minecraft:flowing_water')) {
return true;
}
} catch (error) {
// Block not loaded
}
}
return false;
}
/**
* Custom command to toggle water breathing
*/
world.beforeEvents.chatSend.subscribe((event) => {
const message = event.message.toLowerCase();
const player = event.sender;
if (message === '!waterbreathing' || message === '!wb') {
event.cancel = true;
system.run(() => {