#Script API General
1 messages · Page 38 of 1
How would I check that?
Keep in mind
I think they're stored like this, based on the pack's uuid, meaning packs cannot access each others properties
World
└── Dynamic Properties
├── 3f7242c6-639e-4216-8087-bfe905b36989
│ └── my_property: true
└── 13e2b202-182b-46d3-aec4-2556a2653c6f
├── my_property: 13
└── another_property: "hello"
So if the add-on is removed so are the dynamic properties?
I don't think so
It does matter because they are saved on the uuid of the manifest so if you change your manifest's they will be deleted
And what would be the best way to pass information between addons
Tags, scoreboard, store it on entity. You pick
It also kinda depends what information you are trying to pass
So scoreboards could be considered dynamic properties and they can be read between packs with just getScoreboardObjective
You can say that but it can only store integers so idk what's your idea
how do i fetch player's scoreboard
world.scoreboard.getObjective('test').getScore(player)```
Is this what you meant? Getting the score of the player in an objective?
Ok thanks good info!
yeah
30 of if/else statement time.
pain begins
what if else statement
Aww hell nah... Can't you just use switch?
What are you trying to check in the first place?
I'm aware of switch statement just poking.
otherwise i wouldn't be asking for scoreboard stuff
Oof, I'm kinda bored so I'll convert scoreboard into binary that can convert those into words... What a nice idea, I just need to learn how binary works
why do you need if else statement for scoreboard
attempting to experiment with something.
can't really say anything much tbh.
I've installed NPM typings but my intellisense still isnt working. Any ideas?
that works.
honestly i might attempt to install script-api's type/npm to my neovim to see if it works
Kinda weird to see you do script API, I only saw you as json ui and pixel artist 😭
how do you make custom events/ spawn entities with components
can applyImpulse be used on player
No.
can i change the gravity of applied knockback or no
how do i know whether a fence gate is open or not
small hobby stuff probably
Installation for @minecraft/server
Latest API module install:
npm i @minecraft/[email protected]
Beta API module install:
npm i @minecraft/[email protected]
Preview API module install:
npm i @minecraft/[email protected]
Preview Beta API module install:
npm i @minecraft/[email protected]
Doesnt exist anymore.
you want to use system.runInterval() for game loops.
example:
//say "Hello world" every 20 tick
system.runInterval(()=>{
world.sendMessage("Hello world");
},20)
system.run()to run a code on the next tick (especially to exit beforeEvents scope)system.runTimeout()to delay a code with xxx amount of tick.system.waitTicks()a tick delay in the form of a promise to be used with the conjunction of theawaitkeyword, or.then()
-# Check the System class in the docs
ty
anything wrong with this?
balance => {
const currentMoney = parseInt(balance);
if (currentMoney >= material.cost) {
Looks normal. Maybe you want to tell me what error or the full code you have
function upgradeGenMaterial(player) {
const materialNames = materials.map(mat => mat.name);
new ModalFormData()
.title("Upgrade Gen Material")
.dropdown("Select new material", materialNames, 0)
.show(player)
.then(response => {
if (response.canceled) return;
const selectedMaterialIndex = response.formValues[0];
const material = materials[selectedMaterialIndex];
// Check player's current balance first using scoreboard values
player.runCommandAsync(`scoreboard players get @s money`)
.then(balance => {
const currentMoney = parseInt(balance);
if (currentMoney >= material.cost) {
// Deduct only if enough money is available
player.runCommandAsync(`scoreboard players remove @s money ${material.cost}`);
currentMaterial = material.id;
if (!availableGenTypes.includes(material.name)) {
availableGenTypes.push(material.name);
}
player.sendMessage(`Material upgraded to: ${material.name} for ${material.cost}`);
} else {
player.sendMessage(`Not enough money to buy ${material.name}. You need ${material.cost - currentMoney} more.`);
}
})
.catch(error => {
player.sendMessage(`Error checking balance: ${error}`);
});
});
}```
invalid scoreboard command ig
its say CommandError, so its the runCommandAsync problem
i figured it out
runCommand doesn't return any values apart from wether the command was successful or not
so what you're doing it completely wrong
use world.scoreboard.getObjective('money').getScore(player) instead
How can I make everywhere I get an item execute a script?
pickup item?
Yep
Ironically, getting an accurate reading of "who picks up the item" is the hard part.
easiest way fs:
get everyone's inventory slots near the item and save them, get the dropped item stack, test if the item isn't a thing anymore, then test whose inventory has the item
kinda sarcasm but might actually be the best way
Can you send me an example so I can understand better?
i have no example but I can explain it maybe a tiny bit more specific
Ok
can u give me a get socre?
Get the item (by your methods, either specific item typeid, nametag, etc.), then test for all entities (filter for players) around the item, get the inventory of the player and save all of the inventory slots using a loop to a table (let inventorytable = {}) or a Map like "Zappy NE": {...inventory} where inventory has the item typeid, nametag, etc. and slot number of each item 0-35, then use runInterval either every tick or every like 5 or 10 ticks to see if the item isn't existing, and if so, check the player's inventory and see if the item is in there, for each player it collected inventories from.
Thanks
not working
ofc it's not gonna work if you just drop it in
I typed all that into AI and it gave me a good basis to work off, you can do that if you're still confused
Omniac, do the ?? addObjective ig
you gotta write it properly
can u change it in here?
world.scoreboard.getObjective('money')?.getScore(player)
It's perfect
remove the then()
the new code returns a number, not a promise
no I don't really do that, I can point you in the right direction but I can't do it for you
otherwise you wouldn't really learn anything
I don't recommend using this code exactly how it is, due to AI not being very good with vague context, but this is the idea:
import { world, system, EntityInventoryComponent } from "@minecraft/server";
// Configuration
const CHECK_INTERVAL = 10; // Interval (ticks) to run the check
const TARGET_ITEM_TYPE = "minecraft:diamond"; // Change to the type of item you want to track
const SEARCH_RADIUS = 10; // Radius to search for players around the item
// Storage for tracking
let trackedInventories = new Map(); // Player name as key, inventory as value
let trackedItemLocation = null; // Store the item's location to track
// Function to get all players within a radius of a location
function getPlayersNearby(location, radius) {
return world.getPlayers({
location,
maxDistance: radius,
});
}
// Function to save a player's inventory
function savePlayerInventory(player) {
let inventory = [];
const inventoryComp = player.getComponent("minecraft:inventory");
if (inventoryComp) {
const container = inventoryComp.container;
for (let i = 0; i < container.size; i++) {
const item = container.getItem(i);
if (item) {
inventory.push({
typeId: item.typeId,
nameTag: item.nameTag,
slot: i,
count: item.amount,
});
}
}
}
trackedInventories.set(player.name, inventory);
}
``` ..continued
function isItemStillThere() {
if (!trackedItemLocation) return false;
const blockEntities = world.getEntities({
location: trackedItemLocation,
maxDistance: 1,
type: "minecraft:item",
});
for (const entity of blockEntities) {
if (entity.getComponent("minecraft:item").itemStack.typeId === TARGET_ITEM_TYPE) {
return true;
}
}
return false;
}
// Main function to track the item and nearby players
function trackItemAndInventory(itemEntity) {
trackedItemLocation = itemEntity.location;
const nearbyPlayers = getPlayersNearby(itemEntity.location, SEARCH_RADIUS);
for (const player of nearbyPlayers) {
savePlayerInventory(player);
}
// Periodically check for item existence and player inventories
system.runInterval(() => {
if (!isItemStillThere()) {
for (const player of world.getPlayers()) {
const inventory = trackedInventories.get(player.name) || [];
for (const item of inventory) {
const inventoryComp = player.getComponent("minecraft:inventory");
if (!inventoryComp) continue;
const container = inventoryComp.container;
const slotItem = container.getItem(item.slot);
if (slotItem && slotItem.typeId === item.typeId && slotItem.nameTag === item.nameTag) {
console.log(`Player ${player.name} has the tracked item in slot ${item.slot}`);
}
}
}
// Clear tracking if the item is gone
trackedItemLocation = null;
trackedInventories.clear();
}
}, CHECK_INTERVAL);
}
// Detect and track the item when spawned
world.events.entitySpawn.subscribe((event) => {
const entity = event.entity;
if (entity.typeId === "minecraft:item") {
const itemStack = entity.getComponent("minecraft:item").itemStack;
if (itemStack.typeId === TARGET_ITEM_TYPE) {
console.log(`Tracking item: ${itemStack.typeId}`);
trackItemAndInventory(entity);
}
}
});
made it this not works
new ModalFormData()
.title("Upgrade Gen Material")
.dropdown("Select new material", materialNames, 0)
.show(player)
.then(response => {
if (response.canceled) return;
const selectedMaterialIndex = response.formValues[0];
const material = materials[selectedMaterialIndex];
// Check player's current balance using scoreboard values
world.scoreboard.getObjective('money').getScore(player)
.then(balance => {
const currentMoney = parseInt(balance); // Convert balance to integer
if (currentMoney >= material.cost) {
// Deduct only if enough money is available
player.runCommandAsync(`scoreboard players remove @s money ${material.cost}`);
currentMaterial = material.id;
if (!availableGenTypes.includes(material.name)) {
availableGenTypes.push(material.name);
}
player.sendMessage(`Material upgraded to: ${material.name} for ${material.cost}`);
} else {
player.sendMessage(`Not enough money to buy ${material.name}. You need ${material.cost} to buy this material.`);
}
})
.catch(error => {
player.sendMessage(`Error checking balance: ${error}`);
});
});
}
Ahaha yes, the insane store each itemStack per tick strategy
i said don't use it exactly because it was from AI, change it and triple check
getScore returns a number, not a promise
so remove the .then and do
const balance = // getscore stuff
it actually saves the inventories when the item spawns anyway, not every tick
Ohhhh... Much much thanks
i dont understand
°-°
Just my impression. Or are you just asking people and maybe even AI for ready-made code and not actually learning anything?
That seems to be the case based on their previous messages.
what don't you understand?
Restructure your code. The .then(balance =>{}) is removed.
Then declare a new balance variable with the new getScore code.
// Upgrade Gen Material
function upgradeGenMaterial(player) {
const materialNames = materials.map(mat => mat.name);
new ModalFormData()
.title("Upgrade Gen Material")
.dropdown("Select new material", materialNames, 0)
.show(player)
.then(response => {
if (response.canceled) return;
const selectedMaterialIndex = response.formValues[0];
const material = materials[selectedMaterialIndex];
// Check player's current balance using scoreboard values
const playerScore = world.scoreboard.getObjective('money')?.getScore(player);
if (playerScore !== undefined && playerScore >= material.cost) {
// Deduct money and upgrade material
world.scoreboard.getObjective('money')?.removeScore(player, material.cost);
currentMaterial = material.id;
if (!availableGenTypes.includes(material.name)) {
availableGenTypes.push(material.name);
}
player.sendMessage(`Material upgraded to: ${material.name} for ${material.cost}`);
} else {
player.sendMessage(`Not enough money to buy ${material.name}. You need ${material.cost} to buy this material.`);
}
});
}
yes that should be correct
ah no
removeScore doesn't exist
use setScore, and subtract the value from the playerScore
Oh my god, it's definitely AI! 🔥
so setScore(player, playerValue - material.cost)
yeah lol i said to implement getscore into that code
cuz i didnt understand
just actually learn how to write it
M9SSE use mctools to write if you are in an phone
mctools?
// Upgrade Gen Material
function upgradeGenMaterial(player) {
const materialNames = materials.map(mat => mat.name);
new ModalFormData()
.title("Upgrade Gen Material")
.dropdown("Select new material", materialNames, 0)
.show(player)
.then(response => {
if (response.canceled) return;
const selectedMaterialIndex = response.formValues[0];
const material = materials[selectedMaterialIndex];
// Check player's current balance using scoreboard values
const playerScore = world.scoreboard.getObjective('money')?.getScore(player);
if (playerScore !== undefined && playerScore >= material.cost) {
// Deduct money and upgrade material by subtracting the cost
world.scoreboard.getObjective('money')?.setScore(player, playerScore - material.cost);
currentMaterial = material.id;
if (!availableGenTypes.includes(material.name)) {
availableGenTypes.push(material.name);
}
player.sendMessage(`Material upgraded to: ${material.name} for ${material.cost}`);
} else {
player.sendMessage(`Not enough money to buy ${material.name}. You need ${material.cost} to buy this material.`);
}
});
}```
this fine?
yall know how i can use player.getEntitiesFromViewDirection().filter() to get 1 entity which is closest to my camera cuz i got 0 idea of raycast works thanks
sure
wow
His code is AI-generated. I doubt he's using any tool for coding bruh
im using acode
yall know how i can use player.getEntitiesFromViewDirection().filter() to get 1 entity which is closest to my camera cuz i got 0 idea of raycast works thanks?
the inventory is .container
from there, you can use a loop to iterate over each slot using inventory.getSlot(i) or .getItem for the itemStack
just that code I provided, I make everything else I use
It's an array iirc so just query the first.
so like query.onGround like that?
idk if that a realy query
just an example
Uhh, that message is for someone else actually 👆🤓
(the madara pfp guy)
I meant jist do [0]
Understood
No.
...
player.getEntitiesFromViewDirection()[0]
getEntitiesFromViewDirection()[0].entity
I also just learned EntityRaycastHit has a distance option so you can filter by that as well.
Yeah that works too.
im just reading this is there a website which has information on how i can configure it?
getEntitiesFromViewDirection
returns EntityRaycastHit[] ( Array of EntityRaycastHit )
i figured that part thanks to smokey
but i dont know what to fixed and others do
Documentation for @minecraft/server
Do you need Beta APIs to be enabled for (non-beta) scripts to work or is it only required for scripts using the beta modules?
Only betas.
need help
function switchGenType(player) {
// Ensure the player has bought materials available for selection
if (boughtMaterials.length === 0) {
player.sendMessage("You haven't bought any materials yet!");
return;
}
// Create the dropdown list with material names that are bought
const boughtMaterialNames = boughtMaterials.map(matName => matName);
// Show the form with the bought materials
new ModalFormData()
.title("Switch Gen Type")
.dropdown("Select the new gen type", boughtMaterialNames, 0)
.show(player)
.then(response => {
if (response.canceled) return;
// Get the selected material name from the form
const selectedMaterialName = response.formValues[0];
// Find the full material object based on selected name
const selectedMaterial = materials.find(mat => mat.name === selectedMaterialName);
if (selectedMaterial) {
// Set the current gen type to the selected material's ID
currentMaterial = selectedMaterial.id;
player.sendMessage(`Generation type switched to: ${selectedMaterial.name}`);
} else {
player.sendMessage("Invalid selection for gen type.");
}
});
}
sorry to bother u guys
any help?
world.afterEvents.entitySpawn.subscribe(({
entity
}) => {
if (entity.hasComponent('item')) {
const getDistance = (vector1, vector2) => {
const dx = vector1.x - vector2.x;
const dy = vector1.y - vector2.y;
const dz = vector1.z - vector2.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
};
const player = entity.dimension.getPlayers().find(mob => {
getDistance(mob.getViewDirection(), entity.getViewDirection()) === 0;
});
const item = entity.getComponent('item').itemStack;
if (player && player.getRotation().x === entity.getRotation().x && player.getRotation().y === entity.getRotation().y && player.getRotation().z === entity.getRotation().z) {
console.error(`${player.name} dropped ${item.typeId}x${item.amount}`);
}
}
});```
Inefficient item drop detection.
Yo do I need anything other than @minecraft/server-net module in my manifest to make a simple bedrock to discord link work? (I know I need BDS)
Cuz uh this
Why exactly would I need server admin?..
Or do these depend on the @minecraft/server-net module???
I'm dearly confused
@minecraft/server-admin Module
Contains types related to administering a Bedrock Dedicated Server. These types allow for the configuration of variables and secrets in JSON files in the Bedrock Dedicated Server folder. These types cannot be used on Minecraft clients or within Minecraft Realms.
server-net requires server-admin module because it uses 1 class from that module
Go to config/default/permissions.json and add @minecraft/server-net to the "allowed_modules" array
Remember its only available for bds
which particle has a small circular shape, doesn't move and can have its color modified?
custom particle
i ended up creating my own custom particle.
Great
Can i use replaceitem on runCommandAsync?
Yes.
it seems like a block placed by a block placer component with replace_block_item set to true, doesn't fire the beforeOnPlayerPlace custom component...
Is possible to like translate a form using a .lang file?
Cuz I tried and it didn't work (I did something wrong probably)
Just to be sure
How do i make obsidian destructible by explosion?
I dont know if this can be done with scripting, i've tried and seems to be impossible because getImpactedBlocks doesn't return obsidian blocks.
What am I doing wrong ?😭
.title({ translate: "tracking.form.title" })```
```lang
tracking.form.title=Select a player to track```
yes
you can't
actually... you can
How?
I tried getting impacted blocks but they aren't detected with the before or after event
they are not part of the impacted blocks
get the blocks in radius or using rays and impact them manually
I tried that too
but
source.location is not accesable for this event
in the event
so how am i gonna detect the center of the explosion to get the blocks in radius
maybe get impacted block and get it location?
impacted blocks won't return anything if the only thing around the tnt is obsidian so that doesnt work either
that is true
weird that there is no location property
there is source, you might be able to get its location in the before event
why?
No, you aren't
it just returns undefined
after event?
maybe because you are reading it inside system. run
and at that point the tnt entity is already gone
?
errors?
None
what is the problem
If the first one is correct, the problem was in the .lang file
nope i wasnt even using system.run
tho i already found a method
it's a workaround
a little overcomplicated but works
import { Block, Dimension, system, Vector3, world } from "@minecraft/server";
function getBlocksInRadius(center: Vector3, radius: number, dimension: Dimension) {
const blocks: Block[] = [];
const { x: centerX, y: centerY, z: centerZ } = center;
for (let x = centerX - radius; x <= centerX + radius; x++) {
for (let y = centerY - radius; y <= centerY + radius; y++) {
for (let z = centerZ - radius; z <= centerZ + radius; z++) {
const distance = Math.sqrt(
(x - centerX) ** 2 + (y - centerY) ** 2 + (z - centerZ) ** 2
);
if (distance <= radius) {
const block = dimension.getBlock({ x, y, z });
if (block) {
blocks.push(block);
}
}
}
}
}
return blocks;
}
const tnts: { [id: string]: Vector3 } = {
}
world.afterEvents.entitySpawn.subscribe((res) => {
const entity = res.entity
if (entity.typeId !== 'minecraft:tnt') return;
const tntInterval = system.runInterval(() => {
if (!entity?.isValid()) return system.clearRun(tntInterval)
tnts[entity.id] = entity.location
})
})
world.beforeEvents.explosion.subscribe((res) => {
const tnt = tnts[res?.source?.id]
const dimension = res.dimension
if (!tnt) return;
const blocks = getBlocksInRadius(tnt, 4, dimension)
for (const block of blocks){
if (block?.typeId !== 'minecraft:obsidian') continue;
dimension.runCommandAsync(`setblock ${block.x} ${block.y} ${block.z} air destroy`)
}
delete tnts[res.source.id]
})
saving the tnt location when it spawns
tnt moves you know?
and thats why i put it in a interval lol
take a look at the code
gg
i don't see anything wrong
idk why but now .location is actually working 😭 minecraft sometimes is weird lol
whatever
maybe the translation key are not updated
reload all the packs
/reload all
https://stirante.com/script/server/1.18.0-beta.1.21.60-preview.21/classes/ExplosionBeforeEvent.html
You could use beta before explosion maybe to get location?
Documentation for @minecraft/server
If you guys are ever running low on storage all of a sudden try clearing your content log history
why not use res.source.location directly? or am i messing something
dude
don't enable file log
ok i disable
how do i use the debugger with vsc again, i forgot
95gb of log is crazy too lol
you get errors a lot? or is it you using warn
install vscode extenssion
i get "5ms slowdown in viper |infernal" every tick
i have that but i don't understand how to actually set up the connection and use it correctly
ups what are you doing?
i am doing a lot of stuff
i mean are you generating world or something?
no
checking for armor every second,
then what lol?
checking for held item every tick
ohh
checking claim area every time you interact with block
ya ItemStacks are expensive
though i set up a debounce time 1 tick for this
bc if u held down a block for more than 1 sec
you could basically crash the world
well you should optimize the code
it is kinda confusing tbh
the official doc guide don't work for me, i always have to edit some stuff
how does one optimize this
const effectsMap = {
"minecraft:iron_ingot": { effect: "resistance", amplifier: 0 },
"minecraft:ghast_tear": { effect: "regeneration", amplifier: 0, unique: true },
"minecraft:blaze_powder": { effect: "strength", amplifier: 0 },
"minecraft:sugar": { effect: "speed", amplifier: 1 },
"minecraft:magma_cream": { effect: "fire_resistance", amplifier: 0 },
"minecraft:feather": { effect: "jump_boost", amplifier: 0 }
};
system.runInterval(() => {
for (const player of world.getPlayers()) {
const playerClass = player.getDynamicProperty("class");
if (playerClass === "bard") {
const inventory = player.getComponent("inventory");
const heldItem = inventory.container.getItem(player.selectedSlotIndex) ?? null;
if (!heldItem) continue;
const effectData = effectsMap[heldItem?.typeId ?? null];
if (!effectData) continue;
const team = player.getDynamicProperty("team");
const nearbyPlayers = world
.getDimension(player.dimension.id)
.getEntities({
type: "minecraft:player",
maxDistance: 25,
location: player.location
});
for (const target of nearbyPlayers) {
if (target.getDynamicProperty("team") === team) {
const hasEffect = effectData.unique
? target.getEffect(effectData.effect) != undefined
: false;
if (!hasEffect) {
target.addEffect(effectData.effect, 100, {
amplifier: effectData.amplifier
});
}
}
}
}
}
}, 1);
maybe i should cache different properties more
but idk
wdym
also use ContainerSlot class
what is that used for
when player joins cache slot, component and mroe things
but all those things might change whilst they play
how can you cache a slot if the item can change and you're only supposed to store it on join
I am not saying item i am saying the slot
Check ContainerSlot in docs
its direct reference
what is the difference between that and getItem
how would i cache the slots
[container.getSlot(0...35)] ?
Item is value and slot is reference
use mainHandl slot, not numbered hotbar slot
thats under the equipment componennt
Also try avoiding loops like you do with getEntities as loops inside loops are growing with exponentital complexity
like so?
world.afterEvents.playerSpawn.subscribe(({player, initialSpawn})=>{
if (initialSpawn) {
playerCache[player.id] = {
inventory: player.getComponent("inventory").container,
selectedSlot: player.getComponent("equippable").getEquipmentSlot(EquipmentSlot.Mainhand)
}
}
})
export let playerCache = {}
is that what you meant
and then i use playerCache[player.id].selectedSlot for the heldItem variable or
map can store objects right
if yes then i was actually going to use those at 1st i was just unsure
const PLAYER_CACHE = new WeakMap();
world.afterEvents.playerSpawn.subscribe(({player, initialSpawn})=>{
if (initialSpawn) {
playerCache.set(player, {
inventory: player.getComponent("inventory").container,
selectedSlot: player.getComponent("equippable").getEquipmentSlot(EquipmentSlot.Mainhand)
});
}
})
WeakMap is better bc you don't have to care about deleting old stuff
i forgot what the difference is
example pls my brain can't understand that sentence
i already sended that example tho
wdym by using object directly as keys
also once the player is not available then it gets deleted
but in general there is not much to optimize bc you are calling nested loops anyway
well is there any other viable way i could implement what i'm trying to achieve with the current setup
There is not much you can do i guess, in theory you can add tag to effected entities and exclude them next tick and once the effect fadeout then remove that tag
but not sure if that helps much
why is my game crashing every time i open the world after i implemented this cache thing
TypeScript related:
I have gotten an issue, i want to use Class Proxy for my DataBase but i cant implement dynamic typings to it (example: new DataBase<Properties>();). I would love some help, by either saying it isnt possible or helping.
All i need is to implement T as either a index signature or a typing in another way.
I want Intellisense to show Properties of my given Interface.
This is the code i came up with :
class DataBase<T extends { [key: string]: any }> {
constructor() {
return new Proxy(this, {
get(target, prop: string | symbol, receiver: any) {
const value = new DynamicPropertyManager(prop as string).get();
try {
return JSON.parse(value as string);
} catch {
return value;
};
},
set(target, prop: string | symbol, value: any) {
let replacedValue = value
if (typeof value === "object")
replacedValue = JSON.stringify(value);
new DynamicPropertyManager(prop as string).set(replacedValue);
return true;
}
});
};
// implement either T as a index signature or T as a typing somehow
};
export { DataBase };
it occurs after i updated the heldItem variable
const heldItem = playerCache.get(player).selectedSlot ?? null;
wait could it be bc playerCache is in index and i use heldItem in another file
yea that was it
NEVERMIND, I'm way dumber than I thought, I put the lang file in the behavior and not the resources
Its all right, everyone makes mistakes
You can't detect what items can break a block right?
need help
// Event listener for chat input to trigger custom commands
world.beforeEvents.chatSend.subscribe((ev) => {
const player = ev.sender;
let message = ev.message;
// Check for custom command prefix
Object.values(customCommands).forEach(command => {
if (message.startsWith(command.prefix)) {
const commandName = message.replace(command.prefix, '').trim();
if (customCommands[commandName]) {
// Ensure command is valid before executing
try {
world.getDimension('overworld').runCommand(customCommands[commandName].action); // Or use 'world' context directly
player.sendMessage(`Executed custom command: ${commandName}`);
} catch (error) {
player.sendMessage(`Error executing command: ${error.message}`);
}
}
}
});
});```
Wrap it in system.run(() => { })
like this
system.run(() => {
world.beforeEvents.chatSend.subscribe((ev) => {
const player = ev.sender;
let message = ev.message;
// Check for custom command prefix
Object.values(customCommands).forEach(command => {
if (message.startsWith(command.prefix)) {
const commandName = message.replace(command.prefix, '').trim();
if (customCommands[commandName]) {
// Ensure command is valid before executing
try {
world.getDimension('overworld').runCommand(customCommands[commandName].action); // Or use 'world' context directly
player.sendMessage(`Executed custom command: ${commandName}`);
} catch (error) {
player.sendMessage(`Error executing command: ${error.message}`);
}
}
}
});
});
});
??
No the run command only
ok
world.beforeEvents.chatSend.subscribe((ev) => {
const player = ev.sender;
let message = ev.message;
// Check for custom command prefix
Object.values(customCommands).forEach(command => {
if (message.startsWith(command.prefix)) {
const commandName = message.replace(command.prefix, '').trim();
if (customCommands[commandName]) {
// Wrap the runCommand in system.run()
system.run(() => {
try {
player.runCommand(customCommands[commandName].action);
player.sendMessage(`Executed custom command: ${commandName}`);
} catch (error) {
player.sendMessage(`Error executing command: ${error.message}`);
}
});
}
}
});
});```
Yez²
ty
How do you check for players in an if statement
I'm making a system that detects a player killing a villager using the entityDie
how do u open any gui with script event
world.afterEvents.entityDie.subscribe(({ deadEntity, damageSource }) => {
if (damageSource.damagingEntity instanceof Player) {
}
});```
or you can just put js if (damageSource.damagingEntity.typeId === 'minecraft:player')
Where do you read your documentation I can't find the instance of part
instanceof is a JavaScript thing already it's not just a minecraft thing
Oh I haven't actually finished my JavaScript learning I just went straight up to API script hahaha
^^^^^^^^^
Server form? Do it like you always do
new ActionFormData().title('title')
.button('0')
.button('1')
.show(player)
.then(res => {
if (res.canceled) return;
console.error(res.selection);
});```
But how can we
/scriptevent gui:hi
Or smtg like that
world.afterEvents.entityDie.subscribe(({ damageSource, deadEntity }) => {
const killer = damageSource.damagingEntity;
if (killer?.typeId === 'minecraft:player') {
if (deadEntity.typeId === 'minecraft:villager') {
killer.runCommand('scoreboard players add @s Kills 1');
}
}
})
Just to make sure, the player is the one running the command or any damaging.entity
Which one is better for raw performance: Script API's native methods or tick.json and mcfunctions?
world.afterEvents.playerJoin.subscribe((ev)=>{
})
When the killer is player and killed villager the killer will run the scoreboard command
Yes that's what I want
I'm not sure who runs the command though if the player or any damaging entity
The damaging entity is the player
You defined that the player is damaging entity
since this would only run if the killer is a player
Oh okay thanks for the clarification
import { system, world } from "@minecraft/server";
system.afterEvents.scriptEventReceive.subscribe(({ id, message, sourceEntity, sourceType }) => {
console.error(id); // wiki:test
console.error(message); // Hello World
console.error(sourceEntity); // Player object
console.error(sourceType); // Entity
});
world.getPlayers().forEach((player) => {
player.runCommand("scriptevent wiki:test Hello World");
});```
how do i open a form with it?
?
Use if statement.
import { system, world } from "@minecraft/server";
system.afterEvents.scriptEventReceive.subscribe(({ id, message, sourceEntity, sourceType }) => {
if (id === 'gui:hi') {
... code ...
}
});```
ty
I never thought there are 2 types of villager in Minecraft
I don't know which one is in the game so I just put an if statement with or in it
its not opening the form
stop using AI dude
need help
^^^^^
its not opening a ui
How can I detect an entity that is within a radius of 2 blocks in front of me (which affects whoever is in the two blocks ahead and not just the second one)?
I'm not gonna help you when you keep using dogshit AI
In front or just within 2 block radius?
ok ill not
can u help please
?
in front
You can getEntitiesFromViewDirection and filter the distance within 2 meters.
Idk what you did but I tried mine and it works... And stop using ai for the millionth time
hm ill try
ty
I just said no
I already told you yesterday to not use AI
ok ill not
so 2 questions im still learning all this
- how can I divide my js files? i have everything in 1 js file
- whats wrong with this?
system.runInterval(()=>{
let property = "jay:base_shop"
for (let entity of world.getDimension("overworld").getEntities()) {
if (entity.typeId === "jay:base_shop") {
let containerType = hopper_minecart;
let inventorySize = 54;```
import "./other.js";
you forgot " "
import { system, world } from "@minecraft/server"
import "scripts/generators.js"; ```
right?
system.runInterval(() => {
for (const player of world.getPlayers()) {
for (const stand of world.getDimension('overworld').getEntities({
type: 'dekuh:star_platinum'
})) {
const view = player.getViewDirection();
const dimension = world.getDimension('overworld');
const targets = player.getEntitiesFromViewDirection({
maxDistance: 2
});
if (!stand.hasTag('punch') && stand.hasTag(`stand_of_${player.nameTag}`)) return;
stand.removeTag('punch')
for (const target of targets) {
system.run(() => {
(line 60) target.applyKnockback(view.x, view.z, 6, view.y);
target.applyDamage(15);
})
}
}
}
});
its possible apply an knockback in the detected entities?
How to get entity fieldofview?
targets is an array of EntityRaycastHits (or something like that), but pretty much it's a struct with entity and distance, so just change it to target.entity.applyKnockback
and the same for the other parts using target
or change your for loop to
for (const { entity: target } of targets)
that's an awful explanation lmao
Yo fr...
Lemme delete that and redo it. Or let others do it.. i suck at this 🙃
Can I get the total time of the world since it was created?
The implication for the following is that both scripts reside in the "scripts" folder. In that sense, ./ represents the current folder (the scripts folder).
//entry.js
import something from './file';
📂 BP
├─📂 scripts
│ ├─📄 entry.js
│ └─📄 file.js
└─📄 manifest.json
import { world } from '@minecraft/server';
world.afterEvents.entityDie.subscribe(({ damageSource, deadEntity }) => {
const killer = damageSource.damagingEntity;
if (killer?.typeId == 'minecraft:player') {
if (deadEntity.typeId == 'minecraft:villager' || deadEntity.typeId == 'minecraft:villager_v2') {
killer.runCommand("scoreboard players add @s Kills 1");
}
else if (deadEntity.typeId == 'mz:smile') {
killer.runCommand("")
}
}
})
As you can see in the else if statement I want to run a command that rides the dead entity
However the dead entity actually transforms into another entity will my command still work?
ty bro 😭
I'm not kinda sure since the entity is dead I don't think you can detect entities riding it before.
No I'm actually planning the player who killed it ride the dead entity that transformed if that was possible
You transform it to another entity?
How do you transform it? Using events in the json?
Yes
so if the dead mob is mz:smile you'll transform it to another entity and make player ride that mob?
Yes
Well the mz:smile already transformed upon death so no need for that
Probably possible but idk a way to grab the transformed entity tho...
You see I'm creating a death scene from smile where the entity enters the players mouth so I need the transformed entity grab the player who killed him and do the possession
By riding using the ride command my only problem is if still considers it as a dead entity even though it transformed
Wait is there a function to detect after an entity transforms?
you'll have to do a lil workaround:
- grab the deadEntity's location and dimension
- delay by 1 tick (system.run)
- get entities near that location (within 1 block or something) in the dimension
- filter for your new entity
- do the ride stuff
maybe this? not sure if it would work entirely https://learn.microsoft.com/en-us/minecraft/creator/scriptapi/minecraft/server/datadrivenentitytriggerafterevent?view=minecraft-bedrock-stable
Thanks
Can an if statement contain world.afterEvents.something like this?
what would go into something?
import { system, world } from "@minecraft/server"
import { system, world } from './generators';```
Hey everyone
I have a script to run that shows the title when going to a certain place in Minecraft like the Traveler's Titles mod for Minecraft Java Edition but my script only runs when going to a certain dimension in Minecraft Bedrock like Overworld, Nether and The End, I want my script to run and show the title when going to a certain biome in Minecraft Bedrock too, please help me with this script of mine and I will thank you for helping me with this script
Here is the my titles script:
Yes you can but you need to unsubscribe it after if you don't want it to keep running
I think I found a way to solve it how can I apply this:
I have a variable called
const victim = deadEntity.typeId == 'mz:smile';
And I want to use it in my command 'ride @s start_riding ${victim} teleport_rider'
Can I do that?
import { world } from '@minecraft/server';
world.afterEvents.entityDie.subscribe(({ damageSource, deadEntity }) => {
const killer = damageSource.damagingEntity;
if (killer?.typeId == 'minecraft:player') {
if (deadEntity.typeId == 'minecraft:villager' || deadEntity.typeId == 'minecraft:villager_v2') {
killer.runCommand("scoreboard players add @s Kills 1");
}
else if (deadEntity.typeId == 'mz:smile' && killer.hasTag('survivor')) {
deadEntity.runCommand('event entity @s bridge:dead')
}
else {
deadEntity.runCommand('event entity @s bridge:fake_death')
killer.runCommand('ride @s start_riding ${deadEntity.id}' teleport_rider)
}
}
})
Is there a more flexible way than using ride command?
Cause my ride command doesn't work
rpg item
That's amazing. The use of display entities is mindblowing. Such a great way to demonstrate Script-API in Bedrock Editions.
( My sarcasm was not obvious enough apparently )
I thought my phone was broken cause it keeps becoming black... But God dam it looks so good
is there a way to get data like this
it's java mythic items code lol
that is NOT bedorck 🙏
I don't think your question is clear.
sorry ur right i want to get all the entitys in a radius but it keeps returning unexpected argument expected string
i think its for the dimension
very late reply
Kind of, with async functions and an entity with an environment sensor. But it won't get all types of biomes due to limitations.
These are the only biomes that can be detected
try this
const entities = source.dimension.getEntities({ //... })
it worked thanks
nvm i forgot about has_biome_tag
import { world, EntityRideableComponent } from '@minecraft/server';
world.afterEvents.entityDie.subscribe(({ damageSource, deadEntity }) => {
const killer = damageSource.damagingEntity;
if (killer?.typeId === 'minecraft:player') {
if (deadEntity.typeId === 'minecraft:villager' || deadEntity.typeId === 'minecraft:villager_v2') {
killer.runCommand("scoreboard players add @s Kills 1");
} else if (deadEntity.typeId === 'mz:smile' && killer.hasTag('survivor')) {
deadEntity.runCommand('event entity @s bridge:dead');
} else {
deadEntity.runCommand('event entity @s bridge:fake_death');
const rideableComponent = deadEntity.getComponent('minecraft:rideable');
if (rideableComponent) {
rideableComponent.addRider(killer);
}
}
}
});
So in this code ones the player kills the entity smile it triggers an event called fake death that adds the rideable component to the entity smile then force the killer to ride the entity smile but it doesn't work I tried debugging it in the debugging playground it says no errors, does anyone know what is the problem?
Because the mz:smile is dead when the entity transformed you changed it's entity you need to find a way to grab the entity or id of the transformed entity
No I actually changed that now the entity doesn't transform so it's still the same entity
It just adds a rideable component when it dies
What does the event do?
This one
You can't ride a dead entity.
Hmm the entity doesn't actually die cause I added a minimum value to it's health so no matter what I do either use a kill command it doesn't die
But this is actually interesting I'll try and use the ride command manually in the game if it works
Then that's the thing the function didn't trigger because it didn't die.
The function actually triggered cause the event rideable component got added
That's cool I guess... I'm actually kinda confused about what you are trying to do
Wait I tried the ride command and it works so I think the reason is the timing? I'm not sure
I'm sending a photo right now so you can see what I'm trying to do
world.afterEvents.entityDie.subscribe(({ damageSource: { damagingEntity }, deadEntity }) => {
if (damagingEntity.typeId !== 'minecraft:player) return;
if (deadEntity.typeId === 'minecraft:villager_v2') {
world.scoreboard.getObjective('kills').addScore(damaginEntity, 1);
} else if (deadEntity.typeId === 'mz:smile' && damagingEntity.hasTag('survivor')) {
deadEntity.triggerEvent('bridge:dead');
} else {
deadEntity.triggerEvent('bridge:fake_death');
const ride = deadEntity.getComponent('rideable');
system.runTimeout(() => {
ride?.addRider(damaginEntity);
}, 2);
}
});```
Okay I'll try this
That looks like the creature in that movie called Smile
Yeah I'm recreating the thing where the entity enters the players mouth
Very interesting.
You would import whatever variables, functions, or classes you have defined inside something.js.
You might split up functions you commonly use into their own file so you may import them later.
//something.js
function doOperation(arg) {
return arg;
}
export doOperation;
//entry.js
import {world, system} from '@minecraft/server';
import {doOperation} from './something';
var foo = "hi";
console.warn(doOperation(foo)); //[Script][warning] hi
It doesn't work why?
A typo 'minecraft:player —> 'minecraft:player'
missing '
No already fixed all of that wait I'll update the original code
import { world, system } from '@minecraft/server';
world.afterEvents.entityDie.subscribe(({ damageSource: { damagingEntity }, deadEntity }) => {
if (!damagingEntity || damagingEntity.typeId !== 'minecraft:player') return;
if (deadEntity.typeId === 'minecraft:villager_v2') {
world.scoreboard.getObjective('Kills').addScore(damagingEntity, 1);
} else if (deadEntity.typeId === 'mz:smile' && damagingEntity.hasTag('survivor')) {
deadEntity.triggerEvent('bridge:dead');
} else {
deadEntity.triggerEvent('bridge:fake_death');
const ride = deadEntity.getComponent('minecraft:rideable');
if (ride) {
system.runTimeout(() => {
ride.addRider(damagingEntity);
}, 2);
}
}
});
equivalent to forceShow or smt:
async function forceRide(ride,rider,maxAttempt=200) {
const rideable = ride.getComponent("rideable");
if (!rideable) return;
while (maxAttempt--) {
if (rideable.addRider(rider)) return true;
async system.waitTicks(1);
}
return false
}
How do I get the item (item stack) from the dropped item (entity)?
maybe rearrange the if statements.
if (deadEntity.typeId == "mz:smile")
if (damagingEntity.hasTag("survivor")) {
//missed ")" here xd
//do for survivor
} else {
//do for non-survivor
}
or maybe force Ride using this function? #1067535608660107284 message
entity.getComponent('item').itemStack
Ty
np
You forgot the second closing parenthesis
"nah :D"
-# last word before admitting the mistake
Oh, you mean, that, yey
Is the minecraft:arrow hardcoded to be destroyed upon hitting an entity? I tried putting true on stopOnHit EntityProjectileComponent.
If true, the projectile will stop moving when an entity is hit as though it had been blocked. E.g. Thrown trident on hit behavior.
The arrow disappeared regardless. 😔
( Using it inside entitySpawn )
Likely.
stopOnHit does not cancel destroyOnHit. So here you just need to change the json
damn, JSON takes priority
Or just create a custom arrow
I would prefer it to be on all server-side. Luckily, arrow is spawnable with entitySpawn, so I'll just recreate it... though, It would lack the original damage affected by enchants.
Étés vous influanceur ???
We don't care what language you speak. You need to write in English here.
Do you expect people to keep copy-paste your message on Google Translate? Lol.
-# (Ik there's a bot to translate here)
Translation tools are also not 100% accurate. So English is always preferred.
Well... He is one of the nicest people here, The fact that he asked you to speak in English so he can understand and help you should tell you something
is there a way to have a score act as a players health?
and that is not nice to say...
so if the score is 60 they would have 60 health
yeah
how can i do it?
get the health component and set it as score
need help with that?
system.runInterval(() => {
for (let player of world.getAllPlayers()) {
let hp = player.getComponent("health")
world.scoreboard.getObjective("health").setScore(player, hp.currentValues)
}
})
yh pls
-# I mean, it would suck if I have a 69 score as hp, and my hp doesn't go higher or lower than 69 regardless of how many heals or damage taken.
check Serty reply
huh...
ty
@granite badger
just use microsoft docs 🤪
It's time to go to the dark side)) That is, Microsoft documentation
i use both
just prefer his layout
If that's the dark side, my whole code is a whole pantheon of the dark factions
welp they are almost the same
i only use his "Diff Minecraft Script API" page
I've got a generic "string_property" function and I'm trying to figure out what value to pass to check an entity's nameTag property. I know the Entity class has a nameTag property, but I'd rather not have special logic to handle just that property. Is there a corresponding string value that works for the entity.getproperty() method? I've tried "nameTag" and "name" and "nametag", all of which have come back as undefined.
if the entity doesn't have a nameTag the nameTag property is undefined
i get this error
import { system, world } from "@minecraft/server";
system.runInterval(() => {
for (let player of world.getAllPlayers()) {
let hp = player.getComponent("health")
world.scoreboard.getObjective("health").setScore(player, hp.currentValues)
}
})```
let hp = player.getComponent("health").currentValue
hp.currentValue
wait so what do i change?
hp.currentValues to hp.currentValue
its working now but how do i make it so if the score is 60 the player then has 60 health?
yes, 60hp is 30 hearts
Please use https://jaylydev.github.io/scriptapi-docs/preview instead, I've removed the 1.xx.xx.xx ones since they're the same anyway and they don't get archived like the retail docs
I'm still experimenting with the new typedoc and vuepress layout cuz it looks nice, so some drastic changes might happen
I like the new ui designs you added....
it doesnt let me set the score to 60 it changes it to the health i am
So do you need to manipulate your health through the scoreboard?
yh
so if the players score is 100 they have 100 health and damage done to them will be taken off the scoreboard and not their actual health
import { system, world } from "@minecraft/server";
system.runInterval(() => {
for (let player of world.getAllPlayers()) {
let hp = player.getComponent("health")
hp.setCurrentValue(world.scoreboard.getObjective("health").getScore(player) || hp.currentValue)
}
})
world.afterEvents.entityHealthChanged.subscribe(data => {
let player = data.entity
if (player.typeId == "minecraft:player") {
world.scoreboard.getObjective("health").setScore(player, data.newValue)
}
})
True, I’m not sure about the regeneration of the player’s hearts, since health is not an integer, due to such manipulations, health will always be rounded up and is unlikely to be able to regenerate
you may want to update the link here
Could I not just make a function detecting if the player has above 20 score and give then regen until it’s below the score
Why do you even need to manipulate your health through the scoreboard?
for a addon im making so i can make different things give you different health
Just use the health component or, as a last resort, create functions if you are too lazy to write
this works but how can i make it so the damage gets taken off the score?
What's the best way to detect if the item you used to hit an entity decreased it's durability?
yall im wondering how i can check for the blocks around the player
in commands its execute if block ~~~
i can get the player pos
but idk how to test for the blocks around the player
This how you detect blocks. There's also getBlocks() but you need to import BlockVolume in the server
import { BlockVolume } from '@minecraft/server';
// one block
player.dimension.getBlock({ x: 0, y: 0, z: 0 });
// multiple blocks
const locations = player.dimension.getBlocks(new BlockVolume({ x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0 }), true);
// get all locations inside of the volumes you used in *getBlocks()*
for (const location of locations.getBlockLocationIterator()) {
cosnt block = player.dimension.getBlock(location);
}
^
-# bumping this
Custom components.
Sadly the item is not custom... It's vanilla
run interval is the only option
Would that also detect items going into hoppers tho?
i saw this saw late
-_- this is what i tried to do
yes ik im very dumb
Just do player.dimension.getBlock(player.location)?.typeId === 'minecraft:water'
Tho there's player.isInWater function too
Hmm it kinda sucks but meh it's simple
How can I force show form using custom chat commands beacuse when I tried normally it wouldnt close my chat
you cant close chat
what force show form does is wait for player to close chat, then open form
you can maybe damage the player so it closes?
how?
nvm
👍
I forgor uiManager was not that far yet
So there is no easy option for player to open form using chat commands?
Cant close chat. But you can keep sending form.show() until the player successfully opens it.
Omg
I never realised that the java redstone resource pack was so easily created (scuffed)
system.runInterval(() => {
world.getAllPlayers().forEach((e) => {
const redstoneGrab = e.getBlockFromViewDirection().block.getRedstonePower();
const blockGrab = e.getBlockFromViewDirection().block.typeId.slice(10).replace("_", " ").toUpperCase();
if (redstoneGrab == undefined) e.onScreenDisplay.setActionBar();
e.onScreenDisplay.setActionBar(`§c${blockGrab}: §f${redstoneGrab}`);
})
})
its like 10 lines of code
and itll say the name of the redstone component ur looking at, and its output power
i made it for my redstone friend
I mean, in java you can easily assign textures to each blockstate.
Yeah, idk if that's possible in bedrock tho
so here's the bedrock equivalent
Will adding too many dynamicProperties cause lag? I'm sure they're probably less laggy than scores, but I want to make sure
Thanks it works it just have to do with timing issues
Is it possible to remove the hearts of the entity your riding or make it invisible so it doesn't show?
adding many dp won't lag but setting values on different dp in single tick will definitely cause tps drop
Idk abt removing them, but with resource packs, you can make them invisible
Though you might be able to change onScreenDisplay
Oh don't worry, I only use them for cooldowns and other stuff. I don't ever update them on a runInterval
so I don't have to stress about it?
Nah
Bet, thanks
no comments on this..
-# not allowed here
I may have good news
just spend the 30 bucks tbh, it's pretty hard to find other versions that are safe and up to date
plus you get java with it
yeah, but my laptop holds 50GB
and 25 is taken up thru system files
one last quick question
I need to finally learn how this works
So it would be
setHudVisibility(false,
then how do I call on a hudElements?
setHudVisibility(false, {[HorseHealth]})
?
player.onScreenDisplay.setHudVisibility(0, [ 0, 1 ])```
[0, 1]?
It's number
the number of?
Oh, I thought I called on the name of the element
Even if you're gonna hide only one... It's still array
player.onScreenDisplay.setHudVisibility(0, [ 10 ])```
Ohh, it's an array
I see
Thanks
well, there's ur answer
player.onScreenDisplay.setHudVisibility(0, [10]);
That should hide (0), the HorseHealth (10)
You can hide it thru words just need to import that in the server.
import { HudVisibility, HudElement } from '@minecraft/server';
player.onScreenDisplay.setHudVisibility(HudVisibility.Hide, [ HudElement.HorseHealth ])```
Ah, I see
fun
Is that permanent
or is that something I need to run on an interval?
not permanent*, but toggled
Kinda? If you want it to make it "permanent" run it every tick because player can bring back the visibility
They can bring it back?
Can I pick a certain entity for this or all the entity that I will be riding will have invisible hearts cause I only want it for a specific entity
just getting off and on the horse?
Oh sure
What do u wanna pcik?
someone with a certain tag?
I'll write a code for u rq
I have a custom entity called smile
ah, what's ur caller for it?
mz:smile
custom:smile
oh alr
well, I'm dumb with custom entities
but I gotcha
one sec
is custom smile the one riding the horse?
or the entity ur riding?
I'm riding the custom smile
does it have horseHearts?
Yes
It has 500 health
ho dang
Well it is actually a boss based on the smile horror movie where the smile entity makes you think you beat him but then suddenly it is actually alive and that is where the smile entity forces you to ride and that is where I create the scene where it enters the players mouth
This is what it looks like
oh, creepy
I watched Smile 2
I have an entire theory now
it's a dumb theory
but it's a theory nonetheless
import { world, system } from '@minecraft/server';
system.runInterval(() => {
const overworld = world.getDimension('overworld');
for (const entity of overworld.getEntities({ type: 'mz:smile' })) {
const rideable = entity.getComponent('rideable');
if (rideable) {
const riders = rideable.getRiders();
for (const rider of riders) {
if (rider.typeId === 'minecraft:player') {
rider.onScreenDisplay.setHudVisibility(0, [ 10 ]);
}
}
}
}
for (const player of world.getPlayers()) {
let isRiding = false;
for (const entity of overworld.getEntities({ type: 'mz:smile' })) {
const rideable = entity.getComponent('rideable');
if (rideable && rideable.getRiders().some(rider => rider.id === player.id)) {
isRiding = true;
break;
}
}
if (!isRiding) {
player.onScreenDisplay.setHudVisibility(1, [ 10 ]);
}
}
});
Wait this is it?
I never thought you can control HUD visibility through scripts
When riding the mz:smile remove the horse health to the player otherwise bring the visibility back
alr
oh dang, he did it for me
his script is better too
Thanks for helping @distant gulch and @dim tusk
mine was innaccurate
mine was just
system.runInterval((e) => {
world.getAllPlayers().forEach((player) => {
if (player.getComponent("minecraft:riding") == true) {
const smileGrab = player.dimension.getEntities({ location: { x: player.location.x, y: player.location.y - 1, z: player.location.z }, maxDistance: 1 })
if (smileGrab.length == 0) return;
player.onScreenDisplay.setHudVisibility(0, [10]);
}
})
})
lol
Thanks @dim tusk
idk if mine even works
it's trial and error lol
oh wait
I forgot to detect if the entity wa mz:smile
its alr
Same
I'm in the process of learning script
my biggest scrip is only 250 lines, and it's broken up between multiple tests
import { system, world } from '@minecraft/server';
system.runInterval(() => {
for (const player of world.getPlayers()) {
const entities = player.dimension.getEntities({ type: 'mz:smile' });
let isRiding = false;
for (const entity of entities) {
const rideable = entity.getComponent('rideable');
if (rideable & rideable.getRiders().some(rider => rider.id === player.id)) {
isRiding = true;
break;
}
}
if (isRiding) {
player.sendMessage("riding 'mz:smile'");
} else {
player.sendMessage("not riding 'mz:smile'.");
}
}
});
Smaller version
Well I'm learning too all I know is how to use world.after and before events you know the simple stuffs
I hate beforeEvents
I gotta use async
Who said need that?
And also it does make sense you're modifying it before it happens
U don't? When I try running scripts, it doesn't work in many cases
I usually need to do a runCommand("tag @s")
just use system.run
runCommandAsync
then I run an entire system.runInterval to take the person with a tag
then remove the tag
it's a hassle
system.run(() => {
player.addTag('test');
});```
It works like that?
I was trying to detect when an item was picked up
You're probably doing it wrong.
then do stuff to the person who picked it up
There's no accurate way doing the pick-up and drop one
was yes, but I managed a scuffed way
the issue was that none of my script worked in the beforeEvents category
I had to runAsynch
Async
and use a seperate system.runInterval
and those with that tag
it wouldn't even let me run a dynamicProperties script
it was on an entityRemove
world.beforeEvents.itemUse.subscribe(({ itemStack, source }) => {
if (itemStack.typeId === 'minecraft:stick') {
system.run(() => {
source.addTag('test');
system.runTimeout(() => {
source.removeTag('test');
}, 2); // remove after two ticks
});
}
});```
Kinda like this.
You can't modify world in before events, so you run it in next tick
As long as it runs it different tick it's good, you can use interval, run, runtInterval
dope,thanks a lot!
also, system.run is the same as system.runTimeout with no tick delay right?
world.beforeEvents.entityRemove.subscribe(({
removedEntity
}) => {
if (removedEntity.hasComponent('item')) {
const player = removedEntity.dimension.getPlayers({
location: {
x: removedEntity.location.x - 1.5,
y: removedEntity.location.y - 0.5,
z: removedEntity.location.z - 1.5
},
volume: {
x: 3,
y: 2,
z: 3
}
})[0];
if (player) {
const itemComponent = removedEntity.getComponent('item');
const itemStack = itemComponent.itemStack;
const typeId = itemStack.typeId;
const amount = itemStack.amount;
const nameTag = itemStack.nameTag || '';
const lore = itemStack.getLore() || [];
const durability = itemStack.getComponent('durability')?.damage || 0;
const enchantments = itemStack.getComponent('enchantable')?.getEnchantments() || [];
const cooldown = itemStack.getComponent('cooldown');
const cooldownCategory = cooldown?.cooldownCategory || '';
const cooldownTicks = cooldown?.cooldownTicks || 0;
if (
itemInventory(
player, typeId, amount, nameTag, lore,
durability, enchantments, cooldownCategory, cooldownTicks
)
) {
console.error(`${player.name} picked up ${typeId}x${amount}`);
} else {
console.error(`Item ${typeId}x${amount} not found in ${player.name}'s inventory.`);
}
}
}
});
function itemInventory(player, typeId, amount, nameTag, lore, durability, enchantments, cooldownCategory, cooldownTicks) {
const inventory = player.getComponent('inventory').container;
for (let i = 0; i < inventory.size; i++) {
const slot = inventory.getItem(i);
if (slot) {
const slotDurability = slot.getComponent('durability')?.damage || 0;
const slotEnchantments = slot.getComponent('enchantable')?.getEnchantments() || [];
const slotCooldown = slot.getComponent('cooldown');
const slotCooldownCategory = slotCooldown?.cooldownCategory || '';
const slotCooldownTicks = slotCooldown?.cooldownTicks || 0;
if (
slot.typeId === typeId &&
slot.amount >= amount &&
(nameTag === '' || slot.nameTag === nameTag) &&
(lore.length === 0 || JSON.stringify(slot.getLore()) === JSON.stringify(lore)) &&
slotDurability === durability &&
JSON.stringify(slotEnchantments) === JSON.stringify(enchantments) &&
slotCooldownCategory === cooldownCategory &&
slotCooldownTicks === cooldownTicks
) {
return true;
}
}
}
return false;
}```
I made this pick up detection but not very accurate...
ah, I see
Mine was much less accurate
I also didn't detect WHAT they picked up
just if they were closest player within 2 blocks, had the item that was removed, and then I gave em an effect or whatnot
That's would be inaccurate as hell
It's kinda useless then.
Well it wouldn't be too hard to check for id name
I just didn't
generators.js
function doOperation(arg) {
return arg;
}```
```js
StarterTests.js
import { system, world } from "@minecraft/server"
import {world, system} from '@minecraft/server';
import {doOperation} from './generators';
system.runInterval(()=>{
let property = "jay:base_shop"
for (let entity of world.getDimension("overworld").getEntities()) {
if (entity.typeId === "jay:base_shop") {
containerType = hopper_minecart;
inventorySize = 54;
}
}
}
)```
You imported the server TWICE
export function doOperation(arg) {
return arg;
}```
Yeah, just the system, world from "@minecraft/server";
at least that
Add the export cause you're calling the function from another file into another file.
okay I have a different error now but thats bc my scripts wrong
but my generators is not running anymore
generators.js
export function doOperation(arg) {
system.runInterval(()=>{
let property = "jay:diamondgen"
for (let entity of world.getDimension("overworld").getEntities()) {
if (entity.typeId === "jay:diamondgen") {
let dynamicProperty = entity.getDynamicProperty(property);
if (dynamicProperty === undefined) {
entity.setDynamicProperty(property, 30)
} else
if (dynamicProperty > 0) {
entity.setDynamicProperty(property, dynamicProperty - 1);
} else
if (dynamicProperty <= 0) {
entity.setDynamicProperty(property, 30);
entity.runCommandAsync("execute @s ~~~ function game/gens/diamond");
}
entity.nameTag = `§bDiamond Generator\n§7[ §fSpawns in §c${entity.getDynamicProperty(property)}§fs §7]`
}
}
}, 20)
system.runInterval(()=>{
let property = "jay:emeraldgen"
for (let entity of world.getDimension("overworld").getEntities()) {
if (entity.typeId === "jay:emeraldgen") {
let dynamicProperty = entity.getDynamicProperty(property);
if (dynamicProperty === undefined) {
entity.setDynamicProperty(property, 65)
} else
if (dynamicProperty > 0) {
entity.setDynamicProperty(property, dynamicProperty - 1);
} else
if (dynamicProperty <= 0) {
entity.setDynamicProperty(property, 65);
entity.runCommandAsync("execute @s ~~~ function game/gens/emerald");
}
entity.nameTag = `§2Emerald Generator\n§7[ §fSpawns in §c${entity.getDynamicProperty(property)}§fs §7]`
}
}
}, 20)
return arg;
}```
What would be the maximum script file I can run in one addon?
Cause I have like 5 different scripts running
Your device's RAM and storage are the limit.
Oh okay
can you not run setPermutation in onStepOn in a custom block component?
It should run
hmm
is this correct?
block.setPermutation(BlockPermutation.resolve("sci:redstone_ore_mini", { "sci:poweredState": true }));
What's your block's collision values?
Looks like it, yes.
Any error?
Have you tried breaking and placing back the block :D
it says:
[Scripting][error]-TypeError: not a function at onStepOn (mBFramework.js:231)
just did, same message
What's the code on that line?
this one
onStepOn(evd) {
const block = evd.block;
block.setPermutation()
}
console.log() is saying nothing
this is the whole event
const source = data.entity.typeId;
const block = data.block?.typeId;
if (source === "minecraft:player" && block === "sci:redstone_ore_mini") {
world.sendMessage("This works now");
block.setPermutation(BlockPermutation.resolve("sci:redstone_ore_mini", { "sci:poweredState": true }));
console.log(error)
}
}```
the sendMessage works
wait
block is a string there
did console log wrong
you put .typeId extracting the block identifier.
it says the same without the ?
When .setPermutation is called, is being run on a string, not block class
const block = data.block;
if ( block?.typeId == "identifier") {
block.setPermutation()
}
- move the
?.typeIdinto the if expression
onStepOn: (data) => {
const source = data.entity.typeId;
const block = data.block;
if (source === "minecraft:player" && block?.typeId === "sci:redstone_ore_mini") {
world.sendMessage("This works now");
block.setPermutation(BlockPermutation.resolve("sci:redstone_ore_mini", { "sci:poweredState": true }));
console.log(error)
}
}```
oddly enough that does apply A state, but not THE state i wanted it to use
it set it to the default state instead but did not apply true to the sci:poweredState
You should do withPermutation.
im not seeing anything like that on the docs, do you mean withState?
Yeah.
ah
block.setPermutation(block.permutation.withState("sci:poweredState", true));
i tried this and the console is saying nothing, nor are any of the states changing
console.warn(JSON.stringify(block.permutation.getAllStates())) run this and it'll show each block states
Check if the block state name is correct, or if it indeed affected.
Or you could check if there are things that cause it to revert.
so:
i changed the line so it looks like this: block.setPermutation(block.permutation.withState("sci:poweredState", false).withState("sci:poweredState", true));
and according to this code you sent, it is being affected and correctly setting the state to true. however, the block is not physically being affected
Is it supposed to change the texture or smt?
it is supposed to change the "minecraft:light_emission" to 5
"permutations": [
{
"condition": "q.block_state('sci:poweredState') == true",
"components": {
"minecraft:light_emission": 5
}
}
]```
hmm... Maybe leave and join the world? Cause custom component often needs that
ok
its saying the same thing
should i add a second permutation for when its false?
what error now
sorry, its not saying anything apart from the console.warn you sent
i worded it poorly
"description": {
"identifier": "namespace:name",
"states": {
"namespace:booleans": [false, true]
}
}
Did u set it like that?
yes
"format_version": "1.21.20",
"minecraft:block": {
"description": {
"identifier": "sci:redstone_ore_mini",
"menu_category": {
"category": "nature",
"group": "itemGroup.name.ore"
},
"states": {
"sci:poweredState": [false, true]
}```
So the block has the block state. But it doesn't glow? bruh.
thats what im sayin
it does work with this though: https://discord.com/channels/523663022053392405/1298442196558807091
when i power it with redstone it glows
Try using command: /setblock?
manually setting it
oh
you're using that... well now it make sense
The Redstone event constructor thingy, it overrides the states back to false.
Either you find a way to integrate it with it, or avoid using it
I have a solution. Maybe a new state specifically for onStep, then in permutation, you could use the OR operator || along with the redstone event's block state
(You just need to set it back to false on stepping off)
i considered that, i was just hoping to use the existing permutation
my performance is going through the floor
this block will now have 1,152 permutations
That's crazy
(unless i figure out a way to integrate it)
Why mojang does not allow using c++ for moding in mc worlds
how do you import a function from another script, but not the entire script with it?
nvm lol, I got it runninng
nvm again, bc now it's running my entire script, and not just the function
When you're importing a function from amother script it will run the entore script.
well thats's kinda dumb
I like to have things organized, and ready for use, but not always active
I could also use dynamicProperties to add and remove scripts
but I don't want to make 50 js files, or multiple addons lol
I like to keep organized, but not that organized
What's your use case that you can't have things run when imported?
Well I can have them run
If your files are just exported function you're fine.
but I'd rather not
I'd like to be ableto pick and choose which functions are running while I'm testing
I have 2 scripts in my redstone.js
one tells me the redstone power when I right click the redstone component
one just tells me constantly in the actionbar
Sometimes I only want one of them working, and I don't want to separate them by js files
Curse you minecraft!
That seems like an easy fix? Make it an export function and call it in syste.run in your main file?
Im failing to see an issue.
I thought you said it would run the entire script file?
Things like console.log yeah?
But not functions since they need to be called.
One sec
lemme show u my setup rq
world.afterEvents.itemUse.subscribe((e) => {
if (e.itemStack.typeId == "minecraft:leather") {
const redstoneGrab = e.source.getBlockFromViewDirection({ maxDistance: 25 }).block.getRedstonePower();
const blockGrab = e.source.getBlockFromViewDirection({ maxDistance: 25 }).block.typeId.slice(10).replace("_", " ").toUpperCase();
if (redstoneGrab == undefined) {
e.source.sendMessage("Invalid Block");
return;
}
else if (redstoneGrab == 0) {
e.source.sendMessage(`§c${blockGrab} §fhas no redstone signal`);
return;
}
else if (redstoneGrab > 0) {
e.source.sendMessage(`§c${blockGrab}: §f${redstoneGrab}`);
return;
}
}
});
export function redstoneWatch() {
system.runInterval(() => {
world.getAllPlayers().forEach((e) => {
const redstoneGrab = e.getBlockFromViewDirection({ maxDistance: 25 }).block.getRedstonePower();
const blockGrab = e.getBlockFromViewDirection({ maxDistance: 25 }).block.typeId.slice(10).replace("_", " ").toUpperCase();
if (redstoneGrab == undefined) {
e.onScreenDisplay.setActionBar("");
return;
}
e.onScreenDisplay.setActionBar(`§c${blockGrab}: §f${redstoneGrab}`);
})
})
}
this is in my redstone file
this is my main file
import { world, system } from "@minecraft/server";
import { redstoneWatch } from './productivity.js';
redstoneWatch();
But instead of JUST running that function
it runs my entire redstone file
Again...it ran the entire file which is expected.
I'm confused on your suggestion then
Im...not seeing the issue? Just make your event subscriber a function?
Yes then call it in your main.
Because...once again...it is running the subscribe event.
how could I call random tick in onstepoff
Is there a costume crafting table addon for public reuse?
Here you are
love it
is it possible to query the number of hunger bars of the player in any way?
no
why is the typeid of inBlock not fence_gate?
const distance = 1;
const checkAbove = false;
const checkBelow = false;
const checkIn = true;
const checkBack = true;
const checkPath = true;
world.beforeEvents.entityRemove.subscribe(({ removedEntity }) => {
if (removedEntity.typeId === "minecraft:ender_pearl") {
let owner = removedEntity.getComponent("projectile").owner;
const ownerLoc = JSON.parse(JSON.stringify(owner.location));
const { location, dimension } = removedEntity;
let { x: vx, y: vy, z: vz } = removedEntity.getVelocity();
const loggedLoc = {x: Math.floor(location.x), y: Math.floor(location.y), z: Math.floor(location.z)}
const loggedOwnerLoc = {x: Math.floor(ownerLoc.x), y: Math.floor(ownerLoc.y), z: Math.floor(ownerLoc.z)}
DataUtils.addLog(owner.id,createPearlLog(loggedLoc,loggedOwnerLoc,_isPlayerInDisallowedArea_2(owner,{x: location.x, y: location.y, z: location.z}).areaName,Date.now()),"pearl")
const magnitude = Math.sqrt(vx ** 2 + vy ** 2 + vz ** 2);
const direction = {
x: vx / magnitude,
y: vy / magnitude,
z: vz / magnitude,
};
const backPosition = {
x: location.x - distance * direction.x,
y: location.y - distance * direction.y,
z: location.z - distance * direction.z,
};
const inBlock = dimension.getBlock(location);
Frozen.Info(inBlock.typeId)
const backBlock = dimension.getBlock(backPosition);
const aboveBlock = dimension.getBlock(location).above(1);
const belowBlock = dimension.getBlock(location).below(1);
let path = pearlMap.get(removedEntity.id);
path = (path || "") + "" + dimension.getBlock(location).typeId;
if ((checkPath && path && (path.replace(/minecraft:air/g, '') !== "")) ||
(checkBack && backBlock.isSolid()) ||
(checkBelow && belowBlock.isSolid()) ||
(checkAbove && aboveBlock.isSolid()) ||
(checkIn && inBlock.isSolid())) {
system.run(() => {
owner.teleport(ownerLoc);
pearlMap.delete(removedEntity.id)
});
} else {
Frozen.Info("Teleport condition was not met");
}
}
});
Like an add-on with a costume crafting bench that doesn't have copyright
Probably because the projectile hits the fence and its last location is right in front of the fence. Try to extend his path forward by 1 block
or use the projectileHitBlock event
?
❌ Custom
✅ Costume
guys need help
i pushed out update to my scripts but i forgot what change i made and now it keeps crashin
😭
Remember or look at all of runIntervals
They are usually causing crashes
no i know it's not that
it just suddenly crashed when i reloaded
i tried removing the update i was aware of, still crashes
did ctrl+z on every other file to find changes, nothing
okay idk what it was but i removed something ???? and now it works
i cant even remember what it was that i undid
If by allow you mean add official support, it's because it's not crossplatform friendly
found out what it was, i tried to import a function from main file in another
hello I have a little question where you would have and the dev logs of the minecraft api I'm looking for it but I can't find it thank you
debugger or documentation and updates?
frankly the updates what we change in the api
You can checkout the changelogs or https://stirante.com/script/diff.html
th man
where do start in making a costume crating table?
well a costom container first Ig
ty
how do I stop listening for an event? I'm making events and want them to be as performance-friendly as possible.
#1067869374410657962
unsubscribe
ty
thank you!
How i give an item to the player
const item = player.getComponent("inventory").container.addItem(itemstack)
if(item) player.dimension.spawnItem(item,player.location)
How i made them locked or keep on death?
itemstack.keepOnDeath = true
itemstack.lockMode = "inventory"
I tested lock mode on debug playground but it always send errors to it
what error
#debug-playground
try in game
🤷♂️
it's safer to use API's enums instead strings probably
some typing feature if you're using TypeScript
otherwise idk xd
JSON-UI
🥲
but I think this might be impossible if it's default crafting table
you better ask on #1067876857103536159 or #1067869374410657962 (if it's related to UI)
can be visually deleted, it is still there but you can't interact with it
is there a limit on dynamic properties?
cuz so far i havent reached no problems
@distant tulip do u know?
each property can store 32k bit
the keys too have 32k bit limit, but you will never ever reach that
Yo thanks
i asked chat gpt it said max each entity can have is 16
i was like Huhhhh
consult the experts 😭
It is "custom"
"Costume" are those cosplay clothing
lol, it is 2^32,768 if i am not wrong
assuming each char is 2 bit
A string property can hold 32767 characters max
Might've been referring to entity properties, not entity dynamic properties
i thought it is bits not characters...?
nop
welp, my bad
😭
that would have made sense
How do I work player.getComponent?
if I want to get "is_riding", it's not letting me know
I tried
if (player.getComponent("minecraft:is_riding') == true) {
}
but it didn't work
you have two different quotation marks
also I don't think that's a valid component
at all
none of that is valid
lmao
That component doesn't exist.
Do you mean "rideable"
Use .getRiders() (Array of Entities) and check each Entity.id if your player.id is in it
getComponent("rideable") should be called on your ride
JSON component and Script API Component are different
They must mean this component
https://learn.microsoft.com/en-us/minecraft/creator/scriptapi/minecraft/server/entityridingcomponent?view=minecraft-bedrock-stable
So this should be
if (player.getComponent("riding")) {
...
}
oki
Nah, I got it figured it. It's "minecraft:riding"
Thanks tho
I had the correct quotations in the script.
The only thing that wasn't valid was the component.
Instead of "minecraft:is_riding", it was simply "minecraft:riding"
Is there a smoother way to teleport? In my case, I can't use knockback or impulse. I want to teleport something forward at a constant rate. Is teleport the best option?
Teleporting can work fine if you implemented your own acceleration and velocity.
function setdynamicpropety(unique, entity) {
if (!entity) return;
entity?.setDynamicProperty("defense", 100);
}
TypeError: not a function
Someone help
that's what I thought too
Yeah, my thing was weird abt that too. I'm not sure.
Also, I'm making a custom PvP system using dynamicProperties too lol.
I feel ur pain bc my script looked almost exactly like that
Also, does anyone know how I can get the typeId for the item in a players mainhand?
nvm I figured it out lol
Maybe
Two ways...
// first way
const equip = player.getComponent('equippable');
const mainhand = equip.getEquipment('Mainhand');
console.error(mainhand?.typeId);
// second way
const inventory = player.getComponent('inventory').container;
const mainhand = inventory.getItem(player.selectedSlotIndex);
console.error(mainhand?.typeId);
Yeah I figured it out when I realized there was a selectedSlotIndex lol
Thanks for answering tho, rlly helps out
I needed it for making a braking system for my car
I've installed NPM typings but my intellisense still isnt working. Any ideas?
Show the command you used to install.
npm i @minecraft/server
I've tried doing it locally in ide console and globally on my pc
Gotcha, so that command (npm i @minecraft/server) installs the module locally within your project, not globally on your PC..
When installed locally, you’ll see a node_modules folder in your project directory containing the module. If you're using TypeScript, you'll need a tsconfig.json file to tell TypeScript where to find the types (e.g., using typeRoots or including node_modules/@types).
For plain JavaScript, the IDE might not recognize the types by default unless you install the module globally (npm install -g @minecraft/server).
Ahh i see i will have a look at my files when I'm home and I'll let you know if I get it figured out 👍
Thank you though, that was information I wasn't aware of 😅
I should extend my response by saying that the command you used installs them locally based on the path you are currently located in when using your terminal. So ideally, if in the directory of your project then that is where they will be installed.
I just found it odd as I had it working fine on vsc but when I switched to vs2022 it wouldn't work so it is most likely that
we can set selectedSlotIndex?
✅
i always thought it is read-only
That's how it was when it was in an experimental state
oh
good to know
I've tried this and it still doesn't work. I have the node_modules folder and I tried installing globally with -g but its still not being recognised.
vs2022 may not detect global paths automatically like vsc does.
Command:
npm root -g
That will show the global node_modules directory path.
Once you have that, go to Tools then Options. Navigate to Text Editor > Javascript/Typescript > IntelliSense.
Under Included Directories, add the path to your global node_modules directory. Then close vs2022 and open it again for the changes to take effect.
Been a long while since I used vs2022 so assuming I'm correct that should resolve your issues.
Is it possible to control where a player is facing?
Teleporting only. Would be choppy
Yeah I think that would work cause I removed the players ability to move and rotate it's camera so yeah
Damn, you should use VSCode not VS
vscode doesn't detect global packages anymore
it was removed like few months ago
basicly the TS Langauge Server doesn't do that anymore
I have vsc all working and it was what I was using, I was trying to switch to vs as its my preferred ide.
I am using VS dayly but for JS i think VSCode suits better a lot
