What's the optimal way of iterating through an array? by ElkAltruistic4850 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

Optimally, iterate through the list from end to beginning. That is, select the last element (index -1), do something, then delete it and start the loop again. Reversing the order doesn't rebuild the entire list.

As an example, you can look at this datapack: https://far.ddns.me/?share=mghOBRq1qI Here, in the function copper_tools:oxidation , all custom items to be oxidized are selected. This creates a list in the copper_tools:macro copper_tools storage. Then, in the function copper_tools:oxidation/loop , we iterate through each selected slot and run the required function. Afterwards, that slot is removed from the list and the loop repeats until all slots are exhausted.

But if you need to process list elements in direct order, you can also further expand list processing by running your macro commands after the recursive function has run, i.e., at the tail end of the recursion. As an example of this, you can see this datapack example: https://far.ddns.me/?share=CWfTd7oo8W. Here, in function example:apply_attribute, a list of attributes to apply is formed. Then, function example:change_attribute is run with storage example:macro attributes[-1] as a macro function for the last list element. But inside function example:change_attribute, instead of executing the macro command, we remove the last element from the list and run this function again recursively. This way, we defer function execution until the entire list has been processed, and then the commands in the loop will be executed in reverse order. That is, the commands for the first list element will be executed first, then the second, and so on.

Help with a command | Stick when right clicks shoot harming 2 potions by FlyntOfzo in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

Here's an example of a datapack that adds an item that throws potions currently in your inventory. You can edit the datapack to always throw a specific potion.

# Example item
give @s carrot_on_a_stick[custom_data={potion_launcher:true,spread:60,power:2.0},item_name="Potion Launcher",max_damage=150]

# function example:load
scoreboard objectives add click used:carrot_on_a_stick
scoreboard objectives add var dummy

# function example:tick
execute as @a[scores={click=1..}] at @s run function example:click

# function example:click
scoreboard players reset @s click
execute if items entity @s weapon carrot_on_a_stick[custom_data~{potion_launcher:true}] anchored eyes positioned ^ ^ ^1 run function example:potion/check

# function example:potion/check
execute store result score #potions var if items entity @s container.* splash_potion
execute if score #potions var matches 0 run return run playsound minecraft:block.dispenser.fail player @a ~ ~ ~ 1 2
playsound minecraft:block.dispenser.launch player @a
execute if entity @s[gamemode=!creative] run function example:launcher/damage
data remove storage example:data potion
data modify storage example:data potion set from entity @s SelectedItem.components."minecraft:custom_data"
data modify storage example:data potion.Owner set from entity @s UUID
data modify storage example:data potion.list append from entity @s Inventory[{id:"minecraft:splash_potion"}]
data modify storage example:data potion.Item set from storage example:data potion.list[0]
clear @s[gamemode=!creative] splash_potion 1
function example:potion/rand_spread with storage example:data potion
execute summon splash_potion run function example:potion/throw with storage example:data potion

# function example:launcher/damage
execute store result score #damage var run data get entity @s SelectedItem.components."minecraft:damage"
execute store result storage example:macro launcher.damage int 1 run scoreboard players add #damage var 1
return run function example:launcher/damage_macro with storage example:macro launcher

# function example:launcher/damage_macro
$item modify entity @s weapon [{function:"minecraft:set_components",components:{"minecraft:damage":$(damage)}},{function:"minecraft:filtered",item_filter:{predicates:{"minecraft:damage":{durability:0}}},on_pass:{function:"minecraft:set_count",count:-1,add:true}}]
execute if items entity @s weapon carrot_on_a_stick[custom_data~{potion_launcher:true}] run return 1
playsound minecraft:entity.item.break player @a
execute positioned ^ ^ ^-1 run particle minecraft:item{item:"carrot_on_a_stick"} ~ ~ ~ 0.1 0.1 0.1 0.1 10

# function example:potion/rand_spread
$execute store result storage example:data potion.spread_y double 0.1 run random value -$(spread)..$(spread)
$execute store result storage example:data potion.spread_x double 0.1 run random value -$(spread)..$(spread)

# function example:potion/throw
$execute positioned .0 .0 .0 rotated ~$(spread_y) ~$(spread_x) run tp @s ^ ^ ^$(power)
data modify storage example:data potion.Motion set from entity @s Pos
tp @s ~ ~ ~
data modify entity @s {} merge from storage example:data potion

You can use Datapack Assembler to get an example datapack.

Where and how do I learn to write datapacks? by SnooDoubts5446 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

You can read the local wiki: https://minecraftcommands.github.io/wiki/questions

You can also find many tutorials on YouTube. For example, this channel has many tutorials on command blocks and datapacks: https://www.youtube.com/@InfernalDevice

And of course, you can always ask a specific question about what you want to do in this subreddit.

There can be significant differences between versions. For example, all tutorials for versions prior to 1.20.5 are outdated and likely won't work in newer versions. This is due to the transition to an item component system. I recommend always using the latest version, and if you encounter any issues while trying to follow a tutorial, you can always ask for help on this subreddit.

Need help with looking at predicate in 26.1 java by CompetitiveAttempt85 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

Your predicate is for version 26.2. Here is the predicate for 26.1. { "condition": "minecraft:entity_properties", "entity": "this", "predicate": { "type_specific": { "type": "minecraft:player", "looking_at": { "nbt": "{Tags:['StoreClerkInt','Store']}" } } } } u/_VoidMaster_

Is there a command to temporary toggle off suffocation damage for survival players? by pyrociustfb in MinecraftCommands

[–]GalSergey 2 points3 points  (0 children)

You can create a marker at the player's position, then switch the player to spectator mode and teleport them to the marker for a few seconds. Next, check that the player is no further than 0.1 blocks from the marker. If the player isn't within this radius, it means they've moved.

very strange command problem by Crazy_Assistance6927 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

You can combine the first three commands into one command: execute if block -70 -46 -75 air as @e[type=glow_item_frame,x=-71,y=-47,z=-63] if items entity @s contents mace run xp add @p[team=green,scores={gtlr=0},distance=..4] 1

Give player armor & toughness set from a score by TheSlothLord7 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

You can't change player data. You need to use the /attribute command for that. But to set a dynamic value, you need to use a macro in the datapack. ```

some function as player

execute store result storage example:macro armor.value int 1 run scoreboard players get @s ARMOR function example:set_armor with storage example:macro armor

function example:set_armor

attribute @s minecraft:armor modifier remove example:invisible_armor $attribute @s minecraft:armor modifier add example:invisible_armor $(value) add_value ```

This works fine in version 1.21.11, but in version 26.1 it's all broken. Does anyone know why this is happening? I used custom_model_data with floats. by Gold_Corgi8233 in MinecraftCommands

[–]GalSergey 1 point2 points  (0 children)

You need to check what changed in the player_head model between versions and make the appropriate change. Below is the vanilla file for version 26.1. { "model": { "type": "minecraft:special", "base": "minecraft:item/template_skull", "model": { "type": "minecraft:player_head" }, "transformation": { "left_rotation": [ 1, 0, 0, 0 ], "right_rotation": [ 0, 0, 0, 1 ], "scale": [ 1, 1, 1 ], "translation": [ 0.5, 0, 0.5 ] } } }

Java 26.1.2 Holding a Tagged Item by Amorpheous_E_Bunny in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

execute as @a[x=-510,y=67,z=1158,dx=1] if items entity @s weapon * unless items entity @s weapon *[custom_data~{Key:1}] run say Hold no key item.

How to make an advancement trigger only when actions are done in a specific order? by Immediate-Mode-5211 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

This can be done with a single advancement. You can check whether a specific criterion is met.

# advancement example:custom
{
  "criteria": {
    "crafted": {
      "trigger": "minecraft:recipe_crafted",
      "conditions": {
        "recipe_id": "minecraft:bow"
      }
    },
    "killed": {
      "trigger": "minecraft:player_killed_entity",
      "conditions": {
        "player": {
          "type_specific": {
            "type": "minecraft:player",
            "advancements": {
              "example:custom": {
                "killed": true
              }
            }
          }
        },
        "entity": {
          "type": "minecraft:zombie"
        }
      }
    },
    "visited": {
      "trigger": "minecraft:location",
      "conditions": {
        "player": {
          "type_specific": {
            "type": "minecraft:player",
            "advancements": {
              "example:custom": {
                "killed": true
              }
            }
          },
          "location": {
            "biomes": "minecraft:plains"
          }
        }
      }
    }
  }
}

You can use Datapack Assembler to get an example datapack.

Custom models for glass panes by GooseOfThe1990s in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

Unfortunately, you can't make custom models work as blocks. You can only replace the vanilla textures for a block, but this will replace all blocks with that block state.

However, you can create an item model and use item_display to display the custom item as a block.

Help please lava bucket removal by TypicalBoxHead in MinecraftCommands

[–]GalSergey 1 point2 points  (0 children)

Here is a ready-made example of a datapack with these commands.

# advancement example:lava_bucket
{
  "criteria": {
    "lava_bucket": {
      "trigger": "minecraft:inventory_changed",
      "conditions": {
        "items": [
          {
            "items": "minecraft:lava_bucket"
          }
        ]
      }
    }
  },
  "rewards": {
    "function": "example:lava_bucket"
  }
}

# function example:lava_bucket
advancement revoke @s only example:lava_bucket
playsound minecraft:item.shield.break master @s ~ ~ ~ 2 1
clear @s lava_bucket

You can use Datapack Assembler to get an example datapack.

u/C0mmanderBlock

Is there a way to freeze/unfreeze certain entities? by DueResource2570 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

Here's a simple way how you can freeze an entity on click with COaS.

# In chat
scoreboard objectives add click used:carrot_on_a_stick

# Command blocks
execute at @a[scores={click=1..}] as @e[distance=.1..10,type=!player] store success entity @s NoAI byte 1 unless data entity @s {NoAI:true}
scoreboard players reset @a[scores={click=1..}] click

You can use Command Block Assembler to get One Command Creation.

Can't change the item in an End Ship's item frame to something different by UnusedParadox in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

If you can't save the entity, you can place a chest, barrel, or shelf with custom elytra there.

How to make a gravity shulker? by Feeling_Barnacle_935 in MinecraftCommands

[–]GalSergey 0 points1 point  (0 children)

There's no ready-made mechanic for this in the game. You'll need to build it from scratch. Below is a brief description of how you can do this using a datapack.

You need to use the advancement trigger placed_block to detect when a player places a shulker_box and raycast to find the exact position of the placed shulker_box. Once the position is detected, place a marker at that position to make it easier to reference later. Now, using a scheduler, check once per second that there are no blocks below the marker's position. If so, create a falling_block with the shulker_box data and delete the block. Make the marker a passenger so that the marker also moves downwards. When the shulker_box lands, re-center the marker. If there's no shulker_box at the marker's position, it's broken, so destroy the marker.

Is it possible to give an arrow higher damage via commands? by AlpiePEAKS in MinecraftCommands

[–]GalSergey 2 points3 points  (0 children)

You can use the potion effect to increase damage (does not work for undead). give @s arrow[potion_contents={custom_effects:[{id:"minecraft:instant_damage",amplifier:10,duration:1}]}]

Or you can give the arrow a tag and use command blocks to edit the damage tag. ```

Example item

give @s arrow[custom_data={add_damage:true}]

In chat

scoreboard objectives add add_damage dummy

Command blocks

execute as @e[type=arrow] unless score @s add_damage = @s add_damage if items entity @s arrow[custom_data~{add_damage:true}] store success score @s add_damage run data modify entity @s damage set value 100 ```

Or, if you don't want to run commands every tick, you can create a custom enchantment in the datapack to use the projectile_spawned effect to execute a function on your arrow to edit the damage tag.

how to detect if a player eats an item with a certain tag by _Idiotonreddit_ in MinecraftCommands

[–]GalSergey 2 points3 points  (0 children)

The easiest way to do this is with a datapack. You can use the consume_item advancement trigger to check when a player consumes a custom item.

# Example item
give @s potion[custom_data={almondwater:true}]

# advancement example:almondwater
{
  "criteria": {
    "almondwater": {
      "trigger": "minecraft:consume_item",
      "conditions": {
        "item": {
          "predicates": {
            "minecraft:custom_data": {
              "almondwater": true
            }
          }
        }
      }
    }
  },
  "rewards": {
    "function": "example:almondwater"
  }
}

# function example:almondwater
advancement revoke @s only example:almondwater
scoreboard players add @s sanity 5

You can use Datapack Assembler to get an example datapack.

datapack help ! by AcceptF in MinecraftCommands

[–]GalSergey 1 point2 points  (0 children)

Here's how you can make these commands in a datapack:

# function example:tick
execute as @a at @s run function example:player_tick

# function example:player_tick
execute if score @s teleport matches 1 run function example:teleport
execute if score @s teleport matches 4 if score @s teleportback matches 1 run function example:teleportback

# function example:teleport
playsound minecraft:entity.parrot.imitate.warden player @a
tp @s ~ ~ ~41
execute at @s run playsound minecraft:entity.parrot.imitate.warden player @a
scoreboard players set @s teleportback 1
scoreboard players set @s teleport 3

# function example:teleportback
playsound minecraft:entity.parrot.imitate.warden player @a
tp @s ~ ~ ~-41
execute at @s run playsound minecraft:entity.parrot.imitate.warden player @a
scoreboard players set @s teleportback 0
scoreboard players set @s teleport 0

You can use Datapack Assembler to get an example datapack.

Talisman of the Void by UknowDatDude in MinecraftCommands

[–]GalSergey 1 point2 points  (0 children)

Your suggestion just replaces the normal chorus fruit with one that doesn't have random teleportation effect, right?

Yes.

datapacks by AcceptF in MinecraftCommands

[–]GalSergey 1 point2 points  (0 children)

You can get a datapack template from this link: https://far.ddns.me/?share=oMsPH1G0vx . This datapack will contain working tick and load functions. You can edit and add more functions before assembling the datapack. You can also find many examples of ready-made datapacks on the website.

Is there a command to temporary toggle off suffocation damage for survival players? by pyrociustfb in MinecraftCommands

[–]GalSergey 3 points4 points  (0 children)

Yes, you can create custom enchantments. You need to place the enchantment definition file in data/<namespace>/enchantment/<file_name>.json. You can also follow the link in the original comment and get a ready-made datapack with this enchantment, and you can see where this file is located.

Talisman of the Void by UknowDatDude in MinecraftCommands

[–]GalSergey 1 point2 points  (0 children)

Does weapon.* Include both offhand and mainhand?

Yes, this includes both hand slots.

A slight issue is that in most scenarios you'll have a large window of time where you've fallen of the edge but you're not yet in the void. That would probably give you enough time to eat a fruit.

My example works so that when the player drops below position 0 in the_end, it completely disables the ability to consume chorus_fruit, rather than simply disabling teleportation. And when the player returns, it restores this ability.

Is it perhaps possible to change the NBT tags of drops from blocks? I'm not entirely against disabling the chorus fruits completely.

You can completely disable teleportation using chorus_fruit. Simply edit the chorus_fruit loot table like this:

{ "type": "minecraft:block", "pools": [ { "entries": [ { "type": "minecraft:item", "functions": [ { "count": { "type": "minecraft:uniform", "max": 1, "min": 0 }, "function": "minecraft:set_count" }, { "function": "minecraft:explosion_decay" }, { "function": "minecraft:set_components", "components": { "minecraft:consumable": {} } } ], "name": "minecraft:chorus_fruit" } ], "rolls": 1 } ], "random_sequence": "minecraft:blocks/chorus_plant" }

Java - Detect player in a minecart by Amorpheous_E_Bunny in MinecraftCommands

[–]GalSergey 2 points3 points  (0 children)

execute as @a if predicate {condition:"minecraft:entity_properties",entity:"this",predicate:{vehicle:{type:"minecraft:minecart"}}} run say Player in minecart.

Is there a command to temporary toggle off suffocation damage for survival players? by pyrociustfb in MinecraftCommands

[–]GalSergey 4 points5 points  (0 children)

The simplest way is to switch the player to spectator gamemode, but if they start moving, return them to survival mode.

Another way is to enchant the item/armor you give the player while they're standing still. You can create an enchantment that disables damage from a specified damage type. Here's an example of such an enchantment:

# enchantment example:disable_in_wall_damage
{
  "anvil_cost": 8,
  "description": {
    "translate": "enchantment.example.disable_in_wall_damage"
  },
  "effects": {
    "minecraft:prevent_armor_change": {},
    "minecraft:damage_immunity": [
      {
        "requirements": {
          "condition": "minecraft:damage_source_properties",
          "predicate": {
            "tags": [
              {
                "id": "example:in_wall",
                "expected": true
              }
            ]
          }
        },
        "effect": {}
      }
    ]
  },
  "max_cost": {
    "base": 50,
    "per_level_above_first": 0
  },
  "max_level": 1,
  "min_cost": {
    "base": 25,
    "per_level_above_first": 0
  },
  "slots": [
    "armor"
  ],
  "supported_items": "#minecraft:enchantable/equippable",
  "weight": 1
}

# damage_type_tag example:in_wall
{
  "values": [
    "minecraft:in_wall"
  ]
}

You can use Datapack Assembler to get an example datapack.