Skip to content

Commit

Permalink
Merge pull request #24 from Adr-hyng/main
Browse files Browse the repository at this point in the history
Update to v1.20.6x
  • Loading branch information
Adr-hyng authored Feb 13, 2024
2 parents 215457b + 98ec8f9 commit 2447d56
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 108 deletions.
4 changes: 2 additions & 2 deletions BP/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"format_version": 2,
"header": {
"name": "Lumber Axe BP: Chopping made easy! 1.0.5",
"name": "Lumber Axe BP: Chopping made easy! 1.0.5 [DEBUG]",
"description": "Cut the whole tree with easy as a lumberjack's pie! We create addons/datapack that will ease your survival plays. \n @Created By: @h_YanG_0A & @Brilliant",
"uuid": "4538bab6-a78b-4710-89e5-a2e103e97a6a",
"version": [
Expand Down Expand Up @@ -45,7 +45,7 @@
"dependencies": [
{
"module_name": "@minecraft/server",
"version": "1.8.0-beta"
"version": "1.9.0-beta"
},
{
"module_name": "@minecraft/server-ui",
Expand Down
Binary file modified BP/pack_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 7 additions & 32 deletions BP/scripts/config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
export default {
/**
* Enables debug messages to content logs.
*/
debug: false,
/**
* Lumber Axe durability damage per log destroyed.
*/
durabilityDamagePerBlock: 3,
/**
* 1500 above is not recommended. It Does work but it's not recommended.
*/
chopLimit: 300,
/**
* Included blocks for custom logs, but any custom or vanilla logs also work as long as the block identifier ends with "*_log".
* Check: https://github.com/mcbe-mods/Cut-tree-one-click by Lete114.
*/
includedLog: [],
/**
* Excluded blocks for block logs you don't want to be included in being chopped.
*
* Tip:
* - excludedLog is prioritized over includedLog.
* - It's unnecessary to include log blocks that have "*_log" in their block id.
*/
excludedLog: [],
/**
* Disables the watchDogTerminate Log message. If true, it will only show a warning message when you enable content-ui log in the Minecraft settings.
*/
disableWatchDogTerminateLog: true,
debug: true,
durabilityDamagePerBlock: 3,
chopLimit: 300,
includedLog: [],
excludedLog: [],
disableWatchDogTerminateLog: true,
};

// version (do not change)
export const VERSION = "1.0.5";
export const VERSION = "1.0.5";
70 changes: 39 additions & 31 deletions BP/scripts/functions/tree_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityEquippableComponent, EquipmentSlot, ItemLockMode, ItemStack } from "@minecraft/server";
import { MinecraftBlockTypes } from "../modules/vanilla-types/index";
import { EntityEquippableComponent, EquipmentSlot, ItemDurabilityComponent, ItemEnchantableComponent, ItemLockMode, ItemStack, system } from "@minecraft/server";
import { MinecraftBlockTypes, MinecraftEnchantmentTypes } from "../modules/vanilla-types/index";
import { validLogBlocks, axeEquipments, stackDistribution, durabilityDamagePerBlock, excludedLog, includedLog, chopLimit } from "../index";
async function treeCut(player, dimension, location, blockTypeId) {
const equipment = player.getComponent(EntityEquippableComponent.componentId);
Expand All @@ -12,9 +12,9 @@ async function treeCut(player, dimension, location, blockTypeId) {
return;
if (player.isSurvival())
currentHeldAxe.lockMode = ItemLockMode.slot;
const itemDurability = currentHeldAxe.getComponent('minecraft:durability');
const enchantments = currentHeldAxe.getComponent('minecraft:enchantments').enchantments;
const level = enchantments.hasEnchantment('unbreaking');
const itemDurability = currentHeldAxe.getComponent(ItemDurabilityComponent.componentId);
const enchantments = (currentHeldAxe.getComponent(ItemEnchantableComponent.componentId));
const level = enchantments.getEnchantment(MinecraftEnchantmentTypes.Unbreaking)?.level | 0;
const unbreakingMultiplier = (100 / (level + 1)) / 100;
const unbreakingDamage = durabilityDamagePerBlock * unbreakingMultiplier;
const visited = await getTreeLogs(dimension, location, blockTypeId, (itemDurability.maxDurability - itemDurability.damage) / unbreakingDamage);
Expand Down Expand Up @@ -49,12 +49,14 @@ async function treeCut(player, dimension, location, blockTypeId) {
});
}
}
for (const group of stackDistribution(visited.size)) {
await new Promise((resolve) => {
dimension.spawnItem(new ItemStack(blockTypeId, group), location);
resolve();
});
}
system.runTimeout(async () => {
for (const group of stackDistribution(visited.size)) {
await new Promise((resolve) => {
dimension.spawnItem(new ItemStack(blockTypeId, group), location);
resolve();
});
}
}, 5);
}
function isLogIncluded(blockTypeId) {
if (excludedLog.includes(blockTypeId) || blockTypeId.includes('stripped_'))
Expand All @@ -63,26 +65,32 @@ function isLogIncluded(blockTypeId) {
return true;
return false;
}
async function getTreeLogs(dimension, location, blockTypeId, maxNeeded) {
const visited = new Set();
let queue = getBlockNear(dimension, location);
while (queue.length > 0) {
if (visited.size >= chopLimit || visited.size >= maxNeeded) {
return visited;
}
const _block = queue.shift();
if (!_block || !isLogIncluded(_block?.typeId))
continue;
if (_block.typeId !== blockTypeId)
continue;
const pos = JSON.stringify(_block.location);
if (visited.has(pos))
continue;
visited.add(pos);
queue.push(...getBlockNear(dimension, _block.location));
}
queue = [];
return visited;
function getTreeLogs(dimension, location, blockTypeId, maxNeeded) {
return new Promise((resolve) => {
const traversingTreeInterval = system.runInterval(() => {
const visited = new Set();
let queue = getBlockNear(dimension, location);
while (queue.length > 0) {
if (visited.size >= chopLimit || visited.size >= maxNeeded) {
system.clearRun(traversingTreeInterval);
resolve(visited);
}
const _block = queue.shift();
if (!_block || !isLogIncluded(_block?.typeId))
continue;
if (_block.typeId !== blockTypeId)
continue;
const pos = JSON.stringify(_block.location);
if (visited.has(pos))
continue;
visited.add(pos);
queue.push(...getBlockNear(dimension, _block.location));
}
queue = [];
system.clearRun(traversingTreeInterval);
resolve(visited);
}, 1);
});
}
function getBlockNear(dimension, location, radius = 1) {
const centerX = location.x;
Expand Down
9 changes: 5 additions & 4 deletions BP/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { world, system, ItemDurabilityComponent, ItemEnchantsComponent, WatchdogTerminateReason } from '@minecraft/server';
import { world, system, ItemDurabilityComponent, ItemEnchantableComponent, WatchdogTerminateReason } from '@minecraft/server';
import { FormCancelationReason, ActionFormData } from "@minecraft/server-ui";
import { disableWatchDogTerminateLog, durabilityDamagePerBlock, axeEquipments, forceShow, getTreeLogs, isLogIncluded, treeCut } from "./index";
import { MinecraftEnchantmentTypes } from './modules/vanilla-types/index';
const logMap = new Map();
const playerInteractionMap = new Map();
system.beforeEvents.watchdogTerminate.subscribe((e) => {
Expand Down Expand Up @@ -45,14 +46,14 @@ world.beforeEvents.itemUseOn.subscribe((e) => {
return;
playerInteractionMap.set(player.id, true);
const itemDurability = currentHeldAxe.getComponent(ItemDurabilityComponent.componentId);
const enchantments = currentHeldAxe?.getComponent(ItemEnchantsComponent.componentId)?.enchantments;
const level = enchantments.hasEnchantment('unbreaking');
const enchantments = currentHeldAxe.getComponent(ItemEnchantableComponent.componentId);
const level = enchantments.getEnchantment(MinecraftEnchantmentTypes.Unbreaking)?.level | 0;
const currentDurability = itemDurability.damage;
const maxDurability = itemDurability.maxDurability;
const unbreakingMultiplier = (100 / (level + 1)) / 100;
const unbreakingDamage = durabilityDamagePerBlock * unbreakingMultiplier;
const reachableLogs = (maxDurability - currentDurability) / unbreakingDamage;
getTreeLogs(player.dimension, blockInteracted.location, blockInteracted.typeId, reachableLogs + 1).then(async (treeCollected) => {
getTreeLogs(player.dimension, blockInteracted.location, blockInteracted.typeId, reachableLogs + 1).then((treeCollected) => {
const totalDamage = (treeCollected.size) * unbreakingDamage;
const totalDurabilityConsumed = currentDurability + totalDamage;
const canBeChopped = (totalDurabilityConsumed === maxDurability) || (totalDurabilityConsumed < maxDurability);
Expand Down
2 changes: 1 addition & 1 deletion RP/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"format_version": 2,
"header": {
"name": "Lumber Axe RP: Chopping made easy! 1.0.5",
"name": "Lumber Axe RP: Chopping made easy! 1.0.5 [DEBUG]",
"description": "Cut the whole tree with easy as a lumberjack's pie! We create addons/datapack that will ease your survival plays. \n @Created By: @h_YanG_0A & @Brilliant",
"uuid": "62b4cab4-fa42-405b-bff6-1f47b6958dd6",
"version": [
Expand Down
Binary file modified RP/pack_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@minecraft/server": "^1.8.0-beta.1.20.50-stable",
"@minecraft/server": "^1.9.0-beta.1.20.60-stable",
"@minecraft/server-ui": "^1.2.0-beta.1.20.30-preview.25"
}
}
2 changes: 1 addition & 1 deletion setup/mc_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"bp_dependencies": [
{
"module_name": "@minecraft/server",
"version": "1.8.0-beta"
"version": "1.9.0-beta"
},
{
"module_name": "@minecraft/server-ui",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default {
/**
* Enables debug messages to content logs.
*/
debug: false,
debug: true,
/**
* Lumber Axe durability damage per log destroyed.
*/
Expand Down
69 changes: 39 additions & 30 deletions src/functions/tree_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Block, Dimension, EnchantmentList, EntityEquippableComponent, EquipmentSlot, ItemDurabilityComponent, ItemEnchantsComponent, ItemLockMode, ItemStack, Player, Vector3 } from "@minecraft/server";
import { MinecraftBlockTypes} from "../modules/vanilla-types/index";
import { Block, Dimension, EntityEquippableComponent, EquipmentSlot, ItemDurabilityComponent, ItemEnchantableComponent, ItemLockMode, ItemStack, Player, System, Vector3, system } from "@minecraft/server";
import { MinecraftBlockTypes, MinecraftEnchantmentTypes} from "../modules/vanilla-types/index";

import { validLogBlocks, axeEquipments, stackDistribution, durabilityDamagePerBlock, excludedLog, includedLog, chopLimit } from "../index";

Expand All @@ -19,9 +19,9 @@ async function treeCut(player: Player, dimension: Dimension, location: Vector3,
if (player.isSurvival()) currentHeldAxe.lockMode = ItemLockMode.slot;

//! MAKE THIS D-R-Y
const itemDurability: ItemDurabilityComponent = currentHeldAxe.getComponent('minecraft:durability') as ItemDurabilityComponent;
const enchantments: EnchantmentList = (currentHeldAxe.getComponent('minecraft:enchantments') as ItemEnchantsComponent).enchantments;
const level: number = enchantments.hasEnchantment('unbreaking');
const itemDurability: ItemDurabilityComponent = currentHeldAxe.getComponent(ItemDurabilityComponent.componentId) as ItemDurabilityComponent;
const enchantments: ItemEnchantableComponent = (currentHeldAxe.getComponent(ItemEnchantableComponent.componentId));
const level: number = enchantments.getEnchantment(MinecraftEnchantmentTypes.Unbreaking)?.level | 0;
const unbreakingMultiplier: number = (100 / (level + 1)) / 100;
const unbreakingDamage: number = durabilityDamagePerBlock * unbreakingMultiplier;

Expand All @@ -30,7 +30,7 @@ async function treeCut(player: Player, dimension: Dimension, location: Vector3,
const totalDamage: number = visited.size * unbreakingDamage;
const postDamagedDurability: number = itemDurability.damage + totalDamage;

//! USE Lumberjack Axe Interface / Class for this
//! Put this to Durability interface
// Check if durabiliy is exact that can chop the tree but broke the axe, then broke it.
if (postDamagedDurability + 1 === itemDurability.maxDurability) {
equipment.setEquipment(EquipmentSlot.Mainhand, undefined);
Expand All @@ -45,7 +45,7 @@ async function treeCut(player: Player, dimension: Dimension, location: Vector3,
equipment.setEquipment(EquipmentSlot.Mainhand, currentHeldAxe.clone());
}

//! Lumberjack Axe Interface / Class
//! IDK where to put this.
for (const group of groupAdjacentBlocks(visited)) {
const firstElement = JSON.parse(group[0]);
const lastElement = JSON.parse(group[group.length - 1]);
Expand All @@ -63,12 +63,15 @@ async function treeCut(player: Player, dimension: Dimension, location: Vector3,
}
}

for (const group of stackDistribution(visited.size)) {
await new Promise<void>((resolve) => {
dimension.spawnItem(new ItemStack(blockTypeId, group), location);
resolve();
});
}
system.runTimeout( async () => {
for (const group of stackDistribution(visited.size)) {
await new Promise<void>((resolve) => {
dimension.spawnItem(new ItemStack(blockTypeId, group), location);
resolve();
});
}
}, 5);

}

function isLogIncluded(blockTypeId: string): boolean {
Expand All @@ -77,26 +80,32 @@ function isLogIncluded(blockTypeId: string): boolean {
return false;
}

async function getTreeLogs(dimension: Dimension, location: Vector3, blockTypeId: string, maxNeeded: number): Promise<Set<string>> {
function getTreeLogs(dimension: Dimension, location: Vector3, blockTypeId: string, maxNeeded: number): Promise<Set<string>> {
// Modified Version
// Author: Lete114 <https://github.com/Lete114>
// Project: https://github.com/mcbe-mods/Cut-tree-one-click
const visited: Set<string> = new Set<string>();
let queue: Block[] = getBlockNear(dimension, location);
while (queue.length > 0) {
if(visited.size >= chopLimit || visited.size >= maxNeeded) {
return visited;
}
const _block: Block = queue.shift();
if (!_block || !isLogIncluded(_block?.typeId)) continue;
if (_block.typeId !== blockTypeId) continue;
const pos: string = JSON.stringify(_block.location);
if (visited.has(pos)) continue;
visited.add(pos);
queue.push(...getBlockNear(dimension, _block.location));
}
queue = [];
return visited;
return new Promise<Set<string>>((resolve) => {
const traversingTreeInterval: number = system.runInterval(() => {
const visited: Set<string> = new Set<string>();
let queue: Block[] = getBlockNear(dimension, location);
while (queue.length > 0) {
if(visited.size >= chopLimit || visited.size >= maxNeeded) {
system.clearRun(traversingTreeInterval);
resolve(visited);
}
const _block: Block = queue.shift();
if (!_block || !isLogIncluded(_block?.typeId)) continue;
if (_block.typeId !== blockTypeId) continue;
const pos: string = JSON.stringify(_block.location);
if (visited.has(pos)) continue;
visited.add(pos);
queue.push(...getBlockNear(dimension, _block.location));
}
queue = [];
system.clearRun(traversingTreeInterval);
resolve(visited);
}, 1);
});
}

function getBlockNear(dimension: Dimension, location: Vector3, radius: number = 1): Block[] {
Expand Down
9 changes: 5 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { world, ItemStack, system, Block, BlockPermutation, Player, ItemDurabilityComponent, ItemEnchantsComponent, ItemUseOnBeforeEvent, WatchdogTerminateBeforeEvent, WatchdogTerminateReason, EnchantmentList, PlayerLeaveAfterEvent, PlayerBreakBlockAfterEvent } from '@minecraft/server';
import { world, ItemStack, system, Block, BlockPermutation, Player, ItemDurabilityComponent, ItemEnchantableComponent, ItemUseOnBeforeEvent, WatchdogTerminateBeforeEvent, WatchdogTerminateReason, PlayerLeaveAfterEvent, PlayerBreakBlockAfterEvent, EnchantmentType, EnchantmentTypes } from '@minecraft/server';
import { FormCancelationReason, ActionFormData, ActionFormResponse} from "@minecraft/server-ui";
import { disableWatchDogTerminateLog, durabilityDamagePerBlock ,axeEquipments, forceShow, getTreeLogs, isLogIncluded, treeCut} from "./index"
import { MinecraftEnchantmentTypes } from './modules/vanilla-types/index';

const logMap: Map<string, number> = new Map<string, number>();
const playerInteractionMap: Map<string, boolean> = new Map<string, boolean>();
Expand Down Expand Up @@ -47,14 +48,14 @@ world.beforeEvents.itemUseOn.subscribe((e: ItemUseOnBeforeEvent) => {

//! MAKE THIS D-R-Y
const itemDurability: ItemDurabilityComponent = currentHeldAxe.getComponent(ItemDurabilityComponent.componentId) as ItemDurabilityComponent;
const enchantments: EnchantmentList = (currentHeldAxe?.getComponent(ItemEnchantsComponent.componentId) as ItemEnchantsComponent)?.enchantments;
const level: number = enchantments.hasEnchantment('unbreaking');
const enchantments: ItemEnchantableComponent = (currentHeldAxe.getComponent(ItemEnchantableComponent.componentId) as ItemEnchantableComponent);
const level: number = enchantments.getEnchantment(MinecraftEnchantmentTypes.Unbreaking)?.level | 0;
const currentDurability = itemDurability.damage;
const maxDurability = itemDurability.maxDurability;
const unbreakingMultiplier: number = (100 / (level + 1)) / 100;
const unbreakingDamage: number = durabilityDamagePerBlock * unbreakingMultiplier;
const reachableLogs = (maxDurability - currentDurability) / unbreakingDamage;
getTreeLogs(player.dimension, blockInteracted.location, blockInteracted.typeId, reachableLogs + 1).then( async (treeCollected: Set<string>) => {
getTreeLogs(player.dimension, blockInteracted.location, blockInteracted.typeId, reachableLogs + 1).then( (treeCollected: Set<string>) => {
const totalDamage: number = (treeCollected.size) * unbreakingDamage;
const totalDurabilityConsumed: number = currentDurability + totalDamage;
const canBeChopped: boolean = (totalDurabilityConsumed === maxDurability) || (totalDurabilityConsumed < maxDurability);
Expand Down
2 changes: 1 addition & 1 deletion tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

addon_name = json.loads(open('setup/mc_manifest.json', 'r').read()).get("header").get("bp_name")
build_pack_name = addon_name[:addon_name.rfind(" BP")]
version_tag = 'v1.20.5x-109'
version_tag = 'v1.20.6x-110'

def handleError(err):
if err: exit(err)
Expand Down

0 comments on commit 2447d56

Please sign in to comment.