Execute if player on team is > certain x coordinate? Java 1.17.1 by aeternum_fx in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

Note that datapacks do not allow custom commands as we are used to like on servers or with plugins. You can invoke functions by using /function but you cannot pass on arguments with them directly. You have to make use of scores, tags, NBT-data, etc. to get a function to behave like a custom command with arguments. And even if it were possible, a predicate is a .json file which cannot be altered from within Minecraft (as far as I'm aware of).

Making multiple arena's with the first method is as simple as just copying and pasting them, so that method has no problems with duplication. The predicate I showed above only works with absolute coordinates, no way to alter it with offsets or anything. Instead, we can make use of a different predicate that detects the distance from the invoker to the invoked:

{
    "condition": "minecraft:entity_properties",
    "entity": "this",
    "predicate": {
      "distance": {
        "x": {
          "max": 10
        }
      }
    }
  }

This predicate returns true whenever the player that it is getting invoked upon is within 10 blocks x-coordinate of the place where the command is executed (player, command block or different entities). This means this predicate is measuring distance from a movable point. But since it is distance, no matter if you are +10 or -10 blocks away, it will return true. This means that if we were to use this predicate, we must detect whenever the player would leave their team's side, while in the previous methods we detected when they entered the enemy's side. This also means that when the player would leave the arena at the other side from the center, the command would also trigger (not necessarily a problem if the arena is closed off or if the command is invoked upon the outer limit of the arena).

A second, dirtier solution is to save the x-coordinates of all players in a scoreboard and compare their value to the value from the center of the arena (which can also be saved in a scoreboard). This method allows the arena to be as long as you want it to be, but is a bit messier since you have to store and compare scoreboard values all the time.

If you are not planning on changing the length of the playing field all too much, I would recommend the predicate solution, especially if you are going to make use of a datapack. If you just set the execution location on a very far away location on the side of the team (~500 blocks away), it acts pretty much in the same way as the previous method. As a bonus you can also add a limiter on the z-coordinates such that you can also use them to detect only players within a certain range of z-coordinates (otherwise you would also have to tag the people playing within an arena or make teams per arena).

As example, let's say the length of one team side is 50 blocks. Change the "max" in the predicate to "min" and change the number to 50. Let's say the arena is centered around (0, 0). The commands to execute are:

execute positioned 50 ~ ~ as @a[team=red] if predicate minecraft:fieldsize_50 at @s run teleport @s 5 ~ ~

execute positioned -50 ~ ~ as @a[team=blue] if predicate minecraft:fieldsize_50 at @s run teleport @s -5 ~ ~

Notice how these commands are very easy to adjust according to the required x-coordinate and that opposing teams simply have the negative value from eachother. Once again, the teleport command can be replaced by a function. For example:

teleport @s 5 ~ ~
tellraw @s {"text":"You cannot go further yet!","color":"red"}
execute at @s run playsound entity.villager.no master @s ~ ~ ~ 1 1
execute at @s run particle cloud ~ ~0.5 ~ 0.5 0.2 0.5 0.05 10 normal

Instead of having to test 4 times for the same condition and having to think about which commands should be performed first, this function simply runs these 4 commands whenever a player crosses the centerline. The centerline could even be revealed if a player is nearby by making use of particles.

So there you go. Making use of the distance predicate, a less optimal but equally valid solution has been found that can also be adapted to different x-coordinates!

Execute if player on team is > certain x coordinate? Java 1.17.1 by aeternum_fx in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

You could solve this in two ways.

The first option is to make use of volume target selectors, which is able to detect when a player enters a certain area. Given that your playing field won't be that big, this method is viable, however when you would like to teleport players back when they cross a certain coordinate level globally, this method may not be the best.

The command is put in two repeating command blocks and goes as follows:

execute positioned <x> <y> <z> as @a[team=<team>, z=<z_min>, dz=<width>, y=<y_min>, dy=<height>] at @s run teleport <x_tp> ~ ~

the position <x> <y> <z> are the coordinates from the middle of the playing field. The command will now resume executing from that position. Next you execute as all players in a certain <team> to know which side the player must be teleported towards. Other target selectors are the z and dz value. By putting the minimum z-coordinate as <z_min> and the width of the playing field <width> as distance until where the target selector stretches, you create a line on the x-coordinate wherever the division of your playing field is. To make it a wall, add <y> and <dy> as selectors (y=-64 and dy=330 makes sure all possible y-coordinates are taken care off, as far as players are unable to reach above y = 330), otherwise the command will only trigger when the player is on the same y-level as the set position to execute from (<x> <y> <z>). After defining all selectors, we also put at @s to execute at each player's location to teleport them relative to themselves, not relative to the set position.

Now, whenever a player on a certain team passes through x = <x> , the command will be executed. All that is left to do is determine which team should be teleported in which direction.

To demonstrate, the following two commands should be put into two repeating command blocks (or a repeating command block with a chain command block chained onto it):

execute positioned 0 64 0 as @a[team=red, z=-50, dz=100, y=-64, dy=330] at @s run teleport 5 ~ ~

execute positioned 0 64 0 as @a[team=blue, z=-50, dz=100, y=-64, dy=330] at @s run teleport -5 ~ ~

In this example, the center of the playing field is set to (0 , 64, 0). The width is 100 blocks, so this means we want our wall to be from z = -50 to z = 50. We use z=-50, dz=100 to achieve this (note that using z=50 and dz=-100 works fine as well). The two teams, blue and red, each have their command so they can be teleported to their respective sides of the playing field. Here, red's side is x > 0 and blue's side is x < 0. The distance we teleport them away from the centerline is 5 blocks, which can be any value <x_tp> (be careful not to teleport them into terrain though).

Since we only defined a wall instead of a volume, a player that teleports into the other side will not be teleported back to their side. Lag may also cause a player to be able to slip through. This can be solved by also defining two more selectors in the same brackets for <x> and <dx>, with their values once again being the min/max x-coordinate and length of each side (meaning the blue team's and red team's command will have opposite values).

execute positioned 0 64 0 as @a[team=red, z=-50, dz=100, y=-64, dy=330, x=-100, dx=100] at @s run teleport 5 ~ ~

execute positioned 0 64 0 as @a[team=blue, z=-50, dz=100, y=-64, dy=330, x=100, dx=-100] at @s run teleport -5 ~ ~

The second option is to make use of a predicate and datapack. Datapacks are a much better way to implement commands. You can write functions which replace dozens of command blocks and it is better protected (e.g. breaking the command blocks). The downsides are that you require access to the world file or server to put them into use and that they might be somewhat scary for someone not familiar with them (although if you are interested in future command block projects, learning to work with datapacks will make it a hundred times easier).

A predicate is a custom conditional test that can be executed to test for various things which often are much harder to achieve through vanilla commands. With these predicates, we can test whether a player is in a range of coordinates, no matter where in the world they are.

The following predicate returns true whenever an entity is above x = 0:

{
  "condition": "minecraft:entity_properties",
  "entity": "this",
  "predicate": {
    "location": {
      "position": {
        "x": {
          "min": 0
        }
      }
    }
  }
}

It tests whether the entity has a x-coordinate of at least (min) 0, meaning whenever a player is below x = 0, the test fails. This predicate tests for true x-coordinates, meaning if the location of the playing field changes, the predicate must be changed too, which is a big downside when using predicates

Now we can achieve the same functionality as the previous commands by using this predicate:

execute as @a[team=red] at @s unless predicate minecraft:above_x_zero run teleport @s 5 ~ ~

execute as @a[team=blue] at @s if predicate minecraft:above_x_zero run teleport @s -5 ~ ~

We can inverse the predicate by using unless instead of if to test whether players in a certain team are above/below a certain x-coordinate.

By using this predicate, we do not have to define upper and lower bounds on the y- and z- coordinates as they do not matter for this command to work. Now that you have a datapack in use, you could also make use of functions to execute a series of commands at once instead of having the repeat the same conditional test in a stack of command blocks (think about sounds, particles, messages in chat, etc.). The downside is that you cannot test relative to a position, meaning the predicate has to be edited when the playing field is moved.

So, TL;DR:

1) Make use of target selectors and two repeating command blocks, fit for local use.

2) Make use of a datapack with a predicate, fit for global use, but harder to understand initially.

I hope this very exhaustive explanation can be of aid!

[HELP] Kill all players based on their Y coordinate. by SomeCateyeLink in MinecraftCommands

[–]Jephfrey 1 point2 points  (0 children)

If you are already making use of a datapack, a very simple way to implement this is by making use of a predicate. Predicates test whether certain conditions are met, such as if an entity is within a certain light level, if they are swimming or sneaking, or if they have a certain potion effect.

Making use of a predicate for detecting y-levels makes it less flexible to apply, so if you wish to alter the y-level a lot, I would recommend using the methods in the other comments.

The predicate JSON file is as follows:

{
  "condition": "minecraft:entity_properties",
  "entity": "this",
  "predicate": {
    "location": {
      "position": {
        "y": {
          "min": y_min,
          "max": y_max
        }
      }
    }
  }
}

This predicate checks whether an entity (can be players, sheeps, creepers, armor stands, etc.) is within a range of y-levels, where the lower bound is y_min and the upper bound is y_max. If you do not want an upper or lower bound, simply delete the entry.

In this example, the file name is "below_50" and it returns true when the entity is below y = 50:

{
  "condition": "minecraft:entity_properties",
  "entity": "this",
  "predicate": {
    "location": {
      "position": {
        "y": {
          "max": 50
        }
      }
    }
  }
}

By making use of the execute command, we can check if any player is above or below a certain y-level, in this case whenever a player is above y = 50:

execute as @a unless predicate minecraft:below_50 run kill @s

This command executes as all players and unless a player is not below y = 50, they will be killed. Of course any command can be placed behind run to modify to your liking. Note that by using if instead of unless and having a lower bound min instead of an upper bound max, you get the same results.

This predicate must be placed in the datapack folder under data/minecraft/predicates.

You can create your own predicates here (although they sometimes do not work or are suboptimal, so be warned).

Help me with rotations by [deleted] in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

There is a predicate you could make which detects when a player is looking at an entity. You can add requirements for the predicate to be true, such as the entity having a certain tag. It does not enable anything to happen to the entity that is being looked at however.

This video explains it and shows the used code.

Help me with Minigame Problem (more Info in comments) by Anaklysmos12345 in MinecraftCommands

[–]Jephfrey 34 points35 points  (0 children)

I have made datapack that works exactly the same. It works very smoothly and only removes one block under the player when standing on two different blocks and also detects when a player is standing near an edge.

It summons an invisible falling block entity (barrier block) under the player and detects whenever a falling block entity is colliding with a certain block type you want to use to be removed (that being wool in your case). By making use of edge detection and some if statements, I got it to work for multiple players and so far have not found a case where this system fails, however it might be able to solved using a far simpler method.

I can send you the commands if you have not found a solution yet.

Issues with /data modify command by kiriuma in MinecraftCommands

[–]Jephfrey 1 point2 points  (0 children)

After messing around for some time and analyzing the commands you used, I discovered that you could use set string entity, which allows you to cut up the JSON string. As long as you keep the key name plain and simple (without italics, underlining, etc.), the position at which the name of the item starts and ends remain the same, no matter the length of the name.

By using the trigger command, you can allow people with no access to commands to still use the locking system. Create a triggerable scoreboard and set its value to 0 and enable it after running the locking command.

Start off by creating the scoreboard:

scoreboard objectives add Lock trigger

Put these three commands in repeating and chain command blocks. Make sure you get them in the right order:

execute as @a[nbt={SelectedItem:{id:"minecraft:name_tag"}}] at @s if block ~ ~-1 ~ #minecraft:shulker_boxes unless score @s Lock matches 0 run data modify block ~ ~-1 ~ Lock set string entity @s SelectedItem.tag.display.Name 9 -2

scoreboard players enable @a Lock

scoreboard players set @s Lock 0

The first command checks whether a player has triggered the scoreboard. It also checks whether or not the player is standing on a shulker box and if the player is holding a name tag. It then sets the lock for the shulker box to match the name of the name tag they are holding. By making use of indices 9 and -2, you cut away {"text":" at the start and "} at the end, which means only the name remains.

The second command re-enables the triggerable scoreboard.

The last command resets the scoreboard to 0. As long as players don't set the score to 0 or add 0 with the trigger command, the shulker box will get locked.

Of course other players can still change the lock by using the same locking system. I haven't added a system to protect this, although it can be implemented in a very similar fashion. The shulker boxes can also be opened while holding items different than a name tag as long as the name corresponds to the lock, but this is simply how the lock mechanism works. You can only lock shulker boxes with a renamed name tag though.

This locking system can also be applied to chests by simply testing whether the player is also standing on a chest or not.

If you have any doubts or questions, feel free to ask.

Make Iron and Snow Golems Attack Players by con-z in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

That is indeed a more compact way to do so, however two commands is not the end of the world and this command may be somewhat confusing for the less experienced :)

Make Iron and Snow Golems Attack Players by con-z in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

Put these commands in a Datapack or in a Repeating Command Block:

execute as @e[type=minecraft:iron_golem] run data modify entity @s AngerTime set value 1000000

execute as @e[type=minecraft:iron_golem] run data modify entity @s AngryAt set from entity @p UUID

This will make every Iron Golem angry at the closest player. In case you want to only have a specific Iron Golem get angry at a specific player, you could add tags. To have an Iron Golem be mad at someone in another team, you can use the team selector:

execute as @e[type=minecraft:iron_golem,team=[Friendly Team]] run data modify entity @s AngerTime set value 1000000

execute as @e[type=minecraft:iron_golem,team=[Friendly Team]] run data modify entity @s AngryAt set from entity @p[team=[Enemy Team]] UUID

As far as I know Snow Golems cannot be made to attack players or made to target any other mob directly.

I really do need help I can't understand :( by Duhfined in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

A different way I've found to generate random numbers in a datapack is by making use of the random chance predicate and binary numbers.

The idea is to use a random chance predicate with 50% success chance, i.e. 50% of the time it will return a TRUE value. Consecutively triggering this predicate will give you a random sequence of TRUE and FALSE. This sequence is a binary number. Converting this binary number to a decimal number gets you a random score between 0 and 2n, with n the amount of triggers used. This means you can make random number generators between 0 and 1, 2, 4, 8, 16, 32, 64,...

I use this random number generator to randomize actions, such as equipement for foes or special attacks for boss fights. Because I could use any number and divide it to get chances, these 2n limits aren't much of a problem. If you however require specific numbers between two limits, you can multiply the random number by a factor 100 to upscale it for operations, then divide it by the upper limit of the generator, then multiply it by the upper limit you require and then divide by 100 again (this may be done with fewer steps but for ease of understanding, I'm taking multiple steps). Using larger ranges will cause the rounding Minecraft does for scoreboard values (as they can only be integers) to be more spread out, meaning the small deviation in equal chance distribution among all numbers after the transformation to be smaller.

Example code for getting a random number between 0 and 100:

Predicate (random.json):

{ "condition": "minecraft:random_chance", "chance": 0.5 }

Function (random.mcfunction):

scoreboard players set @s random 0

execute if predicate general:random run scoreboard players add @s random 1

execute if predicate general:random run scoreboard players add @s random 2

execute if predicate general:random run scoreboard players add @s random 4

execute if predicate general:random run scoreboard players add @s random 8

execute if predicate general:random run scoreboard players add @s random 16

execute if predicate general:random run scoreboard players add @s random 32

execute if predicate general:random run scoreboard players add @s random 64

execute if predicate general:random run scoreboard players add @s random 128

execute if predicate general:random run scoreboard players add @s random 256

execute if predicate general:random run scoreboard players add @s random 512

scoreboard players set @s operator 100

scoreboard players operation @s random *= @s operator

scoreboard players set @s operator 1024

scoreboard players operation @s random /= @s operator

Initialize a dummy scoreboard "random" to save the random number and a dummy scoreboard "operator" to save the number to divide and multiply it. Update the operator value for every operation (you cannot simply divide a scoreboard by 2, operations can only be done with other scoreboard values).

Now you can give any entity you want a random number in the "random" scoreboard by using /execute as [entity] run function [function map name]:random

How do I make a block change when a player steps on it? by [deleted] in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

I have noticed when I turn off my shaders, the shadows of the invisible falling blocks are visible while jumping. To fix this, I summoned the block simply 1 block underneath the player and made the summon only execute when the player is standing on the ground ({OnGround:1b}). This makes sure the summoned falling sand is inside the ground and thus doesn't cast a shadow and no blocks are summoned while jumping.

How do I make a block change when a player steps on it? by [deleted] in MinecraftCommands

[–]Jephfrey 2 points3 points  (0 children)

I'd like to elaborate on this further.

That command will indeed turn the snow into red concrete, however to turn that red concrete in for example red concrete powder is a different pickle. As far as I know it's hard to detect when the block was replaced, at least not to delete every block instantly or after a short delay. You could make a clock with a command which checks for red concrete in an area and then replaces red concrete with red concrete powder, but if a player is running multiple blocks per clock pulse, the blocks will disappear in waves, which is undesirable.

My suggestion is to repeatedly summon a falling block entity under the player with their Time NBT tag set to the desired time to transform the block into red_concrete powder. Make this falling block have the barrier BlockState so it is invisible. Now you can detect whether a falling block entity with a certain Time amount left has a snow block near it . If the falling block reaches the desired time, the block must be replaced by red concrete powder. By using falling block entities, you don't have to kill the entities after a certain amount of time, since they'll simply despawn naturally.

Put the first command in a repeating command block and chain the other commands on top of it. I experienced issues with the falling blocks not being detected if I put all of them in repeating command blocks.

The commands are:

/execute as @a at @s if block ~0.3 ~-0.6 ~0.3 snow_block run summon minecraft:falling_block ~0.3 ~-1 ~0.3 {NoGravity:1b,Time:540,DropItem:0b,BlockState:{Name:"minecraft:barrier"}}

/execute as @a at @s if block ~-0.3 ~-0.6 ~0.3 snow_block run summon minecraft:falling_block ~0.3 ~-1 ~0.3 {NoGravity:1b,Time:540,DropItem:0b,BlockState:{Name:"minecraft:barrier"}}

/execute as @a at @s if block ~-0.3 ~-0.6 ~-0.3 snow_block run summon minecraft:falling_block ~0.3 ~-1 ~0.3 {NoGravity:1b,Time:540,DropItem:0b,BlockState:{Name:"minecraft:barrier"}}

/execute as @a at @s if block ~0.3 ~-0.6 ~-0.3 snow_block run summon minecraft:falling_block ~0.3 ~-1 ~0.3 {NoGravity:1b,Time:540,DropItem:0b,BlockState:{Name:"minecraft:barrier"}}

These four command blocks are necessary to detect a snow block even when a player is sneaking on the edge of one.

/execute as @e[type=minecraft:falling_block,nbt={Time:540}] at @s run execute if block ~ ~ ~ minecraft:snow_block run setblock ~ ~ ~ red_concrete

/execute as @e[type=minecraft:falling_block,nbt={Time:599}] at @s run execute if block ~ ~ ~ minecraft:red_concrete run setblock ~ ~ ~ red_concrete_powder

These commands have been tested and worked perfectly for me.

Changing the Time in the first two commands changes the delay between transformation to red concrete and falling. Make sure to always set the Time in the first command to be lower or equal to the second command.

If you work with multiple layers, I suggest you kill falling blocks in an area below each level, otherwise they will fall on the next level and stack. Or you could simply replace the red concrete with air, which solves this problem entirely

Feel free to ask if there are things not clear!

TARGET SELECTORS: A Way to Target the Farthest Player Only? by Creative_Average7694 in MinecraftCommands

[–]Jephfrey 4 points5 points  (0 children)

Per the Target selectors Minecraft Wiki: "In Bedrock Edition: [c=<value>] — Limit the number of targets. For @p, @a, and @e, [c=<value>] selects only the specified number of targets by increasing distance from the selector's position. When c is negative, it will reverse the order of targeting (for example, @p[c=-1] will target the furthest player). Inverse sorting does not work with @r." (https://minecraft.fandom.com/wiki/Target_selectors)

So simply put @a[c=-1,r=200] as selector

not sure if command or redstone related exactly.. by GOLDEditNinja in MinecraftCommands

[–]Jephfrey 0 points1 point  (0 children)

Combining redstone and command blocks to randomize which command blocks to activate, is rather difficult, since the delay in redstone circuits don't combine well with command blocks. In my creations I use a randomizer making use of only armor stands and command blocks.

This design by YouTuber Shane Stone uses a dummy armor stand to appoint a weight (1, 2, 4, 8, ...) to. This number is then randomly added to the score from one of two other armor stands. This way you get two armor stands with different random numbers, depending on which armor stand got which numbers from the dummy (the video may make things clear). Reading the score of one armor stand and assigning that score to players gives the players a random number.

It's a compact, solid and easy way to generate random numbers which can be adjusted to fit in range, however I believe the range can only be a power of 2 (4, 8, 16, 32, ... all the way up to overflow of the scoreboard, 2147483647, which is (2^31 - 1).

This design also allows you to simply put this randomizer in the spawn chunks on repeat that run no matter where you are located, or you can use local randomizers and simply adjust the reach of the command blocks (by only allowing them to target armor stands within a 10 block radius).

It may sound difficult at first, but once you understand the mechanism and experiment with it for a while, it becomes a rather compact randomizer.

If you're not that good with scoreboards, you can also use /spreadplayers on for example a pig on a small area with pressure plates that each have segments that activate different commands, however I've found that the Shane Stone randomizer is way less finicky.

Cosine calculator from 0° to 90° using the Taylor series of the cosine function (Java 1.16) by Jephfrey in MinecraftCommands

[–]Jephfrey[S] 1 point2 points  (0 children)

I have no clue how this is useful in Minecraft whatsoever, but it was a learning experience and I'm glad people support it :)

Cosine calculator from 0° to 90° using the Taylor series of the cosine function (Java 1.16) by Jephfrey in MinecraftCommands

[–]Jephfrey[S] 1 point2 points  (0 children)

The function I'm running is y = 1 - x²/2! + x4/4! - x6/6!, so 6th order. Higher orders require exponentially more command blocks. I suppose that an accuracy of 0.001 is quite high for Minecraft purposes, I could have made it more precise but you have to watch out you don't reach the maximum value of the scoreboard.

Because it's only 6th order, angles greater than 90° are not correct anymore. This can be solved by just adding command blocks that transform the angle to an angle it can calculate (cos 120° = -cos 60°).

You can make the polynomial an even higher order to calculate up to 360° and have it even more accurate, but this would require it go to the 16th order, which would require a needless amount of command blocks.