sort out many edge cases for survival

This commit is contained in:
ForestOfLight
2025-03-16 03:34:55 -07:00
Unverified
parent 21ec447e0e
commit c9927f8e24
2 changed files with 129 additions and 33 deletions
+58 -14
View File
@@ -1,22 +1,32 @@
export const bannedEasyPlaceBlocks = [ export const bannedBlocks = [
'air', 'bed', 'air', 'bed', 'piston_arm_collision', 'sticky_piston_arm_collision', "skeleton_skull", 'standing_banner', 'wall_banner',
'wooden_door', 'spruce_door', 'birch_door', 'jungle_door', 'acacia_door', 'dark_oak_door', 'mangrove_door', 'cherry_door', 'pale_oak_door', 'wooden_door', 'spruce_door', 'birch_door', 'jungle_door', 'acacia_door', 'dark_oak_door', 'mangrove_door', 'cherry_door', 'pale_oak_door',
'bamboo_door', 'iron_door', 'crimson_door', 'warped_door', 'copper_door', 'exposed_copper_door', 'weathered_copper_door', 'oxidized_copper_door', 'bamboo_door', 'iron_door', 'crimson_door', 'warped_door', 'copper_door', 'exposed_copper_door', 'weathered_copper_door', 'oxidized_copper_door',
'waxed_copper_door', 'waxed_exposed_copper_door', 'waxed_weathered_copper_door', 'waxed_oxidized_copper_door', 'waxed_copper_door', 'waxed_exposed_copper_door', 'waxed_weathered_copper_door', 'waxed_oxidized_copper_door',
'piston_arm_collision', 'sticky_piston_arm_collision', "skeleton_skull" 'seagrass', 'kelp'
]; ];
export const bannedDimensionBlocks = {
'overworld': [],
'nether': ['water'],
'end': []
};
export const whitelistedBlockStates = {
'water': { liquid_depth: 0 },
'lava': { liquid_depth: 0 }
};
export const bannedToValidBlockMap = { export const bannedToValidBlockMap = {
'lit_furnace': 'furnace', 'lit_furnace': 'furnace',
'lit_smoker': 'smoker', 'lit_smoker': 'smoker',
'lit_blast_furnace': 'blast_furnace', 'lit_blast_furnace': 'blast_furnace',
'lit_redstone_ore': 'redstone_ore', 'lit_redstone_ore': 'redstone_ore',
'unlit_redstone_torch': 'redstone_torch', 'lit_redstone_lamp': 'redstone_lamp',
'bubble_column': 'water', 'unlit_redstone_torch': 'redstone_torch'
// 'seagrass'? 'tall_seagrass'? kelp?
}; };
export const defaultStates = { export const resetToBlockStates = {
growth: 0, growth: 0,
age: 0, age: 0,
height: 0, height: 0,
@@ -25,15 +35,49 @@ export const defaultStates = {
redstone_signal: 0, redstone_signal: 0,
cluster_count: 0, cluster_count: 0,
respawn_anchor_charge: 0, respawn_anchor_charge: 0,
turtle_egg_count: "one_egg", turtle_egg_count: 0,
cauldron_liquid: "water" cluster_count: 0
}; };
export const allowedBlockStates = { export const chainedStatePlacements = {
water: { depth_level: 0 }, cauldron: {
lava: { depth_level: 0 } cauldron_liquid: {
water: ['water_bucket'],
lava: ['lava_bucket']
}
},
sea_pickle: {
cluster_count: {
1: ['sea_pickle'],
2: ['sea_pickle', 'sea_pickle'],
3: ['sea_pickle', 'sea_pickle', 'sea_pickle']
}
},
turtle_egg: {
turtle_egg_count: {
two_eggs: ['turtle_egg'],
three_eggs: ['turtle_egg', 'turtle_egg'],
four_eggs: ['turtle_egg', 'turtle_egg', 'turtle_egg']
}
},
respawn_anchor: {
respawn_anchor_charge: {
1: ['glowstone'],
2: ['glowstone', 'glowstone'],
3: ['glowstone', 'glowstone', 'glowstone'],
4: ['glowstone', 'glowstone', 'glowstone', 'glowstone']
}
}
}; };
export const bannedDimensionBlocks = { export const blockIdToItemStackMap = {
'nether': ['water', 'bubble_column'] 'water': 'water_bucket',
'lava': 'lava_bucket',
'fire': 'fire_charge',
'soul_fire': 'fire_charge',
}; };
export const specialItemPlacementConversions = {
'water_bucket': 'bucket',
'lava_bucket': 'bucket'
}
+69 -17
View File
@@ -1,8 +1,9 @@
import { Rule } from '../lib/canopy/CanopyExtension'; import { Rule } from '../lib/canopy/CanopyExtension';
import { extension } from '../config'; import { extension } from '../config';
import { EntityComponentTypes, GameMode, system, world } from '@minecraft/server'; import { BlockPermutation, EntityComponentTypes, GameMode, ItemStack, system, world } from '@minecraft/server';
import { structureCollection } from '../classes/StructureCollection'; import { structureCollection } from '../classes/StructureCollection';
import { bannedEasyPlaceBlocks } from '../data'; import { bannedBlocks, bannedToValidBlockMap, whitelistedBlockStates, resetToBlockStates, bannedDimensionBlocks, specialItemPlacementConversions,
blockIdToItemStackMap } from '../data';
const easyPlace = new Rule({ const easyPlace = new Rule({
identifier: 'easyPlace', identifier: 'easyPlace',
@@ -16,9 +17,8 @@ function onPlayerPlaceBlock(event) {
const { player, block } = event; const { player, block } = event;
if (!player || !block) return; if (!player || !block) return;
const structureBlock = fetchStructureBlock(block.location); const structureBlock = fetchStructureBlock(block.location);
if (!structureBlock || isBannedBlock(structureBlock)) if (!structureBlock)
return; return;
const states = structureBlock.getAllStates();
tryPlaceBlock(event, player, block, structureBlock); tryPlaceBlock(event, player, block, structureBlock);
} }
@@ -30,36 +30,76 @@ function fetchStructureBlock(location) {
return structure.getBlock(structure.toStructureCoords(location)); return structure.getBlock(structure.toStructureCoords(location));
} }
function isBannedBlock(structureBlock) {
return bannedEasyPlaceBlocks.some(bannedId => structureBlock.type.id.replace('minecraft:', '') === bannedId);
}
function tryPlaceBlock(event, player, block, structureBlock) { function tryPlaceBlock(event, player, block, structureBlock) {
if (isBannedBlock(player, structureBlock)) return;
structureBlock = tryConvertBannedToValidBlock(structureBlock);
if (player.getGameMode() === GameMode.creative) { if (player.getGameMode() === GameMode.creative) {
placeBlock(block, structureBlock); placeBlock(block, structureBlock);
} else if (player.getGameMode() === GameMode.survival) { } else if (player.getGameMode() === GameMode.survival) {
structureBlock = tryConvertToDefaultState(structureBlock);
tryPlaceBlockSurvival(event, player, block, structureBlock); tryPlaceBlockSurvival(event, player, block, structureBlock);
} }
} }
function isBannedBlock(player, structureBlock) {
const blockId = structureBlock.type.id.replace('minecraft:', '');
if (bannedBlocks.includes(blockId))
return true;
if (bannedDimensionBlocks[player.dimension.id.replace('minecraft:', '')]?.includes(blockId))
return true;
const allowedStates = whitelistedBlockStates[blockId];
if (allowedStates) {
for (const [stateKey, stateValue] of Object.entries(allowedStates)) {
if (structureBlock.getState(stateKey) !== stateValue)
return true;
}
}
return false;
}
function tryConvertBannedToValidBlock(structureBlock) {
const blockId = structureBlock.type.id.replace('minecraft:', '');
if (Object.keys(bannedToValidBlockMap).includes(blockId))
return BlockPermutation.resolve(bannedToValidBlockMap[blockId], structureBlock.getAllStates());
return structureBlock;
}
function tryConvertToDefaultState(structureBlock) {
const newStates = {};
for (const [stateKey, stateValue] of Object.entries(structureBlock.getAllStates())) {
if (resetToBlockStates[stateKey] !== void 0 && stateValue !== resetToBlockStates[stateKey])
newStates[stateKey] = resetToBlockStates[stateKey];
else
newStates[stateKey] = stateValue;
}
return BlockPermutation.resolve(structureBlock.type.id, newStates);
}
function tryPlaceBlockSurvival(event, player, block, structureBlock) { function tryPlaceBlockSurvival(event, player, block, structureBlock) {
const itemSlotToUse = fetchMatchingItemSlot(player, structureBlock); const placeableItemStack = getPlaceableItemStack(structureBlock);
console.warn(`Looking for item to place ${structureBlock?.type.id} (${placeableItemStack?.typeId})...`);
const itemSlotToUse = fetchMatchingItemSlot(player, placeableItemStack?.typeId);
if (itemSlotToUse) { if (itemSlotToUse) {
event.cancel = true; event.cancel = true;
placeBlock(block, structureBlock, itemSlotToUse); placeBlock(block, structureBlock, itemSlotToUse);
} }
} }
function fetchMatchingItemSlot(player, structureBlock) { function getPlaceableItemStack(structureBlock) {
const itemToMatch = structureBlock.getItemStack(); const blockId = structureBlock.type.id.replace('minecraft:', '');
if (!itemToMatch) const newItemId = blockIdToItemStackMap[blockId];
return newItemId ? new ItemStack(newItemId) : structureBlock.getItemStack();
}
function fetchMatchingItemSlot(player, itemToMatchId) {
if (!itemToMatchId)
return void 0; return void 0;
const inventory = player.getComponent(EntityComponentTypes.Inventory)?.container; const inventory = player.getComponent(EntityComponentTypes.Inventory)?.container;
if (!inventory) if (!inventory)
return void 0; return void 0;
for (let index = 0; index < inventory.size; index++) { for (let index = 0; index < inventory.size; index++) {
const itemSlot = inventory.getSlot(index); const itemSlot = inventory.getSlot(index);
if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatch.typeId) if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatchId)
return itemSlot; return itemSlot;
} }
} }
@@ -67,11 +107,23 @@ function fetchMatchingItemSlot(player, structureBlock) {
function placeBlock(block, structureBlock, itemSlot) { function placeBlock(block, structureBlock, itemSlot) {
system.run(() => { system.run(() => {
if (itemSlot) { if (itemSlot) {
if (itemSlot.amount === 1) consumeItem(itemSlot);
itemSlot.setItem(void 0);
else
itemSlot.amount--;
} }
block.setPermutation(structureBlock); block.setPermutation(structureBlock);
}); });
} }
function consumeItem(itemSlot) {
if (specialItemPlacementConversions[itemSlot.typeId.replace('minecraft:', '')]) {
consumeSpecial(itemSlot);
} else {
if (itemSlot.amount === 1)
itemSlot.setItem(void 0);
else
itemSlot.amount--;
}
}
function consumeSpecial(itemSlot) {
itemSlot.setItem(new ItemStack(specialItemPlacementConversions[itemSlot.typeId.replace('minecraft:', '')]));
}