Skip to content

Commit

Permalink
Run Command when generated block is destroyed!
Browse files Browse the repository at this point in the history
  • Loading branch information
HirziDevs committed Jul 4, 2024
1 parent 339e0ca commit 24e0d82
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ CCBG are using Beta APIs to make the generator working, please enable it to make
- [Players and Tags](https://ccbg.znproject.my.id/configuration/players-and-tags)
- [Generator Blocks and Chances](https://ccbg.znproject.my.id/configuration/generator-blocks-and-chances)
- [Summon Mobs](https://ccbg.znproject.my.id/configuration/summon-mobs)
- [Run Commands](https://ccbg.znproject.my.id/configuration/commands)
- [Per Dimension Generator](https://ccbg.znproject.my.id/configuration/per-dimension-generator)
- [Custom Generator](https://ccbg.znproject.my.id/configuration/custom-generator)
- Additional Information
Expand Down
19 changes: 19 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ const config = {
{ identifier: "minecraft:sheep", chance: 20 },
],

/**
* @title Commands
*
* @description
* Enable or disable commands by changing "enableCommands"
* to "true" to enable it or "false" to disable it. without quotation mark
* You can use ((PLAYER)) placeholder to use player name in the command!
* You can use * to run commands on any block in the generator!
* INFO:
* SPECIFIC BLOCK COMMAND ONLY WORKS IF PLAYER DESTROY THE BLOCK!
* SO YOU CANNOT USE ((PLAYER)) IF THE BLOCK IS NOT DESTROYED BY PLAYER
*/
enableCommands: false,

commands: [
{ block: "*", command: `tellraw @a {"rawtext":[{"text":"Hey! someone is using custom generator!"}]}` },
{ block: "minecraft:ancient_debris", command: `tellraw @a {"rawtext":[{"text":"((PLAYER)) just found an ancient debris in custom generator!"}]}` }
],

/**
* @title Per Dimension Generator
*
Expand Down
6 changes: 6 additions & 0 deletions src/custom-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ custom.generators = [
left_block: [],
right_block: [],
under_block: [],
commands: [],
tags: [],
players: [],
particle: false,
Expand Down Expand Up @@ -60,6 +61,7 @@ const custom = {
left_block: ["minecraft:stone"],
right_block: ["minecraft:dripstone_block"],
under_block: [],
commands: [],
tags: [],
players: [],
particle: false,
Expand Down Expand Up @@ -97,6 +99,7 @@ const custom = {
left_block: ["minecraft:flowing_water", "minecraft:water", "WATERLOGGED"],
right_block: ["minecraft:flowing_lava", "minecraft:lava"],
under_block: ["minecraft:netherite_block"],
commands: [],
tags: ["donator"],
players: [],
particle: "minecraft:endrod",
Expand Down Expand Up @@ -131,6 +134,9 @@ const custom = {
left_block: ["minecraft:flowing_water", "minecraft:water", "WATERLOGGED"],
right_block: ["minecraft:flowing_lava", "minecraft:lava"],
under_block: ["minecraft:grass_block"],
commands: [
{ block: "minecraft:netherite_block", command: `tellraw @a {"rawtext":[{"text":"((PLAYER)) just found a netherite block in custom generator!"}]}` }
],
tags: [],
players: [],
particle: false,
Expand Down
26 changes: 22 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function Generator(
generatorType: number,
generatorBlock: Block,
player?: Player,
tool?: ItemStack
tool?: ItemStack,
previousBlock?: string
) {
if (generatorType === 0 && config.disableGeneratorTag && player.hasTag(config.disableGeneratorTag)) return;
const { dimension, location } = generatorBlock;
Expand Down Expand Up @@ -224,8 +225,10 @@ function Generator(
let players: string[] = config.players;
let particle: any = config.particle;
let sound: any = config.sound;
let commands: any = config.commands;
let enableParticle = config.enableParticle;
let enableSound = config.enableSound;
let enableCommands = config.enableCommands;
if (isCustomGenerator && customGenerator.enable) {
blocks = customGenerator.generators[customGeneratorID].blocks;

Expand All @@ -235,6 +238,11 @@ function Generator(
if (customGenerator.generators[customGeneratorID].players)
players = customGenerator.generators[customGeneratorID].players;

if (customGenerator.generators[customGeneratorID].commands) {
enableCommands = true;
commands = customGenerator.generators[customGeneratorID].commands;
}

if (customGenerator.generators[customGeneratorID].particle) {
enableParticle = true;
particle = customGenerator.generators[customGeneratorID].particle;
Expand Down Expand Up @@ -346,6 +354,12 @@ function Generator(
);
}

if (enableCommands && commands) {
for (const command of commands) {
if (previousBlock && command.block === previousBlock) dimension.runCommand(command.command.replaceAll("((PLAYER))", player.name));
else if (command.block === "*") dimension.runCommand(command.command);
}
}
if (enableParticle && particle) {
dimension.spawnParticle(particle.toLowerCase(), { x: location.x, y: location.y + 1.5, z: location.z });
dimension.spawnParticle(particle.toLowerCase(), { x: location.x + 1, y: location.y + 1.5, z: location.z });
Expand Down Expand Up @@ -397,9 +411,13 @@ function Generator(
}

if (config.player !== false)
world.afterEvents.playerBreakBlock.subscribe((event) =>
Generator(0, event.block, event.player, event.itemStackAfterBreak)
);
world.beforeEvents.playerBreakBlock.subscribe((event) => {
const previousBlock = `${event.block.type.id}`;

system.runTimeout(() =>
Generator(0, event.block, event.player, event.itemStack, previousBlock),
1)
});

if (config.explosion !== false)
world.afterEvents.blockExplode.subscribe((event) =>
Expand Down

0 comments on commit 24e0d82

Please sign in to comment.