Files
Create_Addon
Code
25 694
Create_Addon/BP/scripts/main.js
import { ItemStack, system, world } from "@minecraft/server";
const IDS = Object.freeze({
cogwheel: "create_addon:cogwheel",
shaft: "create_addon:shaft",
handCrank: "create_addon:hand_crank",
waterWheel: "create_addon:water_wheel",
press: "create_addon:mechanical_press",
ironSheet: "create_addon:iron_sheet"
});
const KINETIC_BLOCKS = new Set(Object.values(IDS).filter((id) => id !== IDS.ironSheet));
const DIRECTIONS = [
{ 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 }
];
const activeNetworks = new Map();
const processedPresses = new Map();
function locationKey(dimension, location) {
return `${dimension.id}:${location.x},${location.y},${location.z}`;
}
function getAdjacentLocations(location) {
return DIRECTIONS.map((direction) => ({
x: location.x + direction.x,
y: location.y + direction.y,
z: location.z + direction.z
}));
}
function getNetwork(origin) {
const locations = [];
const queue = [origin.location];
const visited = new Set();
while (queue.length > 0 && locations.length < 256) {
const location = queue.shift();
const key = locationKey(origin.dimension, location);
if (visited.has(key)) {
continue;
}
visited.add(key);
const block = origin.dimension.getBlock(location);
if (!block || !KINETIC_BLOCKS.has(block.typeId)) {
continue;
}
locations.push({ x: location.x, y: location.y, z: location.z });
queue.push(...getAdjacentLocations(location));
}
return locations;
}
function isWaterNearby(dimension, location) {
return getAdjacentLocations(location).some((adjacent) => {
const block = dimension.getBlock(adjacent);
return block && (block.typeId === "minecraft:water" || block.typeId === "minecraft:flowing_water");
});
}
function activateNetwork(origin, rpm, player) {
const locations = getNetwork(origin);
if (locations.length === 0) {
return;
}
const network = {
dimension: origin.dimension,
locations,
rpm,
expiresAt: system.currentTick + 120
};
for (const location of locations) {
activeNetworks.set(locationKey(origin.dimension, location), network);
}
if (player) {
player.sendMessage(`§aKinetic network active: §f${rpm} RPM §7(${locations.length} blocks)`);
}
}
function processPress(network) {
for (const location of network.locations) {
const press = network.dimension.getBlock(location);
if (!press || press.typeId !== IDS.press) {
continue;
}
const pressKey = locationKey(network.dimension, location);
const lastProcessed = processedPresses.get(pressKey) ?? -1000;
if (system.currentTick - lastProcessed < 40) {
continue;
}
const inputLocation = { x: location.x, y: location.y - 1, z: location.z };
const input = network.dimension.getBlock(inputLocation);
if (!input || ![
"minecraft:iron_ore",
"minecraft:deepslate_iron_ore",
"minecraft:raw_iron_block"
].includes(input.typeId)) {
continue;
}
input.setType("minecraft:air");
network.dimension.spawnItem(
new ItemStack(IDS.ironSheet, input.typeId === "minecraft:raw_iron_block" ? 9 : 1),
{ x: location.x + 0.5, y: location.y - 0.15, z: location.z + 0.5 }
);
processedPresses.set(pressKey, system.currentTick);
}
}
world.afterEvents.playerInteractWithBlock.subscribe((event) => {
const block = event.block;
if (!block) {
return;
}
if (block.typeId === IDS.handCrank) {
activateNetwork(block, 16, event.player);
return;
}
if (block.typeId === IDS.waterWheel && isWaterNearby(block.dimension, block.location)) {
activateNetwork(block, 8, event.player);
}
});
world.afterEvents.playerPlaceBlock.subscribe((event) => {
const block = event.block;
if (block && block.typeId === IDS.waterWheel && isWaterNearby(block.dimension, block.location)) {
activateNetwork(block, 8);
}
});
system.runInterval(() => {
for (const [key, network] of activeNetworks) {
if (system.currentTick > network.expiresAt) {
activeNetworks.delete(key);
continue;
}
processPress(network);
}
}, 10);
world.afterEvents.playerSpawn.subscribe((event) => {
if (event.initialSpawn) {
event.player.sendMessage("§6Create Addon §7loaded. Place a Hand Crank next to shafts and cogwheels, then interact with it.");
}
});